MCPcopy Create free account
hub / github.com/TanStack/db / trackQuery

Function trackQuery

packages/db/src/indexes/index-registry.ts:96–131  ·  view source on GitHub ↗
(
  collectionId: string,
  fieldPath: Array<string>,
  executionTimeMs: number,
)

Source from the content-addressed store, hash-verified

94 * Track a query for dev mode analysis
95 */
96export function trackQuery(
97 collectionId: string,
98 fieldPath: Array<string>,
99 executionTimeMs: number,
100): void {
101 if (!isDevModeEnabled()) return
102
103 const key = `${collectionId}:${fieldPath.join(`.`)}`
104 const existing = queryPatterns.get(key)
105
106 if (existing) {
107 existing.queryCount++
108 existing.totalTimeMs += executionTimeMs
109 existing.avgTimeMs = existing.totalTimeMs / existing.queryCount
110 } else {
111 queryPatterns.set(key, {
112 fieldPath,
113 queryCount: 1,
114 totalTimeMs: executionTimeMs,
115 avgTimeMs: executionTimeMs,
116 })
117 }
118
119 // Check if we should suggest an index
120 const pattern = queryPatterns.get(key)!
121 if (pattern.avgTimeMs > devModeConfig.slowQueryThresholdMs) {
122 emitIndexSuggestion({
123 type: `slow-query`,
124 collectionId,
125 fieldPath,
126 message: `Queries on "${fieldPath.join(`.`)}" are slow (avg ${pattern.avgTimeMs.toFixed(1)}ms). Consider adding an index.`,
127 queryTimeMs: pattern.avgTimeMs,
128 queryCount: pattern.queryCount,
129 })
130 }
131}
132
133/**
134 * Check collection size and suggest index if needed (dev mode)

Callers

nothing calls this directly

Calls 5

isDevModeEnabledFunction · 0.85
emitIndexSuggestionFunction · 0.85
joinMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected