扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
explain的type列表示该条查询的扫描范围,一共有七种,效果由上到下排列:
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了抚松免费建站欢迎大家使用!system>const>eq_ref>ref>range>index>all。
数据准备:
CREATE TABLE `t_blog` ( `id` int(11) NOT NULL auto_increment, `title` varchar(50) default NULL, `typeId` int(11) default NULL, PRIMARY KEY (`id`), UNIQUE KEY `title_index` (`title`), KEY `type_index` (`typeId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 CREATE TABLE `t_type` ( `id` int(11) NOT NULL auto_increment, `name` varchar(20) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf81、system
该表中只有一行记录,这种情况在日常开发中很少见,不多赘述;
2、const
表示通过索引一次就找到了结果,用于扫描主键和唯一索引,例如:
mysql> explain select * from t_blog where id = 1; +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | t_blog | const | PRIMARY | PRIMARY | 4 | const | 1 | | +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+ 1 row in set在where子句中,id为主键且值为一个常数,在id索引中只有一条数据与之对应。
3、eq_ref
通过主键和唯一索引,只有一条数据与之匹配,例如:
mysql> explain select b.* from t_blog b left join t_type t on b.typeId = t.id; +----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+ | 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 7 | | | 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | Using index | +----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+ 2 rows in seteq_ref和const都表示在唯一索引或主键的作用下,只找到一行与之匹配的数据。const表示按主键和唯一索引读取,eq_ref通常体现在连表上,按连表的主键和唯一索引读取。
4、ref
非唯一索引扫描,有多个行与之匹配
mysql> explain select * from t_blog where typeId = 4; +----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+ | 1 | SIMPLE | t_blog | ref | type_index | type_index | 5 | const | 1 | Using where | +----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+ 1 row in settypeId是表的普通索引,即非唯一索引,与eq_ref大的区别在于ref表示非唯一索引扫描。
5、range
表示范围,使用索引选择行,使用了 > < in beteen等
mysql> EXPLAIN select * from t_blog where id>2; +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | t_blog | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where | +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set6、index
遍历索引树,读全表
mysql> EXPLAIN select id from t_blog; +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | t_blog | index | NULL | PRIMARY | 4 | NULL | 7 | Using index | +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set只查询id,所以只遍历索引文件即可,不需要从硬盘中读取,比all快。
7、all
读全表,不使用任何索引,从硬盘中读数据,最慢
mysql> explain select * from t_blog; +----+-------------+--------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | | +----+-------------+--------+------+---------------+------+---------+------+------+-------+ 1 row in set*在一般的开发过程中,达到ref即可
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流