Skip to content

Commit

Permalink
Change lazy loads from joined to selectin (#8659)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin authored Feb 27, 2023
1 parent e39b8fe commit b14daea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/prefect/server/database/orm_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def result_artifact_id(cls):
def _result_artifact(cls):
return sa.orm.relationship(
"Artifact",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.result_artifact_id],
primaryjoin="Artifact.id==%s.result_artifact_id" % cls.__name__,
)
Expand Down Expand Up @@ -236,7 +236,7 @@ def result_artifact_id(cls):
def _result_artifact(cls):
return sa.orm.relationship(
"Artifact",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.result_artifact_id],
primaryjoin="Artifact.id==%s.result_artifact_id" % cls.__name__,
)
Expand Down Expand Up @@ -524,7 +524,7 @@ def work_queue_id(cls):
def _state(cls):
return sa.orm.relationship(
"FlowRunState",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.state_id],
primaryjoin="FlowRunState.id==%s.state_id" % cls.__name__,
)
Expand Down Expand Up @@ -582,7 +582,7 @@ def parent_task_run(cls):
def work_queue(cls):
return sa.orm.relationship(
"WorkQueue",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.work_queue_id],
)

Expand Down Expand Up @@ -700,7 +700,7 @@ def state_id(cls):
def _state(cls):
return sa.orm.relationship(
"TaskRunState",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.state_id],
primaryjoin="TaskRunState.id==%s.state_id" % cls.__name__,
)
Expand Down Expand Up @@ -868,7 +868,7 @@ def flow(cls):
@declared_attr
def work_queue(cls):
return sa.orm.relationship(
"WorkQueue", lazy="joined", foreign_keys=[cls.work_queue_id]
"WorkQueue", lazy="selectin", foreign_keys=[cls.work_queue_id]
)

@declared_attr
Expand Down Expand Up @@ -959,7 +959,7 @@ def block_type_id(cls):

@declared_attr
def block_type(cls):
return sa.orm.relationship("BlockType", lazy="joined")
return sa.orm.relationship("BlockType", lazy="selectin")

@declared_attr
def __table_args__(cls):
Expand Down Expand Up @@ -1011,7 +1011,7 @@ def block_type_id(cls):

@declared_attr
def block_type(cls):
return sa.orm.relationship("BlockType", lazy="joined")
return sa.orm.relationship("BlockType", lazy="selectin")

@declared_attr
def block_schema_id(cls):
Expand All @@ -1023,7 +1023,7 @@ def block_schema_id(cls):

@declared_attr
def block_schema(cls):
return sa.orm.relationship("BlockSchema", lazy="joined")
return sa.orm.relationship("BlockSchema", lazy="selectin")

@declared_attr
def __table_args__(cls):
Expand Down Expand Up @@ -1142,7 +1142,7 @@ def work_pool_id(cls):
def work_pool(cls):
return sa.orm.relationship(
"WorkPool",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.work_pool_id],
)

Expand Down Expand Up @@ -1240,7 +1240,7 @@ def block_document_id(cls):
def block_document(cls):
return sa.orm.relationship(
"BlockDocument",
lazy="joined",
lazy="selectin",
foreign_keys=[cls.block_document_id],
)

Expand Down
12 changes: 8 additions & 4 deletions src/prefect/server/models/flow_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sqlalchemy as sa
from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload, load_only
from sqlalchemy.orm import load_only, selectinload

import prefect.server.models as models
import prefect.server.schemas as schemas
Expand Down Expand Up @@ -93,7 +93,7 @@ async def create_flow_run(
.limit(1)
.execution_options(populate_existing=True)
.options(
joinedload(db.FlowRun.work_queue).joinedload(db.WorkQueue.work_pool)
selectinload(db.FlowRun.work_queue).selectinload(db.WorkQueue.work_pool)
)
)
result = await session.execute(query)
Expand Down Expand Up @@ -158,7 +158,9 @@ async def read_flow_run(
result = await session.execute(
sa.select(db.FlowRun)
.where(db.FlowRun.id == flow_run_id)
.options(joinedload(db.FlowRun.work_queue).joinedload(db.WorkQueue.work_pool))
.options(
selectinload(db.FlowRun.work_queue).selectinload(db.WorkQueue.work_pool)
)
)
return result.scalar()

Expand Down Expand Up @@ -266,7 +268,9 @@ async def read_flow_runs(
query = (
select(db.FlowRun)
.order_by(sort.as_sql_sort(db))
.options(joinedload(db.FlowRun.work_queue).joinedload(db.WorkQueue.work_pool))
.options(
selectinload(db.FlowRun.work_queue).selectinload(db.WorkQueue.work_pool)
)
)

if columns:
Expand Down

0 comments on commit b14daea

Please sign in to comment.