NHibernate性能插入

7 nhibernate

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

我正在使用mssql2005并使用northwind db.我创建了1000个对象并将它们插入到数据库中.我已将batch_size的值从5更改为100,但未发现性能有任何变化.我的价值大约是300毫秒.使用sql profiler,我在服务器端看到1000个sql insert语句.请帮忙.

的app.config

 <property name="adonet.batch_size">10</property>
Run Code Online (Sandbox Code Playgroud)

    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;
    }
Run Code Online (Sandbox Code Playgroud)

Chr*_*ell 3

以下是一篇关于 Hibernate 批处理的精彩文章,这是 NHibernate 的基础并密切遵循:

http://relation.to/Bloggers/BatchProcessingInHibernate

正如您所看到的,建议的操作是在配置中设置合理的批量大小(您已经完成了),但也每 20 条左右的记录 session.flush()进行调用。session.clear()

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