gpt4 book ai didi

MongoDB:索引内的查询非常慢

转载 作者:可可西里 更新时间:2023-11-01 09:36:46 25 4
gpt4 key购买 nike

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

我收藏了近 200 万份文件。我的问题:非常简单的查询 (find({id: 53})) 运行将近 5 分钟(请参阅下面的所有相关信息)。

我试图通过“id”字段添加索引,试图提示,scanAndOrder 是错误的。我有足够的内存(目前没有交换,服务器上总共只使用了 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"
}

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

有什么建议吗?

最佳答案

看起来您给出的 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,

希望对您有所帮助,

布鲁斯

关于MongoDB:索引内的查询非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com