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=851523.05..851523.55 rows=200 width=8) (actual time=45328.174..45328.178 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=209928 read=81407 written=355 -> HashAggregate (cost=851513.41..851515.41 rows=200 width=8) (actual time=45328.039..45328.046 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=209925 read=81407 written=355 -> HashAggregate (cost=814013.41..829013.41 rows=1500000 width=8) (actual time=44365.251..44988.423 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=209925 read=81407 written=355 -> Hash Right Join (cost=69028.00..739171.00 rows=14968481 width=8) (actual time=1676.012..27920.131 rows=15338409 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=209925 read=81407 written=355 -> Seq Scan on orders (cost=0.00..445615.79 rows=14968481 width=8) (actual time=0.045..10115.806 rows=14838392 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161608 Buffers: shared hit=174647 read=81407 written=355 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1659.953..1659.953 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.024..923.493 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 4.506 ms Execution time: 45357.385 ms (24 rows) COMMIT; COMMIT