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 = 'JAPAN' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4804035.49..4804035.49 rows=1 width=26) (actual time=131865.464..131865.492 rows=100 loops=1) Buffers: shared hit=10386867 read=1182869 dirtied=77 written=18592, temp read=161559 written=161553 -> Sort (cost=4804035.49..4804035.49 rows=1 width=26) (actual time=131865.461..131865.468 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10386867 read=1182869 dirtied=77 written=18592, temp read=161559 written=161553 -> HashAggregate (cost=4804035.47..4804035.48 rows=1 width=26) (actual time=131858.178..131860.707 rows=4009 loops=1) Buffers: shared hit=10386859 read=1182869 dirtied=77 written=18592, temp read=161559 written=161553 -> Nested Loop Anti Join (cost=2654483.24..4804035.46 rows=1 width=26) (actual time=45916.148..131712.327 rows=40061 loops=1) Buffers: shared hit=10386859 read=1182869 dirtied=77 written=18592, temp read=161559 written=161553 -> Nested Loop (cost=2654483.24..4804020.57 rows=1 width=34) (actual time=45906.877..115420.925 rows=708419 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7259755 read=751509 dirtied=77 written=97, temp read=161559 written=161553 -> Hash Semi Join (cost=2654483.24..4804018.61 rows=1 width=38) (actual time=45906.690..98694.490 rows=1467108 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 380957 Buffers: shared hit=1534063 read=600742 dirtied=64 written=42, temp read=161559 written=161553 -> Hash Join (cost=2441.58..1902532.27 rows=799562 width=34) (actual time=25.576..35698.040 rows=1521737 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=636876 read=431454 -> Seq Scan on lineitem l1 (cost=0.00..1817038.70 rows=20015032 width=8) (actual time=0.014..24214.188 rows=37928797 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056532 Buffers: shared hit=635073 read=431402 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=25.515..25.515 rows=4009 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 251kB Buffers: shared hit=1800 read=52 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=4.609..23.413 rows=4009 loops=1) Buffers: shared hit=1800 read=52 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.008..0.020 rows=1 loops=1) Filter: (n_name = 'JAPAN'::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=4.594..21.913 rows=4009 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1799 read=52 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=4.192..4.192 rows=4009 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=2 read=12 -> Hash (cost=1666925.96..1666925.96 rows=60045096 width=8) (actual time=45864.059..45864.059 rows=59985329 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586039kB Buffers: shared hit=897187 read=169288 dirtied=64 written=42, temp written=153747 -> Seq Scan on lineitem l2 (cost=0.00..1666925.96 rows=60045096 width=8) (actual time=0.030..23128.682 rows=59985329 loops=1) Buffers: shared hit=897187 read=169288 dirtied=64 written=42 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.010..0.010 rows=0 loops=1467108) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5725692 read=150767 dirtied=13 written=55 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.42 rows=5 width=8) (actual time=0.022..0.022 rows=1 loops=708419) 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=3127104 read=431360 written=18495 Total runtime: 131869.443 ms (56 rows) COMMIT; COMMIT