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 ('21', '16', '22', '28', '24', '25', '19') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('21', '16', '22', '28', '24', '25', '19') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694725.89..694725.90 rows=3 width=20) (actual time=16233.146..16233.146 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=326349 read=1005 InitPlan 1 (returns $0) -> Aggregate (cost=52903.96..52903.97 rows=1 width=4) (actual time=675.666..675.667 rows=1 loops=1) Buffers: shared hit=35742 read=601 -> Bitmap Heap Scan on customer (cost=6645.79..51949.16 rows=381919 width=4) (actual time=144.220..555.718 rows=381828 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,16,22,28,24,25,19}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35742 read=601 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6550.31 rows=381919 width=0) (actual time=131.267..131.267 rows=381828 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{21,16,22,28,24,25,19}'::text[])) Buffers: shared hit=464 read=601 -> HashAggregate (cost=641821.85..641821.89 rows=3 width=20) (actual time=16233.062..16233.064 rows=7 loops=1) Buffers: shared hit=326347 read=1005 -> Hash Anti Join (cost=598404.66..641376.33 rows=59403 width=20) (actual time=15504.200..16203.183 rows=63253 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=326347 read=1005 -> Bitmap Heap Scan on customer (cost=2909.86..41865.17 rows=140088 width=24) (actual time=774.101..1065.285 rows=190779 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,16,22,28,24,25,19}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=71049 read=1005 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.84 rows=140088 width=0) (actual time=761.591..761.591 rows=190779 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,16,22,28,24,25,19}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35984 read=906 -> Hash (cost=406494.91..406494.91 rows=15119991 width=4) (actual time=14417.962..14417.962 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=255295 -> Seq Scan on orders (cost=0.00..406494.91 rows=15119991 width=4) (actual time=0.041..5433.775 rows=15015000 loops=1) Buffers: shared hit=255295 Total runtime: 16361.281 ms (30 rows) COMMIT; COMMIT