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 = 'CHINA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4813708.86..4813708.87 rows=1 width=26) (actual time=158638.572..158638.603 rows=100 loops=1) Buffers: shared hit=9994022 read=1538967 dirtied=33 written=20831, temp read=161677 written=161671 -> Sort (cost=4813708.86..4813708.87 rows=1 width=26) (actual time=158638.568..158638.582 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9994022 read=1538967 dirtied=33 written=20831, temp read=161677 written=161671 -> HashAggregate (cost=4813708.84..4813708.85 rows=1 width=26) (actual time=158631.417..158633.904 rows=3988 loops=1) Buffers: shared hit=9994014 read=1538967 dirtied=33 written=20831, temp read=161677 written=161671 -> Nested Loop Anti Join (cost=2659811.10..4813708.84 rows=1 width=26) (actual time=56292.083..158352.353 rows=39961 loops=1) Buffers: shared hit=9994014 read=1538967 dirtied=33 written=20831, temp read=161677 written=161671 -> Nested Loop (cost=2659811.10..4813693.89 rows=1 width=34) (actual time=56291.010..137299.376 rows=705871 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7131497 read=856997 dirtied=33 written=1971, temp read=161677 written=161671 -> Hash Semi Join (cost=2659811.10..4813691.93 rows=1 width=38) (actual time=56290.286..119059.865 rows=1460360 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378102 Buffers: shared hit=1316466 read=822605 dirtied=24 written=27, temp read=161677 written=161671 -> Hash Join (cost=2441.58..1906355.77 rows=802215 width=34) (actual time=38.721..48654.135 rows=1514372 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=652639 read=417824 -> Seq Scan on lineitem l1 (cost=0.00..1820684.40 rows=20055371 width=8) (actual time=0.055..32213.614 rows=37966418 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078961 Buffers: shared hit=650798 read=417810 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=38.597..38.597 rows=3988 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1838 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.894..36.110 rows=3988 loops=1) Buffers: shared hit=1838 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.027..0.032 rows=1 loops=1) Filter: (n_name = 'CHINA'::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.860..34.496 rows=3988 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1837 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.453..1.453 rows=3988 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1670269.12..1670269.12 rows=60166112 width=8) (actual time=56215.450..56215.450 rows=60045379 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586617kB Buffers: shared hit=663827 read=404781 dirtied=24 written=27, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1670269.12 rows=60166112 width=8) (actual time=0.032..28774.228 rows=60045379 loops=1) Buffers: shared hit=663827 read=404781 dirtied=24 written=27 -> Index Scan using pk_orders on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.011..0.011 rows=0 loops=1460360) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5815031 read=34392 dirtied=9 written=1944 -> 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=705871) 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=2862517 read=681970 written=18860 Total runtime: 158640.291 ms (56 rows) COMMIT; COMMIT