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 '%gainsboro%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3482669.57..3482674.12 rows=130 width=46) (actual time=430673.790..434284.485 rows=175 loops=1) Buffers: shared hit=29841788 read=949925 dirtied=51 written=3558 -> Sort (cost=3482669.57..3482669.90 rows=130 width=46) (actual time=430660.402..431721.247 rows=3245136 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 554652kB Buffers: shared hit=29841788 read=949925 dirtied=51 written=3558 -> Nested Loop (cost=2814705.79..3482665.01 rows=130 width=46) (actual time=148109.593..407524.749 rows=3245136 loops=1) Buffers: shared hit=29841780 read=949925 dirtied=51 written=3558 -> Nested Loop (cost=2814705.79..3482627.93 rows=130 width=24) (actual time=148109.532..375417.523 rows=3245136 loops=1) Buffers: shared hit=23351508 read=949925 dirtied=51 written=3558 -> Nested Loop (cost=2814705.79..3482371.25 rows=130 width=24) (actual time=148109.429..225476.506 rows=3245136 loops=1) Buffers: shared hit=10567319 read=735930 written=1769 -> Merge Join (cost=2814705.79..3482333.39 rows=130 width=28) (actual time=148109.383..166406.015 rows=3245136 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=813782 read=735930 written=1769 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.29 rows=8000000 width=12) (actual time=0.009..10630.691 rows=7999949 loops=1) Buffers: shared hit=333747 read=100461 written=1769 -> Sort (cost=2814705.65..2822884.49 rows=3271536 width=28) (actual time=148109.317..150807.437 rows=3245136 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 351831kB Buffers: shared hit=480035 read=635469 -> Hash Join (cost=66873.34..2460700.31 rows=3271536 width=28) (actual time=3127.106..143585.115 rows=3245136 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=480032 read=635469 -> Seq Scan on lineitem (cost=0.00..1680216.29 rows=60524029 width=24) (actual time=0.016..34137.162 rows=60044082 loops=1) Buffers: shared hit=479908 read=595068 -> Hash (cost=65522.00..65522.00 rows=108107 width=4) (actual time=3126.839..3126.839 rows=108037 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3799kB Buffers: shared hit=121 read=40401 -> Seq Scan on part (cost=0.00..65522.00 rows=108107 width=4) (actual time=0.018..2819.458 rows=108037 loops=1) Filter: ((p_name)::text ~~ '%gainsboro%'::text) Rows Removed by Filter: 1891963 Buffers: shared hit=121 read=40401 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.016..0.017 rows=1 loops=3245136) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9753537 -> Index Only Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.96 rows=1 width=8) (actual time=0.045..0.045 rows=1 loops=3245136) Index Cond: (o_orderkey = lineitem.l_orderkey) Heap Fetches: 3245136 Buffers: shared hit=12784189 read=213995 dirtied=51 written=1789 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.007..0.007 rows=1 loops=3245136) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6490272 Total runtime: 434383.331 ms (44 rows) COMMIT; COMMIT