Note that there are some explanatory texts on larger screens.

plurals
  1. PORecords disappearing in PDO mssql transaction loop
    primarykey
    data
    text
    <p>I have the following code (more or less) to import anywhere from 500.000 to 4.000.000 rows:</p> <pre><code>$sSql = "Insert into table (a,b,c) VALUES(?,?,?)" $oSQLStmnt = $pdo-&gt;prepare($sSql); $oSQLStmnt-&gt;setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM); if (!$oSQLStmnt) { echo $pdo-&gt;errorInfo(); // Handle errors } $pdo-&gt;beginTransaction(); $iLineCounter = 1; while (($sLine = fgets ($oCSV, 8000)) !== FALSE) { $aLine = explode('|', $sLine); //Fgetscsv did not work properly if ($iLineCounter % 100 == 0) { lo("Inserting row " . $iLineCounter); $pdo-&gt;commit(); sleep(0.15); $pdo-&gt;beginTransaction(); } try { $oSQLStmnt-&gt;execute($aLine); $iSuccesulInserts++; } catch (exception $e) { print_r($e); $iFailedInserts++; } $iLineCounter++; } $pdo-&gt;commit(); </code></pre> <p>As you can see, I perform a commit every 100 lines, and I even added some sleep. I used to run the commit only once every 25.000 lines, and I did not use any sleep. However, at one point, I discovered I was missing records. I started playing with these settings (sleep and number of rows). This way I reduced the number of missing records from 50.000 to about a 100. But I'm still missing records! Where are they going? I know the SQL is ok, because I immediately receive errors when somethings wrong there. </p> <p>I thought I could stack a lot of inserts during a transaction? Could calling beginTransaction be a problem?</p> <p><strong>UPDATE:</strong></p> <p>The bounty ended and I had to award it. Thank you all for your answers. Or tips actually, as none of you actually answered my question. I was not asking for a workaround, although you suggestions are much appreciated. The answer the bounty was awarded to received it because it came closest to actually answering my question. Unfortunately it did not work.</p> <p>For now I'm using CSV bulk import, that works fine, but if anyone has any other tips for fixing this issue, please let me know. As I prefer using my original method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload