【发布时间】:2017-11-16 15:23:20
【问题描述】:
我在 Amazon RDS 生产数据库上有一个相对较大的表(在 2M 条记录范围内)。我希望对多个字段进行分组,包括表中日期的月份(server_time)。为了加快速度,我在主数据库上创建了一个索引,如下所示:
create index on build_requests(group_id, artifact_id, account_id, number_of_interfaces, date_trunc('month', server_build_time));
然后,如您所料,对数据进行分组的查询使用主服务器上的索引:
GroupAggregate (cost=0.55..311308.09 rows=1633231 width=85)
Group Key: group_id, artifact_id, account_id, number_of_interfaces, date_trunc('month'::text, server_build_time)
-> Index Scan using build_requests_group_id_artifact_id_account_id_number_of_in_idx on build_requests (cost=0.55..262417.68 rows=1898335 width=85)
但是,等待一个多小时后,只读副本仍然没有使用索引:
GroupAggregate (cost=434678.88..488313.41 rows=1633179 width=85)
Group Key: group_id, artifact_id, account_id, number_of_interfaces, (date_trunc('month'::text, server_build_time))
-> Sort (cost=434678.88..439424.56 rows=1898274 width=85)
Sort Key: group_id, artifact_id, account_id, number_of_interfaces, (date_trunc('month'::text, server_build_time))
-> Seq Scan on build_requests (cost=0.00..55053.43 rows=1898274 width=85)
使用 pgadmin 登录只读副本,但我看到索引存在。这是一个问题,因为对只读副本的查询速度较慢(5 分钟对 3 秒),导致通过 postgres_fdw(跨数据库查询)包含此查询的其他查询返回 ssl 连接重置(可能超时?)。
知道为什么只读副本没有拾取/使用我在主服务器上定义的索引,我该如何解决这个问题?我在主副本和只读副本上执行的查询是相同的:
SELECT group_id, artifact_id,
account_id, number_of_interfaces,
date_trunc('month', server_build_time) as server_build_month,
count(*)
FROM build_requests
GROUP BY group_id, artifact_id,
account_id, number_of_interfaces,
date_trunc('month', server_build_time);
感谢您的帮助!
【问题讨论】:
-
您是否尝试过 VACUUM ANALYZE build_requests?
-
不,我没有。不过我现在做了,没有效果。您无法清理只读副本,因为它是只读的,并且主服务器已经工作,所以我很好奇它为什么会工作,但无论如何,在主服务器上运行 VACUUM ANALYZE build_requests 没有任何效果。
-
我一无所知,只是在猜测。尝试似乎无害。规划器强烈依赖于许多统计数据(元组计数、索引选择性等)。我能想象到的唯一原因是错误的统计数据会使规划器偏离轨道。我还想象统计数据可能不是复制集的一部分。对不起。
标签: sql postgresql amazon-web-services read-replication