select * from t1 where name='zs';
分析的是优化器按照内置的cost计算算法,最终选择后的执行计划
explain select * from world.city; 或 desc select * from world.city;
说明 | |
table | 此次查询涉及到的表 |
type | 查询类型:全表扫、索引扫 |
possible_keys | 可能用到的索引 |
key | 最后选择的索引 |
key_len | 索引覆盖长度 |
rows | 此次查询需要扫描的行数 |
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:辅助索引等值查询
desc select * from city where countrycode='CHN';
eq_ref:多表链表中,非驱动表链接条件是主键或唯一键。
desc select country.name,city.name from city join country on city.countrycode=country.code where city.population='CHN';
const(system):聚簇索引等值查询
1 desc select * from city where id=10;
ket_len 可以帮助我们判断,此次查询,走了联合索引的几部分。
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
部分覆盖
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的计算:idx(a,b,c)
假设:某条查询可以完全覆盖三列联合索引。例如:
select * from t1 where a= and b= and c=
长度受到:数据类型 , 字符集 影响
长度指的是,列的最大储值字节长度
not null | 没有not null | |
tinyint | 1 | 1+1 |
int | 4 | 4+1 |
bigint | 8 |
a int ---> 5
字符类型: utf8
not null | 没有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 ;
练习:根据 key_len计算验证一下结论:
全部覆盖
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
部分覆盖
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
不覆盖
bc、b、c
desc select * from city where countrycode='CHN' order by population;
未完待续....