Skip to content

Commit 9afa7e9

Browse files
authored
Fix: Improve performance of get snapshots queries by using IN instead of AND/OR (#820)
1 parent 7089185 commit 9afa7e9

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

sqlmesh/core/state_sync/engine_adapter.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -542,46 +542,25 @@ def map_data_versions(
542542

543543
def _snapshot_id_filter(
544544
self, snapshot_ids: t.Iterable[SnapshotIdLike]
545-
) -> t.Union[exp.Or, exp.Boolean]:
545+
) -> t.Union[exp.In, exp.Boolean]:
546546
if not snapshot_ids:
547547
return exp.false()
548548

549-
return exp.or_(
550-
*(
551-
exp.and_(
552-
exp.EQ(
553-
this=exp.to_column("name"),
554-
expression=exp.Literal.string(snapshot_id.name),
555-
),
556-
exp.EQ(
557-
this=exp.to_column("identifier"),
558-
expression=exp.Literal.string(snapshot_id.identifier),
559-
),
560-
)
561-
for snapshot_id in snapshot_ids
562-
)
549+
return t.cast(exp.Tuple, exp.convert((exp.column("name"), exp.column("identifier")))).isin(
550+
*[(snapshot_id.name, snapshot_id.identifier) for snapshot_id in snapshot_ids]
563551
)
564552

565553
def _snapshot_name_version_filter(
566554
self, snapshot_name_versions: t.Iterable[SnapshotNameVersionLike]
567-
) -> t.Union[exp.Or, exp.Boolean]:
555+
) -> t.Union[exp.In, exp.Boolean]:
568556
if not snapshot_name_versions:
569557
return exp.false()
570558

571-
return exp.or_(
572-
*(
573-
exp.and_(
574-
exp.EQ(
575-
this=exp.to_column("name"),
576-
expression=exp.Literal.string(snapshot_name_version.name),
577-
),
578-
exp.EQ(
579-
this=exp.to_column("version"),
580-
expression=exp.Literal.string(snapshot_name_version.version),
581-
),
582-
)
559+
return t.cast(exp.Tuple, exp.convert((exp.column("name"), exp.column("version")))).isin(
560+
*[
561+
(snapshot_name_version.name, snapshot_name_version.version)
583562
for snapshot_name_version in snapshot_name_versions
584-
)
563+
]
585564
)
586565

587566
@contextlib.contextmanager

0 commit comments

Comments
 (0)