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
explain select * from world.city; 或 desc select * from world.city;
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 |
desc select country.name,city.name from city join country on city.countrycode=country.code where city.population='CHN';
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');
desc select countrycode from world.city;
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';
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;
ket_len can help us to determine that the query came several parts joint index.
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=
Length by: data type, character set Effect
Length refers to the maximum stored value of the byte length of the column
not null | No not null | |
tinyint | 1 | 1+1 |
int | 4 | 4+1 |
bigint | 8 |
a int ---> 5
Character type: utf8
not null | No not null | |
char(10) | 3*10 | 3*10+1 |
varchar(10) | 3*10+2 |
b char(10) 31
C varchar(10) not null 32
C varchar(10)
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
desc select * from city where countrycode='CHN' order by population;
To be continued ....