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 '%express%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=918253.44..918253.94 rows=200 width=8) (actual time=32562.357..32562.359 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=280266 read=8537 -> HashAggregate (cost=918243.80..918245.80 rows=200 width=8) (actual time=32562.224..32562.231 rows=46 loops=1) Buffers: shared hit=280263 read=8537 -> HashAggregate (cost=880743.80..895743.80 rows=1500000 width=8) (actual time=31818.778..32256.949 rows=1500000 loops=1) Buffers: shared hit=280263 read=8537 -> Hash Right Join (cost=69028.00..806642.50 rows=14820260 width=8) (actual time=750.400..23996.597 rows=15354119 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=280263 read=8537 -> Seq Scan on orders (cost=0.00..441209.30 rows=14820260 width=8) (actual time=0.036..8909.972 rows=14854097 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160903 Buffers: shared hit=252467 read=1055 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=748.957..748.957 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=27796 read=7482 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..439.680 rows=1500000 loops=1) Buffers: shared hit=27796 read=7482 Total runtime: 32580.886 ms (21 rows) COMMIT; COMMIT