mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +08:00
perf: Optimizing paging queries
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi import APIRouter
|
||||
from apps.settings.models.setting_models import term_model
|
||||
# from common.core.db import get_session
|
||||
from common.core.deps import SessionDep
|
||||
from common.core.pagination import Paginator
|
||||
from common.core.schemas import PaginatedResponse, PaginationParams
|
||||
|
||||
@@ -34,12 +34,13 @@ class Paginator:
|
||||
if value is not None:
|
||||
count_stmt = count_stmt.where(getattr(model, field) == value)
|
||||
|
||||
total = (await self.session.execute(count_stmt)).scalar_one()
|
||||
result = self.session.exec(count_stmt)
|
||||
total = result.first()
|
||||
|
||||
stmt = stmt.offset(offset).limit(size)
|
||||
|
||||
result = await self.session.execute(stmt)
|
||||
items = result.scalars().all()
|
||||
result = self.session.exec(stmt)
|
||||
items = result.all() # 移除 scalars() 调用,直接使用 all()
|
||||
|
||||
return items, total
|
||||
|
||||
|
||||
Reference in New Issue
Block a user