MySQL中的explain命令

概念:

  • explain显示了MySQL如何使用索引来处理select语句以及连接表,我们可以根据explain的结果来优化我们的sql。

结果:

  • 官方文档:

  • id

    • SELECT操作的序号(select_id),用于标识查询中执行SELECT子句或操作表的顺序,id值越大,优先级越高,越先被执行。
  • select_type 查询类型

    • SIMPLE           普通查询 Simple SELECT (not using UNION or subqueries)
    • PRIMARY        Outermost SELECT 
    • UNION             Second or later SELECT statement in a UNION
    • UNION RESULT    Result of a UNION.
  •  table

    • 说明:扫描的表。
  • type

    • 说明:type是一个很重要的指标,type从好到差依次为:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
    • const    使用唯一索引,扫描一次就找到了对应的记录。 eg:根据主键查询数据
    • ref        使用非唯一索引进行扫描。eg:select * from t_message t where t.owner ='jack@xxx.com'    (注:在owner列上建立了非唯一索引)
    • index_merge    使用了使用了索引合并。
    • range    使用索引进行范围查询,即:在索引列上进行 LIKE、BETWEEN、IN、>= 等操作   eg:select * from t_message t where t.owner like 'jack%'    (注:在owner列上建立了非唯一索引)
    • index    按照索引的顺序进行全表扫描。注:index与ALL相比唯一的优势就是:查询出来的数据是按照一定顺序(即:索引的顺序)排列的。eg:select * from t_message order by id        (注:id为主键)
    •  ALL        全表(顺序)扫描,即:不使用索引,直接读取表上的数据。eg:select * from t_message order by title   eg:select * from t_message
  • possible_keys

    • 说明:可以使用的索引。
  •  key

    • 说明:实际使用的索引。
    • null    表示没有使用索引。
    • PRIMARY    表示使用了主键。
  • key_len

    • 说明:实际使用的索引的长度,从这个指标可以判断出复合索引中的哪些列被使用了。
  • ref

    • const    使用索引进行等值查询。 eg:select * from t_message where owner='jxn'         (注:在owner列上建立了索引)
  • rows

    • 说明:执行该sql时,需要扫描的行数。
    • 该值越小越好,若查询条件未命中索引,则全表查询,此时rows为该表的总行数。
  • filtered

    • Percentage of rows filtered by table condition

    • The filtered column indicates an estimated percentage of table rows that are filtered by the table condition. The maximum value is 100, which means no filtering of rows occurred. Values decreasing from 100 indicate increasing amounts of filtering. rows shows the estimated number of rows examined and rows × filtered shows the number of rows that are joined with the following table. For example, if rows is 1000 and filtered is 50.00 (50%), the number of rows to be joined with the following table is 1000 × 50% = 500.

  • Extra

    • 说明:表示其它的一些信息。
    • Using index
      • 只从索引树中查询信息,即该sql只查询索引列的值。 eg:select owner from t_message                (注:在owner列上建立了索引)
    • Using index condition
      • 使用了索引条件下推(index condition pushdown)
    • Using where
      • 使用了where语句。
    • Using filesort 
      • 排序时没有根据索引来排序,一般可以通过在排序字段上建立合适的索引来减少或者避免。
      • 注意:filesort这个名字起的很糟糕,filesort并不意味着使用了外部排序(硬盘排序)!
    • Using temporary      使用了临时表 
    • Using sort_union(idx_2,idx_2) 使用了索引合并
                  
                  
                  
                  
              
              
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值