Skip to content
极高进阶

一句话答案

explain 分析 SQL 性能,关键字段:type(访问类型,避免 ALL)、key(使用的索引)、rows(扫描行数)、Extra。

核心要点

type优劣: system > const > eq_ref > ref > range > index > ALL

重点字段:

字段关注
type避免ALL全表扫描
key是否使用索引
rows扫描行数越少越好
ExtraUsing index(覆盖索引好) / Using filesort(需优化)
追问与易错

追问方向:

  • type 为 ALL 一定需要优化吗?
  • Extra 中 Using filesort 怎么优化?
  • key_len 怎么计算?

易错点:

  • ❌ 有索引 type 就不是 ALL——优化器可能选择不走
  • ❌ 只看 type 不看 rows

💡 记忆锚点

EXPLAIN 是 SQL 的体检报告:type 看体质(ALL 全表扫描最差,const 最优),key 看有没有用索引这根拐杖,rows 看要翻多少页病历,Extra 看有没有坏习惯(Using filesort 需优化,Using index 说明覆盖索引很健康)。优化口诀:消灭 ALL、减少 rows、争取 Using index。