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 '%unusual%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=924177.69..924178.19 rows=200 width=8) (actual time=84465.118..84465.122 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=262485 read=27583 written=1 -> HashAggregate (cost=924168.05..924170.05 rows=200 width=8) (actual time=84465.001..84465.011 rows=46 loops=1) Buffers: shared hit=262482 read=27583 written=1 -> HashAggregate (cost=886668.05..901668.05 rows=1500000 width=8) (actual time=83384.927..84135.494 rows=1500000 loops=1) Buffers: shared hit=262482 read=27583 written=1 -> Hash Right Join (cost=69028.00..811822.20 rows=14969170 width=8) (actual time=2337.812..61785.036 rows=15352769 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=262482 read=27583 written=1 -> Seq Scan on orders (cost=0.00..443410.80 rows=14969170 width=8) (actual time=0.049..19944.586 rows=14852748 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162252 Buffers: shared hit=227204 read=27583 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2335.928..2335.928 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..1554.814 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 84498.662 ms (21 rows) COMMIT; COMMIT