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=5095005.46..5095015.15 rows=277 width=46) Group Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) -> Sort (cost=5095005.46..5095006.15 rows=277 width=46) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) -> Hash Join (cost=4178164.17..5094994.22 rows=277 width=46) Hash Cond: (supplier.s_nationkey = nation.n_nationkey) -> Nested Loop (cost=4178162.61..5094987.46 rows=277 width=24) -> Nested Loop (cost=4178162.17..5093380.01 rows=277 width=24) -> Merge Join (cost=4178161.88..5091824.05 rows=279 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=4178150.04..4215534.86 rows=7476964 width=28) -> Sort (cost=4178150.04..4196842.45 rows=7476964 width=28) Sort Key: part.p_partkey -> Hash Join (cost=69935.03..2966713.27 rows=7476964 width=28) Hash Cond: (lineitem.l_partkey = part.p_partkey) -> Seq Scan on lineitem (cost=0.00..1666958.20 rows=60046720 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)