BEGIN; BEGIN create or replace view revenue2 (supplier_no, total_revenue) as select l_suppkey, sum(l_extendedprice * (1 - l_discount)) from lineitem where l_shipdate >= '1997-01-01' and l_shipdate < date'1997-01-01' + interval '90 days' group by l_suppkey; CREATE VIEW EXPLAIN (ANALYZE, BUFFERS) select s_suppkey, s_name, s_address, s_phone, total_revenue from supplier, revenue2 where s_suppkey = supplier_no and total_revenue = ( select max(total_revenue) from revenue2 ) order by s_suppkey; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Merge Join (cost=2362821.87..2367918.09 rows=3668 width=79) (actual time=25205.357..25205.364 rows=1 loops=1) Merge Cond: (supplier.s_suppkey = revenue2.supplier_no) Buffers: shared hit=832232 read=493160 written=75 InitPlan 1 (returns $0) -> Aggregate (cost=1173001.90..1173001.91 rows=1 width=8) (actual time=10798.397..10798.397 rows=1 loops=1) Buffers: shared hit=439230 read=222271 -> HashAggregate (cost=1172919.37..1172956.05 rows=3668 width=12) (actual time=10735.356..10781.665 rows=100000 loops=1) Group Key: lineitem_1.l_suppkey Buffers: shared hit=439230 read=222271 -> Bitmap Heap Scan on lineitem lineitem_1 (cost=46844.05..1150785.24 rows=2213413 width=12) (actual time=2352.821..8506.992 rows=2247163 loops=1) Recheck Cond: ((l_shipdate >= '1997-01-01'::date) AND (l_shipdate < '1997-04-01 00:00:00'::timestamp without time zone)) Heap Blocks: exact=655363 Buffers: shared hit=439230 read=222271 -> Bitmap Index Scan on lineitem_l_shipdate_l_suppkey__idx (cost=0.00..46290.70 rows=2213413 width=0) (actual time=1770.105..1770.105 rows=2256133 loops=1) Index Cond: ((l_shipdate >= '1997-01-01'::date) AND (l_shipdate < '1997-04-01 00:00:00'::timestamp without time zone)) Buffers: shared read=6138 -> Index Scan using pk_supplier on supplier (cost=0.29..4791.49 rows=100000 width=71) (actual time=0.048..47.248 rows=87911 loops=1) Buffers: shared hit=2390 -> Sort (cost=1189819.66..1189828.83 rows=3668 width=12) (actual time=25144.718..25144.720 rows=1 loops=1) Sort Key: revenue2.supplier_no Sort Method: quicksort Memory: 25kB Buffers: shared hit=829842 read=493160 written=75 -> Subquery Scan on revenue2 (cost=1189519.97..1189602.50 rows=3668 width=12) (actual time=25103.645..25144.681 rows=1 loops=1) Buffers: shared hit=829842 read=493160 written=75 -> HashAggregate (cost=1189519.97..1189565.82 rows=3668 width=12) (actual time=25103.643..25144.675 rows=1 loops=1) Group Key: lineitem.l_suppkey Filter: (sum((lineitem.l_extendedprice * (1::double precision - lineitem.l_discount))) = $0) Rows Removed by Filter: 99999 Buffers: shared hit=829842 read=493160 written=75 -> Bitmap Heap Scan on lineitem (cost=46844.05..1150785.24 rows=2213413 width=12) (actual time=2163.316..10733.956 rows=2247163 loops=1) Recheck Cond: ((l_shipdate >= '1997-01-01'::date) AND (l_shipdate < '1997-04-01 00:00:00'::timestamp without time zone)) Heap Blocks: exact=655363 Buffers: shared hit=390612 read=270889 written=75 -> Bitmap Index Scan on lineitem_l_shipdate_l_suppkey__idx (cost=0.00..46290.70 rows=2213413 width=0) (actual time=1457.110..1457.110 rows=2256133 loops=1) Index Cond: ((l_shipdate >= '1997-01-01'::date) AND (l_shipdate < '1997-04-01 00:00:00'::timestamp without time zone)) Buffers: shared read=6138 Planning time: 3.350 ms Execution time: 25225.540 ms (38 rows) drop view revenue2; DROP VIEW COMMIT; COMMIT