warehouse=# explain (analyze, buffers) with recursive t as ( select min("VBAK_MANDT") as "VBAK_MANDT" from staging.pdw00002_vbak union all select (select min("VBAK_MANDT") as "VBAK_MANDT" from staging.pdw00002_vbak where "VBAK_MANDT" > t."VBAK_MANDT") from t where t."VBAK_MANDT" is not null ) select max("VBAK_VBELN") from staging.pdw00002_vbak where "VBAK_MANDT" in (select "VBAK_MANDT" from t); QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=1169050.81..1169050.82 rows=1 width=32) (actual time=17195.201..17195.202 rows=1 loops=1) Buffers: shared hit=170 read=795662 CTE t -> Recursive Union (cost=0.73..98.31 rows=101 width=32) (actual time=0.031..0.053 rows=2 loops=1) Buffers: shared hit=9 -> Result (cost=0.73..0.74 rows=1 width=32) (actual time=0.031..0.031 rows=1 loops=1) Buffers: shared hit=5 InitPlan 3 (returns $1) -> Limit (cost=0.56..0.73 rows=1 width=32) (actual time=0.027..0.027 rows=1 loops=1) Buffers: shared hit=5 -> Index Only Scan using pdw00002_vbak_pkey on pdw00002_vbak pdw00002_vbak_2 (cost=0.56..2375293.75 rows=14214332 width=32) (actual time=0.026..0.026 rows=1 loops=1) Index Cond: ("VBAK_MANDT" IS NOT NULL) Heap Fetches: 1 Buffers: shared hit=5 -> WorkTable Scan on t t_1 (cost=0.00..9.56 rows=10 width=32) (actual time=0.009..0.010 rows=0 loops=2) Filter: ("VBAK_MANDT" IS NOT NULL) Rows Removed by Filter: 0 Buffers: shared hit=4 SubPlan 2 -> Result (cost=0.93..0.94 rows=1 width=32) (actual time=0.014..0.014 rows=1 loops=1) Buffers: shared hit=4 InitPlan 1 (returns $3) -> Limit (cost=0.56..0.93 rows=1 width=32) (actual time=0.014..0.014 rows=0 loops=1) Buffers: shared hit=4 -> Index Only Scan using pdw00002_vbak_pkey on pdw00002_vbak pdw00002_vbak_1 (cost=0.56..1732075.91 rows=4738111 width=32) (actual time=0.012..0.012 rows=0 loops=1) Index Cond: (("VBAK_MANDT" IS NOT NULL) AND ("VBAK_MANDT" > t_1."VBAK_MANDT")) Heap Fetches: 0 Buffers: shared hit=4 -> Hash Semi Join (cost=3.28..1133416.67 rows=14214332 width=11) (actual time=0.088..10323.225 rows=14214672 loops=1) Hash Cond: ((pdw00002_vbak."VBAK_MANDT")::text = t."VBAK_MANDT") Buffers: shared hit=170 read=795662 -> Seq Scan on pdw00002_vbak (cost=0.00..937966.32 rows=14214332 width=15) (actual time=0.017..5016.514 rows=14214672 loops=1) Buffers: shared hit=161 read=795662 -> Hash (cost=2.02..2.02 rows=101 width=32) (actual time=0.060..0.060 rows=1 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 9kB Buffers: shared hit=9 -> CTE Scan on t (cost=0.00..2.02 rows=101 width=32) (actual time=0.034..0.057 rows=2 loops=1) Buffers: shared hit=9 Planning time: 0.567 ms Execution time: 17195.336 ms (40 rows)