我有一个laravel项目,我需要定期检查3.000个应用程序用户中是否有一个在反洗钱数据库中(5.000.000行大Microsoft 表)。
在前端,我使用ajax完成了异步操作,所以当我单击"Check User“按钮时,我将等待响应。
$users = User::all(); // 3.000 rows
foreach($users as $user){
$aml = DB::table('anti_money_laundering') // 5.000.000 rows
->select('ID')
->whereRaw("LOWER([FULL_NAME]) = ?", [$user->full_name])
->first();
if($aml){
//Bingo, do stuff
//Continue
}
}但是我得到了maximum execution time of 30 seconds错误,并且我认为在php.ini配置中增加请求时间并不能解决我的问题。
我怎么发动汽车呢?大查询/长请求的最佳实践是什么?
发布于 2019-06-30 12:29:36
您目前正在一个循环中运行一个查询--其中一个具有3.000次迭代的循环将进行3.000次查询--这需要时间!
相反,您只需运行一个查询,将两个表连接在一起,并查看是否返回了任何结果。
SELECT aml.id
FROM anti_money_laundering AS aml
JOIN users AS u
ON aml.FULL_NAME = u.full_name雄辩地说,你可以这样做
$query = DB::table('anti_money_laundering ')
->join('users', 'users.full_name', '=', 'anti_money_laundering.full_name')
->select('anti_money_laundering.id')
->get();如果有任何结果,就有匹配。
发布于 2019-06-30 12:40:55
构建一个带有用户名的数组,并检查您的集合中是否有anti_money_laundering记录:
$users = User::all(); // 3.000 rows
$fullNames = [];
foreach($users as $user){
$fullNames[]=$user->full_name;
}
$badUsers = DB::table('anti_money_laundering') // 5.000.000 rows
->select('ID')
->whereIn("LOWER([FULL_NAME]) = ?", $fullNames)
->get();
foreach($badUsers as $badUser){
//Bingo, do stuff
//Continue
}请注意,我不流利的拉拉维尔或雄辩,所以如果我的答案有排字,那就是原因。但是,这个想法比你最初发送3000条请求(非常慢)或另一个答案的想法要好,后者可以进行不必要的连接。
https://stackoverflow.com/questions/56824817
复制相似问题