[英]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 )。
我為陣列中的對象禁用了模式檢查(該方法在服務器上,因此在這種情況下不進行驗證是安全的)。 現在只需不到1毫秒即可更新所有30個文檔。
不知道為什么模式驗證這么慢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.