若结果小于限制,postgresql限制查询将非常缓慢



我在PostgreSQL(版本13.x(中有一个大表,大约有2500万条记录。我有一个限额查询如下:

select sd.id, sd.time
from sensor_data sd 
join sensor_instance si on si.id = sd.sensor_instance_id
join agent ag on ag.id = si.agent_id
where ag.id=373 and sd.id < 24138131
order by sd.id desc 
limit 100

只要我有100多条记录,这个查询就相当快。一旦结果小于100条记录(如果只有5条记录的id小于24138131(,则此查询将变得非常缓慢。所以原因是:结果记录的数量少于限制的100行。

如果结果是少于100条记录,我如何优化查询以返回剩余的行?

这是执行计划。

QUERY PLAN
Limit  (cost=188.18..244.09 rows=7 width=151) (actual time=31.263..127512.031 rows=6 loops=1)
Buffers: shared hit=72910797 read=294836
->  Nested Loop  (cost=188.18..9250116.49 rows=1158024 width=151) (actual time=31.262..127512.026 rows=6 loops=1)
Buffers: shared hit=72910797 read=294836
->  Nested Loop  (cost=188.03..8967959.28 rows=1158024 width=137) (actual time=31.252..127512.008 rows=6 loops=1)
Buffers: shared hit=72910785 read=294836
->  Nested Loop  (cost=185.52..8953479.70 rows=1158024 width=120) (actual time=31.142..127511.881 rows=6 loops=1)
Buffers: shared hit=72910780 read=294836
->  Merge Append  (cost=185.24..1915327.43 rows=23656775 width=91) (actual time=31.122..93377.738 rows=23654729 loops=1)
Sort Key: sd_1.id DESC
Buffers: shared hit=1946636 read=294793
->  Index Scan Backward using "100_226_sensor_data_pkey" on _hyper_9_100_chunk sd_1  (cost=0.29..621.01 rows=20164 width=546) (actual time=9.677..111.407 rows=20184 loops=1)
----REMOVED MIDDLE SECTION-----
Planning:
Buffers: shared hit=2638 read=6
Planning Time: 169.541 ms
Execution Time: 127514.964 ms

抱歉,我无法复制中间部分的所有计划信息,因为它很长,并且我超出了此处允许的字符限制。我不明白的是,为什么它读这么多行?输出只有6行。如果我使用Limit 5,那么它将始终计数5行,并且响应非常快。只要记录计数低于LIMIT计数,响应就会非常快。我有一个关于sensor_data.sensor_instance_id的索引。当然还有id。

我发现了我的案例的问题。我试图查询的表是一个超表。因此,它扫描了时间序列数据段(碎片(的所有块。因此,我不得不修改查询以将搜索限制在给定的时间范围内。然后搜索变得非常快。所以,基本上,在我的情况下,问题是它是一个hypertable。

最新更新