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 = 'ROMANIA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4813683.81..4813683.81 rows=1 width=26) (actual time=161997.587..161997.612 rows=100 loops=1) Buffers: shared hit=10153353 read=1468224 dirtied=285 written=20201, temp read=161745 written=161739 -> Sort (cost=4813683.81..4813683.81 rows=1 width=26) (actual time=161997.583..161997.591 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10153353 read=1468224 dirtied=285 written=20201, temp read=161745 written=161739 -> HashAggregate (cost=4813683.79..4813683.80 rows=1 width=26) (actual time=161989.755..161992.673 rows=4029 loops=1) Buffers: shared hit=10153345 read=1468224 dirtied=285 written=20201, temp read=161745 written=161739 -> Nested Loop Anti Join (cost=2659796.09..4813683.78 rows=1 width=26) (actual time=52154.097..161821.945 rows=39737 loops=1) Buffers: shared hit=10153345 read=1468224 dirtied=285 written=20201, temp read=161745 written=161739 -> Nested Loop (cost=2659796.09..4813668.83 rows=1 width=34) (actual time=52138.675..140650.961 rows=712089 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7243372 read=802515 dirtied=285 written=709, temp read=161745 written=161739 -> Hash Semi Join (cost=2659796.09..4813666.87 rows=1 width=38) (actual time=52138.458..121454.109 rows=1474674 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 381539 Buffers: shared hit=1361974 read=777101 dirtied=257 written=690, temp read=161745 written=161739 -> Hash Join (cost=2441.58..1906347.74 rows=802207 width=34) (actual time=31.953..51818.747 rows=1528930 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=568766 read=501701 written=2 -> Seq Scan on lineitem l1 (cost=0.00..1820677.18 rows=20055178 width=8) (actual time=0.038..33819.180 rows=37966418 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078961 Buffers: shared hit=566921 read=501687 written=2 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=31.829..31.829 rows=4029 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 252kB Buffers: shared hit=1842 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.791..29.643 rows=4029 loops=1) Buffers: shared hit=1842 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.012..0.018 rows=1 loops=1) Filter: (n_name = 'ROMANIA'::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=1.773..28.104 rows=4029 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1841 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.391..1.391 rows=4029 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1670263.34..1670263.34 rows=60165534 width=8) (actual time=52071.158..52071.158 rows=60045379 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586617kB Buffers: shared hit=793208 read=275400 dirtied=257 written=688, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1670263.34 rows=60165534 width=8) (actual time=0.015..27040.815 rows=60045379 loops=1) Buffers: shared hit=793208 read=275400 dirtied=257 written=688 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.012..0.012 rows=0 loops=1474674) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5881398 read=25414 dirtied=28 written=19 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.45 rows=5 width=8) (actual time=0.029..0.029 rows=1 loops=712089) 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=2909973 read=665709 written=19492 Total runtime: 161999.370 ms (56 rows) COMMIT; COMMIT