MySQL improves Insert Performance

Source: Internet
Author: User

MySQL improves Insert Performance

The time required to insert a record is composed of the following factors, and the number indicates the approximate proportion:
• Connection: (3)
• Send a query to the server: (2)
• Analysis query: (2)
• Insert record: (1x record size)
• Insert index: (1x index)
• Close: (1)

This does not take into account the initial overhead of opening a table. Each concurrent query is opened.

The table size slows down index insertion at the speed of logN (B tree.

Some methods to accelerate insertion:

• If many rows are inserted from the same client at the same time, use the INSERT statement containing multiple values to INSERT several rows at the same time. This is faster than using a single-row INSERT Statement (several times faster in some cases ). If you add data to a non-empty table, you can adjust the bulk_insert_buffer_size variable to make data insertion faster.
• If you INSERT many rows from different clients, you can use the insert delayed statement to speed up.
• MyISAM is used. If no row is deleted in the table, the row can be inserted while the SELECT statement is running.
• Use load data infile when loading a table from a text file. This is usually 20 times faster than using many INSERT statements.
• When a table has many indexes, it is possible to do more work to make load data infile faster. Use the following process:

You can use create table to CREATE a TABLE.
Run the flush tables statement or mysqladmin flush-tables command.
Use myisamchk -- keys-used = 0-rq/path/to/db/tbl_name. This removes all indexes from the table.
Use load data infile to insert DATA into the table, because no index is updated, so it is very fast.
If you only want to read the table later, use myisampack to compress it. See section 15.1.3.3 "compression table feature ".
Use myisamchk-r-q/path/to/db/tbl_name to re-create the index. This will create an index tree in memory before writing data to the disk, and it is faster, because it avoids a large number of disk searches. The Results Index Tree is also perfectly balanced.
Run the flush tables statement or the mysqladmin flush-tables command.

Copy code
Locking a table can accelerate the INSERT operation executed with multiple statements

This improves the performance because the index cache is refreshed to the disk only once after all INSERT statements are completed. Generally, the number of INSERT statements is the index cache refresh. If you can use one statement to insert all rows, you do not need to lock them. For transaction TABLES, BEGIN and COMMIT should be used instead of lock tables to speed up insertion.

INSERT, UPDATE, and DELETE operations are fast in MySQL. By locking more than five consecutive INSERT or UPDATE operations in a row, you can achieve better overall performance. If you insert a table multiple times in a row, you can execute lock tables and then immediately execute unlock tables (about every 1000 rows) to allow other threads to access the table. This will also achieve good performance.

-- Lock table
Lock tables 'order' WRITE;
-- Disable the key
Alter table 'order' disable keys;
-- Insert data
Insert into 'order' VALUES (, 'updated ');

Insert into 'order' VALUES (, 'updated ');
-- Enable the key
Alter table 'order' enable keys;

-- Unlock table
Unlock tables;

To speed up load data infile and INSERT in the MyISAM table, increase the key-speed buffer by adding the key_buffer_size system variable.

This article permanently updates the link address:

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.