Combining indexes

However, this doesn't mean the v index is useless. If you are running something that is selective based on the k index, the one on v can be combined with that. With the index in place, this earlier example now uses a complicated query plan:

EXPLAIN ANALYZE SELECT count(*) FROM t WHERE k>9000 AND v=5;
QUERY PLAN                                                        
-------------------------
Aggregate  (cost=787.97..787.98 rows=1 width=0) (actual time=40.545..40.546 rows=1 loops=1)
       ->  Bitmap Heap Scan on t  (cost=170.38..765.03 rows=9176 width=0) (actual time=4.107..24.606 rows=9097 loops=1)
             Recheck Cond: (v = 5)
             Filter: (k > 9000)
             ->  Bitmap Index Scan on i  (cost=0.00..168.08 rows=10110 width=0) (actual time=3.255..3.255 rows=9993 loops=1)
                   Index Cond: (v = 5)
     Total runtime: ...

Get PostgreSQL 10 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.