M8 P2 | getSummary/getTrend 优化:使用 findByDateRange 替代 findAll 全表扫描 #159

Closed
opened 2026-06-08 21:02:07 +08:00 by wangdl · 1 comment
Owner

问题

ReadingController.getSummary()getTrend() 使用 LearningActivityRepository.findAll(userId) 拉取用户全部 DailyLearningActivity 记录,不做日期过滤。

  • getSummary:计算 totalSeconds/weekSeconds/todaySeconds,只需近 7 天 + 全量聚合
  • getTrend:只需要最近 N 天(N ≤ 90)

用户数据量大时性能差。

建议

  1. getTrend 改用 findByDateRange(userId, from, to) 只查询需要的天数
  2. getSummary 的总量(totalSeconds/activeDays)可缓存或用 aggregate 查询

代码位置

  • reading.controller.ts:97getSummary 调用 findAll
  • reading.controller.ts:143getTrend 调用 findAll

审计来源

Batch D 审查 F1。

## 问题 `ReadingController.getSummary()` 和 `getTrend()` 使用 `LearningActivityRepository.findAll(userId)` 拉取用户全部 DailyLearningActivity 记录,不做日期过滤。 - `getSummary`:计算 totalSeconds/weekSeconds/todaySeconds,只需近 7 天 + 全量聚合 - `getTrend`:只需要最近 N 天(N ≤ 90) 用户数据量大时性能差。 ## 建议 1. `getTrend` 改用 `findByDateRange(userId, from, to)` 只查询需要的天数 2. `getSummary` 的总量(totalSeconds/activeDays)可缓存或用 aggregate 查询 ## 代码位置 - `reading.controller.ts:97` — `getSummary` 调用 `findAll` - `reading.controller.ts:143` — `getTrend` 调用 `findAll` ## 审计来源 Batch D 审查 F1。
Author
Owner

修复

getTrend 改用 findByDateRange(userId, from, now),只查需要的天数。getSummary 用 90 天范围查询 week/today + findAll 获取 total。

## 修复 `getTrend` 改用 `findByDateRange(userId, from, now)`,只查需要的天数。`getSummary` 用 90 天范围查询 week/today + `findAll` 获取 total。
Sign in to join this conversation.
No description provided.