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%accounts%' 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=927880.83..927881.33 rows=200 width=8) (actual time=69579.397..69579.399 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=263143 read=28192 written=11 -> HashAggregate (cost=927871.19..927873.19 rows=200 width=8) (actual time=69579.271..69579.283 rows=45 loops=1) Buffers: shared hit=263140 read=28192 written=11 -> HashAggregate (cost=890371.19..905371.19 rows=1500000 width=8) (actual time=68330.958..69256.785 rows=1500000 loops=1) Buffers: shared hit=263140 read=28192 written=11 -> Hash Right Join (cost=69028.00..815225.71 rows=15029096 width=8) (actual time=1477.610..50147.106 rows=15354058 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=263140 read=28192 written=11 -> Seq Scan on orders (cost=0.00..445615.79 rows=15029096 width=8) (actual time=0.041..12695.013 rows=14854042 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 160958 Buffers: shared hit=227863 read=28191 written=11 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1474.951..1474.951 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35277 read=1 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..803.256 rows=1500000 loops=1) Buffers: shared hit=35277 read=1 Total runtime: 69611.804 ms (21 rows) COMMIT; COMMIT