gpt4 book ai didi

MongoDB 索引

转载 作者:可可西里 更新时间:2023-11-01 10:34:08 27 4
gpt4 key购买 nike

我们有一个包含近 4000 万条记录的 MongoDB 集合。该集合的当前大小为 5GB。此集合中存储的数据包含以下字段:

_id: "MongoDB id"
userid: "user id" (int)
mobile: "users mobile number" (int)
transaction: "transaction id" (int)
sms: "message sent to user mobile" (text)
created_dt: "unix timestamp of the transaction"

除了 _id 上的索引(默认创建)之外,我们还在移动和交易字段上定义了单独的索引。

但是,以下查询需要 60 到 120 秒才能完成:

{
mobile:<users mobile number>
}

我使用 RockMongo 访问 MongoDB。 MongoDB 托管在具有 16GB RAM 的服务器上。此服务器上将近 8GB RAM 是免费的。

我这里做错了什么?

更新:

解释的输出:

{
"cursor" : "BasicCursor",
"nscanned" : 37145516,
"nscannedObjects" : 37145516,
"n" : 37145516,
"millis" : 296040,
"nYields" : 1343,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}

查询时mongostat的输出

insert  query update delete getmore command flushes mapped  vsize    res faults locked % idx miss %     qr|qw   ar|aw  netIn netOut  conn       time 
13 2 0 0 0 1 0 168g 336g 6.86g 1 1 0 0|0 1|0 21k 1k 19 11:30:04
16 0 0 0 0 1 0 168g 336g 6.88g 0 0.1 0 0|0 1|0 21k 1k 19 11:30:05
14 0 0 0 0 1 0 168g 336g 6.86g 0 0 0 0|0 1|0 29k 1k 19 11:30:06
10 0 0 0 0 1 0 168g 336g 6.86g 0 0 0 0|0 1|0 19k 1k 19 11:30:07
16 0 0 0 0 1 0 168g 336g 6.88g 0 0.1 0 0|0 1|0 21k 1k 19 11:30:08
9 0 0 0 0 1 0 168g 336g 6.89g 0 0 0 0|0 1|0 13k 1k 19 11:30:09
19 0 0 0 0 1 0 168g 336g 6.89g 0 0 0 0|0 1|0 27k 1k 19 11:30:10
12 0 0 0 0 1 0 168g 336g 6.89g 1 1.2 0 0|0 1|0 24k 1k 19 11:30:11
17 0 0 0 0 1 0 168g 336g 6.89g 1 1.7 0 0|0 1|0 31k 1k 19 11:30:12
15 0 0 0 0 1 0 168g 336g 6.89g 0 0 0 0|0 1|0 19k 1k 19 11:30:13

更新 2:

直到最近,我们还习惯于在同一个 MongoDB 服务器中存储另一个包含大约 13 亿个文档的集合。此集合现已删除(丢弃)。这可以解释上面 mongostat 输出中的 mapped/vsize 列。

服务器还存储了其他 6 个频繁插入的集合。当前总存储大小约为 35GB。

更新 3:

在集合上定义的索引。使用 RockMongo 创建。

[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mymongodb.transaction_sms_details",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"_transaction_mobile_" : 1
},
"ns" : "mymongodb.transaction_sms_details",
"background" : 1,
"name" : "mobile"
},
{
"v" : 1,
"key" : {
"_transaction_transaction_" : 1
},
"ns" : "mymongodb.transaction_sms_details",
"background" : 1,
"name" : "transaction"
}
]

最佳答案

RockMongo 生成的 key 显然不正确。

    "_transaction_mobile_" : 1
"_transaction_transtion_" : 1

我不知道 RockMongo 有什么问题,但我认为这可以解决问题:

db.xxx.dropIndexes();
db.xxx.ensureIndex({mobile: 1});
db.xxx.ensureIndex({transaction: 1});

注意:这可能需要很长时间。不要在正在运行的生产机器上执行此操作。

关于MongoDB 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12633687/

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