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 '%khaki%' ) as profit group by nation, o_year order by nation, o_year desc; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=3377475.35..3377479.80 rows=127 width=46) (actual time=105298.448..108474.001 rows=175 loops=1) Buffers: shared hit=29975503 read=972242 written=7246 -> Sort (cost=3377475.35..3377475.67 rows=127 width=46) (actual time=105285.000..106257.111 rows=3263418 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 557223kB Buffers: shared hit=29975503 read=972242 written=7246 -> Nested Loop (cost=2710176.13..3377470.92 rows=127 width=46) (actual time=39040.392..88164.413 rows=3263418 loops=1) Buffers: shared hit=29975495 read=972242 written=7246 -> Nested Loop (cost=2710176.13..3377434.69 rows=127 width=24) (actual time=39040.305..82335.698 rows=3263418 loops=1) Buffers: shared hit=23448659 read=972242 written=7246 -> Nested Loop (cost=2710176.13..3377187.28 rows=127 width=24) (actual time=39040.118..53512.393 rows=3263418 loops=1) Buffers: shared hit=10671477 read=677892 written=1833 -> Merge Join (cost=2710176.13..3377150.30 rows=127 width=28) (actual time=39039.991..45975.555 rows=3263418 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=863319 read=677892 written=1833 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.26 rows=8000000 width=12) (actual time=0.009..2748.209 rows=7999917 loops=1) Buffers: shared hit=416656 read=17552 written=1833 -> Sort (cost=2710171.07..2718134.94 rows=3185547 width=28) (actual time=39039.884..40633.407 rows=3263418 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 353259kB Buffers: shared hit=446663 read=660340 -> Hash Join (cost=66848.31..2366082.46 rows=3185547 width=28) (actual time=835.562..36096.475 rows=3263418 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=446660 read=660340 -> Seq Scan on lineitem (cost=0.00..1666926.84 rows=60045184 width=24) (actual time=0.009..11100.720 rows=60045669 loops=1) Buffers: shared hit=406135 read=660340 -> Hash (cost=65522.00..65522.00 rows=106105 width=4) (actual time=835.341..835.341 rows=108775 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3825kB Buffers: shared hit=40522 -> Seq Scan on part (cost=0.00..65522.00 rows=106105 width=4) (actual time=0.015..804.342 rows=108775 loops=1) Filter: ((p_name)::text ~~ '%khaki%'::text) Rows Removed by Filter: 1891225 Buffers: shared hit=40522 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.002..0.002 rows=1 loops=3263418) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9808158 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=8) (actual time=0.008..0.008 rows=1 loops=3263418) Index Cond: (o_orderkey = lineitem.l_orderkey) Buffers: shared hit=12777182 read=294350 written=5413 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.001..0.001 rows=1 loops=3263418) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6526836 Total runtime: 108586.006 ms (43 rows) COMMIT; COMMIT