Log stats details for long running SQL queries#5376
Merged
apurvabhaleMS merged 21 commits intomainfrom Feb 19, 2026
Merged
Conversation
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
Contributor
There was a problem hiding this comment.
Pull request overview
Adds best-effort logging of Query Store runtime stats for long-running SQL search queries in the SQL Server search layer, controlled via dbo.Parameters feature flag + threshold.
Changes:
- Adds a runtime feature flag (
Search.LongRunningQueryDetails.IsEnabled) and numeric threshold (Search.LongRunningQueryDetails.Threshold, default 60s). - Measures query execution time around
ExecuteReaderAsyncin both search and include-search paths. - Introduces query-text normalization and a Query Store lookup query to log recent runtime stats for matching statements.
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.
Comments suppressed due to low confidence (1)
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs:570
- The stopwatch starts before ExecuteReaderAsync but stops after the entire reader processing completes, so the measured "execution" time includes result materialization/decompression in addition to SQL execution. If the intent is to detect long-running SQL, consider measuring only the DB call (e.g., around ExecuteReaderAsync / DB read loop) or clearly renaming this to reflect end-to-end processing time.
var st = DateTime.UtcNow;
var executionStopwatch = Stopwatch.StartNew();
try
{
using (var reader = await sqlCommand.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken))
{
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/CachedParameter.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/CachedParameter.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
...oft.Health.Fhir.SqlServer.UnitTests/Features/Search/SqlServerSearchServiceQueryStoreTests.cs
Show resolved
Hide resolved
...oft.Health.Fhir.SqlServer.UnitTests/Features/Search/SqlServerSearchServiceQueryStoreTests.cs
Outdated
Show resolved
Hide resolved
...oft.Health.Fhir.SqlServer.UnitTests/Features/Search/SqlServerSearchServiceQueryStoreTests.cs
Dismissed
Show dismissed
Hide dismissed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Fixed
Show fixed
Hide fixed
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Contributor
|
I do not see "fire and forget" mentioned in description, Is it implemented? |
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Outdated
Show resolved
Hide resolved
…nfo-long-running-queries
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (2)
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/CachedParameter.cs:20
CachedParameter<TLogger>is declaredpubliceven though it appears to be an internal SQL Server implementation detail (replacingProcessingFlag). Making it public expands the surface area ofMicrosoft.Health.Fhir.SqlServerand can become a supported API inadvertently. Consider making the classinternal(and only exposing what’s needed via internal helpers) unless there’s a concrete cross-assembly requirement.
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/CachedParameter.cs:85ReadFromDatabaseblocks on an async call via.Result. This can cause thread-pool starvation under load and wraps exceptions inAggregateException, making diagnostics harder. Prefer awaiting asynchronously (make the call path async) or, if it must be synchronous, useGetAwaiter().GetResult()and ensure the async operation doesn’t capture a context.
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Show resolved
Hide resolved
...oft.Health.Fhir.SqlServer.UnitTests/Features/Search/SqlServerSearchServiceQueryStoreTests.cs
Outdated
Show resolved
Hide resolved
...oft.Health.Fhir.SqlServer.UnitTests/Features/Search/SqlServerSearchServiceQueryStoreTests.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Dismissed
Show dismissed
Hide dismissed
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
Dismissed
Show dismissed
Hide dismissed
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/SqlServerFhirDataStore.cs
Dismissed
Show dismissed
Hide dismissed
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/SqlServerFhirDataStore.cs
Dismissed
Show dismissed
Hide dismissed
src/Microsoft.Health.Fhir.SqlServer/Features/Storage/SqlServerFhirDataStore.cs
Dismissed
Show dismissed
Hide dismissed
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
SergeyGaluzo
approved these changes
Feb 19, 2026
feordin
approved these changes
Feb 19, 2026
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.
Description
This PR adds diagnostic logging for long-running SQL Server search queries by measuring execution time and (optionally) querying SQL Server Query Store for recent runtime stats matching the executed query text. The logging is runtime-configurable via dbo.Parameters, runs asynchronously (fire-and-forget), and is best-effort (never impacts the search request path).
What’s included
Runtime feature flag + threshold (dbo.Parameters)
Search.LongRunningQueryDetails.IsEnabledto enable/disable Query Store stats logging at runtime (default: enabled).Search.LongRunningQueryDetails.Thresholdto configure the “long-running” threshold in milliseconds (default: 5000ms).Cached dbo.Parameters reader (generalized)
ProcessingFlag<T>withCachedParameter<T>:dbo.Parametersand caches them (10-minute TTL).IsEnabled()(1= enabled,0= disabled).CachedParameter<T>.Execution time measurement
Stopwatchtiming aroundExecuteReaderAsyncfor:Query Store lookup + logging (non-blocking, best-effort)
SET STATISTICS ...,DECLARE ...,OPTION (RECOMPILE), and timeout comments;WITHtoWITHINSERT INTO @FilteredData.Task.Run) with a short timeout (2 seconds).Related issues
Addresses AB180196
Testing
Describe how this change was tested.
FHIR Team Checklist
Semver Change (docs)
Patch|Skip|Feature|Breaking (reason)