开发者

How can insert faster using nhibernate?

I need to insert 1 million of object in an oracle database开发者_如何学Python, right now it takes so much time to do this job, how can i insert faster this object inside the database?

I'm using this code to do it:

using (ISession session = NHibernateHelper.OpenSession())
{
  using (ITransaction tranx = session.BeginTransaction())
  {
     session.Save(movimientoPendiente);
     tranx.Commit();
  }
}

Thanks for your help.


Use a stateless session and do all the inserts in the same transaction:

using (var session = NHibernateHelper.GetSessionFactory().OpenStatelessSession())
using (var tranx = session.BeginTransaction())
{
    for(...)
    {
        var movimientoPendiente = ...;
        session.Insert(movimientoPendiente);
    }

    tranx.Commit();
}


In general ORMs are not meant for ETL-style work, such as loading 1m records. The fancy stuff they do, such as change tracking, is pretty lost there and is pretty much unneeded overhead. Straight out ADO.NET is your friend here.

But it would probably help if you used one session and one transaction for more than a single record:

using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction tranx = session.BeginTransaction())
{
     foreach (var rec in MyRecords)
     {
        session.Save(movimientoPendiente);
     }
     tranx.Commit();
}

That at least means you aren't building a somewhat expensive session and burning a rather expensive transaction every single time you send a record.


Use a single session instead of creating a new connection to the database for each of your records. Jimmy Bogard wrote a good blog post about bulk processing using NHibernate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜