我有超过100mln行的表。我必须计算,并提取行,如下面的查询。查询运行得很长。说明显示查询不使用在“created_date”列上创建的b-tree索引。 我在stackoverflow上找到了一些解释,当表有很多行时,b-tree标记无法过滤。
群集索引有一个建议。我应该在“created_date”索引上使用Cluster表,如果我也经常使用查询,那么我在ORDER BY 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))
答案 0 :(得分:1)
从它的外观来看,数据库已经确定,查找一个searches_id
将产生的行数少于查找created_date
范围。 (并且将两个索引扫描的结果与位图结合起来是不值得的......)
如果您经常需要此查询,请考虑在searches_id, created_date
上创建索引,然后这两个条件都应该进入索引条件。