BEGIN; BEGIN EXPLAIN select nation, o_year, sum(amount) as sum_profit from ( select n_name as nation, extract(year from o_orderdate) as o_year, l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount from part, supplier, lineitem, partsupp, orders, nation where s_suppkey = l_suppkey and ps_suppkey = l_suppkey and ps_partkey = l_partkey and p_partkey = l_partkey and o_orderkey = l_orderkey and s_nationkey = n_nationkey and p_name like '%almond%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=1165878.66..1165883.03 rows=125 width=46) Group Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) -> Sort (cost=1165878.66..1165878.97 rows=125 width=46) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) -> Hash Join (cost=66826.10..1165874.30 rows=125 width=46) Hash Cond: (supplier.s_nationkey = nation.n_nationkey) -> Nested Loop (cost=66824.54..1165870.40 rows=125 width=24) -> Nested Loop (cost=66824.11..1165809.82 rows=125 width=24) -> Nested Loop (cost=66823.82..1165769.49 rows=125 width=28) -> Hash Join (cost=66823.25..351945.25 rows=416400 width=16) Hash Cond: (partsupp.ps_partkey = part.p_partkey) -> Seq Scan on partsupp (cost=0.00..250958.00 rows=8000000 width=12) -> Hash (cost=65522.00..65522.00 rows=104100 width=4) -> Seq Scan on part (cost=0.00..65522.00 rows=104100 width=4) Filter: ((p_name)::text ~~ '%almond%'::text) -> Index Scan using lineitem_l_partkey_l_quantity_l_shipmode_idx on lineitem (cost=0.56..1.94 rows=1 width=24) Index Cond: (l_partkey = partsupp.ps_partkey) Filter: (partsupp.ps_suppkey = l_suppkey) -> Index Scan using pk_supplier on supplier (cost=0.29..0.31 rows=1 width=8) Index Cond: (s_suppkey = lineitem.l_suppkey) -> Index Scan using pk_orders on orders (cost=0.43..0.47 rows=1 width=8) Index Cond: (o_orderkey = lineitem.l_orderkey) -> Hash (cost=1.25..1.25 rows=25 width=30) -> Seq Scan on nation (cost=0.00..1.25 rows=25 width=30) Planning time: 37.031 ms (25 rows) COMMIT; COMMIT