Fix slow health summary endpoint#369
Merged
Wiesenwischer merged 3 commits intomainfrom May 5, 2026
Merged
Conversation
…and adding composite index
The /api/health/{environmentId} endpoint became progressively slower as the
HealthSnapshots table grew on long-running instances. Root cause: the raw SQL
in GetLatestForEnvironment wrapped EnvironmentId in UPPER(), which prevented
SQLite from using the index and forced a full table scan on every request.
Changes:
- Remove UPPER() / ToUpperInvariant() from GetLatestForEnvironment,
RemoveForDeployment, and GetTransitions; pass Guid parameters via
FromSqlInterpolated so EF Core's standard Guid-to-TEXT conversion is used
on both sides of the comparison.
- Replace the single-column EnvironmentId index with a composite
(EnvironmentId, DeploymentId, CapturedAtUtc) index that covers the
"latest snapshot per deployment in environment" query directly.
- Lower the default snapshot retention from 30 to 7 days. With a 30s
collection interval this still yields ~20k snapshots per deployment,
while the UI history view requests only the latest 50.
The CI run for the previous commit failed because: - The migration used MigrationBuilder.DropIndex/CreateIndex, which emit plain DROP/CREATE INDEX without IF [NOT] EXISTS guards. On a baseline-skipped legacy database where the InitialCreate index was never materialised, this throws. - The legacy-DB test fixture only created StackSources, so it was not a realistic stand-in for an EnsureCreated()-era database (which would contain the full baseline schema). Changes: - Switch the index swap to raw SQL with DROP INDEX IF EXISTS / CREATE INDEX IF NOT EXISTS, so the migration is safe regardless of whether the legacy baseline materialised the old single-column index. - Extend CreateLegacyStackSourcesTable to also create a minimal HealthSnapshots table with the InitialCreate-era indexes, so MigrationBaselineTests exercise a realistic legacy schema.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
/api/health/{environmentId}endpoint became progressively slower as theHealthSnapshotstable grew on long-running instances (observed ontest-ux-dokker: skeleton loaders never resolved).Root cause: the raw SQL in
GetLatestForEnvironmentwrappedEnvironmentIdinUPPER(), which prevents SQLite from using the index and forces a full table scan on every request. Compounded by the absence of a composite index covering the(env, deployment, latest)access pattern, and a 30-day default retention that lets the table grow large.Changes
UPPER()/ToUpperInvariant()fromGetLatestForEnvironment,RemoveForDeployment, andGetTransitions. Guid parameters are passed viaFromSqlInterpolatedso EF Core's standard Guid-to-TEXT conversion is used on both sides of the comparison.EnvironmentIdindex with a composite(EnvironmentId, DeploymentId, CapturedAtUtc)index that directly covers the "latest snapshot per deployment in environment" query.AddHealthSnapshotsCompositeIndexperforms the index swap.Test plan
dotnet buildclean (0 errors, 0 new warnings)test-ux-dokkerthat/healthpage loads quickly after deploy