BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select s_name, count(*) as numwait from supplier, lineitem l1, orders, nation where s_suppkey = l1.l_suppkey and o_orderkey = l1.l_orderkey and o_orderstatus = 'F' and l1.l_receiptdate > l1.l_commitdate and exists ( select * from lineitem l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey ) and not exists ( select * from lineitem l3 where l3.l_orderkey = l1.l_orderkey and l3.l_suppkey <> l1.l_suppkey and l3.l_receiptdate > l3.l_commitdate ) and s_nationkey = n_nationkey and n_name = 'GERMANY' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4808905.37..4808905.38 rows=1 width=26) (actual time=218617.233..218617.251 rows=100 loops=1) Buffers: shared hit=9666314 read=1996246 dirtied=2 written=10005, temp read=161786 written=161780 -> Sort (cost=4808905.37..4808905.38 rows=1 width=26) (actual time=218617.229..218617.235 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9666314 read=1996246 dirtied=2 written=10005, temp read=161786 written=161780 -> HashAggregate (cost=4808905.35..4808905.36 rows=1 width=26) (actual time=218611.084..218613.381 rows=4049 loops=1) Buffers: shared hit=9666306 read=1996246 dirtied=2 written=10005, temp read=161786 written=161780 -> Nested Loop Anti Join (cost=2657156.65..4808905.35 rows=1 width=26) (actual time=71114.719..218407.045 rows=40144 loops=1) Buffers: shared hit=9666306 read=1996246 dirtied=2 written=10005, temp read=161786 written=161780 -> Nested Loop (cost=2657156.65..4808890.42 rows=1 width=34) (actual time=71110.403..180535.385 rows=715297 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6690349 read=1379136 written=85, temp read=161786 written=161780 -> Hash Semi Join (cost=2657156.65..4808888.47 rows=1 width=38) (actual time=71107.994..145489.834 rows=1481079 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 384589 Buffers: shared hit=892349 read=1244592 written=14, temp read=161786 written=161780 -> Hash Join (cost=2441.58..1904456.16 rows=801410 width=34) (actual time=39.865..56316.106 rows=1535824 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=517692 read=551703 written=14 -> Seq Scan on lineitem l1 (cost=0.00..1818868.26 rows=20035260 width=8) (actual time=0.063..38743.663 rows=37966905 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078698 Buffers: shared hit=516805 read=550741 written=14 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=39.757..39.757 rows=4049 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 254kB Buffers: shared hit=884 read=962 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.596..37.385 rows=4049 loops=1) Buffers: shared hit=884 read=962 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.018 rows=1 loops=1) Filter: (n_name = 'GERMANY'::bpchar) Rows Removed by Filter: 24 Buffers: shared hit=1 -> Bitmap Heap Scan on supplier (cost=79.26..2350.26 rows=4000 width=34) (actual time=2.581..35.842 rows=4049 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=883 read=962 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.958..1.958 rows=4049 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1668603.81..1668603.81 rows=60105781 width=8) (actual time=71004.224..71004.224 rows=60045603 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586633kB Buffers: shared hit=374657 read=692889, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1668603.81 rows=60105781 width=8) (actual time=0.075..29638.715 rows=60045603 loops=1) Buffers: shared hit=374657 read=692889 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.022..0.022 rows=0 loops=1481079) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5798000 read=134544 written=71 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.44 rows=5 width=8) (actual time=0.052..0.052 rows=1 loops=715297) Index Cond: (l_orderkey = l1.l_orderkey) Filter: ((l_receiptdate > l_commitdate) AND (l_suppkey <> l1.l_suppkey)) Rows Removed by Filter: 1 Buffers: shared hit=2975957 read=617110 dirtied=2 written=9920 Total runtime: 218618.586 ms (56 rows) COMMIT; COMMIT