【发布时间】:2021-04-19 12:07:32
【问题描述】:
嘿,postgres 专家。 我们的一张桌子面临着一种奇怪的行为。在增加查询时间戳范围的同时,会出现查询时间突然增加约 5 倍的临界点。
我们正在查看的表格:
Table "measurements.hourly_measurement_recordings"
Column | Type | Collation | Nullable | Default
-----------------------+--------------------------------+-----------+----------+---------
dimension_identifier | integer | | not null |
occasion_identifier | integer | | not null |
items_identifier | bit(64) | | not null |
beginning_of_timeslot | timestamp(0) without time zone | | not null |
dumped_weight | integer | | not null |
Indexes:
"hourly_measurement_recordings_kind_identifier_type_identifier_p" UNIQUE, btree (occasion_identifier, dimension_identifier, items_identifier, beginning_of_timeslot)
"hourly_measurement_recordings_kind_identifier_type_identifier_b" btree (occasion_identifier, dimension_identifier, beginning_of_timeslot)
第一个查询+计划: https://explain.depesz.com/s/LyzL
第二次查询+计划: https://explain.depesz.com/s/ZCvZ
这两个查询之间的唯一区别是增加了时间戳范围:
'2021-03-15 23:00:00Z', '2021-04-19 21:00:00Z'
对
'2021-03-14 23:00:00Z', '2021-04-19 21:00:00Z'
如您所见,从 Parallel Bitmap Heap Scan 到 Parallel Seq Scan 的转换增加了查询时间。
这里是我使用的数据库的一些规格:
- PostgreSQL v13.1
- 在具有 2 个 vCPU、4GB RAM、通用 SSD (40GB) 的 AWS db.t3.medium 上运行
我对数据库配置不是很熟悉,所以我真的不知道哪些参数可能对您有用 - 如果有更多相关信息可以分享,请告诉我。
谢谢你, 尼克
【问题讨论】:
-
您可能想要更改 work_mem,这两个查询都使用外部合并进行排序。尝试类似 100MB:SET work_mem TO '100MB';然后再试一次。
-
我也会尝试这样的索引,从 begin_of_timeslot 开始:"hourly_measurement_recordings_kind_identifier_type_identifier_c" btree (beginning_of_timeslot , scene_identifier, dimension_identifier);
-
tyvm - 我会尝试两个
-
第一个问题:查询是:
WHERE beginning_of_timeslot WHERE (dimension_identifier and occasion_identifier) OR ...那么索引顺序是[beginnig_of_timeslot, dimension_identifier, occasion_identifier]还是[beginning_of_timeslot, occasion_identifier, dimension_identifier] -
哪一列的唯一值最多?那应该是第一位的。
标签: sql postgresql query-optimization