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 ('14', '29', '21', '16', '34', '28', '33') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('14', '29', '21', '16', '34', '28', '33') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=696410.48..696410.49 rows=3 width=20) (actual time=20118.742..20118.744 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=312485 read=15127 dirtied=427 written=12 InitPlan 1 (returns $0) -> Aggregate (cost=52897.26..52897.27 rows=1 width=4) (actual time=1510.236..1510.236 rows=1 loops=1) Buffers: shared hit=29001 read=7344 written=5 -> Bitmap Heap Scan on customer (cost=6644.38..51942.92 rows=381735 width=4) (actual time=269.945..1381.870 rows=382503 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{14,29,21,16,34,28,33}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=29001 read=7344 written=5 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6548.95 rows=381735 width=0) (actual time=252.587..252.587 rows=382503 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{14,29,21,16,34,28,33}'::text[])) Buffers: shared hit=10 read=1057 written=5 -> HashAggregate (cost=643513.14..643513.19 rows=3 width=20) (actual time=20118.529..20118.533 rows=7 loops=1) Buffers: shared hit=312483 read=15127 dirtied=427 written=12 -> Hash Anti Join (cost=598993.22..643069.12 rows=59203 width=20) (actual time=18837.021..20059.291 rows=63658 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=312483 read=15127 dirtied=427 written=12 -> Bitmap Heap Scan on customer (cost=2908.28..41859.63 rows=139937 width=24) (actual time=1663.883..2443.578 rows=191165 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{14,29,21,16,34,28,33}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=58051 read=14008 written=12 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2873.30 rows=139937 width=0) (actual time=1649.097..1649.097 rows=191165 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{14,29,21,16,34,28,33}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=29010 read=7883 written=5 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=17160.640..17160.640 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=254429 read=1119 dirtied=427 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.027..8196.784 rows=15015000 loops=1) Buffers: shared hit=254429 read=1119 dirtied=427 Total runtime: 20282.949 ms (30 rows) COMMIT; COMMIT