Yve*_* V. 3 sql postgresql amazon-web-services read-replication
我在 Amazon RDS 生产数据库上有一个相对较大的表(在 2M 条记录范围内)。我希望对多个字段进行分组,包括表中日期的月份(server_time)。为了加快速度,我在 master 数据库上创建了一个索引,如下所示:
create index on build_requests(group_id, artifact_id, account_id, number_of_interfaces, date_trunc('month', server_build_time));
Run Code Online (Sandbox Code Playgroud)
然后,正如您所期望的,对数据进行分组的查询使用主服务器上的索引:
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)
Run Code Online (Sandbox Code Playgroud)
然而,等待一个多小时后,只读副本仍然没有使用索引:
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)
Run Code Online (Sandbox Code Playgroud)
使用 pgadmin 登录只读副本,我发现索引仍然存在。这是一个问题,因为只读副本上的查询速度较慢(5 分钟 vs 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);
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
问题似乎与亚马逊 RDS 实例的类型有关。原始 RDS 是 t2 介质,而只读副本只是一个微实例。将只读副本缩放到中等后,副本也使用了索引。
此外,即使只读副本确实使用了索引,仅通过 postgres_fdw 执行上述查询也会导致连接超时。直到我使用索引在主服务器上创建了一个视图,查询执行才没有问题。
| 归档时间: |
|
| 查看次数: |
2193 次 |
| 最近记录: |