保存1000+记录到数据库的时间 [英] Saving 1000+ records to the database at a time

查看:110
本文介绍了保存1000+记录到数据库的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NHibernate目前。我有一个情况我需要一束记录保存到这样的数据库:

I'm using NHibernate currently. I have a situation where I need to save a bunch of records to the database like this:

var relatedTopics = GetRelatedTopics(topic);
foreach (var relatedTopic in relatedTopics /* could be anywhere from 10 - 1000+ */)
{
    var newRelatedTopic = new RelatedTopic { RelatedTopicUrl = relatedTopic, TopicUrl = topic.Name };
    _repository.Save(newRelatedTopic);
}

当有一吨的记录保存这显然非常繁重不必访问数据库,很多次。什么是更好的方法?是否有某种形式的批量更新我能做到吗?我是最好使用DataSet?

When there are a ton of records to save this is obviously very taxing having to hit the database that many times. What's a better approach? Is there some sort of batch update I can do? Am I better off using a DataSet?

感谢

推荐答案

设置adonet.batch_size可以改善这一状况。

setting adonet.batch_size could improve the situation.

有关,你必须

  • 在NH配置设置adonet.batch_size

例如:

    m_sessionFactory = Fluently
         .Configure()
         .Database(MsSqlConfiguration
             .MsSql2005
             .ConnectionString(c => c.FromConnectionStringWithKey("testme"))
             )
         .Mappings(m => m.FluentMappings
             .AddFromAssemblyOf<TestImpl>())
         .ExposeConfiguration(config =>
         {
             config.SetProperty("adonet.batch_size", "1");
             m_configuration = config;
         })
         .BuildSessionFactory();

  • 之前设置本届会议的批量保存

    • set the batch size on the session just before the save

      using (ISession session = m_nhibernateSessionFactory.GetSession())
      using (var tx = session.BeginTransaction())
      {    
         session.SetBatchSize(1000);     
         foreach (var server in serverz)
         {
            session.SaveOrUpdate(server);
         }
         tx.Commit();
      }
      

    • 这篇关于保存1000+记录到数据库的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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