SQL Server 2008批量插入

发布于 2024-10-17 06:59:33 字数 1581 浏览 2 评论 0原文

我有一本大约有50,000个单词的字典;每个单词都有许多同义词、反义词等。我正在使用 Fluent NHibernate,并且我已经为应用程序创建了一个 10gb MS SQL Server 实例,我正在尝试将其填充为批量更新:

public class Word
{
     public virtual int Id { get; set; }
     public virtual string Text { get; set; }
     public virtual IList<Word> Synonyms { get; set; }
     public virtual IList<Word> Antonyms { get; set; }
}

public class WordMapping : ClassMap<Word>
{
    public WordMapping()
    {
        Id(x => x.Id).UnsavedValue(0);
        Map(x => x.Text);

        HasMany(x => x.Synonyms).Cascade.AllDeleteOrphan();
        HasMany(x => x.Antonyms).Cascade.AllDeleteOrphan();
    }
}
...

List<Word> words = loadWordsFromFile();

using (IStatelessSession session = session.SessionFactory.OpenStatelessSession())
    using (var transaction = session.BeginTransaction())
        {
            foreach (var word in words)
                   session.Insert(word);
            transaction.Commit();
        }

我已经设置了批量大小到 1000:

 private static ISessionFactory CreateSessionFactory()
 {
    return Fluently.Configure()
                .Database(MsSqlConfiguration
                         .MsSql2008
                         .ConnectionString(connStr)
                         .AdoNetBatchSize(1000))
                .Mappings(M => M.FluentMappings.AddFromAssemblyOf<WordMapping>())
                .ExposeConfiguration(Cfg => _configuration = Cfg)
                .BuildSessionFactory();
 }

它已经运行了几个小时,看不到结束的迹象。这是填充数据库的最佳方法还是在处理大型数据集时是否有更方便的方法?

I have a dictionary of about 50,000 words; each word has-many synonyms, antonyms, etc. I'm using Fluent NHibernate and I've created a 10gb MS SQL Server instance for the app and I am trying to populate it as a batch update:

public class Word
{
     public virtual int Id { get; set; }
     public virtual string Text { get; set; }
     public virtual IList<Word> Synonyms { get; set; }
     public virtual IList<Word> Antonyms { get; set; }
}

public class WordMapping : ClassMap<Word>
{
    public WordMapping()
    {
        Id(x => x.Id).UnsavedValue(0);
        Map(x => x.Text);

        HasMany(x => x.Synonyms).Cascade.AllDeleteOrphan();
        HasMany(x => x.Antonyms).Cascade.AllDeleteOrphan();
    }
}
...

List<Word> words = loadWordsFromFile();

using (IStatelessSession session = session.SessionFactory.OpenStatelessSession())
    using (var transaction = session.BeginTransaction())
        {
            foreach (var word in words)
                   session.Insert(word);
            transaction.Commit();
        }

I've set the batch size to 1000:

 private static ISessionFactory CreateSessionFactory()
 {
    return Fluently.Configure()
                .Database(MsSqlConfiguration
                         .MsSql2008
                         .ConnectionString(connStr)
                         .AdoNetBatchSize(1000))
                .Mappings(M => M.FluentMappings.AddFromAssemblyOf<WordMapping>())
                .ExposeConfiguration(Cfg => _configuration = Cfg)
                .BuildSessionFactory();
 }

It has been running for hours with no end in sight. Is this the best way to populate my database or is there a more expedient approach when dealing with large datasets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-10-24 06:59:33

与 lFoust 一样,我也使用 SQLBulkCopy 实用程序将相当大的 ELT 复制到某些数据库中。

下面是我编写的 SqlBulkCopy 的代码示例:

SqlConnection sqlCon = new SqlConnection("ConnectionStringHere");
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlCon);
DataTable dt = new DataTable();

dt.Rows.Add(// add data to the datatable)

using (sqlBulkCopy)
{

.DestinationTableName = "Putnameoftablehere";
.NotifyAfter = dt.Rows.Count/100; //Notify every 1%
.WriteToServer(dt);
.Close();


}

Like lFoust i also use the SQLBulkCopy utility for quite large ELT into some databases.

Below is a Code Example of the SqlBulkCopy i wrote:

SqlConnection sqlCon = new SqlConnection("ConnectionStringHere");
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlCon);
DataTable dt = new DataTable();

dt.Rows.Add(// add data to the datatable)

using (sqlBulkCopy)
{

.DestinationTableName = "Putnameoftablehere";
.NotifyAfter = dt.Rows.Count/100; //Notify every 1%
.WriteToServer(dt);
.Close();


}
┊风居住的梦幻卍 2024-10-24 06:59:33

这可能有点超出您正在使用的 NHibernate 数据访问层,但是当我们将大量数据加载到我团队的数据库中(我们有几个 20+ TB 的数据库)时,我们使用 SqlBulkCopy。它不是最令人兴奋的技术(使用数据表等),但它工作得非常好而且快。

This may be a bit outside of the NHibernate data access layer you are using but when we load large amounts of data into a database on my team (we have several 20+ TB databases) we use SqlBulkCopy. It isn't the most exciting technology wise (using DataTables etc) but it works really well and fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文