【发布时间】:2021-06-01 19:09:00
【问题描述】:
Postgres 版本:12
查询:
解释(ANALYZE TRUE, VERBOSE TRUE, COSTS TRUE, BUFFERS TRUE, TIMING TRUE) SELECT MIN("id"), MAX("id") FROM "public"."hotel_slot_inventory" WHERE ("updated_at" >= '2021-03-02 13:30:03' AND "updated_at"
查询计划:
Result (cost=512.17..512.18 rows=1 width=8) (actual time=65556.920..65556.926 rows=1 loops=1)
Output: $0, $1
Buffers: shared hit=370 read=454012 written=8
I/O Timings: read=62266.717 write=0.194
InitPlan 1 (returns $0)
-> Limit (cost=0.57..256.09 rows=1 width=4) (actual time=65251.998..65252.001 rows=1 loops=1)
Output: hotel_slot_inventory.id
Buffers: shared hit=1 read=453546 written=8
I/O Timings: read=61967.042 write=0.194
-> Index Only Scan using hotel_slot_inventory_id_updated_at_idx on public.hotel_slot_inventory (cost=0.57..3291347.07 rows=12881 width=4) (actual time=65251.996..65251.997 rows=1 loops=1)
Output: hotel_slot_inventory.id
Index Cond: ((hotel_slot_inventory.id IS NOT NULL) AND (hotel_slot_inventory.updated_at >= '2021-03-02 13:30:03'::timestamp without time zone) AND (hotel_slot_inventory.updated_at < '2021-03-03 06:15:19.127884'::timestamp without time zone))
Heap Fetches: 1
Buffers: shared hit=1 read=453546 written=8
I/O Timings: read=61967.042 write=0.194
InitPlan 2 (returns $1)
-> Limit (cost=0.57..256.09 rows=1 width=4) (actual time=304.902..304.903 rows=1 loops=1)
Output: hotel_slot_inventory_1.id
Buffers: shared hit=369 read=466
I/O Timings: read=299.674
-> Index Only Scan Backward using hotel_slot_inventory_id_updated_at_idx on public.hotel_slot_inventory hotel_slot_inventory_1 (cost=0.57..3291347.07 rows=12881 width=4) (actual time=304.899..304.899 rows=1 loops=1)
Output: hotel_slot_inventory_1.id
Index Cond: ((hotel_slot_inventory_1.id IS NOT NULL) AND (hotel_slot_inventory_1.updated_at >= '2021-03-02 13:30:03'::timestamp without time zone) AND (hotel_slot_inventory_1.updated_at < '2021-03-03 06:15:19.127884'::timestamp without time zone))
Heap Fetches: 3892
Buffers: shared hit=369 read=466
I/O Timings: read=299.674
Planning Time: 0.229 ms
Execution Time: 65556.982 ms
(28 rows)
我们可以看到这个简单的仅索引扫描花费了 65556.982 毫秒。 InitPlan 1 65251.997 ms 花费了大部分时间。为什么会这样?它只需要分别从 btree 索引向前和向后搜索中获取第一条记录,因为查询要求 Min 和 Max...不需要从 btree 索引中获取所有匹配记录
仅供参考: 真空并没有太大帮助。
编辑索引膨胀详细信息:
real_size:3751411712 = 3.49 GB
额外大小:470237184 = 448 MB
额外比率:12.53
填充因子:90
膨胀大小:107053056 = 102 MB
bloat_ratio:2.85
表膨胀大小: 膨胀大小:475283456 = 453 MB
膨胀率:5.088
【问题讨论】:
-
请不要针对同一个问题打开不同的问题:stackoverflow.com/questions/66452891/…
-
@S-Man:在这两个线程中,我都在问不同的问题。这是以前向我推荐过的,我觉得它是合理的。
-
你应该给我们索引定义而不是让我们猜测。
-
您没有回答要求提供有关重复问题的更多信息的问题...我要求您检查索引膨胀的统计信息,因为它必须读取 453546 个缓冲区才能获得一行是不正常的。
-
@bobflux:刚刚做了
标签: postgresql query-optimization explain