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 265205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:44:58+00:00 2026-05-11T22:44:58+00:00

I have a thousands of data parsed from huge XML to be inserted into

  • 0

I have a thousands of data parsed from huge XML to be inserted into database table using PHP and MySQL. My Problem is it takes too long to insert all the data into table. Is there a way that my data are split into smaller group so that the process of insertion is by group? How can set up a script that will process the data by 100 for example? Here’s my code:

foreach($itemList as $key => $item){
     $download_records  = new DownloadRecords();
    //check first if the content exists
    if(!$download_records->selectRecordsFromCondition("WHERE Guid=".$guid."")){
         /* do an insert here */
    } else {
         /*do an update */
    }

}

*note: $itemList is around 62,000 and still growing.

  • 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-05-11T22:44:59+00:00Added an answer on May 11, 2026 at 10:44 pm

    Using a for loop?

    But the quickest option to load data into MySQL is to use the LOAD DATA INFILE command, you can create the file to load via PHP and then feed it to MySQL via a different process (or as a final step in the original process).

    If you cannot use a file, use the following syntax:

    insert into table(col1, col2) VALUES (val1,val2), (val3,val4), (val5, val6)
    

    so you reduce to total amount of sentences to run.

    EDIT: Given your snippet, it seems you can benefit from the INSERT … ON DUPLICATE KEY UPDATE syntax of MySQL, letting the database do the work and reducing the amount of queries. This assumes your table has a primary key or unique index.

    To hit the DB every 100 rows you can do something like (PLEASE REVIEW IT AND FIX IT TO YOUR ENVIRONMENT)

    $insertOrUpdateStatement1 = "INSERT INTO table (col1, col2) VALUES ";
    $insertOrUpdateStatement2 = "ON DUPLICATE KEY UPDATE ";
    $counter = 0;
    $queries = array();
    
    foreach($itemList as $key => $item){
        $val1 = escape($item->col1); //escape is a function that will make 
                                     //the input safe from SQL injection. 
                                     //Depends on how are you accessing the DB
    
        $val2 = escape($item->col2);
    
        $queries[] = $insertOrUpdateStatement1. 
        "('$val1','$val2')".$insertOrUpdateStatement2.
        "col1 = '$val1', col2 = '$val2'";
    
        $counter++;
    
        if ($counter % 100 == 0) {
            executeQueries($queries);
            $queries = array();
            $counter = 0;
        }
    }
    

    And executeQueries would grab the array and send a single multiple query:

    function executeQueries($queries) {
       $data = "";
         foreach ($queries as $query) {
            $data.=$query.";\n";
        }
        executeQuery($data);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have thousands of rows of data coming from a table Index |
I have a database table with hundreds of thousands of forum posts, and I
I need to take some data from one table (and expand some XML on
I have a report that renders data returned from a stored procedure. Using profiler
i have a log file which contains hundreds/thousands of seperate XML messages and need
I have thousands of large sets of tag cloud data; I can retrieve a
I have a UTF-8 formatted data file that contains thousands of floating point numbers.
I am trying to extract some data from an XML input with 6 lines,
I have a class (in C++), call it Data , that has thousands of
I have a simple shopping cart web site that uses a MySQL database to

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.