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 '%chocolate%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3366790.44..3366794.64 rows=120 width=46) (actual time=215464.620..218946.440 rows=175 loops=1) Buffers: shared hit=30075888 read=1066918 dirtied=23 written=10573 -> Sort (cost=3366790.44..3366790.74 rows=120 width=46) (actual time=215449.968..216509.350 rows=3284484 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 560185kB Buffers: shared hit=30075888 read=1066918 dirtied=23 written=10573 -> Nested Loop (cost=2700747.15..3366786.29 rows=120 width=46) (actual time=97599.377..194078.569 rows=3284484 loops=1) Buffers: shared hit=30075880 read=1066918 dirtied=23 written=10573 -> Nested Loop (cost=2700747.15..3366752.07 rows=120 width=24) (actual time=97599.288..180386.837 rows=3284484 loops=1) Buffers: shared hit=23506912 read=1066918 dirtied=23 written=10573 -> Nested Loop (cost=2700747.15..3366516.32 rows=120 width=24) (actual time=97599.167..124223.187 rows=3284484 loops=1) Buffers: shared hit=10819651 read=598271 written=2933 -> Merge Join (cost=2700747.15..3366481.37 rows=120 width=28) (actual time=97599.077..108646.553 rows=3284484 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=948840 read=597688 written=2901 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.26 rows=8000000 width=12) (actual time=0.009..5118.431 rows=7999845 loops=1) Buffers: shared hit=289061 read=145141 written=2901 -> Sort (cost=2700742.09..2708292.66 rows=3020231 width=28) (actual time=97599.015..99888.259 rows=3284484 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 354905kB Buffers: shared hit=659779 read=452547 -> Hash Join (cost=66773.24..2375671.19 rows=3020231 width=28) (actual time=1580.050..92851.614 rows=3284484 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=659776 read=452547 -> Seq Scan on lineitem (cost=0.00..1675246.82 rows=60344882 width=24) (actual time=0.030..15755.127 rows=60045826 loops=1) Buffers: shared hit=637298 read=434500 -> Hash (cost=65522.00..65522.00 rows=100099 width=4) (actual time=1579.839..1579.839 rows=109369 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3846kB Buffers: shared hit=22475 read=18047 -> Seq Scan on part (cost=0.00..65522.00 rows=100099 width=4) (actual time=0.018..1502.666 rows=109369 loops=1) Filter: ((p_name)::text ~~ '%chocolate%'::text) Rows Removed by Filter: 1890631 Buffers: shared hit=22475 read=18047 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.004..0.004 rows=1 loops=3284484) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9870811 read=583 written=32 -> Index Scan using pk_orders on orders (cost=0.00..1.95 rows=1 width=8) (actual time=0.016..0.016 rows=1 loops=3284484) Index Cond: (o_orderkey = lineitem.l_orderkey) Buffers: shared hit=12687261 read=468647 dirtied=23 written=7640 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.002..0.003 rows=1 loops=3284484) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6568968 Total runtime: 219125.603 ms (43 rows) COMMIT; COMMIT