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 >= '1996-02-01' and l_shipdate < date'1996-02-01' + interval '90 days' group by l_suppkey; CREATE VIEW EXPLAIN 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=2355841.89..2360938.93 rows=3705 width=79) Merge Cond: (supplier.s_suppkey = revenue2.supplier_no) InitPlan 1 (returns $0) -> Aggregate (cost=1169460.56..1169460.57 rows=1 width=8) -> HashAggregate (cost=1169377.20..1169414.25 rows=3705 width=12) -> Bitmap Heap Scan on lineitem (cost=47231.72..1147109.01 rows=2226819 width=12) Recheck Cond: ((l_shipdate >= '1996-02-01'::date) AND (l_shipdate < '1996-05-01 00:00:00'::timestamp without time zone)) -> Bitmap Index Scan on lineitem_l_shipdate_l_suppkey__idx (cost=0.00..46675.02 rows=2226819 width=0) Index Cond: ((l_shipdate >= '1996-02-01'::date) AND (l_shipdate < '1996-05-01 00:00:00'::timestamp without time zone)) -> Index Scan using pk_supplier on supplier (cost=0.00..4791.46 rows=100000 width=71) -> Sort (cost=1186381.32..1186390.59 rows=3705 width=12) Sort Key: revenue2.supplier_no -> Subquery Scan on revenue2 (cost=1186078.34..1186161.70 rows=3705 width=12) -> HashAggregate (cost=1186078.34..1186124.65 rows=3705 width=12) Filter: (sum((public.lineitem.l_extendedprice * (1::double precision - public.lineitem.l_discount))) = $0) -> Bitmap Heap Scan on lineitem (cost=47231.72..1147109.01 rows=2226819 width=12) Recheck Cond: ((l_shipdate >= '1996-02-01'::date) AND (l_shipdate < '1996-05-01 00:00:00'::timestamp without time zone)) -> Bitmap Index Scan on lineitem_l_shipdate_l_suppkey__idx (cost=0.00..46675.02 rows=2226819 width=0) Index Cond: ((l_shipdate >= '1996-02-01'::date) AND (l_shipdate < '1996-05-01 00:00:00'::timestamp without time zone)) (19 rows) drop view revenue2; DROP VIEW COMMIT; COMMIT