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 = 'BRAZIL' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4804070.64..4804070.64 rows=1 width=26) (actual time=197742.841..197742.871 rows=100 loops=1) Buffers: shared hit=10401223 read=1122015 dirtied=68 written=15719, temp read=161520 written=161514 -> Sort (cost=4804070.64..4804070.64 rows=1 width=26) (actual time=197742.837..197742.848 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10401223 read=1122015 dirtied=68 written=15719, temp read=161520 written=161514 -> HashAggregate (cost=4804070.62..4804070.63 rows=1 width=26) (actual time=197735.044..197737.741 rows=3995 loops=1) Buffers: shared hit=10401215 read=1122015 dirtied=68 written=15719, temp read=161520 written=161514 -> Nested Loop Anti Join (cost=2654491.08..4804070.61 rows=1 width=26) (actual time=57369.965..197559.518 rows=39548 loops=1) Buffers: shared hit=10401215 read=1122015 dirtied=68 written=15719, temp read=161520 written=161514 -> Nested Loop (cost=2654491.08..4804055.71 rows=1 width=34) (actual time=57369.915..162259.085 rows=704248 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7320281 read=665735 dirtied=68 written=3644, temp read=161520 written=161514 -> Hash Semi Join (cost=2654491.08..4804053.76 rows=1 width=38) (actual time=57368.841..127375.372 rows=1460780 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 377745 Buffers: shared hit=1563115 read=571701 dirtied=68 written=3, temp read=161520 written=161514 -> Hash Join (cost=2441.58..1902544.23 rows=800341 width=34) (actual time=124.594..50182.762 rows=1514828 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=634780 read=433561 -> Seq Scan on lineitem l1 (cost=0.00..1817042.50 rows=20015133 width=8) (actual time=0.041..34874.335 rows=37928797 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056532 Buffers: shared hit=633634 read=432841 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=124.491..124.491 rows=3995 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1143 read=720 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.326..121.676 rows=3995 loops=1) Buffers: shared hit=1143 read=720 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.031 rows=1 loops=1) Filter: (n_name = 'BRAZIL'::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.313..120.047 rows=3995 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1142 read=720 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.005..1.005 rows=3995 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=2 read=12 -> Hash (cost=1666929.00..1666929.00 rows=60045400 width=8) (actual time=57191.490..57191.490 rows=59985329 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586039kB Buffers: shared hit=928335 read=138140 dirtied=68 written=3, temp written=153747 -> Seq Scan on lineitem l2 (cost=0.00..1666929.00 rows=60045400 width=8) (actual time=0.024..30138.717 rows=59985329 loops=1) Buffers: shared hit=928335 read=138140 dirtied=68 written=3 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.022..0.023 rows=0 loops=1460780) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5757166 read=94034 written=3641 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.42 rows=5 width=8) (actual time=0.049..0.049 rows=1 loops=704248) 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=3080934 read=456280 written=12075 Total runtime: 197744.449 ms (56 rows) COMMIT; COMMIT