Mysql之sql语句优化:explain

╰+攻爆jí腚メ 2022-07-12 07:59 392阅读 0赞
  • explain的列分析

        • select_type 查询类型
        • table 查询针对的表
        • possible_key 可能用到的索引
        • key 最终用的索引
        • key_len 使用的索引的最大长度
        • type列 是指查询的方式 非常重要是分析查数据过程的重要依据
        • ref列 指连接查询时 表之间的字段引用关系
        • rows 是指估计要扫描多少行
        • extra

explain的列分析

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

  1. mysql> explain select goods_id,goods_name from goods where goods_id in (sele
  2. ct goods_id from goods where cat_id=4) \G
  3. *************************** 1. row ***************************
  4. id: 1
  5. select_type: PRIMARY
  6. table: goods
  7. type: ALL
  8. possible_keys: NULL
  9. key: NULL
  10. key_len: NULL
  11. ref: NULL
  12. rows: 31
  13. Extra: Using where
  14. *************************** 2. row ***************************
  15. id: 2
  16. select_type: DEPENDENT SUBQUERY
  17. table: goods
  18. type: unique_subquery
  19. possible_keys: PRIMARY,cat_id
  20. key: PRIMARY
  21. key_len: 3
  22. ref: func
  23. rows: 1
  24. Extra: Using where
  25. 2 rows in set (0.00 sec)

select_type: 查询类型

  1. simple (不含子查询)
  2. primary (含子查询或派生查询)
  3. subquery (非from子查询)
  4. derived (from型子查询)
  5. union
  6. union result

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来查询

  1. mysql> explain select goods_name from goods where goods_name='诺基亚N85' \G
  2. *************************** 1. row ***************************
  3. id: 1
  4. select_type: SIMPLE
  5. table: goods
  6. type: ALL
  7. possible_keys: NULL
  8. key: NULL
  9. key_len: NULL
  10. ref: NULL
  11. rows: 31
  12. Extra: Using where
  13. 1 row in set (0.00 sec)
  • index: 比all性能稍好一点,
  1. > 通俗的说: all 扫描所有的数据行,相当于data\_all index 扫描所有的索引节点,相当于index\_all

2种情况可能出现:
1:索引覆盖的查询情况下, 能利用上索引,但是又必须全索引扫描.

  1. mysql> explain select goods_id from goods where goods_id=1 or goods_id+1>20
  2. \G
  3. *************************** 1. row ***************************
  4. id: 1
  5. select_type: SIMPLE
  6. table: goods
  7. type: index
  8. possible_keys: PRIMARY
  9. key: PRIMARY
  10. key_len: 3
  11. ref: NULL
  12. rows: 31
  13. Extra: Using where; Using index
  14. 1 row in set (0.00 sec)
  15. mysql> explain select goods_id,click_count from goods where goods_id=1 or go
  16. ods_id+1>20 \G
  17. *************************** 1. row ***************************
  18. id: 1
  19. select_type: SIMPLE
  20. table: goods
  21. type: ALL
  22. possible_keys: PRIMARY
  23. key: NULL
  24. key_len: NULL
  25. ref: NULL
  26. rows: 31
  27. Extra: Using where
  28. 1 row in set (0.00 sec)

2: 是利用索引来进行排序,但取出所有的节点 select goods_id from goods order by goods_id desc; 分析: 没有加where条件, 就得取所有索引节点,同时,又没有回行,只取索引节点. 再排序,经过所有索引节点.

  1. mysql> explain select goods_id from goods order by goods_id asc\G
  2. *************************** 1. row ***************************
  3. id: 1
  4. select_type: SIMPLE
  5. table: goods
  6. type: index
  7. possible_keys: NULL
  8. key: PRIMARY
  9. key_len: 3
  10. ref: NULL
  11. rows: 31
  12. Extra: Using index
  13. 1 row in set (0.00 sec)
  • 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索引 指向N行goods数据,来查得结果.

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

    mysql> explain select goods_id,shop_price from goods innert join ecs_catego
    y 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

  1. mysql> explain select goods_id,goods_name,click_count from goods wher
  2. _id=4 \G
  3. *************************** 1. row ***************************
  4. id: 1
  5. select_type: SIMPLE
  6. table: goods
  7. type: const
  8. possible_keys: PRIMARY
  9. key: PRIMARY
  10. key_len: 3
  11. ref: const
  12. rows: 1
  13. Extra:
  14. 1 row in set (0.00 sec)
  15. mysql> explain select max(goods_id) from goods \G
  16. *************************** 1. row ***************************
  17. id: 1
  18. select_type: SIMPLE
  19. table: NULL
  20. type: NULL myisam表的max,min,count在表中优化过,不需要\真正查找,为NULL
  21. possible_keys: NULL
  22. key: NULL
  23. key_len: NULL
  24. ref: NULL
  25. rows: NULL
  26. Extra: Select tables optimized away
  27. 1 row in set (0.00 sec)

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

  1. mysql> explain select goods_id,cat_name,goods_name from goods inner join ec
  2. _category using(cat_id) where ecs_category.cat_name='' \G
  3. *************************** 1. row ***************************
  4. id: 1
  5. select_type: SIMPLE
  6. table: goods
  7. type: ALL
  8. possible_keys: cat_id
  9. key: NULL
  10. key_len: NULL
  11. ref: NULL
  12. rows: 31
  13. Extra:
  14. *************************** 2. row ***************************
  15. id: 1
  16. select_type: SIMPLE
  17. table: ecs_category
  18. type: eq_ref
  19. possible_keys: PRIMARY
  20. key: PRIMARY
  21. key_len: 2
  22. ref: shop. goods.cat_id
  23. rows: 1
  24. Extra: Using where
  25. 2 rows in set (0.00 sec)

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

extra:

  1. index: 是指用到了索引覆盖,效率非常高
  2. using where 是指光靠索引定位不了,还得where判断一下
  3. using temporary 是指用上了临时表, group by 与order by 不同列时,或group by ,order by 别的表的列.
  4. using filesort : 文件排序(文件可能在磁盘,也可能在内存), (?????

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

发表评论

表情:
评论列表 (有 0 条评论,392人围观)

还没有评论,来说两句吧...

相关阅读