我有一個txt文件,看起來像這樣:大MySQL的INSERT語句
id,lat,lon,sequence,dist
98372,40.535257,-111.871612,1,0.0
98372,40.536023,-111.872235,2,0.1001
98372,40.535629,-111.872599,3,0.1335
98372,40.535499,-111.872559,4,0.1758
...etc
此文件可以是863650線長! (34MB)
我需要使用MyISAM引擎將每行作爲一行插入到MySQL數據庫中。 (我假設這將是最快的)
我在GoDaddy的服務器上託管,所以我沒有太多的控制Apache和MySQL配置。
這是我目前該會的工作,除了需要大約6+分鐘,服務器超時2所以它永遠不會完成後:
$raw = file_get_contents('file.txt');
$lines = explode("\r\n", $raw);
$tablename = 'tablename';
$tableHeaders = 'id,lat,lon,sequence,dist';
foreach($lines as $line) {
$line_values = explode(",", $line);
$sqlValues = "VALUES(";
foreach($line_values as $value) {
$sqlValues .= "'$value',";
}
$sqlValues = substr($sqlValues, 0, -1) . ")";
$sqlSyntax = "INSERT INTO $tablename ($tableHeaders) $sqlValues";
$mysqlcon->query($sqlSyntax);
}
這能優化好?或者是有可能建立一個巨大的INSERT語句,如:
"INSERT INTO table (id,lat,lon,sequence,dist)
VALUES (98372,40.535257,-111.871612,1,0.0),
(98372,40.536023,-111.872235,2,0.1001),
(98372,40.535629,-111.872599,3,0.1335),
(98372,40.535499,-111.872559,4,0.1758),
...etc (863647 more lines)"
任何幫助將不勝感激!
爲什麼不在INSERT循環中使用INSERT查詢? – Chandresh
@Chandresh,你是什麼意思?這就是我在我的問題中發佈的第一個foreach示例中所做的事情嗎? – ksb86