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=846226.02..846226.52 rows=200 width=8) (actual time=26861.827..26861.829 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=54186 read=235123 -> HashAggregate (cost=846216.38..846218.38 rows=200 width=8) (actual time=26861.730..26861.739 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=54183 read=235123 -> HashAggregate (cost=808716.38..823716.38 rows=1500000 width=8) (actual time=26130.686..26564.297 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=54183 read=235123 -> Hash Right Join (cost=69028.00..734316.76 rows=14879924 width=8) (actual time=975.794..17885.632 rows=15339272 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=54183 read=235123 -> Seq Scan on orders (cost=0.00..442089.90 rows=14879924 width=8) (actual time=0.039..8538.602 rows=14839252 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160748 Buffers: shared hit=54181 read=199847 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=965.528..965.528 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.011..578.789 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Planning time: 5.237 ms Execution time: 26888.150 ms (24 rows) COMMIT; COMMIT