WEB开发网
开发学院WEB开发ASP.NET NHibernate与Ado.Net查询速度的比较 阅读

NHibernate与Ado.Net查询速度的比较

 2006-03-20 17:49:07 来源:WEB开发网   
核心提示:想在开发中使用NHibernate,但担心在性能上的问题,NHibernate与Ado.Net查询速度的比较,对查询的速度和Ado.Net进行了一下简单的比较,过程和代码如下描述,http://kevin-y.cnblogs.com/archive/2006/02/08/327164.htmlhttp://wljcan

想在开发中使用NHibernate,但担心在性能上的问题,对查询的速度和Ado.Net进行了一下简单的比较。过程和代码如下描述,由于了解不深,暂不做结论,希望大家给点意见。

NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952  绑定IList时间(ms) : 70.1008
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576  绑定IList时间(ms) : 80.1152

测试环境:WinForm方式,数据库也在本机
硬件:CPU --  1.2G AMD duron(tm)      RAM -- 384M  DDR266
软件:Windows Server 2003, Sql Server 2000,Visual Studio.Net 2005

测试代码下载http://www.cnblogs.com/Files/liuyuanhuo/NHibernateTest_Performance.rar

测试的代码是在
子非鱼 NHibernate学习 http://sifang2004.cnblogs.com/archive/2005/09/05/230713.aspx
NHibernate 博客园专题之一  http://www.cnblogs.com/Files/sifang2004/NHibernateTest.rar
中下载的示例中作修改的。

测试由两个工程组成,
一.NHibernateTest工程,仅修改两个地方
 1.注释Order.hbm.xml中_Items相关的<bag元素
 2.在Order.cs增加下面两个方法


     /**//// <summary>
     /// 使用NHibernate HQL方式查询
     /// </summary>
     /// <returns>IList,订单对象集合</returns>
     public IList LoadAllByNHibernate()
     {
       IList orders = ObjectLoader.Find( " from Order ", null);
       return orders;
     }

   /**//// <summary>
     /// 使用Ado.Net DbDataAdapter方式查询
     /// </summary>
     /// <returns>DataTable,订单数据表</returns>
     public DataTable LoadAllByAdoNet()
     {
       DataTable dtOrders = new DataTable();

     DbConnection dbConnection = new SqlConnection();
       dbConnection.ConnectionString =
             @"Server=Andy\Andy;initial catalog=Northwind;User id =sa;PassWord=sa";

     DbCommand dbCommand = new SqlCommand();
       dbCommand.Connection = dbConnection;
       dbCommand.CommandType = CommandType.Text;
       dbCommand.CommandText = "SELECT * FROM Orders";

     DbDataAdapter dataAdapter = new SqlDataAdapter();
       dataAdapter.SelectCommand = dbCommand;

     dataAdapter.Fill( dtOrders );

     return dtOrders;
     }

二.添加一个NHibernateTestUI工程,引用NHibernateTest,从NHibernateTest复制一份app.config到NHibernateTestUI,修改连接串,然后添加一个窗体类,加了两个按钮一个DataGridView, 两个按钮分别测试Ado.Net DbDataAdapter方式和NHibernate HQL方式的加载时间,测试代码如下:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NHibernateTestUI
{
   public partial class OrderFrm : Form
   {
     public OrderFrm()
     {
       InitializeComponent();
     }

   NHibernateTest.Order order = new NHibernateTest.Order();

   /**//// <summary>
     /// 使用NHibernate HQL方式查询
     /// </summary>
     PRivate void btnNHLoad_Click( object sender, EventArgs e )
     {
       string strOutput = "";

     // 测试加载数据时间
       DateTime dtStart = System.DateTime.Now;
       IList orders = order.LoadAllByNHibernate(); // 使用NHibernate HQL方式查询
       DateTime dtEnd = System.DateTime.Now;
       TimeSpan ts = dtEnd - dtStart;
       strOutput += "NHibernate HQL方式 : "
         + orders.Count + " 条记录, 加载时间(ms) : "
         + ts.TotalMilliseconds;


       // 测试绑定数据时间
       dtStart = System.DateTime.Now;
       this.dgvOrders.DataSource = orders;
       dtEnd = System.DateTime.Now;
       ts = dtEnd - dtStart;
       strOutput += "  绑定IList时间 : " + ts.TotalMilliseconds;

     Console.WriteLine( strOutput );
     }

   /**//// <summary>
     /// 使用Ado.Net DbDataAdapter方式查询
     /// </summary>
     private void btnADOLoad_Click( object sender, EventArgs e )
     {
       string strOutput = "";

     // 测试加载数据时间
       DateTime dtStart = System.DateTime.Now;
       DataTable dtOrders = order.LoadAllByAdoNet();  // 使用Ado.Net DbDataAdapter方式查询
       DateTime dtEnd = System.DateTime.Now;
       TimeSpan ts = dtEnd - dtStart;
       strOutput += "Ado.Net DbDataAdapter方式 : "
         + dtOrders.Rows.Count + " 条记录; 加载时间(ms) : "
         + ts.TotalMilliseconds;

     // 测试绑定数据时间
       dtStart = System.DateTime.Now;
       this.dgvOrders.DataSource = dtOrders;
       dtEnd = System.DateTime.Now;
       ts = dtEnd - dtStart;
       strOutput += "  绑定IList时间 : " + ts.TotalMilliseconds;

     Console.WriteLine( strOutput );
     }
   }
}

运行后界面如下:

在界面两个按钮点击多次后,两者的查询时间稳定,可以作为比较。下面是输出:
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952  绑定IList时间 : 70.1008
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3304.752  绑定IList时间 : 70.1008
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3354.824  绑定IList时间 : 80.1152
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576  绑定IList时间 : 80.1152
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 40.0576  绑定IList时间 : 90.1296
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 30.0432  绑定IList时间 : 90.1296
NHibernate :select order0_.orderId as orderId, order0_.shippedDate as shippedD4_, order0_.shipPostalCode as shipPos10_, order0_.requiredDate as required3_, order0_.shipCountry as shipCou11_, order0_.shipVia as shipVia, order0_.orderDate as orderDate, order0_.shipAddress as shipAddr7_, order0_.shipRegion as shipRegion, order0_.shipName as shipName, order0_.shipCity as shipCity, order0_.customerId as customerId, order0_.freight as freight, order0_.employeeId as employeeId from Orders order0_
NHibernate HQL方式 : 830 条记录, 加载时间(ms) : 3334.7952  绑定IList时间 : 70.1008
Ado.Net DbDataAdapter方式 : 830 条记录; 加载时间(ms) : 50.072  绑定IList时间 : 90.1296


另外在网上找了一下关于NHibernate性能的讨论的有,
http://kevin-y.cnblogs.com/archive/2006/02/08/327164.html
http://wljcan.cnblogs.com/archive/2005/05/14/155495.aspx
很少,希望能有已经做过NHibernate考量的高手提供点路子,非常感谢。

Tags:NHibernate Ado Net

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接