先来推荐一下监视sql语句方法
- 通过SQL Server Profiler来监控执行的sql语句
- 使用插件MiniProfiler来监控执行的sql语句
下面来说EF的性能优化:
1.在不需要查询关联表中的数据时,可以关闭延迟加载 db.Configuration.LazyLoadingEnabled = false;
2.延迟加载时可以使用 Include来提高性能,单独的查询分页会多次调用数据库,使用Include只需要调用一次。
3.使用AsNoTracking可以提高查询性能,只不过不能对查询的数据做修改操作。
4.多字段排序连续OrderBY后面的会把前面的覆盖掉,要用OrderBy.ThenBy
5.在使用SqlQuery查询时,用Skip和Take,会导致在内存中分页,先查询全部,在取出分页数据。可以在sql语句中查询分页
6.EF调用where,时没有Any效率高