【发布时间】:2021-11-13 16:05:43
【问题描述】:
我正在使用 M10 mongodb atlas (2GB RAM, 2CPU) 进行测试。我在集合中有 101330 个文档。每个文档看起来像
{
_id: ObjectId("618d3bf93de582a797f28df3"),
applicantName: 'a57cgMwpX6EsMca',
amount: 6391,
status: 'Pending',
date: ISODate("2021-11-11T15:51:21.115Z")
}
使用 mongo shell,我运行查询 db.test.find({}).toArray() 以返回所有记录。它不会在 1 分钟内完成。我很惊讶它花了这么长时间。是因为我使用的资源少还是正常。使用 db.test.find({}).explain("executionStats") 运行解释命令以查看详细信息。
{
queryPlanner: {
plannerVersion: 1,
namespace: 'policymatrix_dev.indextest',
indexFilterSet: false,
parsedQuery: {},
winningPlan: { stage: 'COLLSCAN', direction: 'forward' },
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 101330,
executionTimeMillis: 26,
totalKeysExamined: 0,
totalDocsExamined: 101330,
executionStages: {
stage: 'COLLSCAN',
nReturned: 101330,
executionTimeMillisEstimate: 2,
works: 101332,
advanced: 101330,
needTime: 1,
needYield: 0,
saveState: 101,
restoreState: 101,
isEOF: 1,
direction: 'forward',
docsExamined: 101330
}
},
serverInfo: {
host: '',
port: 27017,
version: '4.4.10',
gitVersion: ''
},
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1636723868, i: 1 }),
signature: {
hash:
keyId: Long("6968661901790150660")
}
},
operationTime: Timestamp({ t: 1636723868, i: 1 })
}
从结果中,我可以看到它给出的 executionTimeMillis 等于 26,这是一个非常小的值。现在我很困惑为什么统计数据的实际查询执行时间和 executionTimeMillis 存在差异。它们是相同的还是不同的。
要添加更多内容,我在 AWS lambda 函数中运行相同的查询。它在 5314 毫秒内完成。这真是令人困惑。
module.exports.handler = async() => {
try {
const db = await connectDB("database");
const start = new Date();
const response = await test.find({});
const end = new Date();
console.log("time required", end - start) // returns 5314
console.log(response.length) // returns 101330
return {success: true}
}
【问题讨论】:
标签: mongodb mongodb-query