From 555d5d507bfae89299ab8f022a01e2064b0be81f Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Sun, 23 Aug 2026 01:44:49 +0200 Subject: [PATCH] perf(spend_tracking): index LiteLLM_SpendLogs by (api_key, startTime) Every per-key spend read filters WHERE api_key = ... AND startTime in a range (/spend/logs?api_key=..., the key-filtered UI logs page, external billing readers), but LiteLLM_SpendLogs carries no api_key index, so each such query scans every logged request on the instance. Composite with startTime to match the query shape, mirroring the existing (startTime, request_id) composite. Measured on Postgres 18 with 1.6M spend rows: one key's 7-day SUM goes from a 93.9ms parallel seq scan to a 0.7ms bitmap index scan (~140x); a batch job reading per-key spend for 10k keys went from 20.5s to 1.7s. --- .../migration.sql | 2 ++ litellm-proxy-extras/litellm_proxy_extras/schema.prisma | 1 + litellm/proxy/schema.prisma | 1 + schema.prisma | 1 + 4 files changed, 5 insertions(+) create mode 100644 litellm-proxy-extras/litellm_proxy_extras/migrations/20260823000000_add_spend_logs_api_key_starttime_index/migration.sql diff --git a/litellm-proxy-extras/litellm_proxy_extras/migrations/20260823000000_add_spend_logs_api_key_starttime_index/migration.sql b/litellm-proxy-extras/litellm_proxy_extras/migrations/20260823000000_add_spend_logs_api_key_starttime_index/migration.sql new file mode 100644 index 00000000000..9a061aaed43 --- /dev/null +++ b/litellm-proxy-extras/litellm_proxy_extras/migrations/20260823000000_add_spend_logs_api_key_starttime_index/migration.sql @@ -0,0 +1,2 @@ +-- CreateIndex +CREATE INDEX IF NOT EXISTS "LiteLLM_SpendLogs_api_key_startTime_idx" ON "LiteLLM_SpendLogs"("api_key", "startTime"); diff --git a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma index d9959677116..7a7c47e71d8 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma +++ b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma @@ -647,6 +647,7 @@ model LiteLLM_SpendLogs { @@index([startTime, request_id]) @@index([end_user]) @@index([session_id]) + @@index([api_key, startTime]) } // View spend, model, api_key per request diff --git a/litellm/proxy/schema.prisma b/litellm/proxy/schema.prisma index d9959677116..7a7c47e71d8 100644 --- a/litellm/proxy/schema.prisma +++ b/litellm/proxy/schema.prisma @@ -647,6 +647,7 @@ model LiteLLM_SpendLogs { @@index([startTime, request_id]) @@index([end_user]) @@index([session_id]) + @@index([api_key, startTime]) } // View spend, model, api_key per request diff --git a/schema.prisma b/schema.prisma index d9959677116..7a7c47e71d8 100644 --- a/schema.prisma +++ b/schema.prisma @@ -647,6 +647,7 @@ model LiteLLM_SpendLogs { @@index([startTime, request_id]) @@index([end_user]) @@index([session_id]) + @@index([api_key, startTime]) } // View spend, model, api_key per request