Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB:为什么';按多个键排序是否使用索引?_Mongodb_Indexing_Mongodb Indexes - Fatal编程技术网

MongoDB:为什么';按多个键排序是否使用索引?

MongoDB:为什么';按多个键排序是否使用索引?,mongodb,indexing,mongodb-indexes,Mongodb,Indexing,Mongodb Indexes,问题: 我有一个非常大的集合,它由字段ts:(时间戳)索引 我想得到最后5个条目。令我惊讶的是,查询没有使用索引,因此速度非常慢: > db.events.find().sort({'ts': -1, '_id': -1}).limit(5) 但是,仅按ts或其他字段排序时,应使用索引: > db.events.find().sort({'ts': -1}).limit(5) > db.events.find().sort({'_id': -1}).limit(5) 这是M

问题: 我有一个非常大的集合,它由字段
ts
:(时间戳)索引

我想得到最后5个条目。令我惊讶的是,查询没有使用索引,因此速度非常慢:

> db.events.find().sort({'ts': -1, '_id': -1}).limit(5)
但是,仅按
ts
或其他字段排序时,应使用索引:

> db.events.find().sort({'ts': -1}).limit(5)
> db.events.find().sort({'_id': -1}).limit(5)
这是MongoDB中的一个bug,这确实是一个文档化的特性,还是我做错了什么

其他信息:

> db.events.find().sort({'ts': -1, '_id': -1}).limit(5).explain()
{
    "cursor" : "BasicCursor",
    "nscanned" : 795609,
    "nscannedObjects" : 795609,
    "n" : 5,
    "scanAndOrder" : true,
    "millis" : 22866,
    "nYields" : 73,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {

    }
}
> db.events.find().sort({'ts': -1}).limit(5).explain()
{
    "cursor" : "BtreeCursor ts_-1",
    "nscanned" : 5,
    "nscannedObjects" : 5,
    "n" : 5,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
            "ts" : [
                    [
                            {
                                    "$maxElement" : 1
                            },
                            {
                                    "$minElement" : 1
                            }
                    ]
            ]
    }
}

值得一读索引建议和常见问题解答wiki页面的部分

您可能缺少一些注意事项:

  • MongoDB每个查询只使用一个索引

  • 使用的
    sort
    列必须是索引中的最后一列

因此,对于您的示例,您应该在
ts
\u id
上添加一个复合索引:

ensureIndex({'ts':-1','u id':-1})

。。并使用
explain()
确认排序现在正在使用预期索引:

> db.events.find().sort({'ts': -1, '_id':-1}).limit(5).explain()
{
    "cursor" : "BtreeCursor ts_-1__id_-1",
    "nscanned" : 5,
    "nscannedObjects" : 5,
    "n" : 5,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "ts" : [
            [
                {
                    "$maxElement" : 1
                },
                {
                    "$minElement" : 1
                }
            ]
        ],
        "_id" : [
            [
                {
                    "$maxElement" : 1
                },
                {
                    "$minElement" : 1
                }
            ]
        ]
    }
}

这确实是答案——我想关系数据库已经宠坏了我……:)谢谢为什么排序列必须是索引中的最后一列?
> db.events.find().sort({'ts': -1, '_id':-1}).limit(5).explain()
{
    "cursor" : "BtreeCursor ts_-1__id_-1",
    "nscanned" : 5,
    "nscannedObjects" : 5,
    "n" : 5,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "ts" : [
            [
                {
                    "$maxElement" : 1
                },
                {
                    "$minElement" : 1
                }
            ]
        ],
        "_id" : [
            [
                {
                    "$maxElement" : 1
                },
                {
                    "$minElement" : 1
                }
            ]
        ]
    }
}