Skip to content

Commit 1513862

Browse files
authored
improvement(performance): remove writes to workflow updated_at on position updates for blocks, edges, & subflows (#1531)
* improvement(performance): remove writes to workflow updated_at on position updates for blocks, edges, & subflows * update query pattern for logs routes
1 parent ace83eb commit 1513862

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,18 @@ export async function GET(request: NextRequest) {
9494
workflowUpdatedAt: workflow.updatedAt,
9595
}
9696

97+
// Optimized query: Start by filtering workflows in the workspace with user permissions
98+
// This ensures we scan only relevant logs instead of the entire table
9799
const baseQuery = db
98100
.select(selectColumns)
99101
.from(workflowExecutionLogs)
100-
.innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id))
102+
.innerJoin(
103+
workflow,
104+
and(
105+
eq(workflowExecutionLogs.workflowId, workflow.id),
106+
eq(workflow.workspaceId, params.workspaceId) // Filter workspace during join!
107+
)
108+
)
101109
.innerJoin(
102110
permissions,
103111
and(
@@ -107,8 +115,8 @@ export async function GET(request: NextRequest) {
107115
)
108116
)
109117

110-
// Build conditions for the joined query
111-
let conditions: SQL | undefined = eq(workflow.workspaceId, params.workspaceId)
118+
// Build additional conditions for the query
119+
let conditions: SQL | undefined
112120

113121
// Filter by level
114122
if (params.level && params.level !== 'all') {
@@ -176,11 +184,17 @@ export async function GET(request: NextRequest) {
176184
.limit(params.limit)
177185
.offset(params.offset)
178186

179-
// Get total count for pagination using the same join structure
187+
// Get total count for pagination using the same optimized join structure
180188
const countQuery = db
181189
.select({ count: sql<number>`count(*)` })
182190
.from(workflowExecutionLogs)
183-
.innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id))
191+
.innerJoin(
192+
workflow,
193+
and(
194+
eq(workflowExecutionLogs.workflowId, workflow.id),
195+
eq(workflow.workspaceId, params.workspaceId) // Same optimization
196+
)
197+
)
184198
.innerJoin(
185199
permissions,
186200
and(

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export async function GET(request: NextRequest) {
106106
const conditions = buildLogFilters(filters)
107107
const orderBy = getOrderBy(params.order)
108108

109-
// Build and execute query
109+
// Build and execute query - optimized to filter workspace during join
110110
const baseQuery = db
111111
.select({
112112
id: workflowExecutionLogs.id,
@@ -124,7 +124,13 @@ export async function GET(request: NextRequest) {
124124
workflowDescription: workflow.description,
125125
})
126126
.from(workflowExecutionLogs)
127-
.innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id))
127+
.innerJoin(
128+
workflow,
129+
and(
130+
eq(workflowExecutionLogs.workflowId, workflow.id),
131+
eq(workflow.workspaceId, params.workspaceId) // Filter workspace during join!
132+
)
133+
)
128134
.innerJoin(
129135
permissions,
130136
and(

apps/sim/socket-server/database/operations.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,8 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
168168
try {
169169
const { operation: op, target, payload, timestamp, userId } = operation
170170

171-
// Log high-frequency operations for monitoring
172-
if (op === 'update-position' && Math.random() < 0.01) {
173-
// Log 1% of position updates
174-
logger.debug('Socket DB operation sample:', {
175-
operation: op,
176-
target,
177-
workflowId: `${workflowId.substring(0, 8)}...`,
178-
})
179-
}
180-
181171
await db.transaction(async (tx) => {
182-
// Update the workflow's last modified timestamp first
183-
await tx
184-
.update(workflow)
185-
.set({ updatedAt: new Date(timestamp) })
186-
.where(eq(workflow.id, workflowId))
187-
188-
// Handle different operation types within the transaction
172+
// Handle different operation types within the transaction first
189173
switch (target) {
190174
case 'block':
191175
await handleBlockOperationTx(tx, workflowId, op, payload, userId)
@@ -202,6 +186,13 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
202186
default:
203187
throw new Error(`Unknown operation target: ${target}`)
204188
}
189+
190+
if (op !== 'update-position') {
191+
await tx
192+
.update(workflow)
193+
.set({ updatedAt: new Date(timestamp) })
194+
.where(eq(workflow.id, workflowId))
195+
}
205196
})
206197

207198
// Log slow operations for monitoring

0 commit comments

Comments
 (0)