day7-mysql implementation plan analysis

Analysis of the implementation plan

What is the implementation plan

select * from t1 where name='zs';

Analysis of the implementation of the plan in accordance with the built-in optimizer cost calculation algorithm, the final choice

View the execution plan

explain select * from world.city;
desc select * from world.city;

Show Execution Plan results analysis

display Explanation
table The lookup table involved
type Query types: full table scan, index scan
possible_keys It may be used in the index
key The last selected index
key_len Index covering length
rows The number of lines of inquiry need to scan
Extra Additional information

Output information Introduction

table

When it comes to the look-up table for a query multiple tables, accurate to the questionnaire

desc select country.name,city.name 
from city join country on 
city.countrycode=country.code
where city.population='CHN';

type query type

Full table scan: without any indexes ALL

desc select * from city where countrycode='CHN';
desc select * from city where countrycode != 'CHN';
desc select * from city where 1=1;
desc select * from  city where countrycode like '%ch%';
desc select * from city where countrycode not in ('CHN','USA');

Index scan: index< range< eq_ref< NOTE: the better the performance rightcounst(system)

index: full index scan

desc select countrycode from world.city;

range: the index range query > <> = <= like in or between and

desc select * from city where id<10;
desc select * from city where countrycode like 'CH%';
desc select * from city where countrtcode in ('CHN','USA');
--->改写为 union all
select * from city where countrycode='CHN'
union all
select * from city where countrycode='USA';

Special circumstances: when to go query the primary key range

desc select * from city where id !='10'
desc select * from city where id not in (10,20);

ref: secondary indexes equivalent query

desc select * from city where countrycode='CHN';

eq_ref: multiple tables linked list, with the proviso that the non-driving table links primary or unique key.

desc select country.name,city.name
from city join country
on city.countrycode=country.code
where city.population='CHN';

const (system): clustered index equivalent query

1 desc select * from city where id=10;

possible_keys,key

1 Introduction

possible_keys: may take the index, and all the inquiries about the index.

key: The query index selection.

key_len index covering the joint length

For the joint index index(a,b,c), we want to query, the more the better for full co-indexing application.

ket_len can help us to determine that the query came several parts joint index.

For example: idx (a, b, c) ----> a ab abc

Full coverage

 

select * from t1 where a= and b= and c=
select * from t1 where a in and b in and  c in
select * from t1 where b= and c= and a=
select * from t1 where a and b order by c

Partially covered

select * from t1 where a= and b=
select * from t1 where a=
select * from t1 where a= and c=
select * from t1 where a= and b > < >= <= like and c=
select xxxx from t1 where a order by b

2. key_len calculation of: idx (a, b, c)

Assumptions: some queries can completely cover the three joint index. E.g:

select * from t1 where a= and b= and c=

key_len = a length? + B length? + C length?

Length refers to what?

Length by: data type, character set Effect

Length refers to the maximum stored value of the byte length of the column

Digital Type:

  not null No not null
tinyint 1 1+1
int 4 4+1
bigint 8 8+1

key_len:

a int not null ---> 4

a int ---> 5

Character type: utf8 character maximum duty three characters

  not null No not null
char(10) 3*10 3*10+1
varchar(10) 3*10+2 3*10+2+1

 

b char(10) not null 30

b char(10) 31

C varchar(10) not null 32

C varchar(10) 33

create table t1 (
a  int not null ,                      4
b  int ,                               5
c  char(10) not null ,                 40
d  varchar(10)                         43
)charset = utf8mb4

index(a,b,c,d)

mysql> desc select * from t1  where a=1  and b=1  and  c='a'  and d='a';
mysql> desc select * from t1  where a=1  and b=1  and  c='a' ;
mysql> desc select * from t1  where a=1  and b=1 ;
mysql> desc select * from t1  where a=1 ;

Exercise: According to key_len calculated to test the conclusions:

Full coverage

select * from t1  where a=  and b=  and  c=  
select * from t1  where a in   and b in  and  c in 
select * from t1  where  b=  and  c=  and a=  
select * from t1 where a and b  order by c  

Partially covered

select * from t1  where a=  and b= 
select * from t1  where a=  
select * from t1  where a= and  c=  
select * from t1  where a=  and b > < >= <= like   and  c=  
select xxx  from t1 where  a    order by b

Does not cover

bc、b、c

extra

using filesort: The query uses to indicate the file ordering, description sort operation in the query : order by group by distinct ..

desc select * from city where countrycode='CHN' order by population;

 

 

To be continued ....

Guess you like

Origin www.cnblogs.com/Mercury-linux/p/12339801.html