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 = 'ARGENTINA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4808875.58..4808875.58 rows=1 width=26) (actual time=391457.246..391457.274 rows=100 loops=1) Buffers: shared hit=10286263 read=1286200 dirtied=161 written=7565, temp read=161717 written=161711 -> Sort (cost=4808875.58..4808875.58 rows=1 width=26) (actual time=391457.242..391457.252 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10286263 read=1286200 dirtied=161 written=7565, temp read=161717 written=161711 -> HashAggregate (cost=4808875.56..4808875.57 rows=1 width=26) (actual time=391449.138..391451.928 rows=4007 loops=1) Buffers: shared hit=10286255 read=1286200 dirtied=161 written=7565, temp read=161717 written=161711 -> Nested Loop Anti Join (cost=2657142.27..4808875.55 rows=1 width=26) (actual time=76204.340..391186.660 rows=39901 loops=1) Buffers: shared hit=10286255 read=1286200 dirtied=161 written=7565, temp read=161717 written=161711 -> Nested Loop (cost=2657142.27..4808860.63 rows=1 width=34) (actual time=76163.802..279112.181 rows=708400 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7338224 read=677074 written=218, temp read=161717 written=161711 -> Hash Semi Join (cost=2657142.27..4808858.67 rows=1 width=38) (actual time=76161.068..192392.594 rows=1467591 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 380740 Buffers: shared hit=1533039 read=603916 written=110, temp read=161717 written=161711 -> Hash Join (cost=2441.58..1904447.17 rows=801267 width=34) (actual time=194.151..100341.760 rows=1521941 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=755286 read=314123 written=110 -> Seq Scan on lineitem l1 (cost=0.00..1818861.39 rows=20035077 width=8) (actual time=0.049..71557.587 rows=37966905 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078698 Buffers: shared hit=754858 read=312688 written=110 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=194.034..194.034 rows=4007 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 251kB Buffers: shared hit=425 read=1435 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=6.558..190.879 rows=4007 loops=1) Buffers: shared hit=425 read=1435 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.013..0.022 rows=1 loops=1) Filter: (n_name = 'ARGENTINA'::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.534..188.991 rows=4007 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=424 read=1435 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=6.021..6.021 rows=4007 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1668598.31..1668598.31 rows=60105231 width=8) (actual time=75945.978..75945.978 rows=60045603 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586633kB Buffers: shared hit=777753 read=289793, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1668598.31 rows=60105231 width=8) (actual time=0.041..49942.015 rows=60045603 loops=1) Buffers: shared hit=777753 read=289793 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.057..0.058 rows=0 loops=1467591) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5805185 read=73158 written=108 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.44 rows=5 width=8) (actual time=0.157..0.157 rows=1 loops=708400) 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=2948031 read=609126 dirtied=161 written=7347 Total runtime: 391459.921 ms (56 rows) COMMIT; COMMIT