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=3515582.92..3515587.93 rows=143 width=46) (actual time=261507.396..265025.328 rows=175 loops=1) Buffers: shared hit=30226059 read=701091 written=2405 -> Sort (cost=3515582.92..3515583.28 rows=143 width=46) (actual time=261493.135..262579.218 rows=3260509 loops=1) Sort Key: nation.n_name, (date_part('year'::text, (orders.o_orderdate)::timestamp without time zone)) Sort Method: quicksort Memory: 556814kB Buffers: shared hit=30226059 read=701091 written=2405 -> Nested Loop (cost=2845386.45..3515577.80 rows=143 width=46) (actual time=133144.587..240545.026 rows=3260509 loops=1) Buffers: shared hit=30226051 read=701091 written=2405 -> Nested Loop (cost=2845386.45..3515537.02 rows=143 width=24) (actual time=133144.501..222900.884 rows=3260509 loops=1) Buffers: shared hit=23705033 read=701091 written=2405 -> Nested Loop (cost=2845386.45..3515256.08 rows=143 width=24) (actual time=133144.383..166114.772 rows=3260509 loops=1) Buffers: shared hit=10653714 read=692550 written=2401 -> Merge Join (cost=2845386.45..3515214.44 rows=143 width=28) (actual time=133144.304..146232.277 rows=3260509 loops=1) Merge Cond: ((partsupp.ps_partkey = lineitem.l_partkey) AND (partsupp.ps_suppkey = lineitem.l_suppkey)) Buffers: shared hit=854291 read=692550 written=2401 -> Index Scan using pk_partsupp on partsupp (cost=0.00..603091.33 rows=8000000 width=12) (actual time=0.008..6623.321 rows=7999917 loops=1) Buffers: shared hit=267190 read=167018 written=2395 -> Sort (cost=2845386.32..2854298.45 rows=3564854 width=28) (actual time=133144.241..135425.489 rows=3260509 loops=1) Sort Key: part.p_partkey, lineitem.l_suppkey Sort Method: quicksort Memory: 353032kB Buffers: shared hit=587101 read=525532 written=6 -> Hash Join (cost=66998.45..2457433.75 rows=3564854 width=28) (actual time=1507.259..128109.251 rows=3260509 loops=1) Hash Cond: (lineitem.l_partkey = part.p_partkey) Buffers: shared hit=587098 read=525532 written=6 -> Seq Scan on lineitem (cost=0.00..1675715.18 rows=60361918 width=24) (actual time=0.023..24415.466 rows=59985687 loops=1) Buffers: shared hit=581152 read=490953 written=6 -> Hash (cost=65522.00..65522.00 rows=118116 width=4) (actual time=1507.065..1507.065 rows=108775 loops=1) Buckets: 16384 Batches: 1 Memory Usage: 3825kB Buffers: shared hit=5943 read=34579 -> Seq Scan on part (cost=0.00..65522.00 rows=118116 width=4) (actual time=0.014..1440.828 rows=108775 loops=1) Filter: ((p_name)::text ~~ '%khaki%'::text) Rows Removed by Filter: 1891225 Buffers: shared hit=5943 read=34579 -> Index Scan using pk_supplier on supplier (cost=0.00..0.28 rows=1 width=8) (actual time=0.005..0.005 rows=1 loops=3260509) Index Cond: (s_suppkey = lineitem.l_suppkey) Buffers: shared hit=9799423 -> Index Only Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.95 rows=1 width=8) (actual time=0.016..0.016 rows=1 loops=3260509) Index Cond: (o_orderkey = lineitem.l_orderkey) Heap Fetches: 3260509 Buffers: shared hit=13051319 read=8541 written=4 -> Index Scan using pk_nation on nation (cost=0.00..0.27 rows=1 width=30) (actual time=0.003..0.003 rows=1 loops=3260509) Index Cond: (n_nationkey = supplier.s_nationkey) Buffers: shared hit=6521018 Total runtime: 265156.655 ms (44 rows) COMMIT; COMMIT