我有几个建议:
1) 确保数据库和您正在写入的数据库的日志文件都有足够的可用空间来容纳您正在写入的数据。动态调整数据库大小对于 SQL Server 来说是一项非常昂贵的操作,如果您从一个调整大小百分比很小的小型数据库开始,您将不断调整它的大小。
2)将您的插入命令包装在事务中。这应该会大大提高速度。您可能无法将所有 80k 记录包装在一个事务中,但您可以一次尝试 1000 条左右。伪代码:
Const MAX_RECORDS_PER_LOOP = 1000
Dim Counter As Integer
Dim trans As SqlTransaction
Try
For Each record In File
' If we are not in a transaction, enter one
If trans Is Nothing Then
trans = conn.BeginTransaction()
End If
' Do your insert
' Boost the counter
Counter += 1
' We have reached the max
If Counter = MAX_RECORDS_PER_LOOP Then
' Commit the previous batch and reset the values
trans.Commit()
trans = Nothing
Counter = 0
End If
Next
' Commit the last batch, if any
If trans IsNot Nothing Then
trans.Commit()
End If
Catch theException As Exception
' Report the exception
' Rollback if trans in progress
If trans IsNot Nothing Then
trans.Rollback()
End If
End Try