PHP 3 methods and speed comparison of large-volume insert database, PHP database 3 kinds of _php tutorial

Source: Internet
Author: User
Tags echo date php database mysql command line

PHP 3 ways to insert databases in large batches and speed comparison, 3 types of PHP database


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

$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 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:

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: 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 to run on 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, 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.


PHP's fastest way to insert data into a MySQL database

Inserting 1000 data at a time will increase n times faster than one insert, the main technique is to write SQL, it is not difficult
INSERT INTO Table1 value (v1, V2, v3), (X1,X2,X3),....

Instead of
INSERT INTO Table1 value (v1, V2, v3);
INSERT INTO Table1 value (X1,X2,X3);
Such an insertion of one piece
I hope you can understand
Reference: Ox man small Sea

Php,mysql, how do you import large amounts of Excel data into a database? Before I recorded the data is this way, first translated into CS

About CSV format
If there is a comma in the content, you can enclose the whole field in quotation marks, refer to the Baidu Encyclopedia CSV.
For example
Field 1, "Field 2 with, Number", Field 3

In fact, the key is the PHP read CSV rules, CSV is not necessarily a comma, it can be a semicolon and other symbols.
Change the corresponding PHP reading rules.

http://www.bkjia.com/PHPjc/840763.html www.bkjia.com true http://www.bkjia.com/PHPjc/840763.html techarticle PHP 3 ways to insert a database and speed comparison, PHP database 3 The first method: using insert into the code as follows: $params = Array (' value ' = ' 50′ '); Set_time_

  • Related Article

    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.