I am getting very high 'nscanned' numbers on occasional update queries, while the 'nscannedObjects' are relatively low. I'm getting these numbers from the mongodb log, as part of the automatic logging of slow queries (these updates take anywhere between 100ms and 500ms). The updated collection has 198K items, and is just over 100MB in size. It has many different fields (over 30), and 31 indexes on these fields and their combinations.
Here's the full entry from the MongoDB.log - including the query and the result:
2014-09-22T11:55:22.507+0000 [conn45755] update mydatabase.mycollection query: { _id: ObjectId('53d1365dad547f12b0f31afe') } update: { ....} nscanned:1702130278 nscannedObjects:121 nMatched:1 nModified:1 keyUpdates:4 numYields:0 locks(micros) w:310293 310ms
As I'm making the entry by using { _id: ObjectId('53d1365dad547f12b0f31afe') }
, I expect MongoDB to have a direct hit on the '_id' index and thus have nscanned = 1
I cannot provide the exact query and collection details but here's an example of what I'm doing:
Example document:
{"_id" : ObjectId("53d1365dad547f12b0f31afe"),
"field1" : "val1",
"field2" : "val2",
"field3" : "val3",
...
"field45" : "val45",
}
Update query:
mycollection.update({"_id" : ObjectId("53d1365dad547f12b0f31afe")},
{"$set" : {"field1" : "new_val1",
"field2" : "new_val2",
"field3" : "new_val3",
...
"field45" : "new_val45"}})
Indices exist on the mycollection, on "field1", "field2"..."field31".
If I make the calculation, then even if this query would scan each entry in each of the 31 indices, I'm expected to get 'nscanned' only around 6,138,000, so the above makes no sense !
I am using MongoDB 2.6.4 - is this any kind of a known bug with updates of indices ?
It's a bug in MongoDB 2.6.4 and is fixed in 2.6.5 (not yet released). The nscanned
and nscannedObjects
variables were not initialised in some cases, resulting in these random values. For details, see SERVER-15106 and the commit to fix it.