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=852112.55..852113.05 rows=200 width=8) (actual time=28772.229..28772.234 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=201260 read=90075 dirtied=213 written=464 -> HashAggregate (cost=852102.91..852104.91 rows=200 width=8) (actual time=28772.139..28772.149 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=201257 read=90075 dirtied=213 written=464 -> HashAggregate (cost=814602.91..829602.91 rows=1500000 width=8) (actual time=27992.272..28464.810 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=201257 read=90075 dirtied=213 written=464 -> Hash Right Join (cost=69028.00..739613.13 rows=14997956 width=8) (actual time=1184.053..18737.447 rows=15354058 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=201257 read=90075 dirtied=213 written=464 -> Seq Scan on orders (cost=0.00..445615.79 rows=14997956 width=8) (actual time=0.026..8409.148 rows=14854042 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 160958 Buffers: shared hit=201255 read=54799 dirtied=213 written=120 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1173.260..1173.260 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=344 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.009..736.122 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=344 Planning time: 3.189 ms Execution time: 28798.743 ms (24 rows) COMMIT; COMMIT