BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('11', '13', '18', '15', '14', '22', '23') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('11', '13', '18', '15', '14', '22', '23') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695843.18..695843.19 rows=3 width=20) (actual time=11821.662..11821.664 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=199383 read=128233 written=16 InitPlan 1 (returns $0) -> Aggregate (cost=52875.19..52875.20 rows=1 width=4) (actual time=782.211..782.211 rows=1 loops=1) Buffers: shared hit=14 read=36329 -> Bitmap Heap Scan on customer customer_1 (cost=6640.54..51922.44 rows=381101 width=4) (actual time=92.625..712.406 rows=381724 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{11,13,18,15,14,22,23}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=14 read=36329 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6545.26 rows=381101 width=0) (actual time=83.983..83.983 rows=381724 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{11,13,18,15,14,22,23}'::text[])) Buffers: shared hit=11 read=1054 -> HashAggregate (cost=642967.91..642967.95 rows=3 width=20) (actual time=11821.611..11821.612 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=199380 read=128233 written=16 -> Hash Anti Join (cost=598993.43..642527.76 rows=58686 width=20) (actual time=11454.047..11801.565 rows=63238 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=199380 read=128233 written=16 -> Bitmap Heap Scan on customer (cost=2908.50..41857.56 rows=139850 width=24) (actual time=844.787..1067.348 rows=190466 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{11,13,18,15,14,22,23}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35174 Buffers: shared hit=34608 read=37454 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2873.53 rows=139850 width=0) (actual time=836.130..836.130 rows=190466 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{11,13,18,15,14,22,23}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=25 read=36863 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=10465.845..10465.845 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=164769 read=90779 written=16 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.021..5023.908 rows=15015000 loops=1) Buffers: shared hit=164769 read=90779 written=16 Planning time: 2.988 ms Execution time: 11834.746 ms (34 rows) COMMIT; COMMIT