【发布时间】:2013-01-16 01:43:43
【问题描述】:
需要有关 PostgreSQL 中查询性能的帮助。它似乎与索引有关。
这个查询:
- 根据
type过滤 - 按
timestamp排序,升序:
SELECT * FROM the_table WHERE type = 'some_type' ORDER BY timestamp LIMIT 20
索引:
CREATE INDEX the_table_timestamp_index ON the_table(timestamp);
CREATE INDEX the_table_type_index ON the_table(type);
type 字段的值只是大约 11 个不同字符串之一。
问题是查询似乎在 O(log n) 时间内执行,大多数时候只需要几毫秒,除了 type 的某些值需要几分钟才能运行。
在这些示例查询中,第一个查询仅需几毫秒即可运行,而第二个查询则需要 30 多分钟:
SELECT * FROM the_table WHERE type = 'goq' ORDER BY timestamp LIMIT 20
SELECT * FROM the_table WHERE type = 'csp' ORDER BY timestamp LIMIT 20
我有大约 90% 的把握怀疑我们拥有的索引不是正确的。我认为,在阅读this similar question about index performance 之后,我们最需要的是一个综合索引,超过type 和timestamp。
我运行的查询计划在这里:
-
Expected performance, type-specific index (i.e. new index with the type = 'csq' in the
WHEREclause)。 - Slowest, problematic case, indexes as described above.
- Fast case, same indexes as above.
非常感谢您的帮助!任何指针将不胜感激!
【问题讨论】:
-
索引的大小是多少?以及数据集的大小?
标签: sql performance postgresql indexing