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%requests%' 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=846227.14..846227.64 rows=200 width=8) (actual time=38725.551..38725.551 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=182997 read=106312 dirtied=1 written=3137 -> HashAggregate (cost=846217.50..846219.50 rows=200 width=8) (actual time=38725.417..38725.424 rows=44 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=182994 read=106312 dirtied=1 written=3137 -> HashAggregate (cost=808717.50..823717.50 rows=1500000 width=8) (actual time=37826.123..38416.616 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=182994 read=106312 dirtied=1 written=3137 -> Hash Right Join (cost=69028.00..734317.60 rows=14879980 width=8) (actual time=903.774..23475.889 rows=15353362 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=182994 read=106312 dirtied=1 written=3137 -> Seq Scan on orders (cost=0.00..442089.90 rows=14879980 width=8) (actual time=0.039..8628.190 rows=14853341 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161659 Buffers: shared hit=147716 read=106312 dirtied=1 written=3137 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=880.500..880.500 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.021..412.276 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.366 ms Execution time: 38766.632 ms (24 rows) COMMIT; COMMIT