gpt4 book ai didi

performance - Postgresql - 使用索引过滤表中超过 1 亿行的行

转载 作者:行者123 更新时间:2023-11-29 13:35:27 24 4
gpt4 key购买 nike

我有超过 1 亿行的表格。我必须像下面的查询一样计算并提取行。查询运行时间很长。 Explain 显示查询不使用在“created_date”列上创建的 b 树索引。我在 stackoverflow 上找到了一些解释,当表有很多行时,b 树索引对过滤没有用。

有一个Cluster Index的建议。如果我也经常使用查询,我应该在“created_date”索引上聚簇表吗?

您有什么建议可以加快查询速度?也许我应该阅读更多关于分片的内容?

 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))

最佳答案

从外观上看,数据库已决定查找一个 searches_id 将产生比查找 created_date 范围更少的行。 (并且将两个索引扫描的结果与位图结合起来是不值得的......)

如果你经常需要这个查询,那么考虑在 searches_id, created_date 上创建一个索引,然后这两个条件都应该进入索引条件。

关于performance - Postgresql - 使用索引过滤表中超过 1 亿行的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13846704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com