【问题标题】:MongoDb query too slowMongoDb 查询太慢
【发布时间】:2020-04-08 13:10:23
【问题描述】:

我正在执行一个查询,要从数据库中获取数据,需要超过 6 秒,当我在 studio 3T 中执行相同的查询时,它执行得非常快。 我也执行了explain()

executionStats: {
      executionSuccess: true,
      nReturned: 932,
      executionTimeMillis: 4,
      totalKeysExamined: 0,
      totalDocsExamined: 5149,
      executionStages: [Object],
      allPlansExecution: []
}

如您所见,执行时间很快,但返回结果需要 6 秒。 我正在打印时间看看它需要多少时间

console.log("Before", Date())
const posts = await Post.find({ author: author._id })
console.log("After", Date())

【问题讨论】:

    标签: node.js database mongodb nosql


    【解决方案1】:

    这是您进行的第一个查询,因此它实际上必须连接到服务器?连接过程需要一些时间(虽然 6 秒似乎过长)。

    试试:

    const posts = await Post.find({ author: author._id })
    console.log("Before", Date())
    const posts = await Post.find({ author: author._id })
    console.log("After", Date())
    

    这可确保您对发现进行基准测试,而不是在幕后进行任何设置。

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      相关资源
      最近更新 更多