【问题标题】:mongodb multikey index seems like not being used when sort排序时似乎没有使用mongodb多键索引
【发布时间】:2020-02-07 09:12:06
【问题描述】:

假设我有 tx_collection,其中有 3 个文档,如下所示

{
    "block_number": 1,
    "value": 122
    "transfers": [
        {
            "from": "foo1", 
            "to": "bar1", 
            "amount": 111
        },
        {
            "from": "foo3", 
            "to": "bar3", 
            "amount": 11
        },
    ]
},
{
    "block_number": 2,
    "value": 88
    "transfers": [
        {
            "from": "foo11", 
            "to": "bar11", 
            "amount": 33
        },
        {
            "from": "foo22", 
            "to": "bar22", 
            "amount": 55
        },
    ]
},
{
    "block_number": 3,
    "value": 233
    "transfers": [
        {
            "from": "foo1", 
            "to": "bar1", 
            "amount": 33
        },
        {
            "from": "foo3", 
            "to": "bar3", 
            "amount": 200
        },
    ]
}

针对性能问题,我在transfers.amount上创建了多键索引

当我按transfers.amount排序时,

db.getCollection('tx_transaction').find({}).sort({"transfers.amount":-1})

我期望的文档顺序按子字段transfers.amount 的最大值排序

{
    "block_number": 3,
    "value": 233
    "transfers": [
        {
            "from": "foo1", 
            "to": "bar1", 
            "amount": 33
        },
        {
            "from": "foo3", 
            "to": "bar3", 
            "amount": 200
        },
    ]
},
{
    "block_number": 1,
    "value": 122
    "transfers": [
        {
            "from": "foo1", 
            "to": "bar1", 
            "amount": 111
        },
        {
            "from": "foo3", 
            "to": "bar3", 
            "amount": 11
        },
    ]
},
{
    "block_number": 2,
    "value": 88
    "transfers": [
        {
            "from": "foo11", 
            "to": "bar11", 
            "amount": 33
        },
        {
            "from": "foo22", 
            "to": "bar22", 
            "amount": 55
        },
    ]
}

排序效果很好,因为只有 3 个文档。排序顺序是我期望的块号 3 -> 块号 1 -> block_number 2

我的问题是当有 1900 万个文档时,它会抛出错误消息

按摩是这样的

"errmsg" : "Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",

排序的时候好像没有使用多键索引。

您知道为什么会抛出此错误消息吗?

仅供参考。

  • 我的mongodb版本是3.6.3
  • tx_collection 已分片

【问题讨论】:

    标签: mongodb sorting indexing multikey


    【解决方案1】:

    从 MongoDB 3.6 及更高版本开始,我认为这是可以预料的,正如 Use Indexes to Sort Query Results 中所述:

    由于在 MongoDB 3.6 中对数组字段的排序行为进行了更改,当对使用多键索引索引的数组进行排序时,查询计划包括一个阻塞排序阶段。新的排序行为可能会对性能产生负面影响。

    在阻塞排序中,排序步骤必须先消耗所有输入,然后才能产生输出。在非阻塞或索引排序中,排序步骤扫描索引以按请求的顺序生成结果。

    换句话说,“阻塞排序”意味着存在SORT_KEY_GENERATOR 阶段,该阶段表示内存中的排序。由于SERVER-19402,这已从 MongoDB 3.6 之前的版本更改,以解决围绕数组字段排序的不一致问题。

    有一张票可以改善这种情况:SERVER-31898。不幸的是,目前还没有针对这种行为的解决方法。

    【讨论】:

    • 感谢您的回复。 :) 这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2013-07-14
    • 2014-03-26
    相关资源
    最近更新 更多