Trying it all out

Now that all those optimizations have been discussed, it is time to see which plan PostgreSQL might create for us:

test=# explain SELECT * FROM v, c WHERE v.aid = c.cid AND cid = 4; 
                          QUERY PLAN ----------------------------------------------------------------  Nested Loop (cost=1.71..17.78 rows=1 width=12)    -> Nested Loop (cost=1.14..9.18 rows=1 width=8)          -> Index Only Scan using idx_a on a                (cost=0.57..4.58 rows=1 width=4)                Index Cond: (aid = 4)          -> Index Only Scan using idx_b on b                (cost=0.57..4.59 rows=1 width=4)                Index Cond: (bid = 4)    -> Index Only Scan using idx_c on c                (cost=0.57..8.59 rows=1 width=4)          Index Cond: (cid = 4) (8 rows) 

As you can see, PostgreSQL will use three indexes. It is also interesting to see that ...

Get Mastering PostgreSQL 10 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.