我编写了一个 Node.js API,可以从集合中读取数据。包含 1000 条记录的集合。每条记录包含 300 个子负载数据点。
const data = await csvModel['csvFiles'].find({ "created_datetime": today_datetime });
if (data.length > 0) {
for (let mappData of data) {
await payloadMapping(mappData.payload);
}
}
在payloadMapping内部,我们正在进行字段映射并存储在新集合中
const payloadMapping = async (jsonPayload) => {
try {
let categoryId = await getCategoryID(jsonPayload["category"][0].parent_category);
let totalEvent = jsonPayload["category"][0].event.length;
if (categoryId) {
let allExistingEvent = await getAllEvents(categoryId);
let eventInsertData = [];
for (let i = 0; i < totalEvent; i++) {
let duplicate_event = false;
let eventPayload = {};
eventPayload["categoryId"] = categoryId;
eventPayload["start"] = jsonPayload["category"][0].event[i].start;
/*Check no two event at same times */
if (allExistingEvent.length > 0) {
allExistingEvent.forEach(item => {
let result = item.categoryId.every((element, index) => element === categoryId[index]);
if (result && item.start === eventPayload.start) {
duplicate_event = true;
}
});
}
/*Check ends */
if (!duplicate_event) {
eventPayload["title"] = jsonPayload["category"][0].event[i].title;
eventPayload["description"] = jsonPayload["category"][0].event[i].description;
eventPayload["event_location"] = jsonPayload["category"][0].event[i].location;
eventPayload["duration"] = jsonPayload["category"][0].event[i].duration;
eventPayload["status"] = 1;
eventInsertData.push(eventPayload);
}
}
if (eventInsertData.length > 0) {
try {
await Events.insertMany(eventInsertData);
} catch (mongoErr) {
console.log(mongoErr);
}
}
} else {
console.log("Category collection not exist" + jsonPayload["category"][0].name);
}
} catch (error) {
console.log(error, "catch error")
}
}
此函数根据parent_category列出所有事件
const getCategoryID = (parent_category) => {
return new Promise( (resolve) => {
(async () => {
const query = { parent_category: parent_category};
let categoryArray = [];
let categoryResult = await categoryModel.find(query);
if (categoryResult.length > 0) {
categoryResult.forEach((item) => {
categoryArray.push(item._id.toString());
})
}
resolve(categoryArray);
});
});
};
此函数根据category_id列出所有事件
const getAllEvents = (categoryId) => {
return new Promise( (resolve) => {
(async () => {
try {
const eventLists = await Events.find({ 'categoryId': categoryId }, { "categoryId": 1, "start": 1 });
if (eventLists.length > 0) {
resolve(eventLists);
} else {
resolve([]);
}
} catch (dbErr) {
console.log("dbErr", dbErr);
resolve([]);
}
});
});
};
我已经给POD提供了以下配置
资源限制
资源: 要求:
内存:“250Mi”, 中央处理器:“50m”,
限制: 内存:“8000Mi” 中央处理器:“4000m”
运行 API 时,我的内存和 CPU 利用率很高。请帮我优化代码。
一些注意事项:
getCategoryID
和 getAllEvents
都可能生成一个
新的数据库查询。通过批处理最小化查询数量
操作或重构数据的检索和处理方式。new Promise
与立即调用的异步函数一起使用
getCategoryID
和 getAllEvents
内部是不必要的,因为异步
函数已经返回一个承诺。将这些函数简化为
直接返回查询结果。以下是重构 getCategoryID 以进行简化的方法:
const getCategoryID = async (parent_category) => {
try {
const query = { parent_category: parent_category };
const categoryResult = await categoryModel.find(query);
return categoryResult.map(item => item._id.toString());
} catch (error) {
console.error("Error fetching category ID:", error);
return [];
}
};