perf: 覆盖率查询改为按任务分组取最新

This commit is contained in:
2026-10-09 19:14:36 +08:00
parent 7408be26c0
commit 3dda26307d
6 changed files with 193 additions and 13 deletions
@@ -0,0 +1,19 @@
package com.guo.learningprogresstracker.dto;
import lombok.Data;
/**
* 任务最近一次回忆对比的覆盖率(复习-04)。
*
* <p>原先按任务取「最近一条回忆记录」是把该任务的全部回忆记录(含体积较大的
* {@code compare_result} JSON)拉回内存再逐条取第一条。改为在 SQL 侧按任务分组取最新,
* 返回行数等于任务数,不随复习次数增长。</p>
*/
@Data
public class TaskRecallRatio {
private String taskNum;
/** 最近一次回忆对比的覆盖率(0-1);记录里为空时按 0 处理 */
private Double recallRatio;
}
@@ -1,6 +1,7 @@
package com.guo.learningprogresstracker.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
import org.apache.ibatis.annotations.Mapper;
@@ -21,4 +22,13 @@ public interface ReviewRecallRecordMapper extends BaseMapper<ReviewRecallRecordE
* @param taskNums 需要汇总的任务编号;为空时返回空列表
*/
List<TaskReviewDuration> sumReviewDurationByTask(@Param("taskNums") Collection<String> taskNums);
/**
* 每个任务最近一条回忆记录的覆盖率(复习-04)。
* <p>语句见 {@code resources/mapper/ReviewRecallRecordMapper.xml};返回行数等于任务数,
* 不随复习次数增长,且不读取 {@code compare_result} 大字段。</p>
*
* @param taskNums 需要查询的任务编号;为空时返回空列表
*/
List<TaskRecallRatio> selectLatestRatioByTask(@Param("taskNums") Collection<String> taskNums);
}
@@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.dto.IdRange;
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
import com.guo.learningprogresstracker.dto.StudyReportDetail;
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
import com.guo.learningprogresstracker.dto.TaskContentCount;
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
import com.guo.learningprogresstracker.dto.TaskSessionStats;
@@ -172,14 +173,10 @@ public class ReviewServiceImpl implements ReviewService {
if (taskNums.isEmpty()) {
return Collections.emptyMap();
}
List<ReviewRecallRecordEntity> records = reviewRecallRecordMapper.selectList(
Wrappers.<ReviewRecallRecordEntity>lambdaQuery()
.in(ReviewRecallRecordEntity::getTaskNum, taskNums)
.orderByDesc(ReviewRecallRecordEntity::getCreatedTime));
// 复习-04:在 SQL 侧按任务分组取最新一条,只带回 任务编号 + 覆盖率
Map<String, Double> result = new HashMap<>();
for (ReviewRecallRecordEntity record : records) {
result.putIfAbsent(record.getTaskNum(),
record.getRecallRatio() == null ? 0D : record.getRecallRatio());
for (TaskRecallRatio row : reviewRecallRecordMapper.selectLatestRatioByTask(taskNums)) {
result.put(row.getTaskNum(), row.getRecallRatio() == null ? 0D : row.getRecallRatio());
}
return result;
}