Skip to content
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

Migrations: Avoid redundant IS NOT NULL on indexes #5532

Closed
Tasteful opened this issue May 26, 2016 · 3 comments
Closed

Migrations: Avoid redundant IS NOT NULL on indexes #5532

Tasteful opened this issue May 26, 2016 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@Tasteful
Copy link
Contributor

Steps to reproduce

Create an entity with string property that got an unique-index. Field is marked as required.

builder.Entity<UnitOfMeasurementEntity>(b =>
            {
                b.ToTable("UnitOfMeasurementEntity", "Products");
                b.HasKey(p => p.SystemId);

                b.Property(p => p.Id)
                    .HasMaxLength(100)
                    .IsRequired();
                b.HasIndex(p => p.Id).IsUnique();
            });

The issue

Index is created on non null column, index is still created as a filtered index with an where statement. Somehow the filtered index is not used during all querying and got slow response time from SQL server (SQL 2012r2).

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20160526125022_FixBrokenIndex2')
BEGIN
    CREATE UNIQUE INDEX [IX_UnitOfMeasurementEntity_Id] ON [Products].[UnitOfMeasurementEntity] ([Id]) WHERE [Id] IS NOT NULL;
END

Migration code looks like the following

            migrationBuilder.CreateIndex(
                name: "IX_UnitOfMeasurementEntity_Id",
                schema: "Products",
                table: "UnitOfMeasurementEntity",
                column: "Id",
                unique: true);

The column for the index is non nullable and then the index does not need any filter.

Further technical details

EF Core version: RC1
Operating system: Win10
Visual Studio version: VS2015 RC2

@bricelam bricelam self-assigned this May 27, 2016
@rowanmiller rowanmiller changed the title Unique index on required string-field creating filtered index Migrations: Avoid redundant IS NOT NULL on indexes May 31, 2016
@rowanmiller rowanmiller added this to the 1.0.1 milestone May 31, 2016
@MarcoLoetscher
Copy link

For indexes on SQL Server, it should be possible to set a filter might as string or lamda expression:
modelBuilder.Entity<Blog>() .HasIndex(b => b.Url, "[Url] IS NOT NULL AND [IsActive] = 1");
or
modelBuilder.Entity<Blog>() .HasIndex(b => b.Url, b => b.Url != null && b.IsActive == 1);

@bricelam
Copy link
Contributor

bricelam commented Jun 20, 2016

@MarcoLoetscher Can you create a new issue? This one is specifically about removing WHERE...NOT NULL on NOT NULL columns.

@MarcoLoetscher
Copy link

Yes, I can. #5817

@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 10, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants