Javascript MongoDB查找查询在更新/插入架构时速度较慢
我每天做一次大循环——更新数据库中的现有文档(并插入新文档) 这个循环在一个单独的服务器上执行(防止主服务器的速度变慢),但主要问题是数据库上的所有find查询(在执行循环时)都非常慢(循环会显著降低速度) 这在我的网站上是一个非常大的问题(这个循环必须每天执行一次),我一直在尝试在线找到解决方案,但我没能找到什么。 在插入/更新数据库时,是否有任何方法可以防止find查询变得如此缓慢Javascript MongoDB查找查询在更新/插入架构时速度较慢,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我每天做一次大循环——更新数据库中的现有文档(并插入新文档) 这个循环在一个单独的服务器上执行(防止主服务器的速度变慢),但主要问题是数据库上的所有find查询(在执行循环时)都非常慢(循环会显著降低速度) 这在我的网站上是一个非常大的问题(这个循环必须每天执行一次),我一直在尝试在线找到解决方案,但我没能找到什么。 在插入/更新数据库时,是否有任何方法可以防止find查询变得如此缓慢 uploadProductsManually = async (name, products, map
uploadProductsManually = async (name, products, map, valuesMap) => {
return new Promise(async function (resolve, reject) {
const company = await Company.findOne({ name }).exec();
if (!company) return reject(new errors.NotFound("Company not found"));
const rows = products;
const parsedRows = [];
const findCorrectKey = (key) => {
const correctKey = key.trim();
if (productFields[correctKey]) return productFields[correctKey];
const category = map.find((item) => {
return item.options.some((option) => {
return option.trim().toLowerCase() === correctKey.toLowerCase();
});
});
const categoryName = category && category.name;
return productFields[categoryName];
};
const hashProductValues = (product) => {
let valueToHash;
if (product.productId) {
valueToHash = product.productId;
} else if (product.certificateId) {
valueToHash = product.certificateId;
} else {
valueToHash = JSON.stringify(
product.size + product.color
);
}
return base64encode(valueToHash);
};
rows.forEach(function (row, i) {
var newProduct = {};
for (var key in row) {
var val = row[key];
if (val) {
let normalizedKey = findCorrectKey(key);
if (normalizedKey) {
newProduct[normalizedKey] = val;
}
let normalizedValue = normalizeValue(normalizedKey, val,valuesMap);
newProduct[normalizedKey] = normalizedValue;
}
}
newProduct.channels = [];
if (newProduct.productId) {
parsedRows.push(newProduct);
}
});
fetchProducts();
function fetchProducts() {
Product.find({ company: company._id }).exec(function (err, products) {
if (err) console.log(err);
var map = {};
if (products) {
products.forEach(function (product) {
const productIdentifier = hashProductValues(product);
map[productIdentifier] = product;
if (product.productStatus == "manual") {
// product.isAvailable = false;
// product.save();
} else {
product.status = "deleted";
product.save();
}
});
}
mergeData(map);
});
}
async function mergeData(map) {
let created = 0;
let updated = 0;
let manual = 0;
async.each(
parsedRows,
function (row, callback) {
const productIdentifier = hashProductValues(row);
let product = map[productIdentifier];
if (product) {
map[productIdentifier] = undefined;
Product.findByIdAndUpdate(id, { $set: updatedProduct }, function (
err,
updatedProd
) {
if (err) {
// errors.push(productIdentifier);
console.log("err is:", err);
}
updated++;
callback();
});
} else {
row = new Product(row);
row.save(function (err) {
if (err) {
// errors.push(productIdentifier);
console.log(err);
}
created++;
callback();
});
}
},
(err) => {
if (err) return reject(err);
Company.findByIdAndUpdate(
company._id,
{ lastUpdate: new Date() },
function (err, comp) {
if (err) console.log(err);
}
);
console.log(
`Created: ${created}\nUpdated: ${updated} \manual: ${manual}`
);
resolve({
created,
updated,
manual,
errors,
});
}
);
}
});
};
请提供相关的代码、模式和其他相关信息,以便得到合理的答案。这不是一个具体的问题——它非常通用。我有很多查询(也有与更新的模式无关的查询)基本上数据库中的整个搜索速度要慢得多。你怎么能期望得到一个背景信息如此之少的答案呢?我刚刚添加了在循环@NikosM中执行的代码。但我要再说一遍。。。我想知道在更新/插入模式时,较慢的数据库是否是正常现象,我可以做些什么来提高性能请提供相关代码、模式和其他相关信息,以便得到合理的答案这不是一个特定的问题-它非常通用。我有很多查询(也有与更新的模式无关的查询)基本上数据库中的整个搜索速度要慢得多。你怎么能期望得到一个背景信息如此之少的答案呢?我刚刚添加了在循环@NikosM中执行的代码。但我要再说一遍。。。我想知道在更新/插入模式时,较慢的db是否正常,以及如何提高性能