Ver código fonte

1、调整删除2月之前的流量数据

yys 2 dias atrás
pai
commit
337209b672

+ 1 - 2
fs-service/src/main/java/com/fs/course/mapper/FsCourseTrafficLogMapper.java

@@ -231,10 +231,9 @@ public interface FsCourseTrafficLogMapper
 
 
     /**
-     * 查询过期的流量记录ID列表(分页
+     * 查询过期的流量记录ID列表(取最靠前的一批
      */
     List<Long> selectExpireLinkIds(@Param("createTime") Date createTime,
-                                   @Param("offset") int offset,
                                    @Param("limit") int limit);
 
     /**

+ 12 - 16
fs-service/src/main/java/com/fs/course/service/impl/FsCourseTrafficLogServiceImpl.java

@@ -526,8 +526,8 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
 
     @Override
     public void batchDelTraffic() {
-        // 设置删除的时间条件:三个月之前
-        LocalDate targetLocalDate = LocalDate.now().minusMonths(3).withDayOfMonth(1);
+        // 设置删除的时间条件:两个月之前(当月1日0点)
+        LocalDate targetLocalDate = LocalDate.now().minusMonths(2).withDayOfMonth(1);
         Date targetDate = Date.from(targetLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
 
         int batchSize = 5000; // 每批次处理数量
@@ -540,7 +540,7 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
      * 批量删除过期数据
      */
     private void batchDeleteExpiredData(Date targetDate, int batchSize, int sleepMillis) {
-        int currentPage = 0;
+        int batchNumber = 0;
         long totalDeleted = 0;
 
         try {
@@ -556,9 +556,8 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
             long startTime = System.currentTimeMillis();
 
             while (true) {
-                // 分页查询过期记录的log_id
-                int offset = currentPage * batchSize;
-                List<Long> logIds = fsCourseTrafficLogMapper.selectExpireLinkIds(targetDate, offset, batchSize);
+                // 始终查询最靠前的一批,删除后下一批自然顶上,避免 OFFSET 翻页漏删
+                List<Long> logIds = fsCourseTrafficLogMapper.selectExpireLinkIds(targetDate, batchSize);
 
                 if (logIds == null || logIds.isEmpty()) {
                     log.info("批量删除完成,共删除 {} 条记录", totalDeleted);
@@ -568,20 +567,18 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
                 // 批量删除当前批次的log_id
                 int deletedCount = fsCourseTrafficLogMapper.batchDeleteByIds(logIds);
                 totalDeleted += deletedCount;
+                batchNumber++;
 
                 // 每5批次或最后一批输出日志
-                if (currentPage % 5 == 0 || logIds.size() < batchSize) {
+                if (batchNumber % 5 == 0 || logIds.size() < batchSize) {
                     double progress = (double) totalDeleted / totalCount * 100;
-                    long currentTime = System.currentTimeMillis();
-                    long elapsedSeconds = (currentTime - startTime) / 1000;
+                    long elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000;
 
-                    log.info("批次 {}: 删除 {} 条,进度: {}/{} ({:.2f}%),耗时: {}秒",
-                            currentPage + 1, deletedCount, totalDeleted, totalCount,
-                            progress, elapsedSeconds);
+                    log.info("批次 {}: 删除 {} 条,进度: {}/{} ({}%),耗时: {}秒",
+                            batchNumber, deletedCount, totalDeleted, totalCount,
+                            String.format("%.2f", progress), elapsedSeconds);
                 }
 
-                currentPage++;
-
                 // 批次间短暂休眠,避免数据库压力过大
                 if (sleepMillis > 0 && logIds.size() == batchSize) {
                     try {
@@ -594,8 +591,7 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
                 }
             }
 
-            long endTime = System.currentTimeMillis();
-            long totalSeconds = (endTime - startTime) / 1000;
+            long totalSeconds = (System.currentTimeMillis() - startTime) / 1000;
             log.info("批量删除任务完成,总计删除: {} 条记录,总耗时: {} 秒", totalDeleted, totalSeconds);
 
         } catch (Exception e) {

+ 1 - 1
fs-service/src/main/resources/mapper/course/FsCourseTrafficLogMapper.xml

@@ -339,7 +339,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         FROM fs_course_traffic_log
         WHERE create_time < #{createTime}
         ORDER BY log_id ASC
-            LIMIT #{limit} OFFSET #{offset}
+        LIMIT #{limit}
         ]]>
     </select>