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 '%special%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=927231.68..927232.18 rows=200 width=8) (actual time=61801.792..61801.794 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=251891 read=39950 written=5 -> HashAggregate (cost=927222.04..927224.04 rows=200 width=8) (actual time=61801.690..61801.702 rows=45 loops=1) Buffers: shared hit=251888 read=39950 written=5 -> HashAggregate (cost=889722.04..904722.04 rows=1500000 width=8) (actual time=60794.357..61477.887 rows=1500000 loops=1) Buffers: shared hit=251888 read=39950 written=5 -> Hash Right Join (cost=69028.00..814882.51 rows=14967906 width=8) (actual time=1529.404..43267.394 rows=15337702 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=251888 read=39950 written=5 -> Seq Scan on orders (cost=0.00..446496.39 rows=14967906 width=8) (actual time=0.043..10261.242 rows=14837683 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162317 Buffers: shared hit=251886 read=4674 written=5 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1527.241..1527.241 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..879.951 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 61842.881 ms (21 rows) COMMIT; COMMIT