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 ('21', '13', '23', '22', '18', '25', '19') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('21', '13', '23', '22', '18', '25', '19') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694025.98..694025.98 rows=3 width=20) (actual time=11239.111..11239.112 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=113106 read=213748 dirtied=253 written=8930 InitPlan 1 (returns $0) -> Aggregate (cost=52856.60..52856.61 rows=1 width=4) (actual time=533.675..533.675 rows=1 loops=1) Buffers: shared hit=28331 read=8012 written=679 -> Bitmap Heap Scan on customer customer_1 (cost=6636.58..51905.12 rows=380592 width=4) (actual time=80.278..460.574 rows=381981 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,13,23,22,18,25,19}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=28331 read=8012 written=679 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6541.43 rows=380592 width=0) (actual time=72.754..72.754 rows=381981 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{21,13,23,22,18,25,19}'::text[])) Buffers: shared hit=11 read=1054 written=88 -> HashAggregate (cost=641169.30..641169.34 rows=3 width=20) (actual time=11239.046..11239.047 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=113103 read=213748 dirtied=253 written=8930 -> Hash Anti Join (cost=597215.04..640729.98 rows=58576 width=20) (actual time=10563.338..11216.208 rows=63447 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=113103 read=213748 dirtied=253 written=8930 -> Bitmap Heap Scan on customer (cost=2905.20..41845.91 rows=139532 width=24) (actual time=591.234..1066.189 rows=190594 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,13,23,22,18,25,19}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35172 Buffers: shared hit=28343 read=43718 written=866 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2870.31 rows=139532 width=0) (actual time=583.338..583.338 rows=190594 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,13,23,22,18,25,19}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=28341 read=8548 written=744 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=9862.217..9862.217 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=84757 read=170030 dirtied=253 written=8064 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.019..4993.946 rows=15000000 loops=1) Buffers: shared hit=84757 read=170030 dirtied=253 written=8064 Planning time: 2.661 ms Execution time: 11256.257 ms (34 rows) COMMIT; COMMIT