NHibernate性能插件 [英] NHibernate performance insert

查看:36
本文介绍了NHibernate性能插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nhibernate进行一些测试,并且正在修改batch_size以获得批量插入.

I'm doing some tests with nhibernate and I'm modifying batch_size to get bulk inserts.

我正在使用mssql2005和Northwind数据库. 我创建了1000个对象并将其插入数据库.我已将batch_size的值从5更改为100,但发现性能没有变化.我得到的价值约为300毫秒.使用sql探查器,我在服务器端看到了1000条sql插入语句.请帮忙.

I'm using mssql2005 and using the northwind db. I created 1000 object and insert them to the database. I've changed the values of batch_size from 5 to 100 but found no change in the performance. I'm getting value of around 300ms. Using the sql profiler, I see that 1000 sql insert statements at the sever side. Please help.

 <property name="adonet.batch_size">10</property>

代码

    public bool MyTestAddition(IList<Supplier> SupplierList)
    {
        var SupplierList_ = SupplierList;
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        using (ISession session = dataManager.OpenSession())
        {  
            int counter = 0;
            using (ITransaction transaction = session.BeginTransaction())
            {

                foreach (var supplier in SupplierList_)
                {
                    session.Save(supplier);                       
                }
               transaction.Commit(); 
            }

        }
        stopwatch.Stop();
        Console.WriteLine(string.Format("{0} milliseconds. {1} items added",
                            stopwatch.ElapsedMilliseconds,
                            SupplierList_.Count));
        return true;
    }

推荐答案

以下是有关Hibernate中批处理的精彩文章,这是NHibernate所基于并紧随其后的:

The following is a great post on batch processing in Hibernate, which is what NHibernate is based upon and closely follows:

http://relation.to/Bloggers/BatchProcessingInHibernate

如您所见,建议的操作是在配置中设置合理的批处理大小(已完成),但每20条左右记录也调用一次session.flush()session.clear().

As you can see, the suggested actions are to set a reasonable batch size in the config, which you have done, but to also call session.flush()and session.clear() every 20 or so records.

我们自己采用了这种方法,现在可以在几秒钟内创建和保存1000多个对象.

We have employed this method ourselves and can now create and save 1000+ objects in seconds.

这篇关于NHibernate性能插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆