-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Use CREATE INDEX with DROP_EXISTING=ON for index facet changes (SQL Server) #38271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
m-x-shokhzod
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
m-x-shokhzod:perf/sqlserver-drop-existing-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2132,6 +2132,15 @@ protected override void IndexOptions(MigrationOperation operation, IModel? model | |
| }); | ||
| } | ||
|
|
||
| // When this CreateIndexOperation was rewritten from a Drop+Create pair (an index facet | ||
| // changed and the index needs to be recreated), emit DROP_EXISTING = ON so SQL Server | ||
| // atomically replaces the index without leaving the table un-indexed during the rebuild. | ||
| // See #35067. | ||
| if (operation[SqlServerAnnotationNames.UseDropExisting] is true) | ||
| { | ||
| options.Add("DROP_EXISTING = ON"); | ||
| } | ||
|
|
||
| // Vector index options. | ||
| // Note that the metric facet is mandatory, and used to determine if the index is a vector index. | ||
| if (operation[SqlServerAnnotationNames.VectorIndexMetric] is string vectorMetric) | ||
|
|
@@ -2664,6 +2673,77 @@ private string Uniquify(string variableName, bool increase = true) | |
| return _variableCounter == 0 ? variableName : variableName + _variableCounter; | ||
| } | ||
|
|
||
| private IReadOnlyList<MigrationOperation> RewriteDropAndCreateIndexAsDropExisting( | ||
| IReadOnlyList<MigrationOperation> migrationOperations, | ||
| IModel? model) | ||
| { | ||
| // The differ produces a DropIndexOperation + CreateIndexOperation pair when an index facet | ||
| // changes (e.g. fill factor, sort order, uniqueness, filter, columns). On SQL Server the | ||
| // pair can be collapsed into a single `CREATE INDEX ... WITH (DROP_EXISTING = ON)` which | ||
| // is more efficient: queries can continue using the old index while the new one is being | ||
| // built, instead of going un-indexed during the drop. See #35067. | ||
| // | ||
| // The collapse is only safe when the drop is IMMEDIATELY followed by the matching create. | ||
| // If anything sits between them (e.g. an AlterColumnOperation on the indexed column, which | ||
| // SQL Server only allows once the index is gone), removing the drop would re-introduce the | ||
| // old index before the intermediate operation runs and break the migration. The rewrite is | ||
| // also limited to non-special indexes (no memory-optimized, full-text or vector index, | ||
| // since those use different syntax/restrictions). | ||
|
|
||
| // Short-circuit when no DropIndex+CreateIndex pair is possible. | ||
| if (migrationOperations.Count < 2 | ||
| || !migrationOperations.OfType<DropIndexOperation>().Any() | ||
| || !migrationOperations.OfType<CreateIndexOperation>().Any()) | ||
| { | ||
| return migrationOperations; | ||
| } | ||
|
|
||
| // Scan for adjacent (DropIndex, CreateIndex) pairs with matching identity. | ||
| var dropsToRemove = new HashSet<DropIndexOperation>(); | ||
| for (var i = 0; i < migrationOperations.Count - 1; i++) | ||
| { | ||
| if (migrationOperations[i] is not DropIndexOperation dropOperation | ||
| || dropOperation.Table is null | ||
| || migrationOperations[i + 1] is not CreateIndexOperation createOperation | ||
| || createOperation.Table is null | ||
| || dropOperation.Name != createOperation.Name | ||
| || dropOperation.Table != createOperation.Table | ||
| || dropOperation.Schema != createOperation.Schema) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can call |
||
| // Skip special index types that don't support DROP_EXISTING. | ||
| if (createOperation[SqlServerAnnotationNames.FullTextIndex] is not null | ||
| || createOperation[SqlServerAnnotationNames.VectorIndexMetric] is not null | ||
| || IsMemoryOptimized(createOperation, model, createOperation.Schema, createOperation.Table)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| createOperation.AddAnnotation(SqlServerAnnotationNames.UseDropExisting, true); | ||
| dropsToRemove.Add(dropOperation); | ||
| } | ||
|
|
||
| if (dropsToRemove.Count == 0) | ||
| { | ||
| return migrationOperations; | ||
| } | ||
|
|
||
| var resultOperations = new List<MigrationOperation>(migrationOperations.Count - dropsToRemove.Count); | ||
| foreach (var migrationOperation in migrationOperations) | ||
| { | ||
| if (migrationOperation is DropIndexOperation dropOperation && dropsToRemove.Contains(dropOperation)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| resultOperations.Add(migrationOperation); | ||
| } | ||
|
|
||
| return resultOperations; | ||
| } | ||
|
|
||
| private IReadOnlyList<MigrationOperation> FixLegacyTemporalAnnotations(IReadOnlyList<MigrationOperation> migrationOperations) | ||
| { | ||
| // short-circuit for non-temporal migrations (which is the majority) | ||
|
|
@@ -2797,6 +2877,7 @@ private IReadOnlyList<MigrationOperation> RewriteOperations( | |
| MigrationsSqlGenerationOptions options) | ||
| { | ||
| migrationOperations = FixLegacyTemporalAnnotations(migrationOperations); | ||
| migrationOperations = RewriteDropAndCreateIndexAsDropExisting(migrationOperations, model); | ||
|
|
||
| var operations = new List<MigrationOperation>(); | ||
| var availableSchemas = new List<string>(); | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this short-circuit, it doesn't actually save much time compared to the loop below.