以前一直觉得sql应该减少传递次数,减少和数据库的通信,这样可以提高效率。但现在发现,凡事都有个度的问题,由于业务需要一次性插入大概4000条数据,如果有并发操作,很容易就造成sql超时错误。于是就写了一个能够拆分大sql执行的函数。
'执行大sql语句,传入为StringBuilder以提高效率。
Public Shared Sub ExecuteBigSql(ByRef sbSql As System.Text.StringBuilder, Optional ByVal startWord As String = "insert into", Optional ByVal groupNumber As Integer = 500)
Dim str As String
Dim bGoOn As Boolean = True
Dim iStart As Integer = 0
Dim iNext As Integer = 0
str = sbSql.ToString()
While (bGoOn)
iStart = iNext
For i As Integer = 0 To groupNumber
iNext = str.IndexOf(startWord, iNext)
If (iNext < 0) Then
bGoOn = False
Exit For
Else
iNext += startWord.Length
End If
Next
If (iNext >= 0) Then
iNext -= startWord.Length
End If
If (iNext > 0) Then
ExecuteSql(str.Substring(iStart, iNext - iStart))
Else
ExecuteSql(str.Substring(iStart))
End If
End While
End Sub
Public Shared Sub ExecuteBigSql(ByRef sbSql As System.Text.StringBuilder, Optional ByVal startWord As String = "insert into", Optional ByVal groupNumber As Integer = 500)
Dim str As String
Dim bGoOn As Boolean = True
Dim iStart As Integer = 0
Dim iNext As Integer = 0
str = sbSql.ToString()
While (bGoOn)
iStart = iNext
For i As Integer = 0 To groupNumber
iNext = str.IndexOf(startWord, iNext)
If (iNext < 0) Then
bGoOn = False
Exit For
Else
iNext += startWord.Length
End If
Next
If (iNext >= 0) Then
iNext -= startWord.Length
End If
If (iNext > 0) Then
ExecuteSql(str.Substring(iStart, iNext - iStart))
Else
ExecuteSql(str.Substring(iStart))
End If
End While
End Sub
startWord参数为每个单独sql语句的开始特征。这种情况一般都是insert语句。这里要注意大小写的问题,否则不会被拆开的,倒不会执行不了。
groupNumber为分组的大小。还没上线测试,现在估计500句,经过实践检验再调整。
单独执行sql的部分ExecuteSql很简单,详情请参考:
http://www.cnblogs.com/squirrel_sc/archive/2004/11/04/60447.html