Mysql execution plan 2

Previous article-Mysql execution plan

Covering index

Covering Index (Covering Index), said to be index coverage.
Understanding method 1: select data columns can be obtained from the index only, without reading the data rows, MySQL can use the index to return the fields in the select list, without having to read the data file again according to the index, in other words query the column To be covered by the created index.
Understanding method 2: Index is a way to find rows efficiently, but the general database can also use the index to find the data of a column, so it does not have to read the entire row. After all, the index leaf nodes store the data they index; when the desired data can be obtained by reading the index, there is no need to read rows. An index that contains (or covers) data that meets the query results is called a covering index

Note: If you want to use a covering index, be sure to take out only the required columns from the select list, not select *, because if all fields are indexed together, the index file will be too large and the query performance will decrease. Therefore, you must not create indexes on all columns for query, which will seriously affect the performance of modification and maintenance.

Using where 与 using join buffer

Using where indicates that where filtering is used,
using join buffer, and the connection cache is used:

show VARIABLES like '%join_buffer_size%'

Insert picture description here
Insert picture description here
EXPLAIN select * from t1 JOIN t2 on t1.other_column = t2.other_column

impossible where

The value of the where clause is always false and cannot be used to get any tuples (the impossible query conditions)Insert picture description here

EXPLAIN select * from t1 where 1=2
EXPLAIN select * from t1 where t1.other_column =‘enjoy’ and t1.other_column = ‘edu’

SQL optimization

Optimize actual combat

Strategy 1. Try to match the full value

Insert picture description here

CREATE TABLE `staffs`( id int primary key auto_increment,
 name varchar(24) not null default "" comment'姓名',
 age int not null default 0 comment '年龄', 
 pos varchar(20) not null default "" comment'职位', 
 add_time timestamp not null default current_timestamp comment '入职时间' )charset utf8 comment '员工记录表';
insert into staffs(name,age,pos,add_time) values('z3',22,'manage',now());
 insert into staffs(name,age,pos,add_time) values('july',23,'dev',now()); 
 insert into staffs(name,age,pos,add_time) values('2000',23,'dev',now()); 
 alter table staffs add index idx_staffs_nameAgePos(name,age,pos); EXPLAIN SELECT * FROM staffs WHERE NAME = 'July';

EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' AND age = 25; 
EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' AND age = 25 AND pos = 'dev'

When the index column is created, you can use the index in the wherel condition as much as possible

Strategy 2. The best left prefix rule

If multiple columns are indexed, follow the leftmost prefix rule. It means that the query starts from the leftmost front column of the index and does not skip the columns in the index.
Insert picture description here

EXPLAIN SELECT * FROM staffs WHERE age = 25 AND pos = 'dev' 
EXPLAIN SELECT * FROM staffs WHERE pos = 'dev' 
EXPLAIN SELECT * FROM staffs WHERE NAME = 'July'

Strategy 3. Do not do anything on the index column

Do not do any operation (calculation, function, (automatic or manual) type conversion) on the index column, it will cause the index to fail and turn to the full table scan
Insert picture description here

EXPLAIN SELECT * FROM staffs WHERE NAME = 'July'; 
EXPLAIN SELECT * FROM staffs WHERE left(NAME,4) = 'July';

Strategy 4. Scope conditions put last

Insert picture description here

EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' ; 
EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' and age =22;

 EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' and age =22 and pos='manager' 中间有范围查询会导致后面的索引列全部失效 
 EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' and age >22 and pos='manager'

Strategy 5. Use the covering index as much as possible

Insert picture description here
Try to use a covering index (only access to the index query (the index column and the query column are consistent)), reduce the select *

EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' and age =22 and pos='manager' 
EXPLAIN SELECT name,age,pos FROM staffs WHERE NAME = 'July' and age =22 and pos='manager' 
EXPLAIN SELECT * FROM staffs WHERE NAME = 'July' and age >22 and pos='manager' 
EXPLAIN SELECT name,age,pos FROM staffs WHERE NAME = 'July' and age >22 and pos='manager'

Strategy 6. Doesn't mean you have to use it

MySQL cannot use the index when it is not equal to (!= or <>), which will cause a full table scan
Insert picture description here

EXPLAIN SELECT * FROM staffs WHERE NAME = 'July'; EXPLAIN SELECT * FROM staffs WHERE NAME != 'July'; 
EXPLAIN SELECT * FROM staffs WHERE NAME <> 'July'; 
#如果定要需要使用不等于,请用覆盖索引 
EXPLAIN SELECT name,age,pos FROM staffs WHERE NAME != 'July'; 
EXPLAIN SELECT name,age,pos FROM staffs WHERE NAME <> 'July';

Strategy 7. Null/Not has an impact

Note the possible impact of null/not null on the index

Customize as NOT NULL

Insert picture description here
Insert picture description here
EXPLAIN select * from staffs where name is null
EXPLAIN select * from staffs where name is not null
When the field is not null, using is null or is not null will cause the index to become invalid

Solution: covering index
EXPLAIN select name, age, pos from staffs where name is not null

Customize as NULL or undefined

Insert picture description here
EXPLAIN select * from staffs2 where name is null
Insert picture description here
EXPLAIN select * from staffs2 where name is not null
Insert picture description here
Is not null will cause the index to become invalid

Solution: covering index
EXPLAIN select name, age, pos from staffs where name is not null

Strategy 8. Look out for like queries

like starts with a wildcard ('%abc...') mysql index failure will become a full table scan operation
Insert picture description here

EXPLAIN select * from staffs where name ='july'
EXPLAIN select * from staffs where name like '%july%'
EXPLAIN select * from staffs where name like '%july'
EXPLAIN select * from staffs where name like 'july%' 

Solution: Covering index
EXPLAIN select name, age, pos from staffs where name like'%july%'

Strategy 9. Quote character types

String without single quotation marks index failure
Insert picture description here
EXPLAIN select * from staffs where name = 917

Solution: covering index EXPLAIN select name, age, pos from staffs where name = 917

Solution: please add quotation marks

Strategy 10. OR to UNION is more efficient

Insert picture description here

EXPLAIN select * from staffs where name='July' or name = 'z3' 
EXPLAIN select * from staffs where name='July' UNION select * from staffs where name = 'z3' 

Solution: Covering index
EXPLAIN select name, age from staffs where name='July' or name ='z3'

Test questions

Insert picture description here

Memory summary

Memory summary:

  • Full-time match my favorite, the leftmost prefix must be observed;
  • The leading brother cannot die, and the middle brother cannot be broken;
  • Less calculation on the index column, all invalid after the range;
  • LIKE is written to the right in percent, and the coverage index is not written*;
  • Not equal to the null value and OR, pay attention to the impact of the index;
  • VAR quotes can't be lost, SQL optimization has a trick.

Batch Import

Insert picture description here

insert statement optimization;

  • Turn off automatic submission before submitting
  • Try to use batch insert statements
  • Can use MyISAM storage engine

LOAD DATA INFLIE

LOAD DATA INFLIE;
using LOAD DATA INFLIE, 20 times faster than the general insert statement

 select * into OUTFILE 'D:\\product.txt' from product_info 
 load data INFILE 'D:\\product.txt' into table product_info 
 load data INFILE '/soft/product3.txt' into table product_info
show VARIABLES like 'secure_file_priv'
  • When secure_file_priv is NULL, it means that mysqld is restricted from not allowing import or export.
  • When secure_file_priv is /tmp, it means that mysqld can only be imported and exported in the /tmp directory, and cannot be executed in other directories.
  • When secure_file_priv has no value, it means that mysqld is not restricted from importing and exporting in any directory. secure_file_priv=''

answer:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42292697/article/details/114752985