-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryOptimizer.ts
More file actions
437 lines (388 loc) · 10.9 KB
/
QueryOptimizer.ts
File metadata and controls
437 lines (388 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* Query Optimizer
*
* Provides optimized query helpers and utilities for improved database performance.
* Focuses on FTS5 search optimization, query plan analysis, and index usage.
*
* Features:
* - Optimized FTS5 full-text search with BM25 ranking
* - Query plan analysis and suggestions
* - Automatic index usage detection
* - Query result caching
* - Search highlighting
*/
import type Database from 'better-sqlite3';
export interface SearchOptions {
/** Maximum number of results (default: 50) */
limit?: number;
/** Offset for pagination (default: 0) */
offset?: number;
/** Minimum relevance score (default: 0) */
minScore?: number;
/** Include search highlights (default: false) */
highlight?: boolean;
/** Filter by source connector */
sourceConnector?: string;
/** Filter by tags */
tags?: string[];
/** Date range filter */
dateRange?: {
from?: number;
to?: number;
};
}
export interface SearchResult {
id: string;
title: string;
body: string;
score: number;
highlights?: {
title?: string;
body?: string;
};
source_connector?: string;
created_at?: number;
updated_at?: number;
}
export interface QueryPlan {
query: string;
usesIndex: boolean;
indexName?: string;
estimatedRows: number;
suggestions: string[];
}
/**
* Optimized query helpers for SQLite database
*/
export class QueryOptimizer {
private queryCache = new Map<string, { result: unknown; timestamp: number }>();
private cacheTTL = 60000; // 1 minute cache TTL
constructor(private db: Database.Database) {}
/**
* Optimized full-text search with BM25 ranking and filtering
*/
searchNotes(
searchTerm: string,
options: SearchOptions = {}
): SearchResult[] {
const {
limit = 50,
offset = 0,
minScore = 0,
highlight = false,
sourceConnector,
tags,
dateRange,
} = options;
// Build FTS5 query with proper syntax
const ftsQuery = this.buildFTS5Query(searchTerm);
// Build WHERE conditions
const conditions: string[] = ['NoteSearch MATCH ?'];
const params: unknown[] = [ftsQuery];
if (minScore > 0) {
conditions.push('rank > ?');
params.push(-minScore); // FTS5 rank is negative
}
if (sourceConnector) {
conditions.push('n.source_connector = ?');
params.push(sourceConnector);
}
if (dateRange) {
if (dateRange.from) {
conditions.push('n.created_at >= ?');
params.push(dateRange.from);
}
if (dateRange.to) {
conditions.push('n.created_at <= ?');
params.push(dateRange.to);
}
}
// Build tag filter using EXISTS subquery for better performance
let tagJoin = '';
if (tags && tags.length > 0) {
tagJoin = `
AND EXISTS (
SELECT 1 FROM NoteTag nt
JOIN Tag t ON nt.tag_id = t.id
WHERE nt.note_id = n.id
AND t.name IN (${tags.map(() => '?').join(',')})
)
`;
params.push(...tags);
}
// Build the query with optimized FTS5 ranking
const sql = `
SELECT
n.id,
n.title,
n.body,
n.source_connector,
n.created_at,
n.updated_at,
-rank AS score
${highlight ? ', highlight(NoteSearch, 1, "<mark>", "</mark>") AS title_highlight' : ''}
${highlight ? ', snippet(NoteSearch, 2, "<mark>", "</mark>", "...", 32) AS body_highlight' : ''}
FROM NoteSearch
JOIN Note n ON NoteSearch.note_id = n.id
WHERE ${conditions.join(' AND ')}
AND n.deleted_at IS NULL
${tagJoin}
ORDER BY rank
LIMIT ? OFFSET ?
`;
params.push(limit, offset);
const stmt = this.db.prepare(sql);
const results = stmt.all(...params) as Array<{
id: string;
title: string;
body: string;
score: number;
source_connector?: string;
created_at?: number;
updated_at?: number;
title_highlight?: string;
body_highlight?: string;
}>;
return results.map(row => ({
id: row.id,
title: row.title,
body: row.body,
score: row.score,
source_connector: row.source_connector,
created_at: row.created_at,
updated_at: row.updated_at,
highlights: highlight
? {
title: row.title_highlight,
body: row.body_highlight,
}
: undefined,
}));
}
/**
* Fuzzy search using trigram matching (for typo tolerance)
*/
fuzzySearchNotes(
searchTerm: string,
options: SearchOptions = {}
): SearchResult[] {
const { limit = 50, offset = 0 } = options;
// Use LIKE with wildcards for fuzzy matching
const fuzzyTerm = `%${searchTerm.split('').join('%')}%`;
const sql = `
SELECT
id,
title,
body,
source_connector,
created_at,
updated_at,
(
CASE
WHEN title LIKE ? THEN 100
WHEN title LIKE ? THEN 50
WHEN body LIKE ? THEN 25
ELSE 10
END
) AS score
FROM Note
WHERE deleted_at IS NULL
AND (title LIKE ? OR body LIKE ?)
ORDER BY score DESC, updated_at DESC
LIMIT ? OFFSET ?
`;
const exactMatch = `%${searchTerm}%`;
const stmt = this.db.prepare(sql);
const results = stmt.all(
exactMatch,
fuzzyTerm,
exactMatch,
fuzzyTerm,
fuzzyTerm,
limit,
offset
) as Array<{
id: string;
title: string;
body: string;
score: number;
source_connector?: string;
created_at?: number;
updated_at?: number;
}>;
return results;
}
/**
* Search by tags with relevance ranking
*/
searchByTags(
tags: string[],
options: { limit?: number; offset?: number } = {}
): SearchResult[] {
const { limit = 50, offset = 0 } = options;
if (tags.length === 0) {
return [];
}
// Calculate relevance based on number of matching tags
const sql = `
WITH TagMatches AS (
SELECT
nt.note_id,
COUNT(*) AS tag_count
FROM NoteTag nt
JOIN Tag t ON nt.tag_id = t.id
WHERE t.name IN (${tags.map(() => '?').join(',')})
GROUP BY nt.note_id
)
SELECT
n.id,
n.title,
n.body,
n.source_connector,
n.created_at,
n.updated_at,
(tm.tag_count * 100.0 / ?) AS score
FROM Note n
JOIN TagMatches tm ON n.id = tm.note_id
WHERE n.deleted_at IS NULL
ORDER BY tm.tag_count DESC, n.updated_at DESC
LIMIT ? OFFSET ?
`;
const stmt = this.db.prepare(sql);
const results = stmt.all(...tags, tags.length, limit, offset) as Array<{
id: string;
title: string;
body: string;
score: number;
source_connector?: string;
created_at?: number;
updated_at?: number;
}>;
return results;
}
/**
* Analyze a query plan to check index usage
*/
analyzeQuery(sql: string, params: unknown[] = []): QueryPlan {
const explainStmt = this.db.prepare(`EXPLAIN QUERY PLAN ${sql}`);
const plan = explainStmt.all(...params) as Array<{
detail: string;
}>;
const planText = plan.map(row => row.detail).join('\n');
const usesIndex = planText.includes('USING INDEX') || planText.includes('SEARCH TABLE');
const indexMatch = planText.match(/USING INDEX (\w+)/);
const suggestions: string[] = [];
// Analyze for optimization opportunities
if (planText.includes('SCAN TABLE')) {
suggestions.push('Query performs a full table scan. Consider adding an index.');
}
if (planText.includes('TEMP B-TREE')) {
suggestions.push('Query uses temporary B-tree for sorting. Consider adding an index on ORDER BY columns.');
}
if (!usesIndex && sql.includes('WHERE')) {
suggestions.push('WHERE clause does not use an index. Consider adding an index on filter columns.');
}
return {
query: sql,
usesIndex,
indexName: indexMatch?.[1],
estimatedRows: this.estimateRows(planText),
suggestions,
};
}
/**
* Optimize FTS5 by rebuilding the index
*/
optimizeFTS5(): void {
// Rebuild FTS5 index for better performance
this.db.prepare("INSERT INTO NoteSearch(NoteSearch) VALUES('rebuild')").run();
// Optimize the index structure
this.db.prepare("INSERT INTO NoteSearch(NoteSearch) VALUES('optimize')").run();
}
/**
* Get statistics about the FTS5 index
*/
getFTS5Stats(): {
totalDocs: number;
totalTokens: number;
avgTokensPerDoc: number;
} {
const stats = this.db.prepare(`
SELECT
(SELECT COUNT(*) FROM NoteSearch) AS totalDocs,
(SELECT SUM(length(title) + length(body)) FROM Note) AS totalTokens
`).get() as { totalDocs: number; totalTokens: number };
return {
totalDocs: stats.totalDocs,
totalTokens: stats.totalTokens,
avgTokensPerDoc: stats.totalDocs > 0
? stats.totalTokens / stats.totalDocs
: 0,
};
}
/**
* Clear the query cache
*/
clearCache(): void {
this.queryCache.clear();
}
/**
* Get cached query result if available and not expired
* Currently unused but reserved for future query caching feature
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
private getCached(key: string): unknown | null {
const cached = this.queryCache.get(key);
if (!cached) return null;
const now = Date.now();
if (now - cached.timestamp > this.cacheTTL) {
this.queryCache.delete(key);
return null;
}
return cached.result;
}
/**
* Cache a query result
* Currently unused but reserved for future query caching feature
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
private setCache(key: string, result: unknown): void {
this.queryCache.set(key, {
result,
timestamp: Date.now(),
});
// Limit cache size
if (this.queryCache.size > 100) {
const firstKey = this.queryCache.keys().next().value;
if (firstKey) {
this.queryCache.delete(firstKey);
}
}
}
/**
* Build an optimized FTS5 query from a search term
*/
private buildFTS5Query(searchTerm: string): string {
// Remove special characters that could break FTS5
let cleaned = searchTerm.trim();
// Handle quoted phrases
if (cleaned.startsWith('"') && cleaned.endsWith('"')) {
return cleaned;
}
// Split into words and apply boolean operators
const words = cleaned.split(/\s+/).filter(w => w.length > 0);
if (words.length === 1) {
// Single word: use prefix matching for better results
return `"${words[0]}"*`;
}
// Multiple words: use AND operator with prefix matching
return words.map(w => `"${w.replace(/"/g, '""')}"*`).join(' AND ');
}
/**
* Estimate number of rows from query plan
*/
private estimateRows(planText: string): number {
const rowsMatch = planText.match(/\(~(\d+) rows\)/);
return rowsMatch ? parseInt(rowsMatch[1], 10) : 0;
}
}