SQL Server 2008批量插入
我有一本大约有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 lFoust 一样,我也使用 SQLBulkCopy 实用程序将相当大的 ELT 复制到某些数据库中。
下面是我编写的 SqlBulkCopy 的代码示例:
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:
这可能有点超出您正在使用的 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.