Batch insertion of data to MySQL database using php -


I have thousands of data parsed from the huge XML to insert into the database table using PHP and MySQL. The problem is taking too much time to insert all the data in the table. Is there any way that my data gets split into a small group so that the process of convergence is based on the group? For example, how can one set a script that processes data from 100? Here's my code:

  foreach ($ item is listed as $ Key> $ item) {$ download_records = New DownloadRecords (); // Check if content is present for the first time! (! $ Download_records-> select-referencesSecondation ("WHERE Guid =". $ Guid. "")) {/ * Insert * /} and {/ * Update / /  

}

* Note: $ itemList is approximately 62,000 and is still growing.

using a loop?

But the fastest way to load data in MySQL is to use commands, you can create a file to load through PHP, and then feed it to a different process (or in the original process). As for the final step) is to MySQL.

If you can not use a file, use the following syntax:

  table (call1, cola2) value (val 1, val 2 ), (Val 3, Val 4), (Val 5, Val 6)  

so that you can reduce the total sentences to run.

EDIT: Looking at your snippet, it seems that you can benefit from MySQL syntax, working in the database and reducing the amount of queries, assuming that in your table The primary key or unique index is.

You can do something like this to kill DB for every 100 rows ( please review and fix it in your environment )

  $ InsertOrUpdateStatement1 = "Include table (IN1, col2) values"; $ InsertOrUpdateStatement2 = "On Duplicate Key Update"; $ Counter = 0; $ Queries = array (); Forex Currency ($ item is listed as the $ Key & gt; $ item) {$ val1 = escape ($ item- & gt; col1); // Escape is a function that secures input from SQL injection. // depends how you are reaching DB $ val2 = escape ($ item- & gt; col2); $ Queries [] = $ insertOrUpdateStatement1 "('$ VAL1', '$ val2')". $ InsertOrUpdateStatement2 "Call1 = '$ val1', col2 = '$ val2'"; $ Counter ++; If ($ counter% 100 == 0) {executeQueries ($ queries); $ Queries = array (); $ Counter = 0; }}  

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

  function executeQueries ($ queries) {$ data = ""; Foreign currency ($ query as $ query) {$ data. = $ Query "; \ N"; } ExecuteQuery ($ data); }  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -