EXPLAIN select c_custkey, c_name, sum(l_extendedprice * (1 - l_discount)) as revenue, c_acctbal, n_name, c_address, c_phone, c_comment from customer, orders, lineitem, nation where c_custkey = o_custkey and l_orderkey = o_orderkey and o_orderdate >= date '1994-05-01' and o_orderdate < date '1994-05-01' + interval '3 months' and l_returnflag = 'R' and c_nationkey = n_nationkey group by c_custkey, c_name, c_acctbal, c_phone, n_name, c_address, c_comment order by revenue desc LIMIT 20; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=2735707.98..2735708.03 rows=20 width=175) -> Sort (cost=2735707.98..2737177.54 rows=587826 width=175) Sort Key: (sum((lineitem.l_extendedprice * (1::double precision - lineitem.l_discount)))) -> GroupAggregate (cost=2698022.66..2720066.14 rows=587826 width=175) Group Key: customer.c_custkey, customer.c_name, customer.c_acctbal, customer.c_phone, nation.n_name, customer.c_address, customer.c_comment -> Sort (cost=2698022.66..2699492.23 rows=587826 width=175) Sort Key: customer.c_custkey, customer.c_name, customer.c_acctbal, customer.c_phone, nation.n_name, customer.c_address, customer.c_comment -> Hash Join (cost=385610.23..2541230.15 rows=587826 width=175) Hash Cond: (customer.c_nationkey = nation.n_nationkey) -> Hash Join (cost=385608.67..2533145.98 rows=587826 width=153) Hash Cond: (orders.o_custkey = customer.c_custkey) -> Hash Join (cost=284342.03..2384355.18 rows=587826 width=12) Hash Cond: (lineitem.l_orderkey = orders.o_orderkey) -> Seq Scan on lineitem (cost=0.00..1815233.20 rows=14938478 width=12) Filter: (l_returnflag = 'R'::bpchar) -> Hash (cost=274658.02..274658.02 rows=590241 width=8) -> Bitmap Heap Scan on orders (cost=12526.41..274658.02 rows=590241 width=8) Recheck Cond: ((o_orderdate >= '1994-05-01'::date) AND (o_orderdate < '1994-08-01 00:00:00'::timestamp without time zone)) -> Bitmap Index Scan on i_o_orderdate (cost=0.00..12378.84 rows=590241 width=0) Index Cond: ((o_orderdate >= '1994-05-01'::date) AND (o_orderdate < '1994-08-01 00:00:00'::timestamp without time zone)) -> Hash (cost=50285.17..50285.17 rows=1500117 width=145) -> Seq Scan on customer (cost=0.00..50285.17 rows=1500117 width=145) -> Hash (cost=1.25..1.25 rows=25 width=30) -> Seq Scan on nation (cost=0.00..1.25 rows=25 width=30) (24 rows)