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=923939.98..923940.48 rows=200 width=8) (actual time=62327.721..62327.725 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=212666 read=77910 dirtied=1 written=12 -> HashAggregate (cost=923930.34..923932.34 rows=200 width=8) (actual time=62327.590..62327.599 rows=44 loops=1) Buffers: shared hit=212663 read=77910 dirtied=1 written=12 -> HashAggregate (cost=886430.34..901430.34 rows=1500000 width=8) (actual time=61126.029..61985.194 rows=1500000 loops=1) Buffers: shared hit=212663 read=77910 dirtied=1 written=12 -> Hash Right Join (cost=69028.00..811808.85 rows=14924298 width=8) (actual time=1847.094..44435.671 rows=15352322 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=212663 read=77910 dirtied=1 written=12 -> Seq Scan on orders (cost=0.00..444294.89 rows=14924298 width=8) (actual time=0.046..10131.204 rows=14852304 loops=1) Filter: ((o_comment)::text !~~ '%pending%requests%'::text) Rows Removed by Filter: 162696 Buffers: shared hit=212661 read=42634 dirtied=1 written=10 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1845.116..1845.116 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=2 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..1118.681 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=2 Total runtime: 62358.627 ms (21 rows) COMMIT; COMMIT