EXPLAIN select supp_nation, cust_nation, l_year, sum(volume) as revenue from ( select n1.n_name as supp_nation, n2.n_name as cust_nation, extract(year from l_shipdate) as l_year, l_extendedprice * (1 - l_discount) as volume from supplier, lineitem, orders, customer, nation n1, nation n2 where s_suppkey = l_suppkey and o_orderkey = l_orderkey and c_custkey = o_custkey and s_nationkey = n1.n_nationkey and c_nationkey = n2.n_nationkey and ( (n1.n_name = 'KENYA' and n2.n_name = 'FRANCE') or (n1.n_name = 'FRANCE' and n2.n_name = 'KENYA') ) and l_shipdate between date '1995-01-01' and date '1996-12-31' ) as shipping group by supp_nation, cust_nation, l_year order by supp_nation, cust_nation, l_year; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=2633327.64..2633335.20 rows=3025 width=64) Sort Key: n1.n_name, n2.n_name, (date_part('year'::text, (lineitem.l_shipdate)::timestamp without time zone)) -> HashAggregate (cost=2633107.38..2633152.76 rows=3025 width=64) Group Key: n1.n_name, n2.n_name, date_part('year'::text, (lineitem.l_shipdate)::timestamp without time zone) -> Hash Join (cost=1172274.38..2632219.83 rows=59170 width=64) Hash Cond: (lineitem.l_orderkey = orders.o_orderkey) Join Filter: (((n1.n_name = 'KENYA'::bpchar) AND (n2.n_name = 'FRANCE'::bpchar)) OR ((n1.n_name = 'FRANCE'::bpchar) AND (n2.n_name = 'KENYA'::bpchar))) -> Hash Join (cost=387603.55..1806448.51 rows=1421804 width=42) Hash Cond: (lineitem.l_suppkey = supplier.s_suppkey) -> Bitmap Heap Scan on lineitem (cost=383867.15..1720650.68 rows=18091569 width=20) Recheck Cond: ((l_shipdate >= '1995-01-01'::date) AND (l_shipdate <= '1996-12-31'::date)) -> Bitmap Index Scan on i_l_shipdate (cost=0.00..379344.26 rows=18091569 width=0) Index Cond: ((l_shipdate >= '1995-01-01'::date) AND (l_shipdate <= '1996-12-31'::date)) -> Hash (cost=3636.40..3636.40 rows=8000 width=30) -> Hash Join (cost=1.40..3636.40 rows=8000 width=30) Hash Cond: (supplier.s_nationkey = n1.n_nationkey) -> Seq Scan on supplier (cost=0.00..3180.00 rows=100000 width=8) -> Hash (cost=1.38..1.38 rows=2 width=30) -> Seq Scan on nation n1 (cost=0.00..1.38 rows=2 width=30) Filter: ((n_name = 'KENYA'::bpchar) OR (n_name = 'FRANCE'::bpchar)) -> Hash (cost=761466.98..761466.98 rows=1199988 width=30) -> Hash Join (cost=59433.21..761466.98 rows=1199988 width=30) Hash Cond: (orders.o_custkey = customer.c_custkey) -> Seq Scan on orders (cost=0.00..403276.55 rows=14999855 width=8) -> Hash (cost=57112.10..57112.10 rows=120009 width=30) -> Hash Join (cost=1.40..57112.10 rows=120009 width=30) Hash Cond: (customer.c_nationkey = n2.n_nationkey) -> Seq Scan on customer (cost=0.00..50285.17 rows=1500117 width=8) -> Hash (cost=1.38..1.38 rows=2 width=30) -> Seq Scan on nation n2 (cost=0.00..1.38 rows=2 width=30) Filter: ((n_name = 'FRANCE'::bpchar) OR (n_name = 'KENYA'::bpchar)) (31 rows)