【发布时间】:2017-04-26 20:00:06
【问题描述】:
这是Slow Postgres 9.3 queries 问题的后续。
新索引肯定有帮助。但是我们看到有时查询在实践中比我们运行 EXPLAIN ANALYZE 时要慢得多。下面是一个示例,在生产数据库上运行:
explain analyze SELECT * FROM messages WHERE groupid=957 ORDER BY id DESC LIMIT 20 OFFSET 31980;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=127361.90..127441.55 rows=20 width=747) (actual time=152.036..152.143 rows=20 loops=1)
-> Index Scan Backward using idx_groupid_id on messages (cost=0.43..158780.12 rows=39869 width=747) (actual time=0.080..150.484 rows=32000 loops=1)
Index Cond: (groupid = 957)
Total runtime: 152.186 ms
(4 rows)
启用慢速查询日志记录后,我们看到此查询的实例耗时超过 2 秒。我们也有log_lock_waits=true,大约在同一时间没有报告慢锁。什么可以解释执行时间的巨大差异?
【问题讨论】:
-
当您
explain analyze时,您可能已经在缓存中获得了“热”数据。在这些情况下,您可能不这样做,因此完成了更多 I/O。使用explain (buffers, analyze)查看缓冲区使用情况。 -
你需要偏移量做什么?如果要分页,请阅读this。也许您可以通过不同的方式来加快处理速度。
标签: postgresql indexing sql-execution-plan explain postgresql-performance