perf: 优化 MySQL 查询并接入 pytest
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user