Explorar el Código

优化同步批量修改sql

linzhiwei hace 2 años
padre
commit
113763c005

+ 19 - 1
mysy-scada/src/main/java/com/tofly/scada/service/impl/ScadaSynHisServiceImpl.java

@@ -12,6 +12,7 @@ import cn.hutool.crypto.digest.HMac;
 import cn.hutool.crypto.digest.MD5;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.tofly.common.core.entity.ResultRespone;
 import com.tofly.scada.entity.Scada;
@@ -108,7 +109,24 @@ public class ScadaSynHisServiceImpl extends ServiceImpl<ScadaSynHisMapper, Scada
                 // 批量更新推送数据同步状态
                 List<Long> collect = scadaSynHisList.stream().map(Scada::getId).collect(Collectors.toList());
                 if(collect.size()>0){
-                    baseMapper.updateBatchSynFlag(collect);
+                    int size = collect.size();
+                    //向上取整 , 获得次数,但是次数不多,只有几次,所以可以使用for循环分段查询
+                    int count = (int) Math.ceil((double) size / 1000d);
+                    for (int i = 0; i < count; i++) {
+                        int startIndex = i * 1000;
+                        int endIndex = 1000 + i * 1000;
+                        List<Long> subYhbhList;
+                        //判断是否截取的最后一次数据,如果是最后一次数据,则不能使用endIndex这个参数去截取了,有可能会存在endIndex参数值超过索引最大值的情况
+                        if (i < count - 1) {
+                            //分段采取数据 0~999 ; 1000~1999 , 2000~2999 .....//这里subList方法无法截取最后一位,所以需要多写一位索引
+                            subYhbhList = collect.subList(startIndex, endIndex);
+                        } else {
+                            //分段采取最后的数据
+                            subYhbhList = collect.subList(startIndex, collect.size());
+                        }
+                        baseMapper.updateBatchSynFlag(subYhbhList);
+                    }
+
                 }
             }else {
                 // 记录异常,返回失败

+ 2 - 7
mysy-scada/src/main/resources/mapper/ScadaSynHisMapper.xml

@@ -29,13 +29,8 @@
         update SCADA_SYN_HIS
         set flag = '1'
         where history_Id in
-        <foreach collection="idList" index="index" open="(" close=")" separator="," item="id">
-            <!--防止数据超过1000报错-->
-            <if test="(index % 999) == 998">
-                0 )
-                or history_Id in (
-            </if>
-            #{id}
+        <foreach collection="idList" item="index" open="(" separator="," close=")">
+            #{index}
         </foreach>
     </update>