explain分析sql效果

explain返回的对应参数:


select_type: 查询类型

image.png


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是扫描索引。扫描行数不少,还是不够好)

        image.png

        image.png

        以上两个例子是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个分别指查询优化到常量级别, 甚至不需要查找时间.

                    image.png


猜你喜欢

转载自blog.51cto.com/5660061/2375621