【发布时间】:2021-12-07 12:57:51
【问题描述】:
我正在尝试优化一直在破坏我的数据库的查询。
https://explain.depesz.com/s/isM1
如果您对如何改善这一点有任何见解,请告诉我。
我们使用的是 RDS/Postgres 11.9
explain analyze SELECT "src_rowdifference"."key",
"src_rowdifference"."port_id",
"src_rowdifference"."shipping_line_id",
"src_rowdifference"."container_type_id",
"src_rowdifference"."shift_id",
"src_rowdifference"."prev_availability_id",
"src_rowdifference"."new_availability_id",
"src_rowdifference"."date",
"src_rowdifference"."prev_last_update",
"src_rowdifference"."new_last_update"
FROM "src_rowdifference"
INNER JOIN "src_containertype" ON ("src_rowdifference"."container_type_id" = "src_containertype"."key")
WHERE ("src_rowdifference"."container_type_id" IN
(SELECT U0."key"
FROM "src_containertype" U0
INNER JOIN "notification_tablenotification_container_types" U1 ON (U0."key" = U1."containertype_id")
WHERE U1."tablenotification_id" = 'test@test.com')
AND "src_rowdifference"."new_last_update" >= '2020-01-15T03:11:06.291947+00:00'::timestamptz
AND "src_rowdifference"."port_id" IN
(SELECT U0."key"
FROM "src_port" U0
INNER JOIN "notification_tablenotification_ports" U1 ON (U0."key" = U1."port_id")
WHERE U1."tablenotification_id" = 'test@test.com')
AND "src_rowdifference"."shipping_line_id" IN
(SELECT U0."key"
FROM "src_shippingline" U0
INNER JOIN "notification_tablenotification_shipping_lines" U1 ON (U0."key" = U1."shippingline_id")
WHERE U1."tablenotification_id" = 'test@test.com')
AND "src_rowdifference"."prev_last_update" IS NOT NULL
AND NOT ("src_rowdifference"."prev_availability_id" = 'na'
AND "src_rowdifference"."prev_availability_id" IS NOT NULL)
AND NOT ("src_rowdifference"."key" IN
(SELECT V1."rowdifference_id"
FROM "notification_tablenotificationtrigger_row_differences" V1
WHERE V1."tablenotificationtrigger_id" IN
(SELECT U0."id"
FROM "notification_tablenotificationtrigger" U0
WHERE U0."notification_id" = 'test@test.com'))));
我所有的索引都是 btree + btree(varchar_pattern_ops) "src_rowdifference_port_id_shipping_line_id_9b3465fc_uniq" 唯一约束,btree (port_id, shipping_line_id, container_type_id, shift_id, date, new_last_update)
编辑:我所做的一点不相关的更改是为我的 RDS 实例添加了更多的 ssd 磁盘空间。这对 CPU 使用率产生了巨大影响,进而对我们拥有的连接数量产生了巨大影响。
【问题讨论】:
-
对 WHERE 子句中的所有子查询使用适当的索引。
-
@jjanes 我已经添加了索引类型。
标签: postgresql query-optimization amazon-rds