[SOLVED] Winning plan and differing index

Hi MongoDB teachers !

I’m kind thoughtful concerning the subject of differing index, which is explained in Chapter 5 “Replica Sets with Differing Indexes Part 3”.

At 3m05 : https://youtu.be/5151d9wyAl0?t=182 after having created the differing index, the teacher runs the same query from the replica set. However, the query optimizer doesn’t select the secondary “passive” node to do an IXSCAN as the winning plan, but prefer a COLLSCAN on the primary.

When I read the documentation : “The MongoDB query optimizer processes queries and chooses the most efficient query plan for a query given the available indexes… This mechanism makes a pass/fail decision based on the plan performance and either keeps or evicts the cache entry”.

The teacher next explains how the IXSCAN can be performed (by reading from secondary), but however I’m wondering why the query optimizer, from replica set, doesn’t prefer an IXSCAN from secondary over a COLLSCAN from the primary ? Would that means that query optimizer always prefer reading from primary to avoid potential outdated results ?

Thanks a lot !


Sorry, I got in a complete muddle… I’m gonna answer by myself !

Obviously, QUERY planner is choosing a QUERY winning plan (IXSCAN, COLLSCAN) from what is available : indexes, shard keys, … Query optimizer/planner is never looking for which node performs better !

Only the client choose from which node he prefers or wants to read/write (with read/write concerns or direct connections). Also, we can’t query from the replica set (a supposed entity on top of nodes), we query from a node ! When we connect to the replica set, we are redirected to primary (or secondary when primary isn’t available) and then we can query.

I must go to bed…