【问题标题】:Why is MongoDB logging so many of my queries为什么 MongoDB 记录了这么多我的查询
【发布时间】:2014-06-28 16:19:27
【问题描述】:

我有一个运行良好的 NodeJS MongoDB,我注意到的唯一问题是 mongo 正在记录我的大量查询。我尝试了不同的索引策略,但它保持不变。 mongo和node之间的通信是通过mongoose来完成的。

查询案例1:

Sun May 11 01:15:26.076 [conn5] query api.items query: { $query: { gameid: 209670, appid: 102 }, orderby: { price: -1 } } ntoreturn:0 ntoskip:0 nscanned:29 scanAndOrder:1 keyUpdates:0 numYields: 1 locks(micros) r:382458 nreturned:29 reslen:22686 240ms
Sun May 11 01:24:44.950 [conn5] query api.items query: { $query: { gameid: 248820, appid: 102 }, orderby: { price: -1 } } ntoreturn:0 ntoskip:0 nscanned:20 scanAndOrder:1 keyUpdates:0 locks(micros) r:78688 nreturned:20 reslen:16281 110ms

物品集合:

var itemSchema = new Schema({
    appid    : {type: Number, required: true},
    gameid   : {type: Number, required: true},
    name     : {type: String, required: true},
    hash     : {type: String, index: true},
    price    : Number,
    date     : Date
});

itemSchema.set('versionKey', false);
itemSchema.index({ appid: 1, gameid: 1 });
itemSchema.index({ gameid: 1 });

我有一组大约 20.000 个项目,所有项目的 appid 都是 102,并且有 800 个 gameid。它们都有一个唯一的 ObjectId。

RockMongo 告诉我以下索引可用:

当我对查询进行解释时:

Response from server:
{
   "cursor": "BtreeCursor appid_1_gameid_1",
   "isMultiKey": false,
   "n": NumberInt(20),
   "nscannedObjects": NumberInt(20),
   "nscanned": NumberInt(20),
   "nscannedObjectsAllPlans": NumberInt(60),
   "nscannedAllPlans": NumberInt(60),
   "scanAndOrder": true,
   "indexOnly": false,
   "nYields": NumberInt(0),
   "nChunkSkips": NumberInt(0),
   "millis": NumberInt(25),
   "indexBounds": {
     "appid": [
       [
         102,
         102
      ] 
    ],
     "gameid": [
       [
         248820,
         248820 
      ] 
    ] 
  },
   "allPlans": [
     {
       "cursor": "BtreeCursor appid_1_gameid_1",
       "n": NumberInt(20),
       "nscannedObjects": NumberInt(20),
       "nscanned": NumberInt(20),
       "indexBounds": {
         "appid": [
           [
             102,
             102
          ] 
        ],
         "gameid": [
           [
             248820,
             248820 
          ] 
        ] 
      } 
    },
     {
       "cursor": "BtreeCursor gameid_1",
       "n": NumberInt(20),
       "nscannedObjects": NumberInt(20),
       "nscanned": NumberInt(20),
       "indexBounds": {
         "gameid": [
           [
             248820,
             248820 
          ] 
        ] 
      } 
    },
     {
       "cursor": "BasicCursor",
       "n": NumberInt(0),
       "nscannedObjects": NumberInt(20),
       "nscanned": NumberInt(20),
       "indexBounds": [

      ] 
    } 
  ],
   "oldPlan": {
     "cursor": "BtreeCursor appid_1_gameid_1",
     "indexBounds": {
       "appid": [
         [
           102,
           102
        ] 
      ],
       "gameid": [
         [
           248820,
           248820 
        ] 
      ] 
    } 
  },
   "server": ---
}

【问题讨论】:

  • 通常 MongoDB 会自动记录慢于 100ms 的查询。如果您的查询计划没问题,那么您的服务器可能存在瓶颈。为什么所有的 NumberInt(0) 都在那里?它们应该是价值并有助于分析问题。
  • @yaoxing 我在一个小型免费服务器上运行它,所以我认为它没有那么强大。另外,我真的不知道为什么所有的NumberInt(0) 都在那里,为什么它们没有被填满,你知道吗?
  • 你是用mongo shell来获取执行计划的吗?
  • 不,我使用 RockMongo 解释功能。
  • 嗯,没办法。应该是框架的一些问题。如果可以连接到 shell,mongotopmongostat 将有助于分析瓶颈

标签: node.js mongodb indexing mongoose


【解决方案1】:

MongoDB 记录超过 100 毫秒的慢查询(配置文件级别 1,默认 slowms)。您可以使用profile 命令更改阈值。

通过查看您的explain 命令,我认为您的服务器的性能是问题所在。您的解释命令在 25 毫秒内完成,因此它可能只是由于服务器上的高负载而导致的暂时现象。检查您的彩信统计信息。

关于您的索引的一些建议。 您应该考虑从gameidprice 列创建compound index,因为如果排序函数消耗超过32MB,MongoDB 将返回错误。 如果集合中的所有文档都具有相同的appId,那么您可以从该字段中删除索引,因为它没有被有效地使用。

【讨论】:

  • 感谢您的提示。我在一个免费的 openshift 齿轮上运行它,所以如果它可能有点弱。我还将添加关于gameid和价格的索引。最后一件事,有没有办法将慢查询日志设置为250ms?
猜你喜欢
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
相关资源
最近更新 更多