Chris Muench
Chris Muench

Reputation: 18328

Mongo index not being used (simple one column query)

Explain of find query:

> db.datasources.find({nid: 19882}).explain();         
{
    "cursor" : "BtreeCursor nid_1",
    "nscanned" : 10161684,
    "nscannedObjects" : 10161684,
    "n" : 10161684,
    "millis" : 8988,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "nid" : [
            [
                19882,
                19882
            ]
        ]
    }
}

Here are the indexes for the collection:

> db.datasources.getIndexes()  
[
    {
        "name" : "_id_",
        "ns" : "rocdocs_dev.datasources",
        "key" : {
            "_id" : 1
        }
    },
    {
        "_id" : ObjectId("4edcd725c605da5f200000a2"),
        "ns" : "rocdocs_dev.datasources",
        "key" : {
            "nid" : 1
        },
        "name" : "nid_1"
    },
    {
        "v" : 1,
        "key" : {
            "is_indexed" : 1
        },
        "ns" : "rocdocs_dev.datasources",
        "name" : "is_indexed_1"
    }
]

Upvotes: 0

Views: 212

Answers (1)

Bryan Migliorisi
Bryan Migliorisi

Reputation: 9210

This is using an index as noted by BtreeCursor If it werent, it would say BasicCursor

Though I do see that the query takes 9 seconds and scans what appears to be the entire collection.

Did you add this index after inserting those documents? Perhaps its not done building yet?

I would consider rebuilding the index

db.datasources.reIndex()

Upvotes: 2

Related Questions