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 '%pending%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=924738.26..924738.76 rows=200 width=8) (actual time=65919.135..65919.138 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=288943 read=1886 -> HashAggregate (cost=924728.61..924730.61 rows=200 width=8) (actual time=65919.009..65919.020 rows=46 loops=1) Buffers: shared hit=288940 read=1886 -> HashAggregate (cost=887228.61..902228.61 rows=1500000 width=8) (actual time=64770.313..65581.263 rows=1500000 loops=1) Buffers: shared hit=288940 read=1886 -> Hash Right Join (cost=69028.00..812535.53 rows=14938617 width=8) (actual time=1919.185..46807.848 rows=15353086 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=288940 read=1886 -> Seq Scan on orders (cost=0.00..444735.19 rows=14938617 width=8) (actual time=0.062..11009.500 rows=14853070 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161930 Buffers: shared hit=253662 read=1886 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1917.059..1917.059 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..1114.667 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 65950.622 ms (21 rows) COMMIT; COMMIT