Jelajahi Sumber

perf: 提升批量修改速度

vian 5 bulan lalu
induk
melakukan
6b2ca3e8a0

+ 3 - 1
modules/all_models/std_price_info_items.js

@@ -83,6 +83,8 @@ const priceInfoItems = new Schema({
         default: []
     }
 }, { versionKey: false });
-priceInfoItems.index({ areaID:1,period:1});
+priceInfoItems.index({ areaID: 1, period: 1 });
+priceInfoItems.index({ ID: 1 });
+priceInfoItems.index({ libID: 1 });
 
 mongoose.model('std_price_info_items', priceInfoItems, 'std_price_info_items');

+ 13 - 5
modules/price_info_lib/facade/index.js

@@ -765,6 +765,7 @@ async function exportInfoPriceByCompilation(compilationID) {
 
 // 批量修改同省份下所有相同材料(编号、名称、规格、单位)
 async function batchUpdate(priceItem, prop, val) {
+    const date1 = Date.now();
     const areas = await priceInfoAreaModel.find({ compilationID: priceItem.compilationID }, '-_id ID name').lean();
     const area = areas.find(item => item.ID === priceItem.areaID);
     if (!area || !area.name) {
@@ -778,24 +779,31 @@ async function batchUpdate(priceItem, prop, val) {
     if (!sameProvinceAreas.length) {
         return;
     }
+    console.log('1', date1);
+    const date2 = Date.now();
     const areaIDs = sameProvinceAreas.map(item => item.ID);
     // 根据编号初筛
     const priceItems = await priceInfoItemModel.find({ libID: priceItem.libID, code: priceItem.code || '' }, '-_id ID areaID code name specs unit').lean();
+    const date3 = Date.now();
+    console.log('2', date3 - date2);
     // 批量修改相同材料
-    const bulks = [];
+    // const bulks = [];
     const getKey = (item) => {
         return `${item.code || ''}@${item.name || ''}@${item.specs || ''}@${item.unit || ''}`;
     }
     const key = getKey(priceItem);
+    const updateIDs = [];
     priceItems.forEach(item => {
         if (areaIDs.includes(item.areaID) && getKey(item) === key) {
-            bulks.push({ updateOne: { filter: { ID: item.ID }, update: { $set: { [prop]: val } } } });
+            updateIDs.push(item.ID);
+            // bulks.push({ updateOne: { filter: { ID: item.ID }, update: { $set: { [prop]: val } } } });
         }
     });
-    if (bulks.length) {
-        await priceInfoItemModel.bulkWrite(bulks);
+    if (updateIDs.length) {
+        await priceInfoItemModel.updateMany({ ID: { $in: updateIDs } }, { $set: { [prop]: val } });
     }
-
+    const date4 = Date.now();
+    console.log('3', date4 - date3);
 
 
 }