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=927173.88..927174.38 rows=200 width=8) (actual time=64145.641..64145.641 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=248240 read=43348 dirtied=253 written=120 -> HashAggregate (cost=927164.24..927166.24 rows=200 width=8) (actual time=64145.507..64145.520 rows=45 loops=1) Buffers: shared hit=248237 read=43348 dirtied=253 written=120 -> HashAggregate (cost=889664.24..904664.24 rows=1500000 width=8) (actual time=62886.735..63791.839 rows=1500000 loops=1) Buffers: shared hit=248237 read=43348 dirtied=253 written=120 -> Hash Right Join (cost=69028.00..814748.21 rows=14983206 width=8) (actual time=1786.428..45128.426 rows=15353563 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=248237 read=43348 dirtied=253 written=120 -> Seq Scan on orders (cost=0.00..446056.09 rows=14983206 width=8) (actual time=0.047..10403.424 rows=14853545 loops=1) Filter: ((o_comment)::text !~~ '%pending%packages%'::text) Rows Removed by Filter: 161455 Buffers: shared hit=248235 read=8072 dirtied=253 written=99 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1783.692..1783.692 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=21 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..1109.748 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=21 Total runtime: 64190.624 ms (21 rows) COMMIT; COMMIT