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 ('16', '22', '31', '30', '18', '10', '25') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('16', '22', '31', '30', '18', '10', '25') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694702.99..694703.00 rows=3 width=20) (actual time=19783.690..19783.690 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=159141 read=167956 written=10 InitPlan 1 (returns $0) -> Aggregate (cost=52880.74..52880.75 rows=1 width=4) (actual time=739.688..739.689 rows=1 loops=1) Buffers: shared hit=35233 read=1110 -> Bitmap Heap Scan on customer (cost=6640.85..51927.53 rows=381283 width=4) (actual time=106.834..596.240 rows=381536 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{16,22,31,30,18,10,25}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35233 read=1110 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6545.53 rows=381283 width=0) (actual time=92.435..92.435 rows=381536 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{16,22,31,30,18,10,25}'::text[])) Buffers: shared hit=7 read=1058 -> HashAggregate (cost=641822.17..641822.21 rows=3 width=20) (actual time=19783.624..19783.626 rows=7 loops=1) Buffers: shared hit=159139 read=167956 written=10 -> Hash Anti Join (cost=597809.39..641380.51 rows=58888 width=20) (actual time=18269.398..19723.522 rows=63749 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=159139 read=167956 written=10 -> Bitmap Heap Scan on customer (cost=2907.06..41855.34 rows=139820 width=24) (actual time=847.527..1874.485 rows=190821 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{16,22,31,30,18,10,25}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35244 read=36807 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2872.11 rows=139820 width=0) (actual time=834.200..834.200 rows=190821 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{16,22,31,30,18,10,25}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35242 read=1650 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=17412.486..17412.486 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=123892 read=131149 written=10 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.039..9380.612 rows=15015000 loops=1) Buffers: shared hit=123892 read=131149 written=10 Total runtime: 19843.015 ms (30 rows) COMMIT; COMMIT