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

问题描述 投票:0回答:2

我正在尝试提高查询速度,目前大约需要15秒才能返回所有数据。我有一个Ionic 3应用程序发送邮件请求以获取所有库存,我的Laravel 5.4服务器正在处理请求。

这是我的查询:

    $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 );

返回的数据:

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

最大的问题之一是获取所有图像网址以及所有车辆。

感谢任何人可以帮助我提高我的速度和效率。

php mysql sql laravel laravel-5
2个回答
1
投票

您有太多查询来获取车辆和图像。在车辆情况下,您可以通过加载关系来将每个Inventory记录减少到1:

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

如果图像是Inventory上的关系,您可以将其添加到with方法调用,如果不是,您可以单独收集图像搜索参数然后执行单个选择


2
投票

为什么不把所有内容都放在一个查询中?仅使用joinsselect所需的列。如果你需要一个array,你只需添加toArray()就可以了。此外,如果您没有索引,请添加索引。

© www.soinside.com 2019 - 2024. All rights reserved.