BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) 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 '%tomato%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3388245.15..3388249.59 rows=127 width=46) (actual time=411659.367..414870.322 rows=175 loops=1) Buffers: shared hit=30354846 read=830618 dirtied=79 written=4917 -> Sort (cost=3388245.15..3388245.47 rows=127 width=46) (actual time=411647.102..412529.842 rows=3289434 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 560881kB Buffers: shared hit=30354846 read=830618 dirtied=79 written=4917 -> Nested Loop (cost=2720840.13..3388240.71 rows=127 width=46) (actual time=169081.486..389247.863 rows=3289434 loops=1) Buffers: shared hit=30354838 read=830618 dirtied=79 written=4917 -> Nested Loop (cost=2720840.13..3388204.49 rows=127 width=24) (actual time=169081.428..360926.225 rows=3289434 loops=1) Buffers: shared hit=23775970 read=830618 dirtied=79 written=4917 -> Nested Loop (cost=2720840.13..3387955.40 rows=127 width=24) (actual time=169081.308..235471.998 rows=3289434 loops=1) Buffers: shared hit=10728849 read=702079 written=2710 -> Merge Join (cost=2720840.13..3387918.42 rows=127 width=28) (actual time=169081.276..187150.511 rows=3289434 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=843398 read=702079 written=2710 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.29 rows=8000000 width=12) (actual time=0.038..10315.627 rows=7999989 loops=1) Buffers: shared hit=289426 read=144783 written=2707 -> Sort (cost=2720839.99..2728835.73 rows=3198298 width=28) (actual time=169081.106..171857.488 rows=3289434 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 355292kB Buffers: shared hit=553972 read=557296 written=3 -> Hash Join (cost=66848.31..2375281.91 rows=3198298 width=28) (actual time=3629.284..163913.457 rows=3289434 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=553969 read=557296 written=3 -> Seq Scan on lineitem (cost=0.00..1673595.31 rows=60285531 width=24) (actual time=0.045..43993.733 rows=60046221 loops=1) Buffers: shared hit=544900 read=525840 written=3 -> Hash (cost=65522.00..65522.00 rows=106105 width=4) (actual time=3629.031..3629.031 rows=109571 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3853kB Buffers: shared hit=9066 read=31456 -> Seq Scan on part (cost=0.00..65522.00 rows=106105 width=4) (actual time=0.041..3533.814 rows=109571 loops=1) Filter: ((p_name)::text ~~ '%tomato%'::text) Rows Removed by Filter: 1890429 Buffers: shared hit=9066 read=31456 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.013..0.013 rows=1 loops=3289434) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9885451 -> Index Only Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.95 rows=1 width=8) (actual time=0.037..0.037 rows=1 loops=3289434) Index Cond: (o_orderkey = lineitem.l_orderkey) Heap Fetches: 3289434 Buffers: shared hit=13047121 read=128539 dirtied=79 written=2207 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.006..0.006 rows=1 loops=3289434) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6578868 Total runtime: 414975.195 ms (44 rows) COMMIT; COMMIT