优化数据访问和I/O操做数据库
数据交互以及远程服务一般是程序中最慢的部分,高效的读写数据对性能尤为重要。缓存
建议操做:网络
Where
, .Select
, .Sum
等方法过滤或聚合查询,从而在数据库中进行过滤。在高性能EF中提到下面两个提升性能的方法:app
使用 DbContext 池异步
services.AddDbContextPool<BloggingContext>( options => options.UseSqlServer(connectionString));
使用已编译的查询(compiled query)分布式
// Create an explicitly compiled query private static Func<CustomerContext, int, Customer> _customerById = EF.CompileQuery((CustomerContext db, int id) => db.Customers .Include(c => c.Address) .Single(c => c.Id == id)); // Use the compiled query by invoking it using (var db = new CustomerContext()) { var customer = _customerById(db, 147); }
以上两种方式要在重复评估,性能测试以后再使用,由于已编译的查询在某些状况下可能并不会带来性能提高。工具
查询性能问题能够经过Application Insights或其余分析工具,分析数据访问耗时来发现。大多数数据库会提供频繁查询的统计信息,能够帮助开发人员进行分析。性能