MongoDB请求变得非常慢,即使有索引也是如此

时间:2017-05-29 11:16:33

标签: mongodb mongoose mongodb-indexes

我正在建立一个mongodb应用程序,我只是想知道为什么(即使有索引)我的请求变得非常慢,如果文档达到一个数量> 1.000.000。

我的架构如下:

const reportSchema = new mongo.Schema({

  organisation: {
    type: mongo.Schema.Types.ObjectId,
    ref: 'Organisation',
    required: true
  },

  reference: {
    type: mongo.Schema.Types.ObjectId,
    required: true
  },

  belongsTo: {
    type: String,
    enum: ['endpoint'],
    required: true
  },

  objects: [{
    property: {
      type: String,
      required: true
    },
    value: {
      type: String,
      required: true
    }
  }]

}, {
  timestamps: true
});

我的索引如下:

reportSchema.index({
  createdAt: -1,
  organisation: 1,
  reference: 1
});

我的疑问:

this.model.find({
          organisation: organisationId,
          reference: referenceId
        })
        .select('_id reference objects createdAt')
        .sort('-createdAt')
        .then(reports => resolve(reports))
        .catch(err => {
          console.error(err);
          reject();
        });

谁能告诉我为什么? 指数失败的指标是什么? 我的查询错了吗?

0 个答案:

没有答案