Appearance
读懂执行计划
基本命令
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等;后两者在大查询上需关注。
步骤
- 确认是否走错索引(
possible_keysvskey)。 - 能否 加索引 或 改写 SQL 减少
ALL与filesort。