【发布时间】:2019-06-11 11:22:12
【问题描述】:
我在我的 Laravel 应用程序中有这段代码:我读取了 .csv 文件中的每一行并想要更新一个值。但是对于 8k 行的 .csv,倍数的更新查询非常慢。我怎样才能加快这段代码的速度?谢谢
DB::beginTransaction();
try {
$delimiter = ",";
$firstLine = true;
if ($handle !== FALSE) {
$position = 1;
while (($csv_line = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
if ($firstLine == true) {
$firstLine = false;
continue;
}
$player_uid = $csv_line[0];
DB::table('scores')
->where('season_uid', $season_uid)
->where('day', $day)
->where('player_uid', $player_uid)
->update(['position' => $position]);
$position++;
}
fclose($handle);
}
DB::commit();
return true;
} catch (\Exception $e) {
Log::error($e);
DB::rollBack();
return false;
}
【问题讨论】:
-
我投票结束这个问题,因为它属于 CodeReview
-
.. 但我有根据的猜测是分数表没有正确的索引..
SHOW CREATE TABLE scores -
从查看代码我敢打赌,性能瓶颈在于
SELECTs 的执行,这可能是通过调用DB::table()->where()->...生成的使用Xdebug profiler 来分析这肯定会更清楚进入这个。 -
“非常慢”有多马赫?你希望它有多快?
标签: php mysql laravel eloquent