Skip to content

Commit cafe8fe

Browse files
waleedlatif1claude
andcommitted
perf(log): use startedAt index for cleanup query filter
Switch cleanup WHERE clause from createdAt to startedAt to leverage the existing composite index (workspaceId, startedAt), converting a full table scan to an index range scan. Also remove explanatory comment. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9750eaa commit cafe8fe

File tree

1 file changed

+1
-8
lines changed

1 file changed

+1
-8
lines changed

apps/sim/app/api/logs/cleanup/route.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ export async function GET(request: NextRequest) {
2626
const retentionDate = new Date()
2727
retentionDate.setDate(retentionDate.getDate() - Number(env.FREE_PLAN_LOG_RETENTION_DAYS || '7'))
2828

29-
/**
30-
* Subquery: workspace IDs whose billed account user has no active paid
31-
* subscription. Kept as a subquery (not materialized into JS) so the
32-
* generated SQL is `WHERE workspace_id IN (SELECT ...)` — this avoids
33-
* PostgreSQL's 65535 bind-parameter limit that was breaking cleanup once
34-
* the free-user count grew beyond ~65k.
35-
*/
3629
const freeWorkspacesSubquery = db
3730
.select({ id: workspace.id })
3831
.from(workspace)
@@ -94,7 +87,7 @@ export async function GET(request: NextRequest) {
9487
.where(
9588
and(
9689
inArray(workflowExecutionLogs.workspaceId, freeWorkspacesSubquery),
97-
lt(workflowExecutionLogs.createdAt, retentionDate)
90+
lt(workflowExecutionLogs.startedAt, retentionDate)
9891
)
9992
)
10093
.limit(BATCH_SIZE)

0 commit comments

Comments
 (0)