Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:46:05+00:00 2026-06-07T02:46:05+00:00

I have the following code (more or less) to import anywhere from 500.000 to

  • 0

I have the following code (more or less) to import anywhere from 500.000 to 4.000.000 rows:

$sSql = "Insert into table (a,b,c) VALUES(?,?,?)"
$oSQLStmnt = $pdo->prepare($sSql);
$oSQLStmnt->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM);
if (!$oSQLStmnt) {
    echo $pdo->errorInfo(); // Handle errors
}
$pdo->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->commit();
            sleep(0.15);
            $pdo->beginTransaction();
       }
       try {
            $oSQLStmnt->execute($aLine);
            $iSuccesulInserts++;
       }
       catch (exception $e) {
            print_r($e);
            $iFailedInserts++;
       }

       $iLineCounter++;
}
$pdo->commit();

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.

I thought I could stack a lot of inserts during a transaction? Could calling beginTransaction be a problem?

UPDATE:

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:46:06+00:00Added an answer on June 7, 2026 at 2:46 am

    Have you considered using Sprocs instead of insert statements? writing ANY number of records sequentially- one at a time- is kindof a waste of time / energy.. it’s just not as fast as it should be.

    Are you sure you can’t use BULK INSERT or XML instead to insert multiple rows at a time?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in a c++ listener class (more or less), which
i have the following code: <p class=more><a href=/stories class=more>Read more</a></p> and CSS: a.more, a.back
This is a code review question more then anything. I have the following problem:
More than one time I have encountered the following problem when building code with
I have following code for loading image from url in xml parsing endElement method
I have following code for inserting data into database using PDO. It inserts data
Updated question to be more generic: I have the following code. When you swap
I have the following code which should change the value of an h2 from
I have the following code (inherited!) that will split a line of text into
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.