ds78662302 2016-01-28 09:49
浏览 15

雄辩的逻辑重构

Today I came up with a legacy code. I have a function that sometimes generates very long runtimes and often occurs timeout at our partner. Mainly the function checks for reserved seats at a theatre but instead of get all the data and check for empty seats it runs the query for every single seat. which (i think) generates the huge runtime.

Here is the correspondig code:

public function printTicketChaos(){

        $input = Input::all();

        if( isset($input['date_id']) && !isset($input['reserve_id']) ){
            $program_date = \Model\ProgramDate::find($input['date_id']);
            if($input['piece'] > $program_date->available_capacity){
                //dd($input['piece'] . ' ??? ' . $program_date->available_capacity);
                return true;
            }

            $reserved_seats = Session::get('reserved_seats');

            if($reserved_seats != null && count($reserved_seats) > 0){
                foreach ($reserved_seats as $row => $seats) {
                    foreach ($seats as $key => $seat) {

                        $reserve = \Model\Reserve::whereForeignDateId($program_date->id)->whereTableName('programs')->whereHas('seats', function($query) use($row, $seat){ $query->whereRow($row)->whereSeat($seat); })->first();

                        if($reserve != null){
                            return true;
                        }
                        $oi = \Model\OrderItem::whereForeignId($program_date->id)->whereTableName('programs')->whereHas('order', function($query){ $query->whereStorno(0); })->whereHas('seats', function($query) use($row, $seat){ $query->whereRow($row)->whereSeat($seat); })->first();
                        if($oi != null){
                            return true;
                        }
                    }
                }
            }
        }


        return false;
    }

I think the solution could be that i query all the seats and then check for the empty ones in the foreach. Is this a good idea or what do you think which is the best way to do it?

The code uses Laravel 4.2

Thank you for your aswers!

  • 写回答

1条回答 默认 最新

  • doushi8231 2016-01-28 12:35
    关注

    Okay, i made changes in the query and the logic aswell but it was still slow as hell. The system uses a 3rd party PDF generator to create the "ticket" for the costumer and this was the "bad guy".

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line