explain分析

mysql> explain select goods_id,goods_name from  goods where goods_id in (sele

ct goods_id from  goods where cat_id=4) \G

*************************** 1. row ***************************

           id: 1

  select_type: PRIMARY

        table:  goods

         type: ALL

possible_keys: NULL

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 31

        Extra: Using where

*************************** 2. row ***************************

           id: 2

  select_type: DEPENDENT SUBQUERY

        table:  goods

         type: unique_subquery

possible_keys: PRIMARY,cat_id

          key: PRIMARY

      key_len: 3

          ref: func

         rows: 1

        Extra: Using where

2 rows in set (0.00 sec)

id:  代表select 语句的编号, 如果是连接查询,表之间是平等关系, select 编号都是1,从1开始. 如果某select中有子查询,则编号递增.id相同则从上往下执行,id递增则从下往上执行

select_type: 查询类型

table: 查询针对的表

有可能是

实际的表名  如select * from t1;

表的别名    如 select * from t2 as tmp;

derived      如from型子查询时

null         直接计算得结果,不用走表

type: 是指查询的方式, 非常重要,是分析”查数据过程”的重要依据

①all:  意味着从表的第1行,往后,逐行做全表扫描.,运气不好扫描到最后一行.

②index: 比all性能稍好一点,通俗的说: all 扫描所有的数据行,相当于data_all  index 扫描所有的索引节点,相当于index_all

③range: 意思是查询时,能根据索引做范围的扫描

mysql> explain select goods_id,goods_name,shop_price from  goods where goodsid >25 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table:  goods

         type: range

possible_keys: PRIMARY

          key: PRIMARY

      key_len: 3

          ref: NULL

         rows: 8

        Extra: Using where

1 row in set (0.00 sec)

④ref  意思是指 通过索引列,可以直接引用到某些数据行

mysql> explain select goods_id,goods_name from  goods where cat_id=4 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table:  goods

         type: ref

possible_keys: cat_id

          key: cat_id

      key_len: 2

          ref: const

         rows: 3

        Extra:

1 row in set (0.00 sec)

在这个例子中,通过cat_id索引 指向N行goods数据,来查得结果.

⑤eq_ref 是指,通过索引列,直接引用某1行数据

常见于连接查询中

mysql> explain select goods_id,shop_price from  goods 
innert join ecs_categoy using(cat_id) where goods_id> 25 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: innert

         type: range

possible_keys: PRIMARY,cat_id

          key: PRIMARY

      key_len: 3

          ref: NULL

         rows: 8

        Extra: Using where

*************************** 2. row ***************************

           id: 1

  select_type: SIMPLE

        table: ecs_category

         type: eq_ref

possible_keys: PRIMARY

          key: PRIMARY

      key_len: 2

          ref: shop.innert.cat_id

         rows: 1

        Extra: Using index

2 rows in set (0.00 sec)

⑥const, system, null  这3个分别指查询优化到常量级别, 甚至不需要查找时间.

一般按照主键来查询时,易出现const,system或者直接查询某个表达式,不经过表时, 出现NULL

mysql> explain select goods_id,goods_name,click_count from goods wher_id=4 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table:  goods

         type: const

possible_keys: PRIMARY

          key: PRIMARY

      key_len: 3

          ref: const

         rows: 1

        Extra:

1 row in set (0.00 sec)

mysql> explain select max(goods_id) from  goods \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: NULL

         type: NULL myisam表的max,min,count在表中优化过,不需要\真正查找,为NULL

possible_keys: NULL

          key: NULL

      key_len: NULL

          ref: NULL

         rows: NULL

        Extra: Select tables optimized away

1 row in set (0.00 sec)

possible_key: 可能用到的索引,注意: 系统估计可能用的几个索引,但最终,只能用1个.

key : 最终用的索引.

key_len: 使用的索引的最大长度

ref 指连接查询时, 表之间的字段引用关系.

mysql> explain select goods_id,cat_name,goods_name from  goods 
inner join ec_category using(cat_id) where ecs_category.cat_name='' \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table:  goods

         type: ALL

possible_keys: cat_id

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 31

        Extra:

*************************** 2. row ***************************

           id: 1

  select_type: SIMPLE

        table: ecs_category

         type: eq_ref

possible_keys: PRIMARY

          key: PRIMARY

      key_len: 2

          ref: shop. goods.cat_id

         rows: 1

        Extra: Using where

2 rows in set (0.00 sec)

rows : 是指估计要扫描多少行.

extra:

①index: 是指用到了索引覆盖,效率非常高

②using where 是指光靠索引定位不了,还得where判断一下

③using temporary 是指用上了临时表, group by 与order by 不同列时,或group by ,order by 别的表的列.

④using filesort : 文件排序(文件可能在磁盘,也可能在内存)

select sum(shop_price) from  goods group by cat_id 这句话,用到了临时表和文件排序

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值