PHP 3 ways to insert databases in large batches and speed comparison

Source: Internet
Author: User
Tags bulk insert echo date mysql command line

This article is mainly to say that PHP inserted database speed comparison, methods are found from the Internet, their own practice test. In fact, the main use of solution three to solve their own problems, so here to record

The first method: use INSERT INTO to insert, the code is as follows:

1$params= Array (' Value'=' -);2Set_time_limit (0);3 echo Date ("H:i:s");4   for($i =0; $i <2000000; $i + +){5$connect _mysql->insert ($params);6  };7echo Date ("H:i:s");

The last display is: 23:25:05-01:32:05 that took more than 2 hours!

The second method: use transaction commit, BULK Insert database (under 10W per submission) Last display time consumed is: 22:56:13 23:04:00, altogether 8 minutes 13 seconds, the code is as follows:

1 echo Date ("H:i:s");2$connect _mysql->query (' BEGIN');3$params= Array (' Value'=' -);4  for($i =0; $i <2000000; $i + +){5$connect _mysql->insert ($params);6 if($i%100000==0){7$connect _mysql->query (' COMMIT');8$connect _mysql->query (' BEGIN');9 }Ten } One$connect _mysql->query (' COMMIT'); Aecho Date ("H:i:s");

The third approach: using optimized SQL statements: Splicing SQL statements, using INSERT into table () values (), (), (), () and then inserting them once, if the string is too long,

You need to configure MySQL,

There are two kinds of configurations, which are generally said to run on the MySQL command line: Set global max_allowed_packet = 2*1024*1024*10;

My own use is to directly modify the PHP configuration file php.ini max_allowed_packet =20m; Restart the server

Inserting 200W test data only took 1 minutes! The code is as follows:

1 $sql = "INSERT into twenty_million (value) values"; 2  for ($i =0; $i <2000000; $i + +) {3 $sql. = "('50′),"; 4 }; 5 $sql = substr ($sql,0, strlen ($sql)-1); 6 $connect _mysql->query ($sql);

Finally, when inserting large quantities of data, the first method is undoubtedly the worst, and the second method is widely used in practical applications, and the third method is more suitable for inserting test data or other low requirements, and it is really fast.

3 Ways and speed comparison of PHP BULK INSERT database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.