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 '%powder%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3550833.51..3550838.65 rows=147 width=46) (actual time=413967.095..416800.025 rows=175 loops=1) Buffers: shared hit=29633748 read=1262908 dirtied=172 written=6787 -> Sort (cost=3550833.51..3550833.87 rows=147 width=46) (actual time=413955.009..414707.055 rows=3256327 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 556225kB Buffers: shared hit=29633748 read=1262908 dirtied=172 written=6787 -> Nested Loop (cost=2879532.57..3550828.21 rows=147 width=46) (actual time=159768.824..394076.336 rows=3256327 loops=1) Buffers: shared hit=29633740 read=1262908 dirtied=172 written=6787 -> Nested Loop (cost=2879532.57..3550786.29 rows=147 width=24) (actual time=159768.748..367140.161 rows=3256327 loops=1) Buffers: shared hit=23121086 read=1262908 dirtied=172 written=6787 -> Nested Loop (cost=2879532.57..3550494.10 rows=147 width=24) (actual time=159768.617..226251.103 rows=3256327 loops=1) Buffers: shared hit=10553954 read=786867 written=6274 -> Merge Join (cost=2879532.57..3550451.29 rows=147 width=28) (actual time=159768.532..177720.233 rows=3256327 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=767137 read=786862 written=6274 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.29 rows=8000000 width=12) (actual time=0.093..10199.967 rows=8000000 loops=1) Buffers: shared hit=305907 read=128302 written=1809 -> Sort (cost=2879532.40..2888808.23 rows=3710332 width=28) (actual time=159768.362..162712.107 rows=3256327 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 352705kB Buffers: shared hit=461230 read=658560 written=4465 -> Hash Join (cost=67048.50..2474677.36 rows=3710332 width=28) (actual time=2169.394..154077.571 rows=3256327 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=461227 read=658560 written=4465 -> Seq Scan on lineitem (cost=0.00..1686915.43 rows=60765343 width=24) (actual time=0.036..40505.211 rows=60045297 loops=1) Buffers: shared hit=461222 read=618040 written=3 -> Hash (cost=65522.00..65522.00 rows=122120 width=4) (actual time=2169.119..2169.119 rows=108553 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3817kB Buffers: shared hit=2 read=40520 written=4462 -> Seq Scan on part (cost=0.00..65522.00 rows=122120 width=4) (actual time=0.028..1731.813 rows=108553 loops=1) Filter: ((p_name)::text ~~ '%powder%'::text) Rows Removed by Filter: 1891447 Buffers: shared hit=2 read=40520 written=4462 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.013..0.014 rows=1 loops=3256327) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9786817 read=5 -> Index Only Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.98 rows=1 width=8) (actual time=0.042..0.042 rows=1 loops=3256327) Index Cond: (o_orderkey = lineitem.l_orderkey) Heap Fetches: 3256327 Buffers: shared hit=12567132 read=476041 dirtied=172 written=513 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.005..0.006 rows=1 loops=3256327) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6512654 Total runtime: 416902.532 ms (44 rows) COMMIT; COMMIT