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=926739.41..926739.91 rows=200 width=8) (actual time=70067.812..70067.816 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=253756 read=37579 written=36 -> HashAggregate (cost=926729.76..926731.76 rows=200 width=8) (actual time=70067.706..70067.722 rows=46 loops=1) Buffers: shared hit=253753 read=37579 written=36 -> HashAggregate (cost=889229.76..904229.76 rows=1500000 width=8) (actual time=68830.960..69712.449 rows=1500000 loops=1) Buffers: shared hit=253753 read=37579 written=36 -> Hash Right Join (cost=69028.00..814312.57 rows=14983439 width=8) (actual time=2420.862..50742.759 rows=15352708 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=253753 read=37579 written=36 -> Seq Scan on orders (cost=0.00..445615.79 rows=14983439 width=8) (actual time=0.058..13659.263 rows=14852687 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162313 Buffers: shared hit=238087 read=17967 written=5 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2418.970..2418.970 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=15666 read=19612 written=31 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..1685.070 rows=1500000 loops=1) Buffers: shared hit=15666 read=19612 written=31 Total runtime: 70100.053 ms (21 rows) COMMIT; COMMIT