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 = 'MOZAMBIQUE' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4808928.94..4808928.95 rows=1 width=26) (actual time=166360.760..166360.790 rows=100 loops=1) Buffers: shared hit=9662422 read=1715601 dirtied=147 written=10202, temp read=161557 written=161551 -> Sort (cost=4808928.94..4808928.95 rows=1 width=26) (actual time=166360.755..166360.769 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9662422 read=1715601 dirtied=147 written=10202, temp read=161557 written=161551 -> HashAggregate (cost=4808928.92..4808928.93 rows=1 width=26) (actual time=166352.759..166355.501 rows=3924 loops=1) Buffers: shared hit=9662414 read=1715601 dirtied=147 written=10202, temp read=161557 written=161551 -> Nested Loop Anti Join (cost=2657170.73..4808928.92 rows=1 width=26) (actual time=57425.516..166180.754 rows=38574 loops=1) Buffers: shared hit=9662414 read=1715601 dirtied=147 written=10202, temp read=161557 written=161551 -> Nested Loop (cost=2657170.73..4808913.99 rows=1 width=34) (actual time=57416.408..144439.042 rows=693466 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6745753 read=1150247 dirtied=147 written=2073, temp read=161557 written=161551 -> Hash Semi Join (cost=2657170.73..4808912.04 rows=1 width=38) (actual time=57416.326..122541.781 rows=1437756 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 372930 Buffers: shared hit=1149709 read=987253 written=1, temp read=161557 written=161551 -> Hash Join (cost=2441.58..1904463.63 rows=801418 width=34) (actual time=38.857..51011.103 rows=1491018 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=539814 read=529602 -> Seq Scan on lineitem l1 (cost=0.00..1818874.98 rows=20035439 width=8) (actual time=0.048..34161.104 rows=37966905 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078698 Buffers: shared hit=538949 read=528597 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=38.741..38.741 rows=3924 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 246kB Buffers: shared hit=862 read=1005 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.727..36.493 rows=3924 loops=1) Buffers: shared hit=862 read=1005 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.011..0.017 rows=1 loops=1) Filter: (n_name = 'MOZAMBIQUE'::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.709..34.970 rows=3924 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=861 read=1005 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.329..1.329 rows=3924 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=13 -> Hash (cost=1668609.18..1668609.18 rows=60106318 width=8) (actual time=57343.963..57343.963 rows=60045603 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586633kB Buffers: shared hit=609895 read=457651 written=1, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1668609.18 rows=60106318 width=8) (actual time=0.036..29518.203 rows=60045603 loops=1) Buffers: shared hit=609895 read=457651 written=1 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.014..0.014 rows=0 loops=1437756) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5596044 read=162994 dirtied=147 written=2072 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.44 rows=5 width=8) (actual time=0.030..0.030 rows=1 loops=693466) 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=2916661 read=565354 written=8129 Total runtime: 166362.650 ms (56 rows) COMMIT; COMMIT