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%packages%' 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=850783.37..850783.87 rows=200 width=8) (actual time=46119.413..46119.416 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=211825 read=79257 written=586 -> HashAggregate (cost=850773.73..850775.73 rows=200 width=8) (actual time=46119.288..46119.300 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=211822 read=79257 written=586 -> HashAggregate (cost=813273.73..828273.73 rows=1500000 width=8) (actual time=44912.066..45794.419 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=211822 read=79257 written=586 -> Hash Right Join (cost=69028.00..738506.17 rows=14953512 width=8) (actual time=1100.029..24925.627 rows=15353622 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=211822 read=79257 written=586 -> Seq Scan on orders (cost=0.00..445175.49 rows=14953512 width=8) (actual time=0.031..8588.146 rows=14853604 loops=1) Filter: ((o_comment)::text !~~ '%pending%packages%'::text) Rows Removed by Filter: 161396 Buffers: shared hit=176544 read=79257 written=586 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1085.471..1085.471 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..569.946 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 3.138 ms Execution time: 46155.538 ms (24 rows) COMMIT; COMMIT