dongyi1921 2018-08-07 13:02
浏览 69
已采纳

提高查询速度Ionic / Laravel Post请求

I'm trying to increase the speed of my query, currently it takes around 15 seconds for all the data to get returned. I have an Ionic 3 app which is sending a post request to get all inventory and my Laravel 5.4 server is handling the request.

Here's my query :

    $input = file_get_contents( "php://input" );

    $request = json_decode( $input );
    $dealer_id = $request->dealer_id;

    $tmp = Inventory::where(
            'dealer_id', '=', $dealer_id
        )->where(
            'inventories.is_sold', '=', 0
        )->where(
            'is_active','=', 1
    );

    // dd($tmp);

    $data = collect();
    $pmt = $tmp->get();
    logger( sprintf('# of rows returned: %s', $pmt->count() ) );

    $pmt->each( function($row) use(&$data) {
      logger( sprintf('Row    : %s', $row->toJson() ));

        $data->push( array(
            'stock_number' => $row->stock_number,
            'vehicle_id' => $row->vehicle_id,
            'year' => $row->vehicle()->first()->year,
            'make' => $row->vehicle()->first()->make,
            'model' => $row->vehicle()->first()->model,
            // 'trim' => $row->vehicle()->first()->trim,
            'vin' => $row->vehicle()->first()->vin,
            'status' => $row->vehicle_status,
            'purchase_price' => $row->purchase_price,
            'cost' => $row->cost,
            // 'retail_price' => $row->retail_price,
            'search_meta' => $row->search_meta,
            // 'interior_color' => $row->vehicle()->first()->interior_color,
            // 'exterior_color' => $row->vehicle()->first()->exterior_color,
            'firstImg' => $row->getFirstImage(),
            'images' => Vimage::select('vehicle_id','name'
            )->where(
                'dealer_id', '=', $row->dealer_id
            )->where(
                'vehicle_id', '=', $row->vehicle_id
            )->get()
        ));

    });

    $statusKey = \App\lt_vehicle_status::where(
        'dealer_id', '=', $dealer_id
    )->where(
        'is_active','=', 1
    )->get();

    $response = [
       "status" => "Success",
       "code" => "MAC01",
       "reason" => "MAC - Inventory Gathered Successfully",
       "data" => $data,
       "status_keys" => $statusKey
   ];

   echo json_encode( $response );

Data Returned:

https://i.imgur.com/zxWSNo5.png

One of the biggest issues is getting all the image urls as well as all the vehicles.

Thank you to whomever can help me try to increase my speed and efficiency of this.

  • 写回答

2条回答 默认 最新

  • doujiao2014 2018-08-07 13:46
    关注

    You have too many queries to get the vehicles and images. In the vehicles case, you can reduce them to 1 for each Inventory record by eager-loading the relationship:

    $tmp = Inventory::with('vehicle')
            where(
                'dealer_id', '=', $dealer_id
            )->where(
                'inventories.is_sold', '=', 0
            )->where(
                'is_active','=', 1
        );
    

    If the image is a relationship on Inventory you can add it to the with method call, if not you can collect the image search parameters separately and then perform a single select

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料