Skip to content

Commit 47ea790

Browse files
committed
short-circuit if count=0
1 parent 532d1c5 commit 47ea790

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

sqlmesh/core/state_sync/db/snapshot.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,29 +222,32 @@ def _get_expired_snapshots(
222222
ignore_ttl: bool = False,
223223
) -> t.Tuple[t.Set[SnapshotId], t.List[SnapshotTableCleanupTask]]:
224224
expired_query = exp.select("name", "identifier", "version").from_(self.snapshots_table)
225+
expired_record_count = 0
225226

226227
if not ignore_ttl:
227228
expired_query = expired_query.where(
228229
(exp.column("updated_ts") + exp.column("ttl_ms")) <= current_ts
229230
)
230231

232+
if result := fetchone(
233+
self.engine_adapter, exp.select("count(*)").from_(expired_query.subquery())
234+
):
235+
expired_record_count = result[0]
236+
231237
# we need to include views even if they havent expired in case one depends on a table or view that /has/ expired
232238
# but we only need to do this if there are expired objects to begin with
233-
234-
expired_record_count = fetchone(
235-
self.engine_adapter, exp.select("count(*)").from_(expired_query.subquery())
236-
)
237-
if expired_record_count and expired_record_count[0]:
239+
if expired_record_count > 0:
238240
expired_query = t.cast(
239241
exp.Select, expired_query.or_(exp.column("kind_name").eq(ModelKindName.VIEW))
240242
)
241243

242-
candidates = {
243-
SnapshotId(name=name, identifier=identifier): SnapshotNameVersion(
244-
name=name, version=version
245-
)
246-
for name, identifier, version in fetchall(self.engine_adapter, expired_query)
247-
}
244+
if ignore_ttl or expired_record_count > 0:
245+
candidates = {
246+
SnapshotId(name=name, identifier=identifier): SnapshotNameVersion(
247+
name=name, version=version
248+
)
249+
for name, identifier, version in fetchall(self.engine_adapter, expired_query)
250+
}
248251

249252
if not candidates:
250253
return set(), []

0 commit comments

Comments
 (0)