网上找了相类似情况的文章,有需要的可以参考
https://www.jianshu.com/p/d8c08a7f2099
以下是按我之前的思路,分批写insert字符串,循环插入,7万多条数据我的机器插入到内网服务器mysql库大约12秒,还是可以接受了。
按1000条分割insert语句12秒,10000条就要25秒了。
Dim xx
tt = Timer
lr = Range("a" & Rows.Count).End(xlUp).Row
'lr = 4000
arr = Range("a1:f" & lr)
fg = Cells(1, 7) '----------------输入准备分割记录数,按1000条记录分割效率比较高
ls = lr / fg '1000
If ls = Fix(ls) Then
sjxb = ls
Else
sjxb = Fix(ls) + 1
End If
ReDim xx(1 To sjxb)
'insert_str = ""
If UBound(arr) > fg Then
szsx = fg
Else
szsx = UBound(arr)
End If
For i = 2 To szsx 'UBound(arr)
If arr(i, 6) = True Then
arr(i, 6) = 1
Else
arr(i, 6) = 0
End If
arr(i, 2) = Replace(arr(i, 2), "'", "\'")
arr(i, 3) = Replace(arr(i, 3), "'", "\'")
temp = "('" & arr(i, 1) & "','" & arr(i, 2) & "','" & arr(i, 3) & "','" & arr(i, 4) & "','" & arr(i, 5) & "'," & arr(i, 6) & ")"
If xx(1) = "" Then
xx(1) = temp '"('" & arr(i, 1) & "','" & arr(i, 2) & "','" & arr(i, 3) & "','" & arr(i, 4) & "','" & arr(i, 5) & "'," & arr(i, 6) & ")"
Else
'xx(1) = xx(1) & ",('" & arr(i, 1) & "','" & arr(i, 2) & "','" & arr(i, 3) & "','" & arr(i, 4) & "','" & arr(i, 5) & "'," & arr(i, 6) & ")"
xx(1) = xx(1) & "," & temp
End If
Next
For j = 2 To sjxb
qshs = (j - 1) * fg + 1
jzhs = j * fg
If jzhs > UBound(arr) Then jzhs = UBound(arr)
For i = qshs To jzhs
If arr(i, 6) = True Then
arr(i, 6) = 1
Else
arr(i, 6) = 0
End If
arr(i, 2) = Replace(arr(i, 2), "'", "\'")
arr(i, 3) = Replace(arr(i, 3), "'", "\'")
temp = "('" & arr(i, 1) & "','" & arr(i, 2) & "','" & arr(i, 3) & "','" & arr(i, 4) & "','" & arr(i, 5) & "'," & arr(i, 6) & ")"
If xx(j) = "" Then
xx(j) = temp '"('" & arr(i, 1) & "','" & arr(i, 2) & "','" & arr(i, 3) & "','" & arr(i, 4) & "','" & arr(i, 5) & "'," & arr(i, 6) & ")"
Else
'xx(j) = xx(j) & ",('" & arr(i, 1) & "','" & arr(i, 2) & "','" & arr(i, 3) & "','" & arr(i, 4) & "','" & arr(i, 5) & "'," & arr(i, 6) & ")"
xx(j) = xx(j) & "," & temp
End If
Next
Next
'MsgBox Timer - tt
Call lj_mysql '-----------连接MYSQL数据库
For k = 1 To sjxb
Set rst = New ADODB.Recordset
With rst
.Open "insert into inventory (cinvcode,cinvname,cinvstd,cinvccode,cinvm_unit,sx) values " & xx(k), conn, 1, 3, adCmdText
End With
Next
conn.Close
Set rst = Nothing
Set conn = Nothing
Erase arr
Erase xx
MsgBox "O K!" & Timer - tt
|