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 ('24', '25', '19', '26', '17', '15', '29') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('24', '25', '19', '26', '17', '15', '29') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=691684.71..691684.72 rows=3 width=20) (actual time=13539.346..13539.347 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=233545 read=92292 InitPlan 1 (returns $0) -> Aggregate (cost=52884.16..52884.17 rows=1 width=4) (actual time=755.024..755.024 rows=1 loops=1) Buffers: shared hit=67 read=36275 -> Bitmap Heap Scan on customer customer_1 (cost=6642.44..51930.79 rows=381347 width=4) (actual time=91.295..680.746 rows=382023 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,25,19,26,17,15,29}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=67 read=36275 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6547.10 rows=381347 width=0) (actual time=82.592..82.592 rows=382023 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{24,25,19,26,17,15,29}'::text[])) Buffers: shared hit=9 read=1055 -> HashAggregate (cost=638800.47..638800.52 rows=3 width=20) (actual time=13539.290..13539.291 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=233542 read=92292 -> Hash Anti Join (cost=594855.19..638355.62 rows=59314 width=20) (actual time=13106.476..13518.679 rows=63475 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=233542 read=92292 -> Bitmap Heap Scan on customer (cost=2905.91..41848.41 rows=139600 width=24) (actual time=818.889..1044.075 rows=190812 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,25,19,26,17,15,29}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35168 Buffers: shared hit=35244 read=36812 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2871.01 rows=139600 width=0) (actual time=810.161..810.161 rows=190812 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,25,19,26,17,15,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=77 read=36811 -> Hash (cost=404074.68..404074.68 rows=15029968 width=4) (actual time=11380.991..11380.991 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=198295 read=55480 -> Seq Scan on orders (cost=0.00..404074.68 rows=15029968 width=4) (actual time=0.030..4688.380 rows=15015000 loops=1) Buffers: shared hit=198295 read=55480 Planning time: 2.776 ms Execution time: 13569.780 ms (34 rows) COMMIT; COMMIT