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=4818472.59..4818472.59 rows=1 width=26) (actual time=152359.156..152359.184 rows=100 loops=1) Buffers: shared hit=10047882 read=1490260 written=1152, temp read=161683 written=161677 -> Sort (cost=4818472.59..4818472.59 rows=1 width=26) (actual time=152359.153..152359.165 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10047882 read=1490260 written=1152, temp read=161683 written=161677 -> HashAggregate (cost=4818472.57..4818472.58 rows=1 width=26) (actual time=152351.678..152353.944 rows=3995 loops=1) Buffers: shared hit=10047874 read=1490260 written=1152, temp read=161683 written=161677 -> Nested Loop Anti Join (cost=2662442.40..4818472.56 rows=1 width=26) (actual time=53170.360..152196.832 rows=39578 loops=1) Buffers: shared hit=10047874 read=1490260 written=1152, temp read=161683 written=161677 -> Nested Loop (cost=2662442.40..4818457.59 rows=1 width=34) (actual time=53163.422..134113.290 rows=704978 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7132664 read=865746 written=170, temp read=161683 written=161677 -> Hash Semi Join (cost=2662442.40..4818455.63 rows=1 width=38) (actual time=53163.150..115857.793 rows=1462283 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378197 Buffers: shared hit=1378186 read=763012 written=91, temp read=161683 written=161677 -> Hash Join (cost=2441.58..1908240.78 rows=803009 width=34) (actual time=49.085..47749.979 rows=1516407 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=775828 read=295704 written=91 -> Seq Scan on lineitem l1 (cost=0.00..1822487.01 rows=20075227 width=8) (actual time=0.026..31425.523 rows=37966584 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078719 Buffers: shared hit=773976 read=295690 written=91 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=48.860..48.860 rows=3995 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1849 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.292..46.443 rows=3995 loops=1) Buffers: shared hit=1849 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.019 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.281..44.911 rows=3995 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1848 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=0.970..0.970 rows=3995 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1671922.81..1671922.81 rows=60225681 width=8) (actual time=53091.068..53091.068 rows=60045303 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586635kB Buffers: shared hit=602358 read=467308, temp written=153901 -> Seq Scan on lineitem l2 (cost=0.00..1671922.81 rows=60225681 width=8) (actual time=0.026..28088.482 rows=60045303 loops=1) Buffers: shared hit=602358 read=467308 -> Index Scan using pk_orders on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.011..0.011 rows=0 loops=1462283) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5754478 read=102734 written=79 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.46 rows=5 width=8) (actual time=0.025..0.025 rows=1 loops=704978) 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=2915210 read=624514 written=982 Total runtime: 152362.456 ms (56 rows) COMMIT; COMMIT