NHibernate 无状态会话的插入很慢 [英] Inserts of stateless session of NHibernate are slow

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

问题描述

这几天我一直在努力改进 NHibernate Insert 的性能.

It's been a couple of days that I'm working on improving NHibernate Insert performance.

我在很多帖子(例如这个)中读到过无状态会话每秒可以插入 1000~2000 条记录......但是它可以插入 1243 条记录的最佳时间对我来说超过 9 秒:

I'd read in many posts (such as this one) that stateless session can insert like 1000~2000 records per second.... However the best time that it could insert 1243 records is more than 9 seconds for me :

var sessionFactory = new NHibernateConfiguration().CreateSessionFactory();
using (IStatelessSession statelessSession = sessionFactory.OpenStatelessSession())
{
   statelessSession.SetBatchSize(adjustmentValues.Count);

   foreach (var adj in adjustmentValues)
      statelessSession.Insert(adj);
}

班级:

public partial class AdjustmentValue : PersistentObject, IFinancialValue
{
    public virtual double Amount { get; set; }
    public virtual bool HasManualValue { get; set; }
    public virtual bool HasScaleValue { get; set; }
    public virtual string Formula { get; set; }
    public virtual DateTime IssueDate { get; set; }

    public virtual CompanyTopic CompanyTopic { get; set; }
}

班级地图:

public class AdjustmentValueMap : ClassMap<AdjustmentValue>
{
    public AdjustmentValueMap()
    {
       Id(P => P.Id);

       Map(p => p.Amount);
       Map(p => p.IssueDate);
       Map(p => p.HasManualValue);
       Map(p => p.HasScaleValue);
       Map(p => p.Formula);

       References(p => p.CompanyTopic)
           .Fetch.Join();
    }
}

我错过了什么吗?知道如何加快插入速度吗?

Am I missing something? Any idea how to speed up the inserts?

生成的查询将如下所示:

The generated queries will be same as below :

推荐答案

从 NHProf 结果的外观来看,您正在使用身份作为 POID.因此,您无法利用批量写入.每个插入/更新/删除都是一个单独的命令.这就是为什么需要这么长时间.

from the looks of your NHProf results you are using identity as your POID. Therefore you cannot take advantage of batched writes. every insert/update/delete is a separate command. that is why it's taking so long.

如果您将 POID 更改为 hilo、guid 或 guid.comb 并将批量大小设置为 500 或 1000,那么您将看到写入时间的显着改善.

if you change your POID to hilo, guid or guid.comb and set the batch size to 500 or 1000 then you will see a drastic improvement in the write times.

这篇关于NHibernate 无状态会话的插入很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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