大Sql的切分执行
以前一直觉得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
---------------------------
http://www.cnblogs.com/squirrel_sc
1. 如有引用,请包含本段文字及原始出处。以表示对作者的尊重,且能将有相似想法的人联系起来。
2. 如无特殊说明,本文的文字代表作者的观点和想法。由于人类的记忆和连通性的限制,可能会将别人的观点和想法当成自己的(会尽量避免,但我读书少,别蒙我:D);或有人会有同样的想法(那就太好了)。若有此类情况,请联系我。我会很高兴更新内容。
3. 每篇文章会根据反馈或新的想法,随时更新。我会尽量记得更新版本号。