我有超过 1 亿行的表格。我必须像以下查询一样计算和提取行。查询运行时间很长。解释表明查询不使用在“created_date”列上创建的 b-tree 索引。我在stackoverflow上找到了一些解释,当表有很多行时,b-trees indicies 对过滤是无用的。
集群索引有一个建议。如果我也经常使用查询,我应该在“created_date”索引上聚集表,我在哪里按 ID 排序?
您对更快的查询有何建议?也许我应该阅读更多关于分片的信息?
explain SELECT count(r.id) FROM results_new r
WHERE r.searches_id = 4351940 AND (created_date between '2008-01-01' and '2012-12-13')
Limit (cost=1045863.78..1045863.79 rows=1 width=4)
-> Aggregate (cost=1045863.78..1045863.79 rows=1 width=4)
-> Index Scan using results_new_searches_id_idx on results_new r (cost=0.00..1045012.38 rows=340560 width=4)"
Index Cond: (searches_id = 4351940)"
Filter: ((created_date >= '2008-01-01 00:00:00'::timestamp without time zone) AND (created_date <= '2012-12-13 00:00:00'::timestamp without time zone))