1

Consider a collection with an index {x: 1} consisting of the following documents:

  [
    {x : 5},
    {x : 5},
    {x : 5}
  ]

The explain output on the query predicate {x : 5} shows that the total keys examined = total documents examined = documents returned = 3.

This is not what I expected. I expected the total keys examined to equal 1 and total documents examined = total documents returned = 3. The reason for my expectation is because of how I understand how MongoDB treats documents with the same key value in the BTree; if multiple documents have the same key value, the node with that key will have a reference to all documents with the key value. In this case three documents have the same key value x = 5 so MongoDB should scan only this key and return all referenced documents.

Why is the key count = 3 and not 1, is there something am missing?

5
  • 2
    I guess you mistake "total keys examined" with "distinct keys examined" Commented Jan 4 at 11:45
  • @WernfriedDomscheit my understanding is that the BTree implementation doesn't allow duplicate keys instead a node with a given key references all documents with that key value. How many keys does MongoDB have to check?, just 1. Commented Jan 4 at 11:56
  • are you saying MongoDB will create 3 duplicate keys in the BTree each referencing one document? Isn't that less efficient both in memory and time. Imagine keeping 1 million duplicate keys in the Btree? Commented Jan 4 at 11:57
  • 1
    MongoDB will create index entries for all three documents. This is why you get 3 keys examined in your explain plan. The query planner identified an index that supports the query shape. The index identified by the query planner has 3 entries - one for each record, and correctly returned 3 for total keys examined. Since you did not supply the actual query I cannot comment on the effectiveness of a covered query. Clearly the explain plan shows documents were examined, so not a covered query. Commented Jan 6 at 22:31
  • 1
    Regarding the idea that one index key would relate to many documents, this is patently wrong. Perhaps you were confusing the topic of Index Prefix Compression? mongodb.com/docs/manual/reference/glossary/… Commented Jan 6 at 22:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.