Slow MongoDB query: can you explain why?

2020-03-26 04:50发布

问题:

I have a MongoDB query that's taking an unreasonably long time to run, but it:

  • is only scanning 6 objects
  • hits an index
  • consistently takes ~1500ms (wasn't paging or otherwise occupied)
  • index miss% is 0 in mongostat

It showed up in the profiler (without the explain()), and I don't understand why it's so slow. Any ideas?

gimmebar:PRIMARY> db.assets.find({ owner: "123", avatar: false, private: false }).sort({date: -1}).explain()
{
    "cursor" : "BtreeCursor owner_1_avatar_1_date_-1",
    "nscanned" : 6,
    "nscannedObjects" : 6,
    "n" : 6,
    "millis" : 1567,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "owner" : [
            [
                "123",
                "123"
            ]
        ],
        "avatar" : [
            [
                false,
                false
            ]
        ],
        "date" : [
            [
                {
                    "$maxElement" : 1
                },
                {
                    "$minElement" : 1
                }
            ]
        ]
    }
}

回答1:

Missing the index on the private key?

BtreeCursor owner_1_avatar_1_date_-1 vs .find({ owner: "123", avatar: false, private: false }).sort({date: -1})