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=920930.09..920930.59 rows=200 width=8) (actual time=88780.110..88780.114 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=225916 read=63140 -> HashAggregate (cost=920920.45..920922.45 rows=200 width=8) (actual time=88779.993..88780.004 rows=46 loops=1) Buffers: shared hit=225913 read=63140 -> HashAggregate (cost=883420.45..898420.45 rows=1500000 width=8) (actual time=87549.686..88422.571 rows=1500000 loops=1) Buffers: shared hit=225913 read=63140 -> Hash Right Join (cost=69028.00..808871.88 rows=14909714 width=8) (actual time=3815.912..65134.419 rows=15352734 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=225913 read=63140 -> Seq Scan on orders (cost=0.00..441649.60 rows=14909714 width=8) (actual time=0.052..19023.277 rows=14852713 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162287 Buffers: shared hit=223392 read=30383 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3813.499..3813.499 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2521 read=32757 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..3052.755 rows=1500000 loops=1) Buffers: shared hit=2521 read=32757 Total runtime: 88826.640 ms (21 rows) COMMIT; COMMIT