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 = 'INDONESIA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4813639.70..4813639.71 rows=1 width=26) (actual time=140169.751..140169.798 rows=100 loops=1) Buffers: shared hit=9769590 read=1718667 dirtied=3 written=28107, temp read=161643 written=161637 -> Sort (cost=4813639.70..4813639.71 rows=1 width=26) (actual time=140169.747..140169.764 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9769590 read=1718667 dirtied=3 written=28107, temp read=161643 written=161637 -> HashAggregate (cost=4813639.68..4813639.69 rows=1 width=26) (actual time=140154.950..140160.060 rows=3974 loops=1) Buffers: shared hit=9769582 read=1718667 dirtied=3 written=28107, temp read=161643 written=161637 -> Nested Loop Anti Join (cost=2659787.33..4813639.68 rows=1 width=26) (actual time=47948.209..140008.264 rows=39367 loops=1) Buffers: shared hit=9769582 read=1718667 dirtied=3 written=28107, temp read=161643 written=161637 -> Nested Loop (cost=2659787.33..4813624.73 rows=1 width=34) (actual time=47928.247..122350.062 rows=702169 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6924786 read=1036496 dirtied=3 written=546, temp read=161643 written=161637 -> Hash Semi Join (cost=2659787.33..4813622.77 rows=1 width=38) (actual time=47927.801..106227.174 rows=1453593 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 376747 Buffers: shared hit=1167598 read=971476 dirtied=3 written=6, temp read=161643 written=161637 -> Hash Join (cost=2441.58..1906332.53 rows=801161 width=34) (actual time=36.214..44703.840 rows=1507371 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=649465 read=421001 written=1 -> Seq Scan on lineitem l1 (cost=0.00..1820672.86 rows=20055063 width=8) (actual time=0.057..29533.503 rows=37966418 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078961 Buffers: shared hit=647621 read=420987 written=1 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=36.090..36.090 rows=3974 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 249kB Buffers: shared hit=1841 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.888..34.046 rows=3974 loops=1) Buffers: shared hit=1841 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.011..0.021 rows=1 loops=1) Filter: (n_name = 'INDONESIA'::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.869..32.577 rows=3974 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1840 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.510..1.510 rows=3974 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1670259.89..1670259.89 rows=60165189 width=8) (actual time=47864.224..47864.224 rows=60045379 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586617kB Buffers: shared hit=518133 read=550475 dirtied=3 written=5, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1670259.89 rows=60165189 width=8) (actual time=0.020..24629.869 rows=60045379 loops=1) Buffers: shared hit=518133 read=550475 dirtied=3 written=5 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.010..0.010 rows=0 loops=1453593) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5757188 read=65020 written=540 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.45 rows=5 width=8) (actual time=0.024..0.024 rows=1 loops=702169) 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=2844796 read=682171 written=27561 Total runtime: 140173.417 ms (56 rows) COMMIT; COMMIT