perf: optimize vertex build query for recent records (#5301)
* Optimize query to fetch latest vertex builds. * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
82080ebade
commit
6e162b5a73
1 changed files with 10 additions and 1 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlmodel import col, delete, select
|
||||
from sqlmodel import col, delete, func, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from langflow.services.database.models.vertex_builds.model import VertexBuildBase, VertexBuildTable
|
||||
|
|
@ -10,8 +10,17 @@ from langflow.services.database.models.vertex_builds.model import VertexBuildBas
|
|||
async def get_vertex_builds_by_flow_id(
|
||||
db: AsyncSession, flow_id: UUID, limit: int | None = 1000
|
||||
) -> list[VertexBuildTable]:
|
||||
subquery = (
|
||||
select(VertexBuildTable.id, func.max(VertexBuildTable.timestamp).label("max_timestamp"))
|
||||
.where(VertexBuildTable.flow_id == flow_id)
|
||||
.group_by(VertexBuildTable.id)
|
||||
.subquery()
|
||||
)
|
||||
stmt = (
|
||||
select(VertexBuildTable)
|
||||
.join(
|
||||
subquery, (VertexBuildTable.id == subquery.c.id) & (VertexBuildTable.timestamp == subquery.c.max_timestamp)
|
||||
)
|
||||
.where(VertexBuildTable.flow_id == flow_id)
|
||||
.order_by(col(VertexBuildTable.timestamp))
|
||||
.limit(limit)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue