Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

perf(analytics): covering index for the latency percentile queries (#1019)

suantea (asuan_suan)94ef82ae6435
diff --git a/server/src/__tests__/db/migrate/roundtrip.test.ts b/server/src/__tests__/db/migrate/roundtrip.test.ts
index 02bd05ee..0761bc52 100644
--- a/server/src/__tests__/db/migrate/roundtrip.test.ts
+++ b/server/src/__tests__/db/migrate/roundtrip.test.ts
@@ -34,6 +34,7 @@ const ATTEMPT_KEY_LABEL_FILENAME = '20260823_000003_attempt_key_label.ts';
const PROFILE_AUTO_INCLUDE_FILENAME = '20260823_000004_profile_auto_include.ts';
const IDEMPOTENCY_CLAIMS_FILENAME = '20260901_000001_idempotency_claims.ts';
const QUOTA_OBSERVATION_LOOKUP_FILENAME = '20260901_000002_quota_observation_lookup.ts';
+const ANALYTICS_LATENCY_PERCENTILE_INDEX_FILENAME = '20260902_000001_analytics_latency_percentile_index.ts';
interface SchemaRow {
type: string;
@@ -114,6 +115,7 @@ describe('migration round trip', () => {
PROFILE_AUTO_INCLUDE_FILENAME,
IDEMPOTENCY_CLAIMS_FILENAME,
QUOTA_OBSERVATION_LOOKUP_FILENAME,
+ ANALYTICS_LATENCY_PERCENTILE_INDEX_FILENAME,
]);
} finally {
db.close();
diff --git a/server/src/db/migrate/defaults.ts b/server/src/db/migrate/defaults.ts
index 6ccdc19e..ee9382d8 100644
--- a/server/src/db/migrate/defaults.ts
+++ b/server/src/db/migrate/defaults.ts
@@ -29,6 +29,7 @@ import * as attemptKeyLabel from '../migrations/20260823_000003_attempt_key_labe
import * as profileAutoInclude from '../migrations/20260823_000004_profile_auto_include.js';
import * as idempotencyClaims from '../migrations/20260901_000001_idempotency_claims.js';
import * as quotaObservationLookup from '../migrations/20260901_000002_quota_observation_lookup.js';
+import * as analyticsLatencyPercentileIndex from '../migrations/20260902_000001_analytics_latency_percentile_index.js';
export interface MigrationModule {
up(db: Db): void;
@@ -70,6 +71,7 @@ export const ATTEMPT_KEY_LABEL_FILENAME = '20260823_000003_attempt_key_label.ts'
export const PROFILE_AUTO_INCLUDE_FILENAME = '20260823_000004_profile_auto_include.ts';
export const IDEMPOTENCY_CLAIMS_FILENAME = '20260901_000001_idempotency_claims.ts';
export const QUOTA_OBSERVATION_LOOKUP_FILENAME = '20260901_000002_quota_observation_lookup.ts';
+export const ANALYTICS_LATENCY_PERCENTILE_INDEX_FILENAME = '20260902_000001_analytics_latency_percentile_index.ts';
export const DEFAULT_MIGRATIONS: readonly DefaultMigration[] = [
{ filename: LEGACY_BASELINE_FILENAME, module: legacyBaseline },
@@ -102,4 +104,5 @@ export const DEFAULT_MIGRATIONS: readonly DefaultMigration[] = [
{ filename: PROFILE_AUTO_INCLUDE_FILENAME, module: profileAutoInclude },
{ filename: IDEMPOTENCY_CLAIMS_FILENAME, module: idempotencyClaims },
{ filename: QUOTA_OBSERVATION_LOOKUP_FILENAME, module: quotaObservationLookup },
+ { filename: ANALYTICS_LATENCY_PERCENTILE_INDEX_FILENAME, module: analyticsLatencyPercentileIndex },
];
diff --git a/server/src/db/migrations/20260902_000001_analytics_latency_percentile_index.ts b/server/src/db/migrations/20260902_000001_analytics_latency_percentile_index.ts
new file mode 100644
index 00000000..6620eef7
--- /dev/null
+++ b/server/src/db/migrations/20260902_000001_analytics_latency_percentile_index.ts
@@ -0,0 +1,38 @@
+// Migration: covering index for the analytics latency percentiles
+// Created: 2026-09-02
+//
+// DOWN: reversible
+//
+// GET /api/analytics/summary computes p50 and p95 latency by nearest rank:
+// count the rows in the range that recorded a latency, then
+//
+// SELECT latency_ms FROM requests
+// WHERE created_at >= ? AND latency_ms IS NOT NULL
+// ORDER BY latency_ms ASC LIMIT 1 OFFSET ?
+//
+// twice per call. With only idx_requests_created_at the planner seeks the
+// range, then has to visit every row in it to read latency_ms and sort. The
+// (created_at, latency_ms) index covers the whole query, so the sort runs over
+// the index alone and never touches the table. Measured on a 470k-row log:
+// ~50ms -> ~30ms per percentile.
+//
+// Two other candidates were measured and rejected: (created_at, platform,
+// latency_ms) is never chosen for the per-platform p95 because of the
+// api_keys join, and (created_at, status) loses to idx_requests_created_at
+// on the ORDER BY created_at DESC listing. Both would have cost ~31MB on the
+// per-request insert path for nothing.
+
+import type { Db } from '../types.js';
+
+export function up(db: Db): void {
+ db.exec(`
+ CREATE INDEX IF NOT EXISTS idx_requests_created_latency
+ ON requests(created_at, latency_ms);
+ `);
+}
+
+export function down(db: Db): void {
+ db.exec(`
+ DROP INDEX IF EXISTS idx_requests_created_latency;
+ `);
+}