Skip to content

读懂执行计划 ​

基本命令 ​

sql
EXPLAIN SELECT ...;
EXPLAIN ANALYZE SELECT ...;  -- 8.0.18+,含实际耗时

关键列(习惯) ​

  • type:从优到劣常见 system → const → eq_ref → ref → range → index → ALL(全表扫)。
  • key:实际使用的索引。
  • rows:预估扫描行数,越小越好(估算值)。
  • Extra:Using where、Using index、Using filesort、Using temporary 等;后两者在大查询上需关注。

步骤 ​

  1. 确认是否走错索引(possible_keys vs key)。
  2. 能否 加索引 或 改写 SQL 减少 ALL 与 filesort。