From eadbe00b354c8c99b2397ef078231a436d6451df Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 13 Jun 2026 17:47:49 +0000 Subject: [PATCH] Optimize updateCellBatch to eliminate N+1 prepared statement overhead --- website/public/sqlite-viewer/worker.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/website/public/sqlite-viewer/worker.js b/website/public/sqlite-viewer/worker.js index 7cfd352..85c51ba 100644 --- a/website/public/sqlite-viewer/worker.js +++ b/website/public/sqlite-viewer/worker.js @@ -750,9 +750,29 @@ async function updateCellBatch(table, updates) { db.run('BEGIN TRANSACTION'); try { + const safeTable = table.replace(/"/g, '""'); + + // Group updates by column to minimize statement preparation overhead + const byColumn = {}; for (const update of updates) { - await updateCell(table, update.rowId, update.column, update.value); + const col = update.column; + if (!byColumn[col]) byColumn[col] = []; + byColumn[col].push(update); + } + + for (const column of Object.keys(byColumn)) { + const safeColumn = column.replace(/"/g, '""'); + const stmt = db.prepare(`UPDATE "${safeTable}" SET "${safeColumn}" = ? WHERE rowid = ?`); + + try { + for (const update of byColumn[column]) { + stmt.run([update.value, update.rowId]); + } + } finally { + stmt.free(); + } } + db.run('COMMIT'); } catch (e) { db.run('ROLLBACK');