perf: 优化 MySQL 查询并接入 pytest

This commit is contained in:
yml2213
2026-08-30 18:26:54 +08:00
parent 1413b4f030
commit 9497e1ace6
10 changed files with 178 additions and 24 deletions
+11 -6
View File
@@ -126,7 +126,7 @@ def list_accounts(
total = None
if page is not None:
total = query.order_by(None).count()
total = query.order_by(None).with_entities(func.count(Account.id)).scalar() or 0
query = query.order_by(Account.id)
if page is not None:
query = query.offset((page - 1) * page_size).limit(page_size)
@@ -173,13 +173,18 @@ def accounts_summary(
else:
raise HTTPException(status_code=403, detail="无权查看账号")
total = query.count()
assigned_count = query.filter(Account.assigned_to.isnot(None)).count()
total = query.with_entities(func.count(Account.id)).scalar() or 0
assigned_count = (
query.filter(Account.assigned_to.isnot(None))
.with_entities(func.count(Account.id))
.scalar()
or 0
)
tag_count = (
query.filter(Account.tag != "", Account.tag.isnot(None))
.with_entities(Account.tag)
.distinct()
.count()
.with_entities(func.count(func.distinct(Account.tag)))
.scalar()
or 0
)
return {
"total": total,