explain返回的对应参数:


select_type: 查询类型

explain分析sql效果_效果


table: 查询针对的表

有可能是

实际的表名  select * from t1;

表的别名     select * from t2 as tmp;

derived      from型子查询时

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


possible_key: 可能用到的索引

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

 

key : 最终用的索引.


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


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

可能的值

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

        例: goods_name列上的索引去掉, 并根据goods_name来查询

        mysql> explain select goods_name from goods where goods_name='诺基亚N85' \G

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

          id: 1

          select_type: SIMPLE

             table: goods

             type: ALL

          possible_keys: NULL

          key: NULL

          key_len: NULL

          ref: NULL

          rows: 31

          Extra: Using where

          1 row in set (0.00 sec)

    index:all性能稍好一点,通俗的说: all 扫描所有的数据行,相当于data_all 。 index 扫描所有的索引节点,相当于index_all(也要扫描所有行,只不过all是磁盘上扫描,index是扫描索引。扫描行数不少,还是不够好)

        explain分析sql效果_sql_02

        explain分析sql效果_分析_03

        以上两个例子是goods_id有索引,其他没有

            

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

                    mysql> explain select goods_id,goods_name,shop_price from  goods where goods

                    id >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索引 指向Ngoods数据,来查得结果.


            eq_ref:是指,通过索引列,直接引用某1行数据常见于连接查询中


    const, system, null:3个分别指查询优化到常量级别, 甚至不需要查找时间一般按照主键来查询时,易出现const,system或者直接查询某个表达式,不经过表时, 出现NULL

                    explain分析sql效果_效果_04

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(using index最优,其次using where。其他几个最好不出现):

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

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

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

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






explain误区:

    explain不产生查询(比较复杂的查询,explain也会产生查询。5.6之后不产生查询了

    对limit的解释(用到limit也可能会产生查询)

    explain即执行过程(只是优化器分析出的结果,实际未必)

5.6的变化:

    5.6之后explain不产生查询了

    允许解释非select语句(如update等)