C# – ADO.NET的一些高性能最佳实践/技巧是什么?

我决定不使用orm并将直接使用ADO.NET作为我的项目。 我知道我知道它需要更长的时间来编程,但我只是希望页面即使在高峰时也能以高速加载。

我一遍又一遍地看到一个错误:

在紧密循环中重复调用的方法中实例化并设置所有内容(DbConnection,DbCommand,DbParameters)。

大多数情况下,您可以将这些局部变量重构为类成员,只将它们实例化一次,并仅保留方法内的DbParameters更新。


更新以包含评论中要求的示例代码

免责声明:这是一个快速组装的样本,其唯一目的是展示关于将重复的东西移出循环的观点。 其他更好的做法未必实施。

public static void Show() { List people = new List(); //Everything is done inside the loop PersonDal personDal = new PersonDal(); foreach (Person person in people) { personDal.Insert(person); } //Things are setup once outside the loop. using (DbConnection connection = SqlClientFactory.Instance.CreateConnection()) { // setup the connection BetterPersonDal betterPersonDal = new BetterPersonDal(connection); connection.Open(); foreach (Person person in people) { betterPersonDal.Insert(person); } } } } class Person { public int Id { get; set; } public string Name { get; set; } } 

在第一个实现中,每次调用方法时都会设置每个东西:

 class PersonDal { public int Insert(Person person) { DbProviderFactory factory = SqlClientFactory.Instance; using (DbConnection connection = factory.CreateConnection()) { connection.Open(); connection.ConnectionString = "Whatever"; using (DbCommand command = connection.CreateCommand()) { command.CommandText = "Whatever"; command.CommandType = CommandType.StoredProcedure; DbParameter id = command.CreateParameter(); id.ParameterName = "@Id"; id.DbType = DbType.Int32; id.Value = person.Id; DbParameter name = command.CreateParameter(); name.ParameterName = "@Name"; name.DbType = DbType.String; name.Size = 50; name.Value = person.Name; command.Parameters.AddRange(new DbParameter[] { id, name }); return (int)command.ExecuteScalar(); } } } } 

现在我们将设置移动到对象构造,使其离开循环:

 class BetterPersonDal { private DbProviderFactory factory; private DbConnection connection; private DbCommand command; private DbParameter id; private DbParameter name; public BetterPersonDal(DbConnection connection) { this.command = connection.CreateCommand(); this.command.CommandText = "Whatever"; this.command.CommandType = CommandType.StoredProcedure; this.id = command.CreateParameter(); this.id.ParameterName = "@Id"; this.id.DbType = DbType.Int32; this.name = command.CreateParameter(); this.name.ParameterName = "@Name"; this.name.DbType = DbType.String; this.name.Size = 50; this.command.Parameters.AddRange(new DbParameter[] { id, name }); } public int Insert(Person person) { this.id.Value = person.Id; this.name.Value = person.Name; return (int)command.ExecuteScalar(); } } 

查看“ 改进.NET应用程序性能和可伸缩性”一书(可在MSDN中免费在线获取)。 有一整章关于改进ADO.NET性能。

对连接管理非常聪明。 打开数据库连接可能非常昂贵,因此在编写数据库访问层时请记住这一点。

如果您不打算使用ORM,您是否也不会缓存数据? 这是使用ORM的一大优势。 如果没有数据缓存,您需要查看缓存HTML / JavaScript的方法。 这可以使用OutputCache指令和SqlDependency来完成。 或者通过发布静态HTML和JavaScript文件。 无论哪种方式,如果您不是每次请求都不经常访问数据库,您将能够处理更高的负载。

一些链接:

ASP.NET网站性能改进http://www.codeproject.com/KB/aspnet/aspnetPerformance.aspx

10 ASP.NET性能和可伸缩性秘诀http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx

关于ASP.NET Ajax和.NET 3.5的Omar AL Zabir博客http://msmvps.com/blogs/omar/archive/tags/performance/default.aspx

马丁链接的文章很棒。 我只想补充一点,你肯定想使用DataReader而不是DataSet(我喜欢DataSet,但不是出于性能原因)。

看这些文章:

ADO.NET和SQL Server性能提示

调整.NET应用程序性能

ADO.NET性能