【发布时间】:2021-06-25 07:49:03
【问题描述】:
我有一个简单的应用程序,对 MySQL 进行简单的查询(从表中选择,没有任何连接)。我的数据库记录甚至没有达到 1000 条记录,但是 laravel 获得一个简单选择查询的响应所花费的时间超过 300-400 毫秒。当我在 MySQL 控制台中尝试时,它花费的时间不超过 5 毫秒。 laravel 真的那么慢吗?还是我的代码有问题?尝试使用 eloquent、查询生成器、原始查询,所有这些都花费了 300 毫秒以上。
我的控制器:
public function search(Request $request){
$origin = $request->get('origin');
$destination = $request->get('destination');
$o = explode(",",$origin);
$d = explode(",",$destination);
$response = DB::table('saved_routes')->where([
'from_lat'=>$o[0],
'from_lng'=>$o[1],
'to_lat'=>$d[0],
'to_lng'=>$d[1],
])->get('value');
if($response->isEmpty()){
return response()->json([
['value'=>'Data not found']
]);
}elseif($response[0]->value){
return $response;
}
}
我的 js api 调用:
axios.get("/api/savedroutes/search?"+=${origin.lat},${origin.lng}&destination=${destination.lat},${destination.lng}`)
.then(res => {res;})
PS:我还在学习 Laravel。
谢谢!
【问题讨论】:
-
请将代码更新为问题,以便会员能够指出您是否做错了什么。