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 = 'MOROCCO' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4866383.69..4866383.69 rows=1 width=26) (actual time=338863.487..338863.515 rows=100 loops=1) Buffers: shared hit=10448460 read=1088191 written=2587, temp read=161516 written=161510 -> Sort (cost=4866383.69..4866383.69 rows=1 width=26) (actual time=338863.482..338863.490 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10448460 read=1088191 written=2587, temp read=161516 written=161510 -> HashAggregate (cost=4866383.67..4866383.68 rows=1 width=26) (actual time=338856.041..338858.828 rows=3989 loops=1) Buffers: shared hit=10448452 read=1088191 written=2587, temp read=161516 written=161510 -> Nested Loop Anti Join (cost=2688905.13..4866383.66 rows=1 width=26) (actual time=82939.119..338632.433 rows=39737 loops=1) Buffers: shared hit=10448452 read=1088191 written=2587, temp read=161516 written=161510 -> Nested Loop (cost=2688905.13..4866368.45 rows=1 width=34) (actual time=82858.119..266031.120 rows=703343 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7317550 read=687477 written=1722, temp read=161516 written=161510 -> Hash Semi Join (cost=2688905.13..4866366.45 rows=1 width=38) (actual time=82858.018..201087.471 rows=1458682 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378061 Buffers: shared hit=1544505 read=618004 written=1369, temp read=161516 written=161510 -> Hash Join (cost=2441.58..1927204.34 rows=810854 width=34) (actual time=100.637..103574.927 rows=1512690 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=713131 read=369059 written=62 -> Seq Scan on lineitem l1 (cost=0.00..1840623.75 rows=20274793 width=8) (actual time=0.047..74456.836 rows=37928218 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056758 Buffers: shared hit=711274 read=369045 written=62 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=98.981..98.981 rows=3990 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1854 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=6.391..96.330 rows=3990 loops=1) Buffers: shared hit=1854 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.029..0.039 rows=1 loops=1) Filter: (n_name = 'MOROCCO'::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=6.339..94.722 rows=3990 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1853 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=5.846..5.846 rows=3990 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1688562.80..1688562.80 rows=60824380 width=8) (actual time=82733.981..82733.981 rows=59984976 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 585984kB Buffers: shared hit=831374 read=248945 written=1307, temp written=153752 -> Seq Scan on lineitem l2 (cost=0.00..1688562.80 rows=60824380 width=8) (actual time=0.018..53705.952 rows=59984976 loops=1) Buffers: shared hit=831374 read=248945 written=1307 -> Index Scan using pk_orders on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.043..0.043 rows=0 loops=1458682) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5773045 read=69473 written=353 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.58 rows=5 width=8) (actual time=0.102..0.102 rows=1 loops=703343) 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=3130902 read=400714 written=865 Total runtime: 338867.291 ms (56 rows) COMMIT; COMMIT