Построковый перебор данных с агрегатным подзапросом
select * from t where id in (select id from t where id < (select min(id) from t) + 10);
Запрос долго выполняется, хотя выборка идет по индексированному полю. Причина:
Hash Join (cost=593776.65..2517878.88 rows=8576786 width=290) (actual time=3120.660..15031.631 rows=1 loops=1)
Hash Cond: (t.id = t_1.id)
InitPlan 2 (returns $1)
-> Result (cost=0.62..0.63 rows=1 width=8) (actual time=0.016..0.018 rows=1 loops=1)
InitPlan 1 (returns $0)
-> Limit (cost=0.58..0.62 rows=1 width=8) (actual time=0.013..0.014 rows=1 loops=1)
-> Index Only Scan using t_pkey on t t_2 (cost=0.58..6545674.64 rows=26876428 width=8) (actual time=0.012..0.013 rows=1 loops=1)
Index Cond: (id IS NOT NULL)
Heap Fetches: 1
-> Seq Scan on t (cost=0.00..1478457.41 rows=25522671 width=290) (actual time=0.337..9863.729 rows=34165184 loops=1)
-> Hash (cost=389712.31..389712.31 rows=8576786 width=8) (actual time=111.828..111.830 rows=1 loops=1)
Buckets: 8388608 Batches: 1 Memory Usage: 65537kB
-> Index Only Scan using t_pkey on t t_1 (cost=0.78..769872.98 rows=9781784 width=8) (actual time=111.826..111.826 rows=1 loops=1)
Index Cond: (id < ($1 + 10))
Heap Fetches: 1
Planning Time: 0.927 ms
JIT:
Functions: 13
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 0.833 ms, Inlining 25.898 ms, Optimization 51.166 ms, Emission 34.605 ms, Total 112.503 ms
Execution Time: 15035.509 ms
Почему постгрес не кеширует результат внутреннего подзапроса? Почему используется построчный перебор? Интересует не решение а причина. Спасибо!