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=929867.46..929867.96 rows=200 width=8) (actual time=87167.000..87167.003 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=285602 read=6239 written=1 -> HashAggregate (cost=929857.81..929859.81 rows=200 width=8) (actual time=87166.886..87166.897 rows=46 loops=1) Buffers: shared hit=285599 read=6239 written=1 -> HashAggregate (cost=892357.81..907357.81 rows=1500000 width=8) (actual time=85757.748..86787.489 rows=1500000 loops=1) Buffers: shared hit=285599 read=6239 written=1 -> Hash Right Join (cost=69028.00..816991.13 rows=15073337 width=8) (actual time=2528.813..62265.984 rows=15352727 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=285599 read=6239 written=1 -> Seq Scan on orders (cost=0.00..446496.39 rows=15073337 width=8) (actual time=0.040..20718.803 rows=14852707 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162293 Buffers: shared hit=250321 read=6239 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2526.504..2526.504 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.017..1810.197 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 87201.018 ms (21 rows) COMMIT; COMMIT