【发布时间】:2013-09-20 22:42:31
【问题描述】:
我正在尝试获取表中的最新行。我有一个简单的时间戳created_at,它已被索引。当我查询ORDER BY created_at DESC LIMIT 1 时,它所花费的时间远远超出我的想象(在我的机器上,36k 行大约需要 50 毫秒)。
EXPLAIN-ing 声称它使用 反向索引扫描,但我确认将索引更改为 (created_at DESC) 不会改变查询计划器中的成本简单的索引扫描。
如何优化这个用例?
运行 postgresql 9.2.4。
编辑:
# EXPLAIN SELECT * FROM articles ORDER BY created_at DESC LIMIT 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..0.58 rows=1 width=1752)
-> Index Scan Backward using index_articles_on_created_at on articles (cost=0.00..20667.37 rows=35696 width=1752)
(2 rows)
【问题讨论】:
-
能否将 EXPLAIN 的结果添加到问题中?
标签: sql postgresql indexing sql-order-by postgresql-performance