Implementation of statement performance analysis by MySQL BULK INSERT Database

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

Suppose our table structure is as follows

The code is as follows

CREATE TABLE Example (
example_id INT not NULL,
Name VARCHAR not NULL,
Value VARCHAR is not NULL,
Other_value VARCHAR (m) not NULL
)

Typically, a single inserted SQL statement will write this:

The code is as follows

INSERT into example
(example_id, name, value, Other_value)
VALUES
(+, ' Name 1 ', ' Value 1 ', ' other 1 ');

MySQL allows us to bulk insert data in an SQL statement, as follows SQL statement:

The code is as follows

INSERT into example
(example_id, name, value, Other_value)
VALUES
(+, ' Name 1 ', ' Value 1 ', ' other 1 '),
(A, ' Name 2 ', ' Value 2 ', ' other 2 '),
(102, ' Name 3 ', ' Value 3 ', ' other 3 '),
(The "Name 4", ' Value 4 ', ' other 4 ');

If we insert columns in the same order as the columns in the table, we can omit the definition of the column name, as follows SQL

The code is as follows

INSERT into example
VALUES
(+, ' Name 1 ', ' Value 1 ', ' other 1 '),
(A, ' Name 2 ', ' Value 2 ', ' other 2 '),
(102, ' Name 3 ', ' Value 3 ', ' other 3 '),
(The "Name 4", ' Value 4 ', ' other 4 ');

It doesn't seem to be a problem, so I'll use the tips for optimizing the SQL statement, which will be tested separately, with the goal of inserting an empty data sheet 200W data

The first way: INSERT into inserts with the following code:

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

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:

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.

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.