perf(coderd/database): optimize GetAPIKeysLastUsedAfter (#19725)

This change adds a support-index for `GetAPIKeysLastUsedAfter`.

On dogfood (24h): called 4.4k times, 380ms average.

Change for tested time range: `170ms` -> `3.3ms`.
This commit is contained in:
Mathias Fredriksson
2025-09-08 13:13:12 +03:00
committed by GitHub
parent fb9753edf2
commit 21402c7aaa
3 changed files with 7 additions and 0 deletions
+4
View File
@@ -2861,6 +2861,10 @@ ALTER TABLE ONLY workspace_resources
ALTER TABLE ONLY workspaces
ADD CONSTRAINT workspaces_pkey PRIMARY KEY (id);
CREATE INDEX api_keys_last_used_idx ON api_keys USING btree (last_used DESC);
COMMENT ON INDEX api_keys_last_used_idx IS 'Index for optimizing api_keys queries filtering by last_used';
CREATE INDEX idx_agent_stats_created_at ON workspace_agent_stats USING btree (created_at);
CREATE INDEX idx_agent_stats_user_id ON workspace_agent_stats USING btree (user_id);