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 ('19', '34', '31', '25', '28', '14', '29') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('19', '34', '31', '25', '28', '14', '29') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=696329.75..696329.76 rows=3 width=20) (actual time=20124.822..20124.823 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=286066 read=41556 InitPlan 1 (returns $0) -> Aggregate (cost=52851.59..52851.60 rows=1 width=4) (actual time=2213.766..2213.766 rows=1 loops=1) Buffers: shared hit=11 read=36332 -> Bitmap Heap Scan on customer (cost=6634.67..51900.37 rows=380484 width=4) (actual time=261.246..2087.998 rows=381826 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{19,34,31,25,28,14,29}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=11 read=36332 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6539.55 rows=380484 width=0) (actual time=245.005..245.005 rows=381826 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{19,34,31,25,28,14,29}'::text[])) Buffers: shared hit=8 read=1057 -> HashAggregate (cost=643478.09..643478.13 rows=3 width=20) (actual time=20124.756..20124.758 rows=7 loops=1) Buffers: shared hit=286064 read=41556 -> Hash Anti Join (cost=598988.48..643035.52 rows=59009 width=20) (actual time=19377.725..20089.455 rows=63648 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=286064 read=41556 -> Bitmap Heap Scan on customer (cost=2903.55..41842.84 rows=139478 width=24) (actual time=2331.113..2672.831 rows=190785 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{19,34,31,25,28,14,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35196 read=36873 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.68 rows=139478 width=0) (actual time=2313.988..2313.988 rows=190785 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{19,34,31,25,28,14,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=20 read=36870 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=17036.143..17036.143 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=250865 read=4683 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.038..7208.905 rows=15015000 loops=1) Buffers: shared hit=250865 read=4683 Total runtime: 20240.178 ms (30 rows) COMMIT; COMMIT