perf(db): 增加查询索引并提升 redo log 容量

This commit is contained in:
yml
2026-11-20 22:26:10 +08:00
parent 409e22be90
commit 2eff8e1124
8 changed files with 100 additions and 33 deletions
+28 -31
View File
@@ -291,6 +291,26 @@ def _visible_tasks_query(db: Session, current: User):
raise HTTPException(status_code=403, detail="无权查看斗鱼任务")
def _split_task_scope_query(query, handbook_scope: str | None) -> list:
"""Build index-friendly branches for a workbench scope.
Legacy rows map to the requested workbench by task type. A single
``task_type IN (...) ORDER BY id DESC`` query makes MySQL scan the broad
handbook-scope index, so use one range per task type instead.
"""
if not handbook_scope:
return [query]
branches = [query.filter(DouyuTask.handbook_scope == handbook_scope)]
branches.extend(
query.filter(
DouyuTask.handbook_scope == "legacy",
DouyuTask.task_type == task_type,
)
for task_type in DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]
)
return branches
def _list_visible_tasks_for_support(
db: Session,
current: User,
@@ -305,25 +325,13 @@ def _list_visible_tasks_for_support(
(DouyuTask.account_assigned_to == current.id,),
(DouyuTask.created_by == current.id,),
]
scope_filters: list[tuple[Any, ...]]
if handbook_scope:
scope_filters = [
(DouyuTask.handbook_scope == handbook_scope,),
(
DouyuTask.handbook_scope == "legacy",
DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]),
),
]
else:
scope_filters = [()]
take = offset + limit
task_ids: list[int] = []
for owner_filter in owner_filters:
for scope_filter in scope_filters:
query = db.query(DouyuTask.id).filter(*owner_filter, *scope_filter)
if batch_id:
query = query.filter(DouyuTask.batch_id == batch_id)
base_query = db.query(DouyuTask.id).filter(*owner_filter)
if batch_id:
base_query = base_query.filter(DouyuTask.batch_id == batch_id)
for query in _split_task_scope_query(base_query, handbook_scope):
rows = query.order_by(DouyuTask.id.desc()).limit(take).all()
task_ids.extend(int(row[0]) for row in rows)
@@ -359,23 +367,12 @@ def _list_visible_tasks_for_admin(
offset: int = 0,
) -> list[DouyuTask]:
"""管理员任务列表按作用域拆索引分支,避免 OR + JOIN 触发大排序。"""
if handbook_scope:
scope_filters: list[tuple[Any, ...]] = [
(DouyuTask.handbook_scope == handbook_scope,),
(
DouyuTask.handbook_scope == "legacy",
DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]),
),
]
else:
scope_filters = [()]
take = offset + limit
task_ids: list[int] = []
for scope_filter in scope_filters:
query = db.query(DouyuTask.id).filter(*scope_filter)
if batch_id:
query = query.filter(DouyuTask.batch_id == batch_id)
base_query = db.query(DouyuTask.id)
if batch_id:
base_query = base_query.filter(DouyuTask.batch_id == batch_id)
for query in _split_task_scope_query(base_query, handbook_scope):
rows = query.order_by(DouyuTask.id.desc()).limit(take).all()
task_ids.extend(int(row[0]) for row in rows)