SQL optimization - insert data optimization (use of load instruction)

  • Optimization when inserting data
  • primary key optimization
  • order by optimization
  • group by optimization
  • limit optimization
  • count optimization
  • update optimization

1. Optimization when inserting data

When inserting data in batches, it is best not to exceed a thousand at most. If tens of thousands of data are inserted in batches at a time, it can be divided into multiple insert statements for insertion.

MySQL’s transaction submission method is automatically submitted by default. When an insert statement is executed, the transaction is submitted, and an insert is executed again. The transaction is started before execution, and the transaction is automatically submitted after execution. This will involve frequent opening and submission of transactions, so it is recommended to control transactions manually. Start the transaction before executing the insert statement, and submit the transaction uniformly after multiple insert statements are executed.

Inserted in primary key order.

insert image description here

1.1 Insert data in bulk

To insert data in large batches, it is recommended to use the load command instead of the insert command.
insert image description here
The syntax format of mysql view parameters:

select @@local--infile;

insert image description here
Turn on the local_infile switch, and then the data can be loaded in through the load command.

set global local_infile = 1;

insert image description here
Upload the data file and use the load command to load it into the mysql database table

load data local infile '/root/load_user_100w_sort.sql' into table tb_user fields terminated by ',' lines terminated by '\n';

insert image description here
It takes about 17s to load 100w of data into mysql. If you use the insert statement to load the data, it will take about ten minutes. The efficiency of this load loading data is still very obvious.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44860226/article/details/131861472