简体   繁体   English

SQL Server 2008 INSERT优化

[英]SQL Server 2008 INSERT Optimization

I've to INSERT a lot of rows (more than 1.000.000.000) to a SQL Server data base. 我必须向SQL Server数据库插入很多行(超过1.000.000.000)。 The table has an AI Id, two varchar(80) cols and a smalldatetime with GETDATE as default value. 该表具有一个AI ID,两个varchar(80)cols和一个较小的datetime(默认值为GETDATE)。 The last one is just for auditory, but necesary. 最后一个仅用于听觉,但必要。

I'd like to know the best (fastest) way to INSERT the rows. 我想知道最好(最快)的方法来插入行。 I've been reading about BULK INSERT. 我一直在阅读有关BULK INSERT的信息。 But if posible I'd like to avoid it because the app does not run on the same server where database is hosted and I'd like to keep them as isolated as posible. 但是,如果可能的话,我想避免这种情况,因为该应用程序不在托管数据库的同一服务器上运行,并且我希望将它们尽可能保持隔离。

Thanks! 谢谢!

Diego 迭戈

Another option would be bcp . 另一个选择是bcp

Alternatively, if you're using .NET you can use the SqlBulkCopy class to bulk insert data. 另外,如果您使用的是.NET,则可以使用SqlBulkCopy类批量插入数据。 This is something I've blogged about on the performance of, which you may be interested in as I compared SqlBulkCopy vs another way of bulk loading data to SQL Server from .NET (using SqlDataAdapter). 这是我关于性能的博客,在比较SqlBulkCopy与将数据从.NET(使用SqlDataAdapter)从SQL Server批量加载到SQL Server的另一种方式中,您可能会对它感兴趣。 Basic example loading 100,000 rows took 0.8229s using SqlBulkCopy vs. 25.0729s using the SqlDataAdapter approach. 使用SqlBulkCopy加载100,000行的基本示例花费了0.8229s,而使用SqlDataAdapter方法花费了25.0729s。

创建一个SSIS包,它将文件复制到SQL Server计算机,然后使用数据流任务将数据从文件导入SQL Server数据库。

没有比BULK INSERT更快/更有效的方法,当您处理如此大量的数据时,甚至不用考虑.NET中的任何内容,因为有了GC,管理内存中的数百万个对象会导致性能大幅下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM