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=4856816.07..4856816.08 rows=1 width=26) (actual time=143915.232..143915.259 rows=100 loops=1) Buffers: shared hit=9791407 read=1809795 dirtied=1283 written=19103, temp read=161724 written=161718 -> Sort (cost=4856816.07..4856816.08 rows=1 width=26) (actual time=143915.227..143915.236 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9791407 read=1809795 dirtied=1283 written=19103, temp read=161724 written=161718 -> HashAggregate (cost=4856816.05..4856816.06 rows=1 width=26) (actual time=143907.647..143910.215 rows=4009 loops=1) Buffers: shared hit=9791399 read=1809795 dirtied=1283 written=19103, temp read=161724 written=161718 -> Nested Loop Anti Join (cost=2683619.14..4856816.05 rows=1 width=26) (actual time=49279.530..143751.386 rows=40101 loops=1) Buffers: shared hit=9791399 read=1809795 dirtied=1283 written=19103, temp read=161724 written=161718 -> Nested Loop (cost=2683619.14..4856800.88 rows=1 width=34) (actual time=49259.163..125034.526 rows=709150 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6843889 read=1196366 dirtied=1174 written=4705, temp read=161724 written=161718 -> Hash Semi Join (cost=2683619.14..4856798.89 rows=1 width=38) (actual time=49256.131..106502.199 rows=1468513 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 381319 Buffers: shared hit=1075772 read=1082459 dirtied=1069 written=98, temp read=161724 written=161718 -> Hash Join (cost=2441.58..1923416.13 rows=809399 width=34) (actual time=30.475..44624.877 rows=1523150 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=371061 read=708982 written=98 -> Seq Scan on lineitem l1 (cost=0.00..1836999.43 rows=20234971 width=8) (actual time=0.023..29640.691 rows=37966174 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078820 Buffers: shared hit=369220 read=708968 written=98 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=30.402..30.402 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=1.387..28.205 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.016 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=1.374..26.773 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.067..1.067 rows=4009 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1685237.14..1685237.14 rows=60704914 width=8) (actual time=49198.381..49198.381 rows=60044994 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586568kB Buffers: shared hit=704711 read=373477 dirtied=1069, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1685237.14 rows=60704914 width=8) (actual time=0.059..26741.974 rows=60044994 loops=1) Buffers: shared hit=704711 read=373477 dirtied=1069 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.011..0.012 rows=0 loops=1468513) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5768117 read=113907 dirtied=105 written=4607 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.56 rows=5 width=8) (actual time=0.025..0.025 rows=1 loops=709150) 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=2947510 read=613429 dirtied=109 written=14398 Total runtime: 143917.302 ms (56 rows) COMMIT; COMMIT