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 '%burnished%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=5090257.33..5090267.02 rows=277 width=46) Group Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) -> Sort (cost=5090257.33..5090258.02 rows=277 width=46) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) -> Hash Join (cost=4173944.32..5090246.09 rows=277 width=46) Hash Cond: (supplier.s_nationkey = nation.n_nationkey) -> Nested Loop (cost=4173942.76..5090239.33 rows=277 width=24) -> Nested Loop (cost=4173942.32..5088632.54 rows=277 width=24) -> Merge Join (cost=4173942.03..5087082.16 rows=278 width=28) Merge Cond: (partsupp.ps_partkey = lineitem.l_partkey) Join Filter: (lineitem.l_suppkey = partsupp.ps_suppkey) -> Index Scan using i_ps_partkey on partsupp (cost=0.43..378678.59 rows=8000241 width=12) -> Materialize (cost=4173930.19..4211277.11 rows=7469385 width=28) -> Sort (cost=4173930.19..4192603.65 rows=7469385 width=28) Sort Key: part.p_partkey -> Hash Join (cost=69935.03..2963777.36 rows=7469385 width=28) Hash Cond: (lineitem.l_partkey = part.p_partkey) -> Seq Scan on lineitem (cost=0.00..1665268.56 rows=59985856 width=24) -> Hash (cost=65626.29..65626.29 rows=262619 width=4) -> Seq Scan on part (cost=0.00..65626.29 rows=262619 width=4) Filter: ((p_name)::text ~~ '%burnished%'::text) -> Index Scan using supplier_pkey on supplier (cost=0.29..5.57 rows=1 width=8) Index Cond: (s_suppkey = lineitem.l_suppkey) -> Index Scan using orders_pkey on orders (cost=0.43..5.79 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) (27 rows)