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%deposits%' 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=845346.04..845346.54 rows=200 width=8) (actual time=25024.000..25024.003 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=259714 read=29089 -> HashAggregate (cost=845336.40..845338.40 rows=200 width=8) (actual time=25023.867..25023.876 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=259711 read=29089 -> HashAggregate (cost=807836.40..822836.40 rows=1500000 width=8) (actual time=24319.061..24729.147 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=259711 read=29089 -> Hash Right Join (cost=69028.00..733436.63 rows=14879955 width=8) (actual time=934.332..16860.390 rows=15354693 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=259711 read=29089 -> Seq Scan on orders (cost=0.00..441209.30 rows=14879955 width=8) (actual time=0.031..8008.160 rows=14854672 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160328 Buffers: shared hit=246741 read=6781 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=926.941..926.941 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=12970 read=22308 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..547.777 rows=1500000 loops=1) Buffers: shared hit=12970 read=22308 Planning time: 4.983 ms Execution time: 25038.486 ms (24 rows) COMMIT; COMMIT