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 = 'UNITED KINGDOM' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4804074.34..4804074.35 rows=1 width=26) (actual time=105616.112..105616.134 rows=100 loops=1) Buffers: shared hit=10016768 read=1448111 dirtied=563 written=22089, temp read=161476 written=161470 -> Sort (cost=4804074.34..4804074.35 rows=1 width=26) (actual time=105616.108..105616.117 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10016768 read=1448111 dirtied=563 written=22089, temp read=161476 written=161470 -> HashAggregate (cost=4804074.32..4804074.33 rows=1 width=26) (actual time=105610.577..105612.676 rows=3973 loops=1) Buffers: shared hit=10016760 read=1448111 dirtied=563 written=22089, temp read=161476 written=161470 -> Nested Loop Anti Join (cost=2654504.08..4804074.32 rows=1 width=26) (actual time=37451.967..105492.992 rows=39497 loops=1) Buffers: shared hit=10016760 read=1448111 dirtied=563 written=22089, temp read=161476 written=161470 -> Nested Loop (cost=2654504.08..4804059.42 rows=1 width=34) (actual time=37449.596..92590.712 rows=699887 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7032746 read=916152 dirtied=563 written=506, temp read=161476 written=161470 -> Hash Semi Join (cost=2654504.08..4804057.46 rows=1 width=38) (actual time=37449.210..81002.004 rows=1451532 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 376263 Buffers: shared hit=1335965 read=798862 dirtied=547 written=400, temp read=161476 written=161470 -> Hash Join (cost=2441.58..1902544.79 rows=799724 width=34) (actual time=37.511..30150.873 rows=1505240 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=632916 read=435436 -> Seq Scan on lineitem l1 (cost=0.00..1817048.61 rows=20015296 width=8) (actual time=0.025..20960.389 rows=37928797 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056532 Buffers: shared hit=632381 read=434094 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=37.381..37.381 rows=3973 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 249kB Buffers: shared hit=532 read=1342 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.336..35.399 rows=3973 loops=1) Buffers: shared hit=532 read=1342 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.018..0.023 rows=1 loops=1) Filter: (n_name = 'UNITED KINGDOM'::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.310..34.091 rows=3973 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=531 read=1342 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.001..1.001 rows=3973 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1666933.89..1666933.89 rows=60045889 width=8) (actual time=37394.473..37394.473 rows=59985329 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586039kB Buffers: shared hit=703049 read=363426 dirtied=547 written=400, temp written=153747 -> Seq Scan on lineitem l2 (cost=0.00..1666933.89 rows=60045889 width=8) (actual time=0.013..18657.699 rows=59985329 loops=1) Buffers: shared hit=703049 read=363426 dirtied=547 written=400 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.007..0.007 rows=0 loops=1451532) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5696781 read=117290 dirtied=16 written=106 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.42 rows=5 width=8) (actual time=0.018..0.018 rows=1 loops=699887) 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=2984014 read=531959 written=21583 Total runtime: 105618.064 ms (56 rows) COMMIT; COMMIT