Skip to content

perf(backend): 14 端点缓存+threadpool + N+1 修复 + 性能索引 + system.py 计数 - #43

Merged
Color2333 merged 1 commit into
mainfrom
perf/pr3-backend
Jul 17, 2026
Merged

perf(backend): 14 端点缓存+threadpool + N+1 修复 + 性能索引 + system.py 计数#43
Color2333 merged 1 commit into
mainfrom
perf/pr3-backend

Conversation

@Color2333

Copy link
Copy Markdown
Owner

PR3 — 后端 API 性能(中风险)

graph.py(14 端点补缓存 + threadpool)

  • overview/bridges/frontier:补 run_in_threadpool(此前 sync+cache 仍阻塞线程池槽)
  • cocitation-clusters O(N²):300s 缓存
  • citation-tree/citation-network/topic/similar-via-citation:60s 缓存
  • timeline/quality:120s 缓存
  • weekly_evolution/survey(LLM):300s 缓存
  • research-gaps(最重 2×timeline+2×LLM):600s 缓存
  • citation-detail/topic_deep_trace/auto_link:含外部 API 副作用,不加缓存(如实标注)

N+1 修复

  • papers.similar:循环 get_by_id 改 list_by_ids(5-20 次→1 次)
  • topics.list_topics:每主题 2 次查询改 2 次批量聚合

性能索引迁移(d4e5f6a7b8c9)

  • papers.publication_date + collection_actions.created_at 索引

system.py

  • 拉 200 行 ORM 仅为 len() 改 count_all()

验证

  • pytest 13 通过;alembic 迁移链完整;5 PR 合并零冲突

PR3 — 后端 API 性能

graph.py(14 端点补缓存 + threadpool,内联 cluster_map 模式):
- overview/bridges/frontier:补 run_in_threadpool(此前 sync+cache 仍阻塞线程池槽)
- cocitation-clusters O(N²) 配对:加 300s 缓存 + threadpool
- citation-tree/citation-network/topic/similar-via-citation:加 60s 缓存 + threadpool
- timeline/quality:加 120s 缓存 + threadpool
- weekly_evolution/survey(含 LLM):加 300s 缓存 + threadpool
- research-gaps(最重,2×timeline+2×LLM):加 600s 缓存 + threadpool
- citation-detail/topic_deep_trace/auto_link:含外部 API 副作用,不加缓存(如实标注)

N+1 修复:
- papers.similar:循环 get_by_id 改 list_by_ids 一次查完 + 按 ids 顺序构建(5-20 次查询→1 次)
- topics.list_topics:每主题 2 次查询改 2 次批量聚合(paper_count GROUP BY + 最近 action 按 topic 分组取首条)

性能索引迁移(alembic d4e5f6a7b8c9):
- papers.publication_date 索引(list_paginated 排序/frontier 过滤/年份分组,此前全表扫)
- collection_actions.created_at 索引(ORDER BY created_at DESC/每主题最近行动查询)
- models.py 对应字段加 index=True 声明
- CREATE INDEX IF NOT EXISTS 幂等(PG/SQLite 通用)

system.py:
- 拉 200 行 ORM 仅为 len() 改 count_all() 一次 COUNT 查询
@github-actions

Copy link
Copy Markdown

🔍 OpenCode PR Review Required

这是一个受保护的分支,merge 前需要进行 code review。

请运行以下命令进行 OpenCode review:

/oc review https://github.com/Color2333/PaperMind/pull/$PR_NUM

或者在 PR 页面评论 /oc 来触发 OpenCode review。


This is an automated reminder from PR Review Gate.

@Color2333
Color2333 merged commit 4ea11e6 into main Jul 17, 2026
2 checks passed
@Color2333
Color2333 deleted the perf/pr3-backend branch July 17, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant