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%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=927990.23..927990.73 rows=200 width=8) (actual time=62914.040..62914.040 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=251678 read=40163 written=6 -> HashAggregate (cost=927980.59..927982.59 rows=200 width=8) (actual time=62913.919..62913.929 rows=45 loops=1) Buffers: shared hit=251675 read=40163 written=6 -> HashAggregate (cost=890480.59..905480.59 rows=1500000 width=8) (actual time=61601.401..62575.135 rows=1500000 loops=1) Buffers: shared hit=251675 read=40163 written=6 -> Hash Right Join (cost=69028.00..815489.35 rows=14998248 width=8) (actual time=1874.261..44912.329 rows=15352333 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=251675 read=40163 written=6 -> Seq Scan on orders (cost=0.00..446496.39 rows=14998248 width=8) (actual time=0.050..9340.496 rows=14852315 loops=1) Filter: ((o_comment)::text !~~ '%pending%requests%'::text) Rows Removed by Filter: 162685 Buffers: shared hit=251673 read=4887 written=6 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1871.701..1871.701 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.006..1073.567 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 62967.701 ms (21 rows) COMMIT; COMMIT