MongoDB:索引中的查询非常慢

时间:2013-11-01 12:20:06

标签: mongodb indexing

我见过很多类似的问题,但他们没有解决我的问题(123)。

我收集了近2M文件。我的问题:非常简单的查询(find({id:53}))运行差不多5分钟(请参阅下面的所有相关信息)。

我尝试通过“id”字段添加索引,尝试提示, scanAndOrder 为false。我有足够的内存(现在没有交换,服务器上只使用600MB(仍然大约1GB是免费的))。它是debian 7和mongodb 2.4.7。

也许这很重要:昨天我运行了repairDatabase,之后注意到了这个问题(但我不能确定它是否曾经存在过)。但无论如何,我对该集合进行了全面验证,一切似乎都没问题。昨天我又添加了一个索引({id:1})。今天我还重新编制了该系列。

在所有这些长时间查询期间,我的磁盘被广泛使用。

> db.results.count()
1819411

> db.results.stats()
{
    "ns" : "spider.results",
    "count" : 1819411,
    "size" : 26662218768,
    "avgObjSize" : 14654.313273911172,
    "storageSize" : 28309077856,
    "numExtents" : 34,
    "nindexes" : 4,
    "lastExtentSize" : 2146426864,
    "paddingFactor" : 1,
    "systemFlags" : 0,
    "userFlags" : 0,
    "totalIndexSize" : 207245248,
    "indexSizes" : {
        "_id_" : 53144000,
        "datetime_-1" : 45793776,
        "id_1_datetime_1" : 62513696,
        "id_1" : 45793776
    },
    "ok" : 1
}

> db.results.getIndices()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "spider.results",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "datetime" : -1
        },
        "ns" : "spider.results",
        "name" : "datetime_-1"
    },
    {
        "v" : 1,
        "key" : {
            "id" : 1,
            "datetime" : 1
        },
        "unique" : true,
        "ns" : "spider.results",
        "name" : "id_1_datetime_1"
    },
    {
        "v" : 1,
        "key" : {
            "id" : 1
        },
        "ns" : "spider.results",
        "name" : "id_1"
    }
]

> db.results.validate(true)
{
    "ns" : "spider.results",
    "firstExtent" : "0:258000 ns:spider.results",
    "lastExtent" : "18:2000 ns:spider.results",
    "extentCount" : 34,
    "extents" : [
        {
            "loc" : "0:258000",
            "xnext" : "0:25a000",
            "xprev" : "null",
            "nsdiag" : "spider.results",
            "size" : 8192,
            "firstRecord" : "0:2580b0",
            "lastRecord" : "0:2598b0"
        },
        {
            "loc" : "0:25a000",
            "xnext" : "0:29d000",
            "xprev" : "0:258000",
            "nsdiag" : "spider.results",
            "size" : 274432,
            "firstRecord" : "0:25a0b0",
            "lastRecord" : "0:29c6b0"
        },
        {
            "loc" : "0:29d000",
            "xnext" : "0:3a9000",
            "xprev" : "0:25a000",
            "nsdiag" : "spider.results",
            "size" : 1097728,
            "firstRecord" : "0:29d0b0",
            "lastRecord" : "0:3a6c30"
        },
        {
            "loc" : "0:3a9000",
            "xnext" : "0:7d9000",
            "xprev" : "0:29d000",
            "nsdiag" : "spider.results",
            "size" : 4390912,
            "firstRecord" : "0:3a90b0",
            "lastRecord" : "0:7d8ab0"
        },
        {
            "loc" : "0:7d9000",
            "xnext" : "0:d81000",
            "xprev" : "0:3a9000",
            "nsdiag" : "spider.results",
            "size" : 5931008,
            "firstRecord" : "0:7d90b0",
            "lastRecord" : "0:d7fcb0"
        },
        {
            "loc" : "0:d81000",
            "xnext" : "0:1524000",
            "xprev" : "0:7d9000",
            "nsdiag" : "spider.results",
            "size" : 8007680,
            "firstRecord" : "0:d810b0",
            "lastRecord" : "0:15238b0"
        },
        {
            "loc" : "0:1524000",
            "xnext" : "0:1f74000",
            "xprev" : "0:d81000",
            "nsdiag" : "spider.results",
            "size" : 10813440,
            "firstRecord" : "0:15240b0",
            "lastRecord" : "0:1f73ab0"
        },
        {
            "loc" : "0:1f74000",
            "xnext" : "1:2000",
            "xprev" : "0:1524000",
            "nsdiag" : "spider.results",
            "size" : 14598144,
            "firstRecord" : "0:1f740b0",
            "lastRecord" : "0:2d5f6b0"
        },
        {
            "loc" : "1:2000",
            "xnext" : "1:12ce000",
            "xprev" : "0:1f74000",
            "nsdiag" : "spider.results",
            "size" : 19709952,
            "firstRecord" : "1:20b0",
            "lastRecord" : "1:12cd8b0"
        },
        {
            "loc" : "1:12ce000",
            "xnext" : "1:2c2f000",
            "xprev" : "1:2000",
            "nsdiag" : "spider.results",
            "size" : 26611712,
            "firstRecord" : "1:12ce0b0",
            "lastRecord" : "1:2c2eab0"
        },
        {
            "loc" : "1:2c2f000",
            "xnext" : "1:4e72000",
            "xprev" : "1:12ce000",
            "nsdiag" : "spider.results",
            "size" : 35926016,
            "firstRecord" : "1:2c2f0b0",
            "lastRecord" : "1:4e719b0"
        },
        {
            "loc" : "1:4e72000",
            "xnext" : "2:2000",
            "xprev" : "1:2c2f000",
            "nsdiag" : "spider.results",
            "size" : 48500736,
            "firstRecord" : "1:4e720b0",
            "lastRecord" : "1:7cb27b0"
        },
        {
            "loc" : "2:2000",
            "xnext" : "2:3e74000",
            "xprev" : "1:4e72000",
            "nsdiag" : "spider.results",
            "size" : 65478656,
            "firstRecord" : "2:20b0",
            "lastRecord" : "2:3e71d30"
        },
        {
            "loc" : "2:3e74000",
            "xnext" : "3:2000",
            "xprev" : "2:2000",
            "nsdiag" : "spider.results",
            "size" : 88399872,
            "firstRecord" : "2:3e740b0",
            "lastRecord" : "2:92c1ab0"
        },
        {
            "loc" : "3:2000",
            "xnext" : "3:71d2000",
            "xprev" : "2:3e74000",
            "nsdiag" : "spider.results",
            "size" : 119341056,
            "firstRecord" : "3:20b0",
            "lastRecord" : "3:71d1ab0"
        },
        {
            "loc" : "3:71d2000",
            "xnext" : "3:10b78000",
            "xprev" : "3:2000",
            "nsdiag" : "spider.results",
            "size" : 161112064,
            "firstRecord" : "3:71d20b0",
            "lastRecord" : "3:10b70ab0"
        },
        {
            "loc" : "3:10b78000",
            "xnext" : "4:2000",
            "xprev" : "3:71d2000",
            "nsdiag" : "spider.results",
            "size" : 217501696,
            "firstRecord" : "3:10b780b0",
            "lastRecord" : "3:1dae29b0"
        },
        {
            "loc" : "4:2000",
            "xnext" : "4:11809000",
            "xprev" : "3:10b78000",
            "nsdiag" : "spider.results",
            "size" : 293629952,
            "firstRecord" : "4:20b0",
            "lastRecord" : "4:118088b0"
        },
        {
            "loc" : "4:11809000",
            "xnext" : "5:2000",
            "xprev" : "4:2000",
            "nsdiag" : "spider.results",
            "size" : 396402688,
            "firstRecord" : "4:118090b0",
            "lastRecord" : "4:29212930"
        },
        {
            "loc" : "5:2000",
            "xnext" : "5:1fe5d000",
            "xprev" : "4:11809000",
            "nsdiag" : "spider.results",
            "size" : 535146496,
            "firstRecord" : "5:20b0",
            "lastRecord" : "5:1fe5ca30"
        },
        {
            "loc" : "5:1fe5d000",
            "xnext" : "6:2000",
            "xprev" : "5:2000",
            "nsdiag" : "spider.results",
            "size" : 722448384,
            "firstRecord" : "5:1fe5d0b0",
            "lastRecord" : "5:4af553b0"
        },
        {
            "loc" : "6:2000",
            "xnext" : "7:2000",
            "xprev" : "5:1fe5d000",
            "nsdiag" : "spider.results",
            "size" : 975306752,
            "firstRecord" : "6:20b0",
            "lastRecord" : "6:3a21d6b0"
        },
        {
            "loc" : "7:2000",
            "xnext" : "8:2000",
            "xprev" : "6:2000",
            "nsdiag" : "spider.results",
            "size" : 1316667392,
            "firstRecord" : "7:20b0",
            "lastRecord" : "7:4e7ab0b0"
        },
        {
            "loc" : "8:2000",
            "xnext" : "9:2000",
            "xprev" : "7:2000",
            "nsdiag" : "spider.results",
            "size" : 1777504256,
            "firstRecord" : "8:20b0",
            "lastRecord" : "8:69f278b0"
        },
        {
            "loc" : "9:2000",
            "xnext" : "10:2000",
            "xprev" : "8:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "9:20b0",
            "lastRecord" : "9:7fefdab0"
        },
        {
            "loc" : "10:2000",
            "xnext" : "11:2000",
            "xprev" : "9:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "10:20b0",
            "lastRecord" : "10:7feff630"
        },
        {
            "loc" : "11:2000",
            "xnext" : "12:2000",
            "xprev" : "10:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "11:20b0",
            "lastRecord" : "11:7feffa30"
        },
        {
            "loc" : "12:2000",
            "xnext" : "13:2000",
            "xprev" : "11:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "12:20b0",
            "lastRecord" : "12:7feffb30"
        },
        {
            "loc" : "13:2000",
            "xnext" : "14:2000",
            "xprev" : "12:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "13:20b0",
            "lastRecord" : "13:7fefb7b0"
        },
        {
            "loc" : "14:2000",
            "xnext" : "15:2000",
            "xprev" : "13:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "14:20b0",
            "lastRecord" : "14:7feffb30"
        },
        {
            "loc" : "15:2000",
            "xnext" : "16:2000",
            "xprev" : "14:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "15:20b0",
            "lastRecord" : "15:7fefba30"
        },
        {
            "loc" : "16:2000",
            "xnext" : "17:2000",
            "xprev" : "15:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "16:20b0",
            "lastRecord" : "16:7feff9b0"
        },
        {
            "loc" : "17:2000",
            "xnext" : "18:2000",
            "xprev" : "16:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "17:20b0",
            "lastRecord" : "17:7feff9b0"
        },
        {
            "loc" : "18:2000",
            "xnext" : "null",
            "xprev" : "17:2000",
            "nsdiag" : "spider.results",
            "size" : 2146426864,
            "firstRecord" : "18:20b0",
            "lastRecord" : "18:1f8328b0"
        }
    ],
    "datasize" : 26662218768,
    "nrecords" : 1819411,
    "lastExtentSize" : 2146426864,
    "padding" : 1,
    "firstExtentDetails" : {
        "loc" : "0:258000",
        "xnext" : "0:25a000",
        "xprev" : "null",
        "nsdiag" : "spider.results",
        "size" : 8192,
        "firstRecord" : "0:2580b0",
        "lastRecord" : "0:2598b0"
    },
    "lastExtentDetails" : {
        "loc" : "18:2000",
        "xnext" : "null",
        "xprev" : "17:2000",
        "nsdiag" : "spider.results",
        "size" : 2146426864,
        "firstRecord" : "18:20b0",
        "lastRecord" : "18:1f8328b0"
    },
    "objectsFound" : 1819411,
    "invalidObjects" : 0,
    "bytesWithHeaders" : 26691329344,
    "bytesWithoutHeaders" : 26662218768,
    "deletedCount" : 14,
    "deletedSize" : 1617742528,
    "nIndexes" : 4,
    "keysPerIndex" : {
        "spider.results.$_id_" : 1819411,
        "spider.results.$datetime_-1" : 1819411,
        "spider.results.$id_1_datetime_1" : 1819411,
        "spider.results.$id_1" : 1819411
    },
    "valid" : true,
    "errors" : [ ],
    "ok" : 1
}

> db.results.find({id:6}).explain()
{
    "cursor" : "BtreeCursor id_1_datetime_1",
    "isMultiKey" : false,
    "n" : 47402,
    "nscannedObjects" : 47402,
    "nscanned" : 47402,
    "nscannedObjectsAllPlans" : 47402,
    "nscannedAllPlans" : 47402,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 248,
    "nChunkSkips" : 0,
    "millis" : 254485,
    "indexBounds" : {
        "id" : [
            [
                6,
                6
            ]
        ],
        "datetime" : [
            [
                {
                    "$minElement" : 1
                },
                {
                    "$maxElement" : 1
                }
            ]
        ]
    },
    "server" : "ip-10-137-56-52:27017"
}

我还没有做过的唯一事情是备份修复然后恢复。但我不确定它会有所帮助......

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

看起来您为id为6的示例查询与47402个文档匹配:

> db.results.find({id:6}).explain()
    "n" : 47402,

,平均文档大小约为14654字节:

> db.results.stats()
    "avgObjSize" : 14654.313273911172,

这是否与您对情况的看法相符?

这意味着此查询的结果大约为662 MB 尺寸。如果查询返回的662 MB文档不在 内存,必须从磁盘中获取然后很多磁盘活动 结果和查询将花费很长时间。

有几种方法可以改善效果,具体取决于你的 申请要求:

    例如,
  • 使用匹配较少文档的更具体的查询 使用_id来获取单个文档或指定日期时间 您查询中的ID。

  • 重构您的文档架构,使您希望查询的文档更小。

  • 在查询中使用投影仅选择14KB的子集 要返回并构建包含其中字段的索引的文档 查询投影,这样查询就可以完全满足了 指数。但请注意,这仅适用于选择较小的情况 足够的文档子集,以便您的索引不会增长 很大,让你回到必须寻呼的相同情况 从磁盘索引。 db.results.stats()输出告诉你有多大 你的索引是。

  • 为您的服务器配备足够的内存,以便保留收集 完全在记忆中。集合大小约为26 GB,所以你会这样做 这种方法需要至少那么多的记忆:

      

    db.results.stats()       “size”:26662218768,

希望这有帮助,

布鲁斯