|
@@ -59,6 +59,9 @@ public class ClearExpireDataJobHandler {
|
|
if (StringUtils.isNotBlank(jobParam)) {
|
|
if (StringUtils.isNotBlank(jobParam)) {
|
|
days = Integer.parseInt(jobParam.trim());
|
|
days = Integer.parseInt(jobParam.trim());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ boolean ignoreStatus = (days == -1);
|
|
|
|
+
|
|
// 默认:最少30天
|
|
// 默认:最少30天
|
|
days = Math.max(days, 30);
|
|
days = Math.max(days, 30);
|
|
|
|
|
|
@@ -68,26 +71,33 @@ public class ClearExpireDataJobHandler {
|
|
String expireTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
|
|
String expireTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
|
|
|
|
|
|
// 获取总数
|
|
// 获取总数
|
|
- final String totalSql = "select count(1) from ec_oes_exam_record_data where sync_status = 'SYNCED' and creation_time <= '%s'";
|
|
|
|
- Long totalSize = jdbcTemplate.queryForObject(String.format(totalSql, expireTime), Long.class);
|
|
|
|
- log.warn("清理过期数据任务!jobParam:{} expireTime:{} totalSize:{}", jobParam, expireTime, totalSize);
|
|
|
|
- if (totalSize == null || totalSize == 0) {
|
|
|
|
|
|
+ StringBuilder countSql = new StringBuilder();
|
|
|
|
+ countSql.append(" select count(1) from ec_oes_exam_record_data");
|
|
|
|
+ countSql.append(" where 1=1");
|
|
|
|
+ if (!ignoreStatus) {
|
|
|
|
+ countSql.append(" and sync_status = 'SYNCED'");
|
|
|
|
+ }
|
|
|
|
+ countSql.append(" and creation_time <= '").append(expireTime).append("'");
|
|
|
|
+ Long totalCount = jdbcTemplate.queryForObject(countSql.toString(), Long.class);
|
|
|
|
+ log.warn("清理过期数据任务!jobParam:{} expireTime:{} totalSize:{}", jobParam, expireTime, totalCount);
|
|
|
|
+ if (totalCount == null || totalCount == 0) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
long startId = 0L, finishCount = 0L;
|
|
long startId = 0L, finishCount = 0L;
|
|
- final String querySql = new StringBuilder()
|
|
|
|
- .append(" select id from ec_oes_exam_record_data")
|
|
|
|
- .append(" where sync_status = 'SYNCED'")
|
|
|
|
- .append(" and id > %s")
|
|
|
|
- .append(" and creation_time <= '").append(expireTime).append("'")
|
|
|
|
- .append(" order by id asc")
|
|
|
|
- .append(" limit 5000")
|
|
|
|
- .toString();
|
|
|
|
|
|
+ StringBuilder querySql = new StringBuilder();
|
|
|
|
+ querySql.append(" select id from ec_oes_exam_record_data");
|
|
|
|
+ querySql.append(" where id > %s");
|
|
|
|
+ if (!ignoreStatus) {
|
|
|
|
+ querySql.append(" and sync_status = 'SYNCED'");
|
|
|
|
+ }
|
|
|
|
+ querySql.append(" and creation_time <= '").append(expireTime).append("'");
|
|
|
|
+ querySql.append(" order by id asc");
|
|
|
|
+ querySql.append(" limit 5000");
|
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
long startTime = System.currentTimeMillis();
|
|
while (true) {
|
|
while (true) {
|
|
- List<Long> tempIds = jdbcTemplate.queryForList(String.format(querySql, startId), Long.class);
|
|
|
|
|
|
+ List<Long> tempIds = jdbcTemplate.queryForList(String.format(querySql.toString(), startId), Long.class);
|
|
if (CollectionUtils.isEmpty(tempIds)) {
|
|
if (CollectionUtils.isEmpty(tempIds)) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -95,9 +105,9 @@ public class ClearExpireDataJobHandler {
|
|
this.doClear(tempIds);
|
|
this.doClear(tempIds);
|
|
|
|
|
|
finishCount += tempIds.size();
|
|
finishCount += tempIds.size();
|
|
- float finishRate = finishCount * 100f / totalSize;
|
|
|
|
|
|
+ float finishRate = finishCount * 100f / totalCount;
|
|
long cost = (System.currentTimeMillis() - startTime) / 1000L;
|
|
long cost = (System.currentTimeMillis() - startTime) / 1000L;
|
|
- log.warn("totalSize:{} finishCount:{} finishRate:{}% startId:{} cost:{}s", totalSize, finishCount, finishRate, startId, cost);
|
|
|
|
|
|
+ log.warn("totalSize:{} finishCount:{} finishRate:{}% startId:{} cost:{}s", totalCount, finishCount, finishRate, startId, cost);
|
|
startId = tempIds.get(tempIds.size() - 1);
|
|
startId = tempIds.get(tempIds.size() - 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -120,6 +130,7 @@ public class ClearExpireDataJobHandler {
|
|
|
|
|
|
private void batchDelete(String deleteSql, List<Long> tempIds) {
|
|
private void batchDelete(String deleteSql, List<Long> tempIds) {
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
|
|
+
|
|
jdbcTemplate.batchUpdate(deleteSql, new BatchPreparedStatementSetter() {
|
|
jdbcTemplate.batchUpdate(deleteSql, new BatchPreparedStatementSetter() {
|
|
@Override
|
|
@Override
|
|
public void setValues(PreparedStatement ps, int n) throws SQLException {
|
|
public void setValues(PreparedStatement ps, int n) throws SQLException {
|