论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

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

文章类别:Asp.net | 发表日期:2008-10-5 21:53:56

想在开发中使用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考量的高手提供点路子,非常感谢。

上一篇:{应用}ADO.NET起步 人气:4743
下一篇:{应用}通过DataTable获得表的主键 人气:5115
视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058