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 = 'EGYPT' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4856789.56..4856789.56 rows=1 width=26) (actual time=389308.125..389308.144 rows=100 loops=1) Buffers: shared hit=9969189 read=1560722 written=3186, temp read=161663 written=161657 -> Sort (cost=4856789.56..4856789.56 rows=1 width=26) (actual time=389308.121..389308.128 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9969189 read=1560722 written=3186, temp read=161663 written=161657 -> HashAggregate (cost=4856789.54..4856789.55 rows=1 width=26) (actual time=389302.844..389304.900 rows=3981 loops=1) Buffers: shared hit=9969181 read=1560722 written=3186, temp read=161663 written=161657 -> Nested Loop Anti Join (cost=2683605.58..4856789.53 rows=1 width=26) (actual time=88493.638..389009.141 rows=39458 loops=1) Buffers: shared hit=9969181 read=1560722 written=3186, temp read=161663 written=161657 -> Nested Loop (cost=2683605.58..4856774.37 rows=1 width=34) (actual time=88484.378..295278.933 rows=703554 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6978311 read=1019011 written=1450, temp read=161663 written=161657 -> Hash Semi Join (cost=2683605.58..4856772.38 rows=1 width=38) (actual time=88484.301..218179.049 rows=1457791 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378445 Buffers: shared hit=1181169 read=977083 written=1422, temp read=161663 written=161657 -> Hash Join (cost=2441.58..1923407.62 rows=809254 width=34) (actual time=87.202..113861.033 rows=1512401 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=448006 read=632058 written=25 -> Seq Scan on lineitem l1 (cost=0.00..1836993.00 rows=20234800 width=8) (actual time=0.046..84297.545 rows=37966174 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078820 Buffers: shared hit=446144 read=632044 written=25 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=86.995..86.995 rows=3981 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 249kB Buffers: shared hit=1859 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.010..84.256 rows=3981 loops=1) Buffers: shared hit=1859 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.016 rows=1 loops=1) Filter: (n_name = 'EGYPT'::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.994..82.611 rows=3981 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1858 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.592..1.592 rows=3981 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1685232.00..1685232.00 rows=60704400 width=8) (actual time=88368.227..88368.227 rows=60044994 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586568kB Buffers: shared hit=733163 read=345025 written=1397, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1685232.00 rows=60704400 width=8) (actual time=0.037..62880.534 rows=60044994 loops=1) Buffers: shared hit=733163 read=345025 written=1397 -> Index Scan using pk_orders on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.051..0.051 rows=0 loops=1457791) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5797142 read=41928 written=28 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.56 rows=5 width=8) (actual time=0.132..0.132 rows=1 loops=703554) 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=2990870 read=541711 written=1736 Total runtime: 389309.591 ms (56 rows) COMMIT; COMMIT