title, type, tags, last_updated
| title |
type |
tags |
last_updated |
| Query Plan Analysis |
concept |
| database |
| postgresql |
| explain |
| performance |
| query-optimization |
|
2026-05-01 |
Query Plan Analysis
Definition
查询计划分析是通过 EXPLAIN ANALYZE 命令解读数据库优化器生成的查询执行计划,识别性能瓶颈(如 Seq Scan 全表扫描、估算偏差大)并针对性优化的方法。
Key Scan Types
| 扫描类型 |
评估 |
说明 |
| Seq Scan |
⚠️ 差 |
全表扫描,通常意味着缺索引 |
| Index Scan |
✅ 好 |
索引扫描,直接定位数据 |
| Bitmap Heap Scan |
🟡 可接受 |
先索引定位再堆查,适合中等结果集 |
| Index Only Scan |
✅ 最佳 |
完全在索引中完成,无需回表 |
What to Look For
- actual time vs estimated rows:与估算差异大说明统计信息过时
- cost=... loops=...:高 cost 或多次 loops 需优化
- Buffers: shared hit/read:大量 read 说明缓存未命中
- Seq Scan on large tables:大表全表扫描通常是问题信号
Example
Source
Connections