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