I am having an issue with bulk inserting into a staging table in MSSQLSRV 2008 R2.
I am inserting a CSV with ~200,000 lines and it is taking roughly 5 minutes to complete.
I tried using both PDO and the sqlsrv driver. They both seem to yield poor performance.
Here is the code to give an idea of what I am doing (I included both SQLSRV and PDO code):
...
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...
...
I've been scouring the internet looking for possible solutions but haven't been able to find anything that worked.
I've found this post which basically says to batch rows together (which I've already done).
And I found another post that said for PDO, to set connectionpooling=0. I tried that and didn't see any increase in performance.
Has anyone else run into this issue with SQLSRV and PHP?
Cheers,