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 '%medium%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3514462.13..3514467.17 rows=144 width=46) (actual time=267221.894..270812.846 rows=175 loops=1) Buffers: shared hit=30408529 read=606031 dirtied=658 written=1880 -> Sort (cost=3514462.13..3514462.49 rows=144 width=46) (actual time=267203.352..268346.108 rows=3270538 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 558224kB Buffers: shared hit=30408529 read=606031 dirtied=658 written=1880 -> Nested Loop (cost=2843898.38..3514456.97 rows=144 width=46) (actual time=126076.616..246355.212 rows=3270538 loops=1) Buffers: shared hit=30408521 read=606031 dirtied=658 written=1880 -> Nested Loop (cost=2843898.38..3514415.90 rows=144 width=24) (actual time=126076.521..227607.204 rows=3270538 loops=1) Buffers: shared hit=23867445 read=606031 dirtied=658 written=1880 -> Nested Loop (cost=2843898.38..3514134.41 rows=144 width=24) (actual time=126076.478..161813.989 rows=3270538 loops=1) Buffers: shared hit=10888646 read=484873 dirtied=654 written=827 -> Merge Join (cost=2843898.38..3514092.48 rows=144 width=28) (actual time=126076.429..139026.337 rows=3270538 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=1058472 read=484873 dirtied=654 written=827 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.33 rows=8000000 width=12) (actual time=0.015..6067.794 rows=7999993 loops=1) Buffers: shared hit=329424 read=104785 written=230 -> Sort (cost=2843898.36..2852932.16 rows=3613517 width=28) (actual time=126076.249..128598.934 rows=3270538 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 353815kB Buffers: shared hit=729048 read=380088 dirtied=654 written=597 -> Hash Join (cost=67023.48..2450296.52 rows=3613517 width=28) (actual time=1931.769..122065.484 rows=3270538 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=729045 read=380088 dirtied=654 written=597 -> Seq Scan on lineitem (cost=0.00..1670269.12 rows=60166112 width=24) (actual time=0.035..23426.252 rows=60045379 loops=1) Buffers: shared hit=727258 read=341350 dirtied=654 written=597 -> Hash (cost=65522.00..65522.00 rows=120118 width=4) (actual time=1931.372..1931.372 rows=108950 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3831kB Buffers: shared hit=1784 read=38738 -> Seq Scan on part (cost=0.00..65522.00 rows=120118 width=4) (actual time=0.027..1842.513 rows=108950 loops=1) Filter: ((p_name)::text ~~ '%medium%'::text) Rows Removed by Filter: 1891050 Buffers: shared hit=1784 read=38738 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.005..0.006 rows=1 loops=3270538) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9830174 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=8) (actual time=0.019..0.019 rows=1 loops=3270538) Index Cond: (o_orderkey = lineitem.l_orderkey) Buffers: shared hit=12978799 read=121158 dirtied=4 written=1053 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.003..0.004 rows=1 loops=3270538) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6541076 Total runtime: 270922.966 ms (43 rows) COMMIT; COMMIT