From a727f71b1968f9fbefde6b94daabb7aee384633a Mon Sep 17 00:00:00 2001 From: Carlo Alberto Ferraris Date: Wed, 26 Nov 2025 12:36:25 +0900 Subject: [PATCH] Optimize date filtering for spend logs queries (#17073) This should allow postgres to perform a more efficient index scan instead of a sequential table scan. These two queries consistently show up in the longest-running ones in our instance, and are a major latency source for the usage page on the admin UI. --- litellm/proxy/spend_tracking/spend_management_endpoints.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index a167f564fd..5dfedcc0b8 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -1427,7 +1427,7 @@ async def _get_spend_report_for_time_range( LEFT JOIN "LiteLLM_TeamTable" t ON s.team_id = t.team_id WHERE - s."startTime"::DATE >= $1::date AND s."startTime"::DATE <= $2::date + s."startTime" >= $1::date AND s."startTime" < ($2::date + INTERVAL '1 day') GROUP BY t.team_alias ORDER BY @@ -1441,7 +1441,7 @@ async def _get_spend_report_for_time_range( jsonb_array_elements_text(request_tags) AS individual_request_tag, SUM(spend) AS total_spend FROM "LiteLLM_SpendLogs" - WHERE "startTime"::DATE >= $1::date AND "startTime"::DATE <= $2::date + WHERE "startTime" >= $1::date AND "startTime" < ($2::date + INTERVAL '1 day') GROUP BY individual_request_tag ORDER BY total_spend DESC; """