我遇到了批量插入到MSSQLSRV 2008 R2臨時表中的問題。PHP有w/MS SQL批量插入緩慢
我插入的CSV大約200,000行,大約需要5分鐘才能完成。
我嘗試使用PDO和sqlsrv驅動程序。他們似乎都表現不佳。
下面是代碼給我在做什麼的想法(我同時SQLSRV和PDO代碼):
...
try {
//create structure table record
foreach ($mapped_data as $k => $v) {
$insert .= $k . ",";
$values .= $v . ",";
}
$insert = substr($insert, 0, -1); //remove last ,
$values = substr($values, 0, -1);
$tableName = $table;
if ($stageData) {
$tableName = "stage_$table";
}
if ($query == "")
$query = "INSERT INTO $tableName ($insert) VALUES ";
$query .= "($values),";
// Insert in blocks of 1000 lines
if ($line % 1000 == 0) {
$log->logInfo("Executing @ line: $line");
$query = substr($query, 0, -1); //remove last ,
$query .= ";";
// ======================
// = SQLSRV DRIVER CODE =
// ======================
sqlsrv_begin_transaction($sqlsrvConn);
$queryResult = sqlsrv_query($sqlsrvConn,$query);
if ($queryResult) {
sqlsrv_commit($sqlsrvConn);
} else {
sqlsrv_rollback($sqlsrvConn);
}
// ===================
// = PDO DRIVER CODE =
// ===================
$conn->beginTransaction();
$res = $conn->prepare($query);
if($res->execute() === false) {
$errInfo = $res->errorInfo();
if ($conn->inTransaction()) {
$conn->rollback();
}
$log->logInfo('Data importing error on line: ' . $line . $errInfo[2]);
$errors[] = 'Data importing error on line: ' . $line . $errInfo[2];
} else {
if ($conn->inTransaction()) {
$conn->commit();
$query = "";
$importedRows += ($line - 6) - $importedRows;
}
}
}
}
catch (PDOException $e) {
if ($conn->inTransaction()) {
$conn->rollBack();
}
$log->logInfo('PDO Exception: ' . $e->getMessage());
$errors[] = 'PDO Exception: ' . $e->getMessage();
}
$line++;
} // End of while loop through each CSV Line
fclose($handle);
$totalRows = $line - 6;
$importedRows += $totalRows - $importedRows;
// Insert remaing queries afterwards...
...
我一直在網上淘尋找可能的解決方案,但避風港」無法找到任何有效的工具。
我發現this post基本上說批處理行(我已經完成)。
而且我發現另一篇文章說PDO,設置connectionpooling = 0。我試過了,沒有看到性能有任何提升。
有沒有其他人遇到這個問題與SQLSRV和PHP?
乾杯,
+1使用PDO,但必須使用準備好的語句才能得到更多保護。 – bksi
數據庫服務器與PHP服務器在同一網絡上運行嗎?你可能有帶寬問題? – MonkeyZeus
您是否嘗試過對腳本進行基準測試,以查看減速是否實際上是INSERT? – MonkeyZeus