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', '16', '25', '30', '22', '21', '26') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('11', '16', '25', '30', '22', '21', '26') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=691165.14..691165.15 rows=3 width=20) (actual time=8317.187..8317.187 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=323993 read=1596 InitPlan 1 (returns $0) -> Aggregate (cost=52891.53..52891.54 rows=1 width=4) (actual time=416.340..416.341 rows=1 loops=1) Buffers: shared hit=35286 read=1056 -> Bitmap Heap Scan on customer (cost=6643.13..51937.58 rows=381579 width=4) (actual time=89.435..338.508 rows=381423 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{11,16,25,30,22,21,26}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35286 read=1056 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6547.74 rows=381579 width=0) (actual time=81.165..81.165 rows=381423 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{11,16,25,30,22,21,26}'::text[])) Buffers: shared hit=8 read=1056 -> HashAggregate (cost=638273.53..638273.58 rows=3 width=20) (actual time=8317.105..8317.107 rows=7 loops=1) Buffers: shared hit=323991 read=1596 -> Hash Anti Join (cost=594266.92..637827.29 rows=59499 width=20) (actual time=8016.368..8297.690 rows=63505 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=323991 read=1596 -> Bitmap Heap Scan on customer (cost=2907.78..41857.84 rows=139888 width=24) (actual time=469.344..602.258 rows=190877 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{11,16,25,30,22,21,26}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=70466 read=1596 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2872.81 rows=139888 width=0) (actual time=462.114..462.114 rows=190877 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{11,16,25,30,22,21,26}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35295 read=1596 -> Hash (cost=403671.84..403671.84 rows=15014984 width=4) (actual time=7540.743..7540.743 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=253522 -> Seq Scan on orders (cost=0.00..403671.84 rows=15014984 width=4) (actual time=0.055..3991.710 rows=15015000 loops=1) Buffers: shared hit=253522 Total runtime: 8344.249 ms (30 rows) COMMIT; COMMIT