【发布时间】:2015-11-19 17:40:44
【问题描述】:
我在优化 Meteor 集合的更新性能时遇到问题。
我从一个集合 (CollectionA) 中获取一个文档,对其进行修改,挑选一些内容,然后在另一个集合 (CollectionB) 中更新它的几乎副本。如果新文档已添加到 CollectionA 中,我也会进行更新
一切都在几毫秒内发生,但更新可能需要 10-30+ 秒,具体取决于要更新的文档的大小。文档通常在 30kb 左右...
我尝试过不使用 Meteor.defer,使用 writeConcern : 0,以及在本地和云副本集集群上。也尝试过使用插入而不是更新。没有什么明显的区别。
cronTask(){
CollectionA.find({isPublished : true, "approval.status" : { $gte : 1 } }).forEach((doc)=>{
let newDoc = {
parentId : doc._id,
slug : doc.slug, // these never change, only the content array changes...
title : doc.title,
description: doc.description,
tags : doc.tags,
category : doc.category,
createdAt : new Date(),
content: [...,...,...] // the content of the new document is cherrypicked from the parents before saving
}
while(doc.content.length) {
// cherry-picking and pushing to newDoc.content
// super quick, a couple of MS
}
Meteor.defer(()=>{
CollectionB.update({parentId : doc._id}, {$set : newDoc}, {upsert : true}, (err,res)=>{
if (!err) {
console.log(`Saved child for ${doc.title}`);
} else {
console.log(`Error saving child for ${doc.title}: ${err}`);
}
});
});
});
}
【问题讨论】:
-
让我猜猜:您这样做是在打开一个对您要添加到的集合中的更改做出反应的页面时进行的?这是一个常见问题,对此有一些答案。我会尽力为你挖掘它们。
-
下面的回答对你有帮助吗? stackoverflow.com/a/19599027/1087119
-
感谢克里斯蒂安,实际上并非如此……但无论如何,这对我来说阅读起来非常有用。经过大量的挠头后,我发现问题是模式验证需要很长时间,然后实际更新在 2 毫秒内完成了大约 30 次,所以我可以接受。 ;) 不知道为什么模式验证花了这么长时间,但在这种情况下它在服务器上,所以我只是禁用了它,因为这不是必须的。
-
太棒了!如果您可以将其表述为答案,那么我认为值得在这里为其他人添加它。听起来是一个不容易发现的陷阱。
标签: mongodb meteor mongodb-query database-performance