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 = 'KENYA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4856779.29..4856779.29 rows=1 width=26) (actual time=125089.255..125089.275 rows=100 loops=1) Buffers: shared hit=10105669 read=1580612 dirtied=296 written=3099, temp read=161796 written=161790 -> Sort (cost=4856779.29..4856779.29 rows=1 width=26) (actual time=125089.251..125089.260 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10105669 read=1580612 dirtied=296 written=3099, temp read=161796 written=161790 -> HashAggregate (cost=4856779.27..4856779.28 rows=1 width=26) (actual time=125084.513..125086.464 rows=4044 loops=1) Buffers: shared hit=10105661 read=1580612 dirtied=296 written=3099, temp read=161796 written=161790 -> Nested Loop Anti Join (cost=2683612.31..4856779.26 rows=1 width=26) (actual time=40734.583..124960.336 rows=39695 loops=1) Buffers: shared hit=10105661 read=1580612 dirtied=296 written=3099, temp read=161796 written=161790 -> Nested Loop (cost=2683612.31..4856764.09 rows=1 width=34) (actual time=40732.408..112077.378 rows=715272 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6980437 read=1113238 dirtied=170 written=247, temp read=161796 written=161790 -> Hash Semi Join (cost=2683612.31..4856762.11 rows=1 width=38) (actual time=40731.461..98851.022 rows=1481846 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 384073 Buffers: shared hit=1047540 read=1110691 dirtied=170 written=238, temp read=161796 written=161790 -> Hash Join (cost=2441.58..1923403.80 rows=808522 width=34) (actual time=26.612..43416.095 rows=1536595 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=349241 read=730802 written=102 -> Seq Scan on lineitem l1 (cost=0.00..1836996.19 rows=20234885 width=8) (actual time=0.043..29250.212 rows=37966174 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078820 Buffers: shared hit=347400 read=730788 written=102 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=26.514..26.514 rows=4044 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 253kB Buffers: shared hit=1838 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.814..24.386 rows=4044 loops=1) Buffers: shared hit=1838 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.010..0.017 rows=1 loops=1) Filter: (n_name = 'KENYA'::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.796..22.901 rows=4044 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.438..1.438 rows=4044 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1685234.55..1685234.55 rows=60704655 width=8) (actual time=40680.925..40680.925 rows=60044994 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586568kB Buffers: shared hit=698299 read=379889 dirtied=170 written=136, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1685234.55 rows=60704655 width=8) (actual time=0.050..20110.429 rows=60044994 loops=1) Buffers: shared hit=698299 read=379889 dirtied=170 written=136 -> Index Scan using pk_orders on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.008..0.008 rows=0 loops=1481846) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5932897 read=2547 written=9 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.56 rows=5 width=8) (actual time=0.017..0.017 rows=1 loops=715272) 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=3125224 read=467374 dirtied=126 written=2852 Total runtime: 125090.548 ms (56 rows) COMMIT; COMMIT