我正在用Laravel5.6和Vuejs构建一个应用程序,从Laravel后端的API请求数据。在我的开发环境中,查询根本不需要时间,但是在生产环境中,API上的响应需要85.7秒,而在开发环境中则需要0.1秒。两个环境中的数据相同。
使用控制器中的以下操作命令,我可以获得运行的确切查询,并可以逐个测试它们:
DB::enableQueryLog();
// then my eloquent stuff
$query = Sportevent::whereHas('photos')->with('photos');
if($request->input('year')){
$year = $request->input('year');
$query = $query->where('date','like', $year.'%');
}
if($request->input('country')){
$country = $request->input('country');
$query = $query->where('country',$country);
}
$sportevents = $query->orderBy('date',"DESC")->paginate(10);
// then I display the queries:
dd(DB::getQueryLog());
以下是未选择年份或国家/地区而生成的查询,包括在两种环境中随时间变化的性能:
// --------------------------------------------------------------
// DEV: 0.0208 seconds | PROD: 73 seconds (had to use stopwatch)
// --------------------------------------------------------------
select count(*) as aggregate from `events` where exists
(select * from `photos` where `events`.`id` = `photos`.`eventID`
and `active` = 1)
// ------------------------------------------
// DEV: 0.025 seconds | PROD: 38.9721 seconds
// ------------------------------------------
select * from `events` where exists (select * from `photos` where
`events`.`id` = `photos`.`eventID` and `active` = 1)
order by `date` desc limit 10 offset 0
// ------------------------------------------
// DEV: 0.0112 seconds | PROD: 0.0141 seconds
// ------------------------------------------
select * from `photos` where `active` = 1 and `photos`.`eventID` in
(11194, 11087, 10506, 10797, 9910, 10118, 10212, 9655, 10047, 10049)
表事件包含大约6000个条目和少于50000个条目的照片。如果您在讨论表结构时需要进一步的详细信息,请在“注释”部分的“下投票”前通知我:—)
在生产服务器上,有许多其他的应用程序在同一个MySQL安装中使用Laravel或Wordpress运行,它们都没有类似的问题。