NHibernate 性能插入 [英] NHibernate performance insert

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

问题描述

我正在用 nhibernate 做一些测试,我正在修改 batch_size 以获得批量插入.

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

我使用的是 mssql2005 和北风数据库.我创建了 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

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

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天全站免登陆