Linq to SQL的Select性能测试

前几天看到neuhawk 的文章linq to sql 的动态条件查询方法,文章最后选取了Ricom关于Linq to SQL的性能分析,里面说道Linq to SQL性能不错,有时候比ADO.NET还要好,当时觉得这分析结果难以让人相信,应该Linq to SQL底层还是用ADO.NET 2.0实现的,即使效率再高也应该不能超越。加上最近几天在MSDN论坛上看到有些人质疑Linq to SQL的效率,所以我做了一个很简单的测试,测试Linq to SQL的select性能。

  以Northwind数据库为例,对Product表进行查询(我增加了里面的数据,增加到3000条),只选取ProductID和ProductName,进行3000次查询,每次查询一条记录。首先看看使用SqlDataReader的代码:

 Stopwatch watch = new Stopwatch();
watch.Start();
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
if (conn.State != System.Data.ConnectionState.Open)
conn.Open();
for (int i = 1; i <= 3000; i++)
{
SqlCommand cmd = new SqlCommand(
"SELECT ProductID,ProductName FROM Products WHERE ProductID=" + i.ToString(), conn);
cmd.CommandType = System.Data.CommandType.Text;
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
Product p = new Product();
p.ProductID = (int)reader["ProductID"];
p.ProductName = (string)reader["ProductName"];
}
}
}
}
watch.Stop();
Console.WriteLine("[Using SqlReader] total time: " + watch.Elapsed.ToString());
这里的Product是用Linq to SQL Designer自动生成的,只有ProductID和ProductName属性。运行的结果如下:

 [Using SqlReader] total time: 00:00:00.6521155

然后对Linq to SQL进行测试,代码如下:

 Stopwatch watch = new Stopwatch();
watch.Start();
NorthwindDataContext ctx = new NorthwindDataContext();
for (int i = 1; i <= 3000; i++)
{
Product product = ctx.Products.Single(p => p.ProductID == i);
}
watch.Stop();
Console.WriteLine("[Using Linq to SQL] total time: " + watch.Elapsed.ToString());

  运行结果:[Using Linq to SQL] total time: 00:00:21.2273942
  这样看来使用Linq to SQL效率差很多。

  在上面的代码中,每进行一次查询,数据库连接都会open和close,也就是会执行一次reset_connection这个存储过程,看看能不能优化一下:

 Stopwatch watch = new Stopwatch();
watch.Start();
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
if (conn.State != System.Data.ConnectionState.Open)
conn.Open();
NorthwindDataContext ctx = new NorthwindDataContext(conn);
for (int i = 1; i <= 3000; i++)
{
Product product = ctx.Products.Single(p => p.ProductID == i);
}
}
watch.Stop();
Console.WriteLine("[Using Linq to SQL] total time: " + watch.Elapsed.ToString());

  这里在new DataContext的时候,显式的把已经打开的connection传递到这个DataContext中去,避免了每次执行都会open和close数据库连接的问题,但是运行结果差距非常之小,因为ADO.NET的数据库连接是基于连接池。

  这只是一个很简单的测试,没有用benchmark工具和方法,不能作为参考依据,不过也可以看出一些问题,Linq to SQL的运行效率肯定不如直接写SQL语句用DataReader好,前提是SQL语句相同。当然,选择Linq to SQL是选择了它的ORM,开发效率高,代码简洁美观,编译器和语言平台的支持,在这基础上要有所取舍了。

  PS:我的机器配置是:P4 2.8G,内存2.0G, 操作系统Windows Server 2003(Enterprise),数据库SQL Server 2005。

 

  经网友jyk,Adrian,jjx等提示(谢谢他们),上面的测试例子花费的时间主要在Lambda表达式上,每次查询的时候会生成Expression对象和Func的delegate对象。下面就用CompiledQuery进行测试:

 Stopwatch watch = new Stopwatch();
watch.Start();
var query = CompiledQuery.Compile((NorthwindDataContext nw, int index)
=> nw.Products.Single(p => p.ProductID == index));
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
if (conn.State != System.Data.ConnectionState.Open)
conn.Open();
NorthwindDataContext ctx = new NorthwindDataContext(conn);
for (int i = 1; i <= 3000; i++)
{
Product product = query(ctx, i);
}
}
watch.Stop();
Console.WriteLine("[Using Linq to SQL compiled] total time: " + watch.Elapsed.ToString());

 平均每次运行1秒左右,效率已经很让人满意了。

posted on 2009-12-04 17:28  J-Pei  阅读(386)  评论(1编辑  收藏  举报

导航