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 '%pending%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=847833.14..847833.64 rows=200 width=8) (actual time=29618.905..29618.909 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=157923 read=132145 dirtied=234 written=1518 -> HashAggregate (cost=847823.50..847825.50 rows=200 width=8) (actual time=29618.813..29618.820 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=157920 read=132145 dirtied=234 written=1518 -> HashAggregate (cost=810323.50..825323.50 rows=1500000 width=8) (actual time=28790.056..29299.093 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=157920 read=132145 dirtied=234 written=1518 -> Hash Right Join (cost=69028.00..735852.33 rows=14894235 width=8) (actual time=1257.619..18874.320 rows=15353608 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=157920 read=132145 dirtied=234 written=1518 -> Seq Scan on orders (cost=0.00..443410.80 rows=14894235 width=8) (actual time=0.031..8300.748 rows=14853590 loops=1) Filter: ((o_comment)::text !~~ '%pending%packages%'::text) Rows Removed by Filter: 161410 Buffers: shared hit=157918 read=96869 dirtied=234 written=1518 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1248.882..1248.882 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.018..822.534 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Planning time: 3.645 ms Execution time: 29640.533 ms (24 rows) COMMIT; COMMIT