当前位置:网站首页>MySQL performance optimization - index optimization
MySQL performance optimization - index optimization
2022-06-25 15:28:00 【ITenderL】
1. avoid sql Statement does not hit index
Use explain see SQL Implementation plan of ,
explain select * from tb_item where id = 1;

Field | meaning |
id | select The serial number of the query , It's a set of numbers , Represents execution in query select Clause or the order of the operation table . |
select_type | Express SELECT The type of , Common values are SIMPLE( A simple watch , That is, no table join or subquery is used )、PRIMARY( Main query , That is, the outer query )、UNION(UNION The second or subsequent query statement in )、SUBQUERY( First in subquery SELECT) etc. |
table | Output result set table |
type | Indicates the connection type of the table , The connection types with good to poor performance are ( system ---> const -----> eq_ref ------> ref -------> ref_or_null----> index_merge ---> index_subquery -----> range -----> index ------> all ) |
possible_keys | When representing a query , Possible indexes |
key | Indicates the index actually used |
key_len | Length of index field |
rows | Number of scan lines |
extra | Description and description of the implementation |
Preparation conditions :
create table `tb_seller` (
`sellerid` varchar (100),
`name` varchar (100),
`nickname` varchar (50),
`password` varchar (60),
`status` varchar (1),
`address` varchar (100),
`createtime` datetime,
primary key(`sellerid`)
)engine=innodb default charset=utf8mb4;
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('alibaba',' Alibaba ',' Ali shop ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('baidu',' Baidu Technology Co., Ltd ',' Baidu store ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('huawei',' Huawei Technology Co., Ltd ',' Huawei store ','e10adc3949ba59abbe56e057f20f883e','0',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('itcast',' Chuanzhi podcast Education Technology Co., Ltd ',' Spreading wisdom Podcast ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('itheima',' Black horse programmer ',' Black horse programmer ','e10adc3949ba59abbe56e057f20f883e','0',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('luoji',' Logitech Technology Co., Ltd ',' Logitech shop ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('oppo','OPPO Technology Co., Ltd ','OPPO Official flagship store ','e10adc3949ba59abbe56e057f20f883e','0',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('ourpalm',' Zhangqu Technology Co., Ltd ',' Zhangqu shop ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('qiandu',' QIANDU Technology ',' QianDu store ','e10adc3949ba59abbe56e057f20f883e','2',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('sina',' Sina Technology Co., Ltd ',' Sina's official flagship store ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('xiaomi',' Xiaomi Tech ',' Xiaomi's official flagship store ','e10adc3949ba59abbe56e057f20f883e','1',' Xi'an City ','2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('yijia',' IKEA ',' IKEA flagship store ','e10adc3949ba59abbe56e057f20f883e','1',' The Beijing municipal ','2088-01-01 12:00:00');
create index idx_seller_name_sta_addr on tb_seller(name,status,address);2. Full match , Specify specific values for all columns in the index
explain select * from tb_seller where name=' Xiaomi Tech ' and status='1' and address=' The Beijing municipal '\G;
3. The leftmost prefix rule
If you index multiple columns , Follow the leftmost prefix rule . It means that the query starts from the top left of the index , And don't skip columns in the index .
Match the leftmost prefix rule , Go to the index :
Illegal leftmost prefix rule , Index failure :
If the law of the left is the best , But there was a jump in a column , Only the leftmost column index takes effect :
4. Range query right column , Index failure .
explain select * from tb_seller where name=' Xiaomi Tech ' and status>'1' and address=' The Beijing municipal '\G;
According to the first two fields name , status Queries are indexed , But the last condition address No index is used .
5. Do not perform operations on columns of the index , Can cause indexes to fail .
explain select * from tb_seller where substring(name,2,3)=' Technology ';
6. String without single quotes , Index failure .
explain select * from tb_seller where name=' Xiaomi Tech ' and status=1 and address=' The Beijing municipal '\G;
because , The query is , No single quotes for Strings ,MySQL The query optimizer for , It will automatically perform type conversion , Cause index invalidation .
7. Try to use overlay index , Avoid using select *
Overlay index ( The column queried is the index column )
If the query column , Out of index columns , It also reduces performance .
8.OR If OR The column before the keyword uses the index , The following fields do not use indexes , The index will not be used .
Example ,name Fields are index columns , and createtime It's not an index column , In the middle is or Connection is not indexed :
explain select * from tb_seller where name=' Black horse programmer ' or createtime = '2088-01-01 12:00:00'\G;
9. With % Fuzzy query starting with the , The index will fail . If it's just tail blur matching , The index will not fail ; If it's a fuzzy head match , The index will fail .( You can solve this problem by overriding the index )
10. If MySQL Evaluating the use of an index is slower than a full table scan , Will abandon the use of indexes , Perform a full table scan .
11.is null 、is not null Sometimes the index fails .
12.in Use index ,not in No index .
13. Single column index and composite index .
Try to use composite indexes , Use less single column indexes
14.<> I can walk the index
EXPLAIN select fullname from t_user where user_id <> 5;
create index idx_name_sta_address on tb_seller(name, status, address);
It's like creating three indexes :
name
name + status
name + status + address
Create a single column index
create index idx_seller_name on tb_seller(name);
create index idx_seller_status on tb_seller(status);
create index idx_seller_address on tb_seller(address);The database will choose an optimal index ( The most recognizable index ) To use , Not all indexes will be used .
2. Mass insert data optimization
1. If you need to insert many rows of data into a table at the same time , You should try to use more than one value table insert sentence , This way will greatly reduce the connection between the client and the database 、 Turn off consumption, etc . Make the efficiency ratio separate from the single execution insert Fast sentence .
Example , The original way is :
insert into tb_test values(1,'Tom');
insert into tb_test values(2,'Cat');
insert into tb_test values(3,'Jerry');
The optimized scheme is :
insert into tb_test values(1,'Tom'),(2,'Cat'),(3,'Jerry');
- Data insertion in transactions .
start transaction;
insert into tb_test values(1,'Tom');
insert into tb_test values(2,'Cat');
insert into tb_test values(3,'Jerry');
commit;
- Data is inserted in order
insert into tb_test values(4,'Tim');
insert into tb_test values(1,'Tom');
insert into tb_test values(3,'Jerry');
insert into tb_test values(5,'Rose');
insert into tb_test values(2,'Cat');
After optimization
insert into tb_test values(1,'Tom');
insert into tb_test values(2,'Cat');
insert into tb_test values(3,'Jerry');
insert into tb_test values(4,'Tim');
insert into tb_test values(5,'Rose');
3.ORDER BY Statements to optimize
CREATE TABLE `emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL,
`salary` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into `emp` (`id`, `name`, `age`, `salary`) values('1','Tom','25','2300');
insert into `emp` (`id`, `name`, `age`, `salary`) values('2','Jerry','30','3500');
insert into `emp` (`id`, `name`, `age`, `salary`) values('3','Luci','25','2800');
insert into `emp` (`id`, `name`, `age`, `salary`) values('4','Jay','36','3500');
insert into `emp` (`id`, `name`, `age`, `salary`) values('5','Tom2','21','2200');
insert into `emp` (`id`, `name`, `age`, `salary`) values('6','Jerry2','31','3300');
insert into `emp` (`id`, `name`, `age`, `salary`) values('7','Luci2','26','2700');
insert into `emp` (`id`, `name`, `age`, `salary`) values('8','Jay2','33','3500');
insert into `emp` (`id`, `name`, `age`, `salary`) values('9','Tom3','23','2400');
insert into `emp` (`id`, `name`, `age`, `salary`) values('10','Jerry3','32','3100');
insert into `emp` (`id`, `name`, `age`, `salary`) values('11','Luci3','26','2900');
insert into `emp` (`id`, `name`, `age`, `salary`) values('12','Jay3','37','4500');
create index idx_emp_age_salary on emp(age,salary);1. Two ways of sorting use filesort( Low efficiency ),use index( Direct return of ordered data through index )
2. Query overlay index , The use of use index
3. Multi field sorting one up and one down does not use the index ( Unified ascending descending order )
4. The sorting field order should be consistent with the index field order
5.filesort Optimize
Two scan algorithm
One scan algorithm
1). The first is to sort the returned data , That is to say filesort Sort , All sorts that do not return sorting results directly through index are called FileSort Sort .

2). The second way is to return ordered data directly through ordered index scanning , This is the case using index, No need for extra sorting , High operating efficiency .

Multi field sorting

Try to minimize the extra ordering , Return ordered data directly through index .where Conditions and Order by Use the same index , also Order By In the same order as the index , also Order by The fields of are all in ascending order , Or in descending order . Otherwise, extra operation is necessary , And then there will be FileSort.
4.group by
group by The bottom layer should also be sorted , If you don't want to sort, add order by null;
stay group by Create an index on the field of
create index index_name on table( Name , Name );
5.OR use UNION Instead of OR
To contain OR Query clause for , If you want to use indexes , be OR Each condition column between must use an index , And you can't use composite indexes ; If there is no index , You should consider adding indexes .
1.or The following fields have no index , The index becomes invalid
or Associated conditions , Every field must have an index
2. Composite indexes cannot be used ( Do not walk in accordance with the index )
3. Use union Instead of or
select * from table where id = 1 nuion select * from table where id = 10;
select * from table where id = 1 or age = 20;
select * from table where id = 1 union select· * from table where age= 20;
6. Paging optimization
General paging query , Better performance can be achieved by creating indexes . A common and very troublesome problem is limit 2000000,10 , At this time need MySQL Before ordering 2000010 Record , Just go back to 2000000 - 2000010 The record of , Other records discarded , The cost of query sorting is very high .
Optimization idea 1
Complete sort paging operation on Index , Finally, according to the primary key Association, return to the original table to query other column contents .
. Inquire about id, take id Sort , In association query
select * from table a, (select id from table order by id limit 200000,10) b where a.id = b.id;
Optimization idea II
This scheme is applicable to tables with self increasing primary key , You can put Limit The query is converted to a query in a certain location
( Tables with self incrementing primary keys , And the primary key cannot be faulted )
select * from table where id > 200000 limit 10;
7. Use SQL Tips
After the table name in the query statement , add to use index To provide hope MySQL To refer to the index list , You can make MySQL No longer consider other available indexes .
create index idx_seller_name on tb_seller(name);
USE INDEX
select * from table use index(index_name)where
IGNORE INDEX
If the user just wants to let MySQL Ignore one or more indexes , You can use ignore index As hint .
explain select * from tb_seller ignore index(idx_seller_name) where name = ' Xiaomi Tech ';
FORCE INDEX
explain select * from tb_seller force index(idx_seller_name) where name = ' Xiaomi Tech ';
边栏推荐
- CPU over high diagnosis and troubleshooting
- Data feature analysis skills - correlation test
- Graphic control and layout basis of R visualization
- Software packaging and deployment
- Js- get the mouse coordinates and follow them
- Efficient pytorch: how to eliminate training bottlenecks
- 在国信金太阳开股票账户安全吗?
- Business layer - upper and lower computer communication protocol
- Daily question, magic square simulation
- 55 specific ways to improve program design (2)
猜你喜欢

How to download and install Weka package

1090.Phone List

Dynamic memory allocation

Why do I need message idempotence?

Design and implementation of timer

(2) Relational database

Completabilefuture of asynchronous tools for concurrent programming

Review of arrays and pointers triggered by a topic

System Verilog — interface

5 connection modes of QT signal slot
随机推荐
Learning notes on February 5, 2022 (C language)
Qmake uses toplevel or topbuilddir
Sampling method and descriptive statistical function in R language
Iterator failure condition
Is it safe to open an account for new bonds? What preparations are needed
Summary of regularization methods
Efficient pytorch: how to eliminate training bottlenecks
Design and implementation of timer
About?: Notes for
Detailed description of crontab command format and summary of common writing methods
0706-- replace fields in the use case, such as mobile phone number or ID
Some usage records about using pyqt5
[untitled] PTA check password
iconv_ Open returns error code 22
Single user mode
股票开户用什么app最安全?知道的给说一下吧
The last glory of the late Ming Dynasty - the battle of Korea
Markdown learning
Brain tree (I)
客户经理给的开户链接办理股票开户安全吗?我想开个户