4

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 ?

Baruch Oxman
  • 1,616
  • 14
  • 24
  • 1
    When it is not a duplicate, it would be helpful when you would provide an example of your data, your query and your indexes. – Philipp Sep 22 '14 at 13:42
  • I am not sure this is directly related to http://stackoverflow.com/questions/13910097/explain-in-mongodb-differences-between-nscanned-and-nscannedobjects, as in my case it seems that nscanned has a huge number - that might only be explained as the sum of all entries in all indices together. Why would an update query cause such a large number of indices scanned ? – Baruch Oxman Sep 22 '14 at 13:53
  • Nope. Closing it until the OP can provide a reasonable explanation otherwise. And that is how it should be. There are many reasons as explained before. Where is you index? What are you intending to update? Paragraphs don't cut it. Code does. This is what this site is for. – Neil Lunn Sep 22 '14 at 13:53
  • I have updated the question. It is definitely not a duplicate, since the other question is a general question on what 'nscanned' means, and here I'm asking how can I get such a high value for 'nscanned'. – Baruch Oxman Sep 22 '14 at 14:08
  • We can't help you unless you provide the three things that Philipp asked for above: 1. Sample document, 2. your `update` code, 3. your existing indexes. – JohnnyHK Sep 22 '14 at 16:29
  • JohnnyHK, I cannot provide the exact details as they relate to customer data and internal business logic, but I have provided a similar sample document, update code and the info on the indices. In any case, if you reread my question after the update you will see it's not a duplicate of the other question, so please unmark it as duplicate. – Baruch Oxman Sep 22 '14 at 19:40
  • Perfect, thanks! Your question is already no longer marked as a duplicate. That's a _lot_ of indexes to update. – JohnnyHK Sep 22 '14 at 19:52
  • I agree, that's a lot of indices to update, but this is the main "work" collection, so we're making a lot of different queries on it. Is this the reason for the high nscanned count ? – Baruch Oxman Sep 22 '14 at 20:01
  • I would assume so, yes. – JohnnyHK Sep 22 '14 at 20:05
  • Can you provide a .explain() on these queries? I know they are intermittent but if you can get one it would be helpful. Definitely something funny going on as it should be nscanned = 1 for a match on _id. – wdberkeley Sep 23 '14 at 16:54

1 Answers1

2

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.

jamestyj
  • 76
  • 3