Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explain() in Mongodb: differences between "nscanned" and "nscannedObjects"

Tags:

I cannot get the exact difference between "nscanned" and "nscannedObjects" in the Mongodb's explain query output.

On MongoDB Explain documentation I can read:

nscanned Number of items (documents or index entries) examined. Items might be objects or index keys. If a "covered index" is involved, nscanned may be higher than nscannedObjects.

nscannedObjects Number of documents scanned.

What's the different between these two fields? And more specific what does exactly mean when I have a query, which uses a BtreeCursor (an index), and these two fields have two different values, for example:

{     "cursor" : "BtreeCursor a_1_b_1",     "isMultiKey" : false,     "n" : 5,     "nscannedObjects" : 5,     "nscanned" : 9,      (...) } 

I know what a "covered index" is. I would like to understand exactly what the query did in the example above. Did it pass through ("scanned") 9 elements (nscanned = 9), where all of them are index entries and read ("examined") the value of only 5 of them (nscannedObjects = 5) to produce the result set?

like image 479
Luigi Massa Gallerano Avatar asked Dec 17 '12 07:12

Luigi Massa Gallerano


2 Answers

This means that :
The query returned 5 documents - n
scanned 9 documents from the index - nscanned
and then read 5 full documents from the collection - nscannedObjects

Similar example is given at :
http://docs.mongodb.org/manual/reference/method/cursor.explain/#cursor.explain

like image 106
Ajay George Avatar answered Oct 21 '22 16:10

Ajay George


if "cursor" is Index ==> nscanned = no. of index scanned else if "cursor" is FullTableScan ==> nscanned = no. of document scanned

nscannedObjects ==> No. of document scanned

when querying, try to minimise all count, i.e. nscanned and nscannedObjects both are minimum means your query should run faster!

like image 37
Raxit Sheth Avatar answered Oct 21 '22 15:10

Raxit Sheth