流星合集(MongoDB)更新性能极差
Meteor collection (MongoDB) update very poor performance
我在优化 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}`);
}
});
});
});
}
原来问题实际上不是更新,而是模式验证(使用 https://github.com/aldeed/meteor-simple-schema)。
我禁用了数组中对象的模式检查(该方法在服务器上,因此在这种情况下不进行验证是安全的)。现在更新所有 30 个文档需要 <1 毫秒。
不确定架构验证为何如此缓慢。
我在优化 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}`);
}
});
});
});
}
原来问题实际上不是更新,而是模式验证(使用 https://github.com/aldeed/meteor-simple-schema)。
我禁用了数组中对象的模式检查(该方法在服务器上,因此在这种情况下不进行验证是安全的)。现在更新所有 30 个文档需要 <1 毫秒。
不确定架构验证为何如此缓慢。