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=4818438.60..4818438.61 rows=1 width=26) (actual time=271678.340..271678.365 rows=100 loops=1) Buffers: shared hit=9624931 read=1960714 dirtied=568 written=7448, temp read=161721 written=161715 -> Sort (cost=4818438.60..4818438.61 rows=1 width=26) (actual time=271678.336..271678.348 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9624931 read=1960714 dirtied=568 written=7448, temp read=161721 written=161715 -> HashAggregate (cost=4818438.58..4818438.59 rows=1 width=26) (actual time=271670.883..271673.806 rows=4009 loops=1) Buffers: shared hit=9624923 read=1960714 dirtied=568 written=7448, temp read=161721 written=161715 -> Nested Loop Anti Join (cost=2662426.47..4818438.58 rows=1 width=26) (actual time=82101.936..271462.490 rows=40100 loops=1) Buffers: shared hit=9624923 read=1960714 dirtied=568 written=7448, temp read=161721 written=161715 -> Nested Loop (cost=2662426.47..4818423.61 rows=1 width=34) (actual time=82091.759..219405.141 rows=709138 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6693864 read=1329715 dirtied=568 written=1929, temp read=161721 written=161715 -> Hash Semi Join (cost=2662426.47..4818421.64 rows=1 width=38) (actual time=82091.557..169926.488 rows=1468596 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 381342 Buffers: shared hit=868073 read=1273114 dirtied=568 written=690, temp read=161721 written=161715 -> Hash Join (cost=2441.58..1908229.54 rows=802736 width=34) (actual time=78.071..72389.557 rows=1523257 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=290438 read=781083 written=127 -> Seq Scan on lineitem l1 (cost=0.00..1822479.27 rows=20075021 width=8) (actual time=0.036..51261.295 rows=37966584 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078719 Buffers: shared hit=288597 read=781069 written=127 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=77.976..77.976 rows=4009 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 251kB Buffers: shared hit=1838 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.334..74.884 rows=4009 loops=1) Buffers: shared hit=1838 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.008..0.015 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=2.321..73.372 rows=4009 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1837 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.920..1.920 rows=4009 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1671916.62..1671916.62 rows=60225062 width=8) (actual time=81981.545..81981.545 rows=60045303 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586635kB Buffers: shared hit=577635 read=492031 dirtied=568 written=563, temp written=153901 -> Seq Scan on lineitem l2 (cost=0.00..1671916.62 rows=60225062 width=8) (actual time=0.018..54048.300 rows=60045303 loops=1) Buffers: shared hit=577635 read=492031 dirtied=568 written=563 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.032..0.032 rows=0 loops=1468596) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5825791 read=56601 written=1239 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.46 rows=5 width=8) (actual time=0.072..0.072 rows=1 loops=709138) 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=2931059 read=630999 written=5519 Total runtime: 271685.985 ms (56 rows) COMMIT; COMMIT