1.执行计划分析
1.1什么是执行计划
select * from t1 where name='zs'; 分析的是优化器按照内置的cost计算算法,最终选择后的执行计划。 cost? 代价,成本。 对于计算机来讲,代价是什么? IO ,CPU,MEM
1.2查看执行计划
explain select * from world.city ; mysql> desc select * from world.city ;
1.3执行计划的认识
1 table : 此次查询涉及到的表 2 type : 查询类型: 全表扫,索引扫 3 possible_keys : 可能用到的索引 4 key : 最后选择的索引 5 key_len : 索引覆盖长度 6 rows : 此次查询需要扫,扫描的行数 7 Extra : 额外的信息
1.4 table
此次查询涉及到的表,真对一个查询中多个表时,精确到问题表。 desc select country.name ,city.name from city join country on city.countrycode=country.code where city.population='CHN';
1.5 type 查询类型
全盘扫面:不适用任何索引 。ALL
例如:
mysql> desc select * from city; mysql> desc select * from city where 1=1 ; mysql> desc select * from city where countrycode like '%ch%'; mysql> desc select * from city where countrycode not in ('CHN','USA'); mysql> desc select * from city where countrycode != 'CHN';
索引扫描:index < range < ref < eq_ref < const(system)
index:全索引扫描
mysql> desc select countrycode from world.city ;
range:索引范围查询: > < >= <= like in or between and
mysql> desc select * from city where id<10; mysql> desc select * from city where countrycode like 'CH%'; mysql> desc select * from city where countrycode in ('CHN','USA');
ref: 辅助索引等值查询
desc select * from city where countrycode='CHN';