Skip to content

Filter stat for ReferenceSearchParam table#5531

Open
apurvabhaleMS wants to merge 14 commits into
mainfrom
personal/abhale/filter-stat-for-reference-uri-table
Open

Filter stat for ReferenceSearchParam table#5531
apurvabhaleMS wants to merge 14 commits into
mainfrom
personal/abhale/filter-stat-for-reference-uri-table

Conversation

@apurvabhaleMS

@apurvabhaleMS apurvabhaleMS commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Description

Adds 3-column filtered statistics on dbo.ReferenceSearchParam for the ReferenceResourceId column, filtered by (ResourceTypeId, SearchParamId, ReferenceResourceTypeId). This gives SQL Server per-target-type histograms, enabling better cardinality estimates for reference joins (e.g., DiagnosticReport?subject=Patient/123).

What changed

Schema (V115)

CreateResourceSearchParamStats: new optional @ReferenceResourceTypeId smallint = NULL parameter. When provided, creates a stat named ST_ReferenceResourceId_WHERE_ResourceTypeId_X_SearchParamId_Y_ReferenceResourceTypeId_Z with a 3-column WHERE filter.
GetResourceSearchParamStats: new optional @ReferenceResourceTypeId parameter for lookup. Uses LIKE with no trailing % on ReferenceResourceTypeId to prevent false positives (e.g., _2 matching _20).

Related issues

Addresses AB189018.

Testing

Describe how this change was tested.

FHIR Team Checklist

  • Update the title of the PR to be succinct and less than 65 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Build, Dependencies, Enhancement, New-Feature or Documentation
  • Tag the PR with Open source, Azure API for FHIR (CosmosDB or common code) or Azure Healthcare APIs (SQL or common code) to specify where this change is intended to be released.
  • Tag the PR with Schema Version backward compatible or Schema Version backward incompatible or Schema Version unchanged if this adds or updates Sql script which is/is not backward compatible with the code.
  • When changing or adding behavior, if your code modifies the system design or changes design assumptions, please create and include an ADR.
  • CI is green before merge Build Status
  • Review squash-merge requirements

Semver Change (docs)

Patch|Skip|Feature|Breaking (reason)

@apurvabhaleMS apurvabhaleMS requested a review from a team as a code owner April 29, 2026 22:10
@codecov-commenter

codecov-commenter commented Apr 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@816452f). Learn more about missing BASE report.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #5531   +/-   ##
=======================================
  Coverage        ?   77.69%           
=======================================
  Files           ?      985           
  Lines           ?    36192           
  Branches        ?     5498           
=======================================
  Hits            ?    28120           
  Misses          ?     6717           
  Partials        ?     1355           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@apurvabhaleMS apurvabhaleMS requested a review from Copilot April 30, 2026 18:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds filtered-statistics support for additional SQL search parameter tables (ReferenceSearchParam and UriSearchParam) and verifies the behavior with new unit/integration tests.

Changes:

  • Extend GetKeyColumns to include dbo.ReferenceSearchParam and dbo.UriSearchParam key columns.
  • Add unit tests covering GetKeyColumns across single/two-column tables and unknown/null tables.
  • Add integration tests asserting filtered stats are created (and skipped for :missing / NotExists expressions).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
test/Microsoft.Health.Fhir.Shared.Tests.Integration/Persistence/SqlServerCreateStatsTests.cs Adds integration coverage for stats creation for Reference/Uri params and for NotExists behavior.
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs Expands key-column mapping for filtered stats to Reference/Uri tables and shares it with the stats pipeline.
src/Microsoft.Health.Fhir.SqlServer.UnitTests/Features/Search/SqlServerSearchServiceTests.cs Adds unit tests validating the expanded GetKeyColumns behavior.

Comment thread src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs Outdated
@apurvabhaleMS apurvabhaleMS added Area-SQL Area related to the SQL Server data provider Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs performance No-PaaS-breaking-change No-ADR ADR not needed labels Apr 30, 2026
@apurvabhaleMS apurvabhaleMS added this to the FY26\Q4\2Wk\2Wk22 milestone Apr 30, 2026
@apurvabhaleMS apurvabhaleMS added the Enhancement Enhancement on existing functionality. label Apr 30, 2026
@SergeyGaluzo

Copy link
Copy Markdown
Contributor

I think stats on ref search param should be ST_ReferenceResourceId_WHERE_ResourceTypeId_40_SearchParamId_1219_ReferenceResourceTypeId_XXX

@github-actions github-actions Bot added the SQL Scripts If SQL scripts are added to the PR label Jun 24, 2026
feordin
feordin previously approved these changes Jun 26, 2026
@feordin feordin changed the title Filter stat for ReferenceSearchParam and UriSearchParam table Filter stat for ReferenceSearchParam table Jun 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.

Comment on lines +2010 to 2013
internal static ICollection<(string TableName, string ColumnName, short ResourceTypeId, short SearchParamId, short? ReferenceResourceTypeId)> GetStatsFromCache()
{
return _resourceSearchParamStats.GetStatsFromCache();
}
Comment on lines 2031 to +2036
var split = stats.Split("_");
var column = split[1];
var resorceTypeId = short.Parse(split[4]);
var searchParamId = short.Parse(split[6]);
return ("dbo." + table, column, resorceTypeId, searchParamId);
short? referenceResourceTypeId = split.Length > 8 ? short.Parse(split[8]) : null;
return ("dbo." + table, column, resorceTypeId, searchParamId, referenceResourceTypeId);
AND (T.name LIKE @Table OR @Table IS NULL)
AND (S.name LIKE '%ResourceTypeId[_]'+convert(varchar,@ResourceTypeId)+'[_]%' OR @ResourceTypeId IS NULL)
AND (S.name LIKE '%SearchParamId[_]'+convert(varchar,@SearchParamId) OR @SearchParamId IS NULL)
AND (S.name LIKE '%SearchParamId[_]'+convert(varchar,@SearchParamId)+'%' OR @SearchParamId IS NULL)
AND S.name LIKE 'ST[_]%'
AND (T.name LIKE @Table OR @Table IS NULL)
AND (S.name LIKE '%ResourceTypeId[_]'+convert(varchar,@ResourceTypeId)+'[_]%' OR @ResourceTypeId IS NULL)
AND (S.name LIKE '%SearchParamId[_]'+convert(varchar,@SearchParamId)+'%' OR @SearchParamId IS NULL)
Comment on lines +135 to +137
Assert.True(
cacheAfter.Count(MatchesStat) > cacheBefore.Count(MatchesStat),
"Expected the search to add a filtered statistics entry in the cache for ReferenceResourceId with ReferenceResourceTypeId filter.");
Comment on lines +140 to +142
Assert.True(
databaseAfter.Count(MatchesStat) > databaseBefore.Count(MatchesStat),
"Expected the search to add a filtered statistics entry in the database for ReferenceResourceId with ReferenceResourceTypeId filter.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-SQL Area related to the SQL Server data provider Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Enhancement Enhancement on existing functionality. No-ADR ADR not needed No-PaaS-breaking-change performance Schema Version unchanged SQL Scripts If SQL scripts are added to the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants