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=928692.36..928692.86 rows=200 width=8) (actual time=66785.291..66785.295 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=291299 read=289 written=11 -> HashAggregate (cost=928682.71..928684.71 rows=200 width=8) (actual time=66785.157..66785.171 rows=45 loops=1) Buffers: shared hit=291296 read=289 written=11 -> HashAggregate (cost=891182.71..906182.71 rows=1500000 width=8) (actual time=65629.059..66452.309 rows=1500000 loops=1) Buffers: shared hit=291296 read=289 written=11 -> Hash Right Join (cost=69028.00..815962.99 rows=15043945 width=8) (actual time=1517.637..47315.912 rows=15354056 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=291296 read=289 written=11 -> Seq Scan on orders (cost=0.00..446056.09 rows=15043945 width=8) (actual time=0.052..11527.935 rows=14854040 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 160960 Buffers: shared hit=256109 read=198 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1515.272..1515.272 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35187 read=91 written=10 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.017..848.285 rows=1500000 loops=1) Buffers: shared hit=35187 read=91 written=10 Total runtime: 66826.879 ms (21 rows) COMMIT; COMMIT