Skip to content

Commit 4e071d5

Browse files
committed
perf(database): normalize logged SQL without extracting tables
Call the existing normalization helper directly for base query-log records. Statement type and table extraction remain in the optional analysis path, where their results are used. Verify the normalized SQL actually persisted by the logger. Complete in-memory SQLite read-and-log workload on a shared M3 Pro laptop, Bun 1.3.14: 23.581 to 22.3115 us per operation (5.38% less time), three alternating pairs, all 3000 records verified each round. No disk HTTP throughput claim. Passed 1377 router/database tests, 366 unit tests, lint, typecheck, and both review axes.
1 parent e888d06 commit 4e071d5

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

storage/framework/core/database/src/query-logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { memoryUsage } from 'node:process'
44
import { AsyncLocalStorage } from 'node:async_hooks'
55
import { config } from '@stacksjs/config'
66
import { log } from '@stacksjs/logging'
7-
import { parseQuery } from './query-parser'
7+
import { normalizeQuery, parseQuery } from './query-parser'
88
import { db } from './utils'
99

1010
/**
@@ -225,7 +225,7 @@ async function createQueryLogRecord(
225225
const connection = config.database.default || 'unknown'
226226

227227
// Get normalized query (replace specific values with placeholders)
228-
const normalizedQuery = parseQuery(query).normalized || query
228+
const normalizedQuery = normalizeQuery(query) || query
229229

230230
// Extract stack trace and caller information
231231
const { trace, caller } = extractTraceInfo()

storage/framework/core/database/src/query-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function extractTables(sql: string, type: string): string[] {
129129
/**
130130
* Normalize a SQL query by replacing literal values with placeholders
131131
*/
132-
function normalizeQuery(sql: string): string {
132+
export function normalizeQuery(sql: string): string {
133133
try {
134134
let normalizedSql = sql
135135

storage/framework/core/database/tests/fixtures/query-logger-dispatch.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ try {
104104
await Bun.sleep(0)
105105
await createLogTable()
106106
await queryWithDiagnostics('query_logger_after_failed_store')
107+
108+
const { logQuery } = await import('../../src/query-logger')
109+
const literalQuery = "SELECT 42 AS total, 'Ada' AS name, TRUE AS active, NULL AS missing FROM query_logger_fixture"
110+
await logQuery({ query: { sql: literalQuery }, queryDurationMillis: 1 })
111+
const normalized = await db.unsafe('SELECT normalized_query FROM query_logs WHERE query = ?', [literalQuery]).execute()
112+
if (normalized.length !== 1 || normalized[0]?.normalized_query !== 'SELECT ? AS total, ? AS name, ? AS active, ? AS missing FROM query_logger_fixture')
113+
throw new Error('Persisted query normalization changed')
107114
}
108115
console.log('query-logger-dispatch-ok')
109116
}

0 commit comments

Comments
 (0)