Skip to content

granular indexes with create on write and sp clustered indexes - #6100

Open
SergeyGaluzo wants to merge 15 commits into
mainfrom
users/sergal/granular-indexes-create-on-write-sp-clustered
Open

SergeyGaluzo wants to merge 15 commits into
mainfrom
users/sergal/granular-indexes-create-on-write-sp-clustered

Conversation

@SergeyGaluzo

Copy link
Copy Markdown
Contributor

No description provided.

@SergeyGaluzo
SergeyGaluzo requested a review from a team as a code owner November 24, 2026 21:56
Copilot AI balanced review requested due to automatic review settings November 24, 2026 21:56
@github-actions github-actions Bot added the SQL Scripts If SQL scripts are added to the PR label Nov 24, 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.

🟡 Changes recommended

Schema 123 blocks upgrades for initialized databases, rejects TokenText during writes, and omits required index and cleanup-procedure changes from the migration.

5 open issues
What changed in this PR

Introduces experimental, per-search-parameter indexes in the SQL Server storage layer, with index creation before writes and target-table metadata for resource cleanup.

Changes:

  • Adds granular-index creation procedures and write-time checks with client-side caching.
  • Moves search-parameter clustered keys to lead with SearchParamId and updates cleanup queries.
  • Adds schema-123 wiring and unit tests for target mapping, key discovery, caching, and retries.

Static SQL assessment: the new leading keys align with the rewritten cleanup predicates. Runtime performance was not verified.

File Description
src/​Microsoft.Health.Fhir.SqlServer/​Microsoft.Health.Fhir.SqlServer.csproj Sets the latest schema to 123.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Storage/​SqlStoreClient.cs Discovers index keys and caches creation results.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Storage/​SqlServerFhirModel.cs Supplies target-table metadata during initialization.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Storage/​SqlServerFhirDataStore.cs Ensures indexes before writes and reindex batches.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Storage/​SearchParameterTargetTable.cs Maps parameter definitions to storage tables.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Storage/​Registry/​SqlServerSearchParameterStatusDataStore.cs Persists target tables with status updates.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​VectorSearchParam.sql Leads the clustered primary key with SearchParamId.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​UriSearchParam.sql Reorders the URI clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenTokenCompositeSearchParam.sql Reorders the token/token clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenText.sql Reorders the token-text clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenStringCompositeSearchParam.sql Reorders the token/string clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenSearchParam.sql Reorders the token clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenQuantityCompositeSearchParam.sql Reorders the token/quantity clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenNumberNumberCompositeSearchParam.sql Reorders the token/number/number clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​TokenDateTimeCompositeSearchParam.sql Reorders the token/date-time clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​StringSearchParam.sql Reorders the string clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​SearchParam.sql Adds required target-table metadata.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​ReferenceTokenCompositeSearchParam.sql Reorders the reference/token clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​ReferenceSearchParam.sql Reorders the reference clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​QuantitySearchParam.sql Reorders the quantity clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​NumberSearchParam.sql Reorders the number clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Tables/​DateTimeSearchParam.sql Reorders the date-time clustered index.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Sprocs/​MergeSearchParams.sql Merges target metadata with parameter statuses.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Sprocs/​MergeResourcesAndSearchParams.sql Forwards target metadata during combined writes.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Sprocs/​MergeResources.sql Uses target mappings to select cleanup rows.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Sprocs/​InitializeBase.sql Initializes registry rows with target tables.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Sprocs/​CreateGranularSecondaryIndexes.sql Creates filtered indexes from disabled templates.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Sql/​Sprocs/​CreateAllGranularSecondaryIndexes.sql Schedules index creation for eligible parameters.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​SchemaVersionConstants.cs Updates schema bounds and adds a feature gate.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​SchemaVersion.cs Adds version 123.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Schema/​Migrations/​123.diff.sql Adds experimental migration metadata and procedures.
src/​Microsoft.Health.Fhir.SqlServer.UnitTests/​Features/​Storage/​SqlStoreClientGranularIndexTests.cs Tests key extraction, caching, retries, and schema gating.
src/​Microsoft.Health.Fhir.SqlServer.UnitTests/​Features/​Storage/​SearchParameterTargetTableTests.cs Tests parameter-to-table mappings.
src/​Microsoft.Health.Fhir.Shared.Core.UnitTests/​Features/​Search/​SearchParameters/​SearchParameterBehaviorTests.cs Verifies queued statuses retain definitions.
src/​Microsoft.Health.Fhir.Core/​Features/​Search/​Registry/​ResourceSearchParameterStatus.cs Carries definitions with pending status changes.
src/​Microsoft.Health.Fhir.Core/​Features/​Search/​Parameters/​CreateOrUpdateSearchParameterBehavior.cs Queues definitions with resolved composite components.

Review effort: Balanced

Comment on lines +1 to +3
IF EXISTS (SELECT 1 FROM dbo.SearchParam)
THROW 50000, 'This experimental migration requires an empty SearchParam table.', 1
ALTER TABLE dbo.SearchParam ADD TargetTable varchar(128) NOT NULL
EXECUTE dbo.LogEvent @Process=@SP,@Mode=@Mode,@Status='Error',@Start=@st;
THROW
END CATCH
GO
IF @@trancount <> 0
RAISERROR('Granular secondary indexes must be created outside transaction.', 18, 128)

IF @ObjectId IS NULL OR @SearchParamId IS NULL OR NOT @TableName LIKE '%SearchParam'
Comment on lines +20 to +27
INSERT INTO @Targets (TableName, SearchParamId)
SELECT TargetTable, SearchParamId
FROM dbo.SearchParam
WHERE Status IN ('Enabled', 'Supported') AND TargetTable NOT IN ('Resource', 'VectorSearchParam')

IF EXISTS (
SELECT * FROM @Targets T
WHERE T.TableName NOT LIKE '%SearchParam' OR NOT EXISTS (
DELETE A FROM @PreviousSurrogateIds P CROSS JOIN (SELECT SearchParamId FROM dbo.SearchParam WHERE TargetTable = 'ReferenceSearchParam') S JOIN dbo.ReferenceSearchParam A ON A.SearchParamId = S.SearchParamId AND A.ResourceTypeId = P.TypeId AND A.ResourceSurrogateId = P.SurrogateId
SET @AffectedRows += @@rowcount
DELETE FROM dbo.TokenSearchParam WHERE EXISTS (SELECT * FROM @PreviousSurrogateIds WHERE TypeId = ResourceTypeId AND SurrogateId = ResourceSurrogateId)
DELETE A FROM @PreviousSurrogateIds P CROSS JOIN (SELECT SearchParamId FROM dbo.SearchParam WHERE TargetTable = 'TokenSearchParam') S JOIN dbo.TokenSearchParam A ON A.SearchParamId = S.SearchParamId AND A.ResourceTypeId = P.TypeId AND A.ResourceSurrogateId = P.SurrogateId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

SQL Scripts If SQL scripts are added to the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants