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 ('18', '30', '24', '19', '31', '22', '23') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '30', '24', '19', '31', '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=691148.11..691148.12 rows=3 width=20) (actual time=9124.067..9124.068 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=323261 read=2313 InitPlan 1 (returns $0) -> Aggregate (cost=52900.23..52900.24 rows=1 width=4) (actual time=382.188..382.188 rows=1 loops=1) Buffers: shared hit=35290 read=1053 -> Bitmap Heap Scan on customer customer_1 (cost=6645.85..51945.76 rows=381787 width=4) (actual time=80.533..310.690 rows=381317 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,30,24,19,31,22,23}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35290 read=1053 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6550.41 rows=381787 width=0) (actual time=72.759..72.759 rows=381317 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,30,24,19,31,22,23}'::text[])) Buffers: shared hit=12 read=1053 -> HashAggregate (cost=638247.80..638247.85 rows=3 width=20) (actual time=9123.979..9123.980 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=323258 read=2313 -> Hash Anti Join (cost=594268.93..637805.85 rows=58927 width=20) (actual time=8881.639..9102.433 rows=63589 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=323258 read=2313 -> Bitmap Heap Scan on customer (cost=2909.79..41862.18 rows=139977 width=24) (actual time=441.455..549.076 rows=190506 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,30,24,19,31,22,23}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35158 Buffers: shared hit=70458 read=1588 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.79 rows=139977 width=0) (actual time=433.306..433.306 rows=190506 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,30,24,19,31,22,23}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35300 read=1588 -> Hash (cost=403671.84..403671.84 rows=15014984 width=4) (actual time=8351.678..8351.678 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=252797 read=725 -> Seq Scan on orders (cost=0.00..403671.84 rows=15014984 width=4) (actual time=0.014..3516.668 rows=15015000 loops=1) Buffers: shared hit=252797 read=725 Planning time: 4.443 ms Execution time: 9127.846 ms (34 rows) COMMIT; COMMIT