سؤال

I created a multi-key compound index via Casbah (Scala library for Mongo):

db.collection.ensureIndex(MongoDBObject("Header.records.n" -> 1) ++ MongoDBObject("Header.records.v" -> 1) ++ MongoDBObject("Header.records.l" -> 1))

Then, via the Mongo Shell I had performed a db.collection.find(...).explain where the nScannedObjects exceeded the db.collection.count(). Looking at the Mongo docs, it appears that ensureIndex needs to be called once, and then any writes will force an update of the index.

However, I saw a post and this one that it's only required to call db.collection.ensureIndex(...) once.

EDIT

>db.collection.find( {"Header.records" : {$all : [ 
{$elemMatch: {n: "Name", v: "Kevin", 
                         "l" : { "$gt" : 0 , "$lt" : 15}} }]}}, 
             {_id : 1}).explain()
    {
            "cursor" : "BtreeCursor         
     Header.records.n_1_Header.records.v_1_Header.records.l_1",
            "isMultiKey" : true,
            "n" : 4098,
            "nscannedObjects" : 9412,
            "nscanned" : 9412,
            "nscannedObjectsAllPlans" : 9412,
            "nscannedAllPlans" : 9412,
            "scanAndOrder" : false,
            "indexOnly" : false,
            "nYields" : 0,
            "nChunkSkips" : 0,
            "millis" : 152,
            "indexBounds" : {
                    "Header.records.n" : [
                            [
                                    "Name",
                                    "Name"
                            ]
                    ],
                    "Header.records.v" : [
                            [
                                    "Kevin",
                                    "Kevin"
                            ]
                    ],
                    "Header.records.l" : [
                            [
                                    0,
                                    1.7976931348623157e+308
                            ]
                    ]
            },
            "server" : "ABCD:27017"

Note that nScanned (9412) > count(4248).

> db.collection.count()
4248

Why?

هل كانت مفيدة؟

المحلول

About "nscanned" exceeding the count, that is probable since you actually have way more index entries than you have documents: each item in your list is an index entry. It seems like here you have on average 2 items in list per document. "nscannedObjects" follows the same principle since that counter is incremented whenever a document is looked at, even if the same document was already looked at earlier as part of the same query.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top