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 '%unusual%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=929552.58..929553.08 rows=200 width=8) (actual time=94167.135..94167.138 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=266777 read=25317 written=157 -> HashAggregate (cost=929542.94..929544.94 rows=200 width=8) (actual time=94167.027..94167.033 rows=46 loops=1) Buffers: shared hit=266774 read=25317 written=157 -> HashAggregate (cost=892042.94..907042.94 rows=1500000 width=8) (actual time=92986.196..93827.802 rows=1500000 loops=1) Buffers: shared hit=266774 read=25317 written=157 -> Hash Right Join (cost=69028.00..816827.29 rows=15043130 width=8) (actual time=3361.451..68183.220 rows=15353656 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=266774 read=25317 written=157 -> Seq Scan on orders (cost=0.00..446936.69 rows=15043130 width=8) (actual time=0.036..18735.828 rows=14853638 loops=1) Filter: ((o_comment)::text !~~ '%unusual%requests%'::text) Rows Removed by Filter: 161362 Buffers: shared hit=249445 read=7368 written=157 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3359.534..3359.534 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=17329 read=17949 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..2459.074 rows=1500000 loops=1) Buffers: shared hit=17329 read=17949 Total runtime: 94219.599 ms (21 rows) COMMIT; COMMIT