Fix SQL generation for selective chained searches - #5725
Draft
Mikael Weaver (mikaelweave) wants to merge 5 commits into
Draft
Fix SQL generation for selective chained searches#5725Mikael Weaver (mikaelweave) wants to merge 5 commits into
Mikael Weaver (mikaelweave) wants to merge 5 commits into
Conversation
Generate the selective target reference CTE first for the affected forward reference-reference-date chain, then use local loop joins for reference traversal and a hash join for the source date filter. This avoids parameter-sensitive nested-loop date probes while preserving the existing CTE query pattern. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Mikael Weaver (mikaelweave)
marked this pull request as draft
August 15, 2026 14:27
Move the specialized query generation into a focused SqlQueryGenerator partial, consolidate its sequencing state, and fall back for preceding source filters and sorted searches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add production-driven regression coverage for top, count-only, include persistence, intervening source predicates, multiple target predicates, and chained token targets without source date filters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move the specialized regression coverage into a focused partial test class and cover a following source predicate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5725 +/- ##
=======================================
Coverage ? 77.41%
=======================================
Files ? 1012
Lines ? 37432
Branches ? 5688
=======================================
Hits ? 28978
Misses ? 7083
Partials ? 1371 🚀 New features to boost your workflow:
|
* Support sorting in target-first chained search Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Strengthen target-first sort coverage Preserve later sorts in the generic pipeline so intervening predicates are applied before sort projection, while making atomic match consumption and CTE identities explicit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Cover unsupported adjacent numeric sort Verify the target-first matcher rejects the atomic rewrite and preserve the generic numeric SortValue path for both sort directions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Revert "Cover unsupported adjacent numeric sort" This reverts commit 3c394b6. * Retry CI after Cosmos provisioning lock Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Related work item
Summary
Root cause
The generated query began with the broad source-reference side. Under cached compilation, SQL Server selected nested loops and performed hundreds of thousands of date index probes. Reordering the specialized shape and using the validated local join strategies produces a reusable plan without requiring
OPTION (RECOMPILE)or global query hints.Validation
dotnet test src/Microsoft.Health.Fhir.SqlServer.UnitTests/Microsoft.Health.Fhir.SqlServer.UnitTests.csproj --no-restore -- --no-progress