我有一個我想插入MySQL數據庫的10.000記錄列表。在數據庫快速插入數據php?
我試圖用foreach和簡單的INSERT INTO插入它們,但是它花了很多時間插入。
是否有任何方法可以更快地插入這些行?
$contents = file_get_contents("table.txt.001");
$pollfields = explode(',', $contents);
foreach ($pollfields as $key) {
$query = "INSERT INTO `table` (`id`, `name`) VALUES (NULL, '$key')";
mysqli_query($conn, $query) or trigger_error(mysqli_error()." in ".$query);
}
我看了服用點約puting像更多的價值,從而
"INSERT INTO `table` (`id`, `name`) VALUES (NULL, '$key'),(NULL, '$key'),(NULL, '$key')";
的問題是他怎麼每次如果使用的foreach插入下一個5例如知道。
http://php.net/manual/en/function.array-chunk.php – CBroe
使用預準備語句將提高性能。 – Juan
如果這是從csv文件加載,然後[LOAD DATA INFILE](https://dev.mysql.com/doc/refman/5.7/en/load-data.html) –