特定于 Entity Framework Core SQL Server 提供程序的索引功能Index features specific to the Entity Framework Core SQL Server provider

此页详细介绍了特定于 SQL Server 提供程序的索引配置选项。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. 有关详细信息,请参阅 有关聚集索引和非聚集索引的 SQL Server 文档For more information, see the SQL Server documentation on clustered and non-clustered indexes.

默认情况下,表的主键列由聚集索引隐式支持,所有其他索引为非聚集索引。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:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>().HasIndex(b => b.PublishedOn).IsClustered();
}

填充因子Fill factor

备注

EF Core 5.0 中已引入了此功能。This feature is introduced in EF Core 5.0.

提供索引填充因子选项是为了优化索引数据存储和性能。The index fill-factor option is provided for fine-tuning index data storage and performance. 有关详细信息,请参阅 有关填充因子的 SQL Server 文档For more information, see the SQL Server documentation on fill factor.

您可以按如下所示配置索引的填充因子:You can configure an index's fill factor as follows:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>().HasIndex(b => b.PublishedOn).HasFillFactor(10);
}

联机创建Online creation

ONLINE 选项允许在创建索引期间并发用户访问基础表或聚集索引数据以及任何关联的非聚集索引,以便用户可以继续更新和查询基础数据。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. 当脱机执行数据定义语言 (DDL) 操作(例如,生成或重新生成聚集索引)时,这些操作对基础数据和关联索引持有排他锁。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. 有关详细信息,请参阅 联机索引选项上的 SQL Server 文档For more information, see the SQL Server documentation on the ONLINE index option.

可以使用 ONLINE 选项配置索引,如下所示:You can configure an index with the ONLINE option as follows:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>().HasIndex(b => b.PublishedOn).IsCreatedOnline();
}