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=924311.86..924312.36 rows=200 width=8) (actual time=64009.650..64009.654 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=281869 read=8707 written=21 -> HashAggregate (cost=924302.21..924304.21 rows=200 width=8) (actual time=64009.517..64009.527 rows=46 loops=1) Buffers: shared hit=281866 read=8707 written=21 -> HashAggregate (cost=886802.21..901802.21 rows=1500000 width=8) (actual time=62819.129..63681.508 rows=1500000 loops=1) Buffers: shared hit=281866 read=8707 written=21 -> Hash Right Join (cost=69028.00..812106.35 rows=14939173 width=8) (actual time=1584.673..45985.659 rows=15354006 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=281866 read=8707 written=21 -> Seq Scan on orders (cost=0.00..444294.89 rows=14939173 width=8) (actual time=0.044..11074.526 rows=14853990 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161010 Buffers: shared hit=246614 read=8681 written=21 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1582.732..1582.732 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35252 read=26 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..854.172 rows=1500000 loops=1) Buffers: shared hit=35252 read=26 Total runtime: 64042.930 ms (21 rows) COMMIT; COMMIT