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=927553.56..927554.06 rows=200 width=8) (actual time=63365.771..63365.774 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=254390 read=37198 written=9 -> HashAggregate (cost=927543.91..927545.91 rows=200 width=8) (actual time=63365.645..63365.656 rows=45 loops=1) Buffers: shared hit=254387 read=37198 written=9 -> HashAggregate (cost=890043.91..905043.91 rows=1500000 width=8) (actual time=62092.930..63025.249 rows=1500000 loops=1) Buffers: shared hit=254387 read=37198 written=9 -> Hash Right Join (cost=69028.00..815051.95 rows=14998393 width=8) (actual time=1682.445..44823.065 rows=15353563 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=254387 read=37198 written=9 -> Seq Scan on orders (cost=0.00..446056.09 rows=14998393 width=8) (actual time=0.042..10205.246 rows=14853545 loops=1) Filter: ((o_comment)::text !~~ '%pending%packages%'::text) Rows Removed by Filter: 161455 Buffers: shared hit=254331 read=1976 written=9 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1680.445..1680.445 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=56 read=35222 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..999.996 rows=1500000 loops=1) Buffers: shared hit=56 read=35222 Total runtime: 63400.833 ms (21 rows) COMMIT; COMMIT