[MySQL database] 7. SQL optimization

1. Insert data optimization

(1) insert statement

① Insert data in batches

insert into tb_test values(1, 'Tom'), (2, 'Cat'), (3, 'Jerry');

② Manually control transactions

start transaction;
insert into tb_test values(1,'Tom'),(2,'Cat'),(3,'Jerry');
insert into tb_test values(4,'Tom'),(5,'Cat'),(6,'Jerry');
insert into tb_test values(7,'Tom'),(8,'Cat'),(9,'Jerry');
commit;

③ The primary key is inserted in sequence, and the performance is higher than that of out-of-order insertion

Primary key insertion out of sequence: 8 1 9 21 88 2 4 15 89 5 7 3
Primary key sequence insertion: 1 2 3 4 5 7 8 9 15 21 88 89【☆】

(2) load Insert data in bulk【☆❀

① If you need to insert a large amount of data (millions of levels) at one time, the performance of using the insert statement is very low.
② You can use loadthe instructions to insert

insert image description here


The following commands can be executed to load the data in the data script file into the table structure:

-- 客户端连接服务端时,加上参数 -–local-infile
mysql –-local-infile -u root -p

-- 设置全局参数local_infile为1,开启从本地加载文件导入数据的开关
set global local_infile = 1;

-- 执行load指令将准备好的数据,加载到表结构中
load data local infile '/root/sql1.log' into table tb_user fields
terminated by ',' lines terminated by '\n' ;

2. Primary key optimization

主键顺序插入的性能是要高于乱序插入的, 为什么?

(1) Data organization form

  • In the InnoDB storage engine, table data is organized and stored according to the order of the primary key
  • A table with this storage method is called an index organized table (index organized table IOT)

insert image description here

insert image description here

In the InnoDB storage engine, data rows are recorded in the logical structure page , and the size of each page is fixed, the default is 16K. That also means that the rows stored in a page are also limited. If the inserted data row is not too small in the page, it will be stored in the next page, and the pages will be connected by pointers .

(2) page split

  • Pages can be empty, half filled, or 100% filled
  • Each page contains 2-Nrows of data
  • If a row of data is too large, it will overflow and arrange according to the primary key

insert image description here
insert image description here

insert image description here


insert image description here

insert image description here

insert image description here

(3) Page merge

insert image description here

insert image description here

insert image description here

(4) Primary key design principles

  • In the case of meeting business needs, try to reduce the length of the primary key
  • When inserting data, try to choose sequential insertion, choose to use AUTO_INCREMENTauto- increment primary key
  • Try not to use UUID as the primary key or other natural primary keys, such as ID number
  • During business operations, avoid modifying the primary key

3. Orber by optimization

  • MySQL sorting, there are two ways:
  • Using filesort: scan the whole table, read the data rows that meet the conditions, and then complete the sorting operation in the sort buffer . All sorts that do not directly return sorted results through indexes are called FileSort sorts .
  • Using index : The ordered data is directly returned by sequential scanning of the ordered index. In this case, the using index does not require additional sorting, and the operation efficiency is high.
  • Using index has high performance, while Using filesort has low performance

  • Establish a suitable index based on the sorting field. When sorting by multiple fields, it also follows the leftmost prefix rule.
  • Use covering indexes whenever possible.
  • Multi-field sorting, one ascending and one descending, at this time, you need to pay attention to the rules (ASC/DESC) when creating the joint index.
  • If filesort is unavoidable, when sorting a large amount of data, you can appropriately increase the sort buffer size sort_buffer_size (default 256k).

insert image description here

Four, group by optimization

  • During grouping operations, indexes can be used to improve efficiency.
  • When grouping operations, the use of indexes also satisfies the leftmost prefix rule

Five, limit optimization (pagination)

  • When the amount of data is relatively large, if limit paging query is performed, the efficiency of paging query will be lower as the query goes further.

  • When performing a paging query, if it is executed limit 2000000,10, MySQL needs to sort the first 2000010 records at this time, only the records 2000000 - 2000010returned , and other records are discarded. The cost of query sorting is very high

Optimization idea: In general pagination query, the performance 覆盖索引can be , which can be optimized in the form of covering index plus subquery (multi-table joint query)

insert image description here

Six, count optimization

  • count()It is an aggregation function: for the returned result set, judge line by line, if the parameter count 函数of is not NULL , add 1 to the cumulative value, otherwise, do not add, and finally return the cumulative value.

usage:

  • count(*)
  • count (primary key)
  • count (field)
  • count (number)

insert image description here

Seven, update optimization

insert image description here

InnoDB's row lock is a lock on the index , not a lock on the record. And the index cannot be invalidated, otherwise it will be upgraded from a row lock to a table lock

Guess you like

Origin blog.csdn.net/m0_54189068/article/details/131137680