技术笔记分享

explain语法

1.查看中国和美国的城市人口数量

2.查看三个命令的执行计划

3.查询结果注释

查询数据的方式

全表扫描

索引扫描

#从上到下查询速度依次越来越快
1.index #全索引扫描
mysql> explain select name from city;

2.range #范围查询使用该级别,但是当查询数据量过大的时候不走索引
mysql> explain select name,population from city where countrycode='CHN' or countrycode='USA';
mysql> explain select name,population from city where population > 3000000;

3.ref #使用精确查询
mysql> explain select name,population from city where countrycode='CHN';

4.eq_ref #使用join on时可能出现该级别
mysql> explain select city.name,city.population,country.name from country join city on city.countrycode=country.code where city.population < 100;

5.const #当查询条件是主键或者唯一键的时候
mysql> explain select * from city where id='1';

6.system #跟const平级,当查询的数据所在表数据量很小的时候,并且查询条件使用主键或者唯一键

7.null #当不用读取数据库数据的时候
mysql> explain select max(population) from city;

发表评论

邮箱地址不会被公开。 必填项已用*标注