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 = 'VIETNAM' 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=388720.030..388720.050 rows=100 loops=1) Buffers: shared hit=9996955 read=1550162 written=7636, temp read=161678 written=161672 -> Sort (cost=4856789.56..4856789.56 rows=1 width=26) (actual time=388720.025..388720.031 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9996955 read=1550162 written=7636, temp read=161678 written=161672 -> HashAggregate (cost=4856789.54..4856789.55 rows=1 width=26) (actual time=388715.128..388716.983 rows=3987 loops=1) Buffers: shared hit=9996947 read=1550162 written=7636, temp read=161678 written=161672 -> Nested Loop Anti Join (cost=2683605.58..4856789.53 rows=1 width=26) (actual time=97318.734..388473.727 rows=39314 loops=1) Buffers: shared hit=9996947 read=1550162 written=7636, temp read=161678 written=161672 -> Nested Loop (cost=2683605.58..4856774.37 rows=1 width=34) (actual time=97305.668..288284.261 rows=704634 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7037978 read=970939 written=849, temp read=161678 written=161672 -> Hash Semi Join (cost=2683605.58..4856772.38 rows=1 width=38) (actual time=97305.589..208853.413 rows=1460656 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378545 Buffers: shared hit=1232550 read=925689 written=2, temp read=161678 written=161672 -> Hash Join (cost=2441.58..1923407.62 rows=809254 width=34) (actual time=86.553..95279.739 rows=1514472 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=743426 read=336625 written=2 -> Seq Scan on lineitem l1 (cost=0.00..1836993.00 rows=20234800 width=8) (actual time=0.048..68067.190 rows=37966174 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078820 Buffers: shared hit=741577 read=336611 written=2 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=86.396..86.396 rows=3987 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1846 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.040..83.602 rows=3987 loops=1) Buffers: shared hit=1846 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.011..0.016 rows=1 loops=1) Filter: (n_name = 'VIETNAM'::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.023..81.916 rows=3987 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1845 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.645..1.645 rows=3987 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=97187.692..97187.692 rows=60044994 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586568kB Buffers: shared hit=489124 read=589064, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1685232.00 rows=60704400 width=8) (actual time=0.071..70791.129 rows=60044994 loops=1) Buffers: shared hit=489124 read=589064 -> Index Scan using pk_orders on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.053..0.053 rows=0 loops=1460656) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5805428 read=45250 written=847 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.56 rows=5 width=8) (actual time=0.141..0.141 rows=1 loops=704634) 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=2958969 read=579223 written=6787 Total runtime: 388721.370 ms (56 rows) COMMIT; COMMIT