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 '%royal%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3671535.30..3671541.14 rows=167 width=46) (actual time=698849.713..701817.604 rows=175 loops=1) Buffers: shared hit=30387823 read=605808 dirtied=17 written=2127 -> Sort (cost=3671535.30..3671535.72 rows=167 width=46) (actual time=698837.132..699663.069 rows=3267872 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 557849kB Buffers: shared hit=30387823 read=605808 dirtied=17 written=2127 -> Nested Loop (cost=2996753.03..3671529.13 rows=167 width=46) (actual time=192024.500..676905.889 rows=3267872 loops=1) Buffers: shared hit=30387815 read=605808 dirtied=17 written=2127 -> Nested Loop (cost=2996753.03..3671481.51 rows=167 width=24) (actual time=192022.727..558243.362 rows=3267872 loops=1) Buffers: shared hit=23852071 read=605808 dirtied=17 written=2127 -> Nested Loop (cost=2996753.03..3671153.42 rows=167 width=24) (actual time=192022.685..336070.023 rows=3267872 loops=1) Buffers: shared hit=10854658 read=513597 written=598 -> Merge Join (cost=2996753.03..3671104.78 rows=167 width=28) (actual time=192022.639..216134.479 rows=3267872 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=1032937 read=513597 written=598 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.29 rows=8000000 width=12) (actual time=0.010..15246.840 rows=7999941 loops=1) Buffers: shared hit=355396 read=78812 written=589 -> Sort (cost=2996753.00..3007172.63 rows=4167853 width=28) (actual time=192022.563..195484.863 rows=3267872 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 353607kB Buffers: shared hit=677541 read=434785 written=9 -> Hash Join (cost=67248.69..2538479.37 rows=4167853 width=28) (actual time=3556.955..187617.879 rows=3267872 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=677538 read=434785 written=9 -> Seq Scan on lineitem (cost=0.00..1675244.29 rows=60344629 width=24) (actual time=0.020..63892.383 rows=60045826 loops=1) Buffers: shared hit=657863 read=413935 written=9 -> Hash (cost=65522.00..65522.00 rows=138135 width=4) (actual time=3556.748..3556.748 rows=108805 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3826kB Buffers: shared hit=19672 read=20850 -> Seq Scan on part (cost=0.00..65522.00 rows=138135 width=4) (actual time=0.271..3473.432 rows=108805 loops=1) Filter: ((p_name)::text ~~ '%royal%'::text) Rows Removed by Filter: 1891195 Buffers: shared hit=19672 read=20850 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.035..0.035 rows=1 loops=3267872) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9821721 -> Index Scan using pk_orders on orders (cost=0.00..1.95 rows=1 width=8) (actual time=0.066..0.066 rows=1 loops=3267872) Index Cond: (o_orderkey = lineitem.l_orderkey) Buffers: shared hit=12997413 read=92211 dirtied=17 written=1529 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.033..0.034 rows=1 loops=3267872) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6535744 Total runtime: 701938.357 ms (43 rows) COMMIT; COMMIT