首页 | IT新闻 | 硬件 | 操作系统 | 开发 | 网络编程 | 数据库 | 热门框架 | 网络安全 | 组网 | 建站指南 | 网页制作 | 特效 | 实用技巧 | 服务器 | 办公 | QQ | 探索 | 社区

  • 技术部落
  • 部落首页 > 热门框架 > Hibernate开源框架 > 正文
  • NHibernate与Ado.Net查询速度的比较
      2007-2-26  来源:网络资源  编辑:Jsbulo  热度:

    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