「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景( 二 )


NULL > system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALLsystem > const > eq_ref > ref > range > index > ALL?
一般至少要达到range级别,最好达到ref。
const唯一索引,非关联查询
eq_ref , refeq_ref 跟 const 的区别是:两者都利用唯一索引,但前者是关联查询,后者只是普通查询?eq_ref 跟 ref 的区别:后者是非唯一索引
index,all都是读全表,区别在于index是遍历索引树读取,而ALL是从硬盘中读取 。

「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
不走索引就会遍历全表
「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
possible_keys , keypossible_keys : 显示可能应用在这张表的索引,一个或多个 。?
key :实际使用的索引,如果为NULL,则没有使用索引 。(可能是没有走索引,需要分析)?
key_len : 表示索引中使用的字节数,在不损失精确性的前提下,长度越短越好。
  • 单列索引,那么需要将整个索引长度算进去;
  • 多列索引,不是所有列都能用到 , 需要计算查询中实际用到的列 。

「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
ref显示索引的哪一列被使用了,如果可能的话,是一个常数 。
  • 当使用常量等值查询 , 显示const
  • 当关联查询时,会显示相应关联表的关联字段
  • 如果查询条件使用了表达式、函数,或者条件列发生内部隐式转换,可能显示为func
  • 其他情况为null
  1. id是索引,而且是id=1,一个常数,故ref = const
  2. user_id不是索引,ref直接为null

「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
t1.id是索引,且=号后边不是常量,故显示t1.id,即显示相应关联表的关联字段
「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
rows
扫描行的数量 , 一般越小越好
  • 用索引 rows 就为1,无论是唯一索引还是非唯一索引
  • 其他情况一般是全表扫描,rows等于表的行数 。

「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
filtered表里符合条件的记录数的所占的百分比 。
extra其他的额外的执行计划信息,在该列展示,需要把前两个优化为using index 。
EXTRA含义using filesort说明mysql会对数据使用一个外部的索引排序 , 而不是按照表内的索引顺序进行读取,表示无法利用索引完成的排序操作,称为 “文件排序”, 效率低 。using temporary使用了临时表保存中间结果,MySQL在对查询结果排序时使用临时表 。常见于 order by 和 group by; 效率低using index表示相应的select操作使用了覆盖索引,直接从索引中过滤掉不需要的结果 , 无需回表,效率不错 。using index condition索引下推?。〔檎沂褂昧怂饕切枰乇聿檠荩耸本褪且蛭饕忻挥型耆檠?/td>
具体using index condition中的索引下推是什么意思,可以参考这篇 索引的原理&&设计原则
using where
不同版本好像不一样
5.7:表示 MySQL 首先从数据表(存储引擎)中读取记录 , 返回给 MySQL 的 server 层,然后在 server 层过滤掉不满足条件的记录,即无法直接在存储引擎过滤掉 。简单来说,就是查询时where中用的不是索引 。
「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景

文章插图
现在 , 我们知道怎么用explain来分析SQL语句了,自然可以来剖析我们的SQL语句的性能,不过早有先人给我们总结了几个需要优化的场景-->索引失效?
【二、索引失效】的几个场景0. SQL准备create table `tb_seller` ( `sellerid` varchar (100), `name` varchar (100), `nickname` varchar (50), `password` varchar (60), `status` varchar (1), `address` varchar (100), `createtime` datetime,primary key(`sellerid`))engine=innodb default charset=utf8mb4; insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('alibaba','阿里巴巴','阿里小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('baidu','百度科技有限公司','百度小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('huawei','华为科技有限公司','华为小店','e10adc3949ba59abbe56e057f20f883e','0','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('itcast','传智播客教育科技有限公司','传智播客','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('itheima','黑马程序员','黑马程序员','e10adc3949ba59abbe56e057f20f883e','0','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('luoji','罗技科技有限公司','罗技小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('oppo','OPPO科技有限公司','OPPO官方旗舰店','e10adc3949ba59abbe56e057f20f883e','0','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('ourpalm','掌趣科技股份有限公司','掌趣小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('qiandu','千度科技','千度小店','e10adc3949ba59abbe56e057f20f883e','2','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('sina','新浪科技有限公司','新浪官方旗舰店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('xiaomi','小米科技','小米官方旗舰店','e10adc3949ba59abbe56e057f20f883e','1','西安市','2088-01-01 12:00:00');insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('yijia','宜家家居','宜家家居旗舰店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00');-- 创建联合索引create index idx_seller_name_sta_addr on tb_seller(name,status,address);

推荐阅读