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 '%special%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=847729.50..847730.00 rows=200 width=8) (actual time=30774.367..30774.370 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=187802 read=101507 written=263 -> HashAggregate (cost=847719.86..847721.86 rows=200 width=8) (actual time=30774.267..30774.276 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=187799 read=101507 written=263 -> HashAggregate (cost=810219.86..825219.86 rows=1500000 width=8) (actual time=29882.176..30451.821 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=187799 read=101507 written=263 -> Hash Right Join (cost=69028.00..735444.37 rows=14955098 width=8) (actual time=1300.246..19711.690 rows=15353275 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=187799 read=101507 written=263 -> Seq Scan on orders (cost=0.00..442089.90 rows=14955098 width=8) (actual time=0.029..9067.229 rows=14853258 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161742 Buffers: shared hit=187796 read=66232 written=263 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1288.625..1288.625 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=3 read=35275 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.020..833.496 rows=1500000 loops=1) Buffers: shared hit=3 read=35275 Planning time: 3.376 ms Execution time: 30793.307 ms (24 rows) COMMIT; COMMIT