EF Core 建立非聚集索引

一 通过EF代码创建

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DataSample>(entity =>
            {
                entity.HasIndex(e => e.Sort).IsClustered(false);
            });
        }


二 通过语句创建,及相关注意事项

use [DB]

set statistics io on
select OrderNo,DoctorName,OrderAmount from Orders where DoctorName like '%关键词%' ;
set statistics io off

----创建非聚集索引
--create nonclustered index nc_Orders_DoctorName on Orders(DoctorName)
----创建非聚集索引 关联的查询字段
--create nonclustered index nc_Orders_DoctorName1 on Orders(DoctorName) include(OrderNo,DoctorName,OrderAmount) with(online=on,MAXDOP=2)

-- 多表连接,一般在inner join 左边的表的ID 做索引
--多表连接:第一次查询的表是少记录的表,第二次查询多记录的表
--索引视图(一般做在变更不大的表)
--索引个数一般做5个,不要超过10个

先创建聚集索引,再创建非聚集索引
一般为 为主键列创建聚集索引,为条件列创建非聚集索引
如果有复合索引,但是条件里面没有相关字段,就会执行表扫描
建立复合索引时:频率大的放前面
当有窄列和宽列时,一般对窄列建立聚集索引
使用聚集索引时机:Group By ,Order By
过滤索引,业务上,肯定会有此条件的时候,
create nonclustered index nc_ColName
on Orders(IndexColName) include(ShowColName1,ShowColName2)
with(online=on,MAXDOP=2)
where IsValid=1 --过滤索引

参考: https://www.cnblogs.com/s-b-b/p/8334593.html https://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html

原文地址:https://www.cnblogs.com/jasonlai2016/p/14326336.html