【问题标题】:MongoDB indexingMongoDB 索引
【发布时间】:2012-09-19 23:10:12
【问题描述】:

我们有一个包含近 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"
}
]

【问题讨论】:

  • 你索引移动领域了吗? mongodb.org/display/DOCS/Indexes
  • 问题说他们做到了(_id、mobile 和 transaction 的三个索引)。
  • 每个手机号码返回多少文件?
  • 是的。我们索引了 3 个字段: 1. _id 字段(默认创建); 2.移动领域,3.交易领域
  • 文档数量可能不同。平均在 60 左右。任何手机号码的条目不能超过 160 个。

标签: mongodb indexing


【解决方案1】:

RockMongo 生成的密钥显然不正确。

    "_transaction_mobile_" : 1
    "_transaction_transtion_" : 1

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

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

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

【讨论】:

  • 谢谢。 RockMongo 允许编辑索引的名称以及要索引的字段的名称。我认为创建索引的人更新了字段名称而不是索引名称。 RockMongo 没有检查以确保被索引的字段确实存在于集合中。
  • 你应该在后台操作模式下重新索引。
  • 它可能不应该做检查,因为你可以为未来的字段添加索引。也许是一个警告...... :) 至少你有你的解决方案!
猜你喜欢
  • 2010-12-17
  • 2023-03-26
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 2018-09-27
相关资源
最近更新 更多