Php 提高查询速度Ionic/Laravel Post请求
我正在尝试提高查询速度,目前返回所有数据大约需要15秒。我有一个Ionic 3应用程序正在发送post请求以获取所有库存,我的Laravel 5.4服务器正在处理该请求 我的问题是:Php 提高查询速度Ionic/Laravel Post请求,php,mysql,sql,laravel,laravel-5,Php,Mysql,Sql,Laravel,Laravel 5,我正在尝试提高查询速度,目前返回所有数据大约需要15秒。我有一个Ionic 3应用程序正在发送post请求以获取所有库存,我的Laravel 5.4服务器正在处理该请求 我的问题是: $input = file_get_contents( "php://input" ); $request = json_decode( $input ); $dealer_id = $request->dealer_id; $tmp = Inventory::where(
$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 );
返回的数据:
最大的问题之一是获取所有图像URL以及所有车辆
感谢所有能帮助我提高速度和效率的人。为什么不把所有内容都放在一个查询中?使用
连接
和仅选择所需的列。如果需要数组
,只需将添加到array()
即可。此外,如果没有索引,请添加索引 为什么不在一个查询中包含所有内容?使用连接
和仅选择所需的列。如果需要数组
,只需将添加到array()
即可。此外,如果没有索引,请添加索引 您的查询太多,无法获取车辆和图像。在车辆的情况下,您可以通过加载关系将每个库存记录的数量减少到1:
$tmp = Inventory::with('vehicle')
where(
'dealer_id', '=', $dealer_id
)->where(
'inventories.is_sold', '=', 0
)->where(
'is_active','=', 1
);
如果图像是Inventory
上的关系,则可以使用方法调用将其添加到,如果不是,则可以单独收集图像搜索参数,然后执行单个选择如果查询太多,无法获取车辆和图像。在车辆的情况下,您可以通过加载关系将每个库存记录的数量减少到1:
$tmp = Inventory::with('vehicle')
where(
'dealer_id', '=', $dealer_id
)->where(
'inventories.is_sold', '=', 0
)->where(
'is_active','=', 1
);
如果图像是Inventory
上的一个关系,您可以使用方法调用将其添加到,如果不是,您可以单独收集图像搜索参数,然后执行一次选择在“经销商id”、“车辆id”和“处于活动状态”列上添加一个复合索引。您在循环中执行的查询太多了。据我所知,您至少有6n个查询。例如:每次使用$row->vehicle()->first()
,您都会再次查询数据库。在“经销商id”、“车辆id”和“处于活动状态”列上添加一个复合索引。您在循环中执行的查询太多了。据我所知,您至少有6n个查询。例如:每次使用$row->vehicle()->first()
,您都会再次查询数据库。我正在尝试您的路线,目前,它在两个表中搜索,并在655毫秒内返回163个原始结果。我循环了一遍,然后全部返回到我的原始json中,最终返回数据的速度大约是650毫秒。现在,在应用程序进入主页之前,就已经收集了所有库存,这真是太棒了。谢谢。很高兴我能帮上忙:)我正在尝试你的路线,目前它在两个表中搜索,并在655毫秒内返回163个原始结果。我循环了一遍,然后全部返回到我的原始json中,返回数据的最终速度大约是650毫秒。现在,在应用程序进入主页之前,就已经收集了所有库存,这真是太棒了。谢谢。很高兴我能帮忙:)