The first way: INSERT into inserts with the following code:
$params = Array (' value ' => ' 50′);
Set_time_limit (0);
echo Date ("H:i:s");
For ($i =0 $i <2000000; $i + +) {
$connect _mysql->insert ($params);
echo Date ("H:i:s");
The final display is: 23:25:05 01:32:05 it took more than 2 hours!
The second method: use transaction commits, BULK Insert database (under 10W) the last display of time is: 22:56:13 23:04:00, altogether 8 minutes 13 seconds, the code is as follows:
echo Date ("H:i:s");
$connect _mysql->query (' BEGIN ');
$params = Array (' value ' => ' 50′);
For ($i =0 $i <2000000; $i + +) {
$connect _mysql->insert ($params);
if ($i%100000==0) {
$connect _mysql->query (' COMMIT ');
$connect _mysql->query (' BEGIN ');
}
$connect _mysql->query (' COMMIT ');
echo Date ("H:i:s");
The third approach is to use an optimized SQL statement: Splice The SQL statement, use INSERT into table () values (), (), (), and then insert again, if the string is too long,
You need to configure MySQL to run in the MySQL command line: Set global max_allowed_packet = 2*1024*1024*10; consumption time: 11:24:06 11:25:06;
Inserting 200W test data only took 1 minutes! The code is as follows:
$sql = "INSERT into twenty_million (value) values";
For ($i =0 $i <2000000; $i + +) {
$sql. = "(' 50′),";
};
$sql = substr ($sql, 0,strlen ($sql)-1);
$connect _mysql->query ($sql);
Finally, when inserting large quantities of data, the first method is undoubtedly the worst, while the second method is more widely used in practical application, and the third method is more suitable for inserting test data or other low requirements, and it is fast indeed.