BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select c_count, count(*) as custdist from ( select c_custkey, count(o_orderkey) from customer left outer join orders on c_custkey = o_custkey and o_comment not like '%express%packages%' group by c_custkey ) as c_orders (c_custkey, c_count) group by c_count order by custdist desc, c_count desc; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=925439.88..925440.38 rows=200 width=8) (actual time=82422.873..82422.876 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=250718 read=39858 written=19 -> HashAggregate (cost=925430.24..925432.24 rows=200 width=8) (actual time=82422.763..82422.778 rows=46 loops=1) Buffers: shared hit=250715 read=39858 written=19 -> HashAggregate (cost=887930.24..902930.24 rows=1500000 width=8) (actual time=81344.136..82096.897 rows=1500000 loops=1) Buffers: shared hit=250715 read=39858 written=19 -> Hash Right Join (cost=69028.00..813008.77 rows=14984294 width=8) (actual time=4267.797..60373.721 rows=15354088 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=250715 read=39858 written=19 -> Seq Scan on orders (cost=0.00..444294.89 rows=14984294 width=8) (actual time=0.056..14598.957 rows=14854067 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160933 Buffers: shared hit=250713 read=4582 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=4264.733..4264.733 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=19 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..3227.943 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=19 Total runtime: 82482.607 ms (21 rows) COMMIT; COMMIT