Skip to content

Commit

Permalink
Add docs on SQL Server-specific index config (#2600)
Browse files Browse the repository at this point in the history
Closes #2378
Closes #878
  • Loading branch information
roji authored Sep 1, 2020
1 parent 5c4324f commit b76af2b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
40 changes: 40 additions & 0 deletions entity-framework/core/providers/sql-server/indexes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Microsoft SQL Server Database Provider - Indexes - EF Core
description: Index features specific to the Entity Framework Core SQL Server provider
author: roji
ms.author: shrojans
ms.date: 9/1/2020
uid: core/providers/sql-server/indexes
---
# Index features specific to the Entity Framework Core SQL Server provider

This page details index configuration options that are specific to the SQL Server provider.

## Clustering

Clustered indexes sort and store the data rows in the table or view based on their key values. Creating the right clustered index for your table can significantly improve the speed of your queries, as data is already laid out in the optimal order. There can be only one clustered index per table, because the data rows themselves can be stored in only one order. For more information, see [the SQL Server documentation on clustered and non-clustered indexes](/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described).

By default, the primary key column of a table is implicitly backed by a clustered index, and all other indexes are non-clustered.

You can configure an index or key to be clustered as follows:

[!code-csharp[ClusteredIndex](../../../../samples/core/SqlServer/Indexes/ClusteredIndexContext.cs?name=ClusteredIndex)]

## Fill factor

> [!NOTE]
> This feature is introduced in EF Core 5.0.
The index fill-factor option is provided for fine-tuning index data storage and performance. For more information, see [the SQL Server documentation on fill factor](/sql/relational-databases/indexes/specify-fill-factor-for-an-index).

You can configure an index's fill factor as follows:

[!code-csharp[IndexFillFactor](../../../../samples/core/SqlServer/Indexes/IndexFillFactorContext.cs?name=IndexFillFactor)]

## Online creation

The ONLINE option allows concurrent user access to the underlying table or clustered index data and any associated nonclustered indexes during index creation, so that users can continue to update and query the underlying data. When you perform data definition language (DDL) operations offline, such as building or rebuilding a clustered index; these operations hold exclusive locks on the underlying data and associated indexes. For more information, see [the SQL Server documentation on the ONLINE index option](/sql/relational-databases/indexes/perform-index-operations-online).

You can configure an index with the ONLINE option as follows:

[!code-csharp[IndexOnline](../../../../samples/core/SqlServer/Indexes/IndexOnlineContext.cs?name=IndexOnline)]
2 changes: 2 additions & 0 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@
items:
- name: Overview
href: core/providers/sql-server/index.md
- name: Indexes
href: core/providers/sql-server/indexes.md
- name: Memory-optimized tables
href: core/providers/sql-server/memory-optimized-tables.md
- name: Specify Azure SQL Database options
Expand Down
11 changes: 11 additions & 0 deletions samples/core/SqlServer/Indexes/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SqlServer.Indexes
{
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public DateTime PublishedOn { get; set; }
}
}
16 changes: 16 additions & 0 deletions samples/core/SqlServer/Indexes/ClusteredIndexContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;

namespace SqlServer.Indexes
{
public class ClusteredIndexContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region ClusteredIndex
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().HasIndex(b => b.PublishedOn).IsClustered();
}
#endregion
}
}
16 changes: 16 additions & 0 deletions samples/core/SqlServer/Indexes/IndexFillFactorContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;

namespace SqlServer.Indexes
{
public class IndexFillFactorContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region IndexFillFactor
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().HasIndex(b => b.PublishedOn).HasFillFactor(10);
}
#endregion
}
}
16 changes: 16 additions & 0 deletions samples/core/SqlServer/Indexes/IndexOnlineContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;

namespace SqlServer.Indexes
{
public class IndexOnlineContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region IndexOnline
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().HasIndex(b => b.PublishedOn).IsCreatedOnline();
}
#endregion
}
}
2 changes: 1 addition & 1 deletion samples/core/SqlServer/SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-preview.8.20407.4" />
</ItemGroup>

</Project>

0 comments on commit b76af2b

Please sign in to comment.