So I have written an application in VBA(Excel) to interface with a Firebird Datbase. The database used to run locally but have moved it onto a hosted solution which I access over VPN. (I have a 25/4 Mb internet connection at home)
Now when it ran locally my code was sloppy and I used generated SQL statements where I would concatenate the variables into an sqlstring and then execute. I.e: "INSERT INTO BLA..& VALUE1 & VALUE2" etc.
This worked fine the, but when moved to the hosted solution, was painfully slow, and for 623 inserts took 450 seconds.
Ok so I realized I didn't do it right the first time, so I rewrote the code as a paramterized sql query, the code is below.
Set adoComm = New ADODB.Command
Set adoConn = New ADODB.Connection
Call clientcleancall
mytimer = Timer
Dim sconnect As String
sconnect = "Provider=MSDASQL.1;DSN=SOSARemoteTesting;auto_commit=true"
Dim Conn As New ADODB.Connection
adoConn.Open sconnect
adoComm.ActiveConnection = adoConn
Application.ScreenUpdating = False
adoComm.Parameters.Append adoComm.CreateParameter("p1", adChar, adParamInput, 40)
adoComm.Parameters.Append adoComm.CreateParameter("p2", adChar, adParamInput, 40)
adoComm.Parameters.Append adoComm.CreateParameter("p3", adChar, adParamInput, 35)
adoComm.Parameters.Append adoComm.CreateParameter("p4", adChar, adParamInput, 20)
adoComm.Parameters.Append adoComm.CreateParameter("p5", adChar, adParamInput, 20)
adoComm.Parameters.Append adoComm.CreateParameter("p6", adInteger, adParamInput, 50)
adoComm.Parameters.Append adoComm.CreateParameter("p7", adChar, adParamInput, 60)
adoComm.Parameters.Append adoComm.CreateParameter("p8", adChar, adParamInput, 60)
adoComm.Parameters.Append adoComm.CreateParameter("p9", adChar, adParamInput, 60)
adoComm.Parameters.Append adoComm.CreateParameter("p10", adChar, adParamInput, 60)
adoComm.Parameters.Append adoComm.CreateParameter("p11", adChar, adParamInput, 10)
adoComm.Parameters.Append adoComm.CreateParameter("p12", adChar, adParamInput, 100)
Dim s As String
s = "INSERT INTO HARDWARE_ASSETS (HW_SERIAL, HW_NAME, HW_MACH_ID, HW_CUST, HW_INSTALL," & _
"HW_INSTALL_DATE, HW_LOCATION, HW_STATUS, HW_BILLABLE, HW_OWNER, HW_VIRTUAL, HW_SOURCE) " & _
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
adoComm.CommandText = s
For lrow = 3 To 624
'flex1.Row = lRow
'flex1.Col = 0
Cells(lrow, 1).Select
adoComm.Parameters("P1").value = Selection
adoComm.Parameters("P2").value = Selection.Offset(0, 1).value
adoComm.Parameters("P3").value = Selection.Offset(0, 2).value
adoComm.Parameters("P4").value = Selection.Offset(0, 3).value
'adoComm.Parameters("P5").AppendChunk (Mid(GetString(flex1.Text), 1, 20000))
adoComm.Parameters("P5").value = Selection.Offset(0, 4).value
adoComm.Parameters("P6").value = Selection.Offset(0, 5).value
adoComm.Parameters("P7").value = Selection.Offset(0, 6).value
adoComm.Parameters("P8").value = Selection.Offset(0, 7).value
adoComm.Parameters("P9").value = Selection.Offset(0, 8).value
adoComm.Parameters("P10").value = Selection.Offset(0, 9).value
adoComm.Parameters("P11").value = Selection.Offset(0, 10).value
adoComm.Parameters("P12").value = Selection.Offset(0, 11).value
adoComm.Execute s, , adExecuteNoRecords
Next lrow
bcommited = True
adoConn.Close
Application.ScreenUpdating = True
MsgBox (Timer - mytimer)
Now a BIG improvement in my execution time, but still 621 Inserts requires 250 seconds.
To access the server I must authenticate through a firewall, on top of the VPN. Some trouble shooting I have done is pinging the server, and the first packet always takes ~200ms, then the rest take about 100. Average is usually around 150ms. The firebird database is used by large organizations, with some as big a Terabyte, used in production, so I don't think the file-based nature of the database is the culprit.
My concrete questions:
1) How long should my code take to execute, and is the current 250 too long?
2) Is there anything else I can do to streamline my code?
3) If 1 & 2 are not the culprit I'd like to discuss things with my network admin as think the high ping times may be compounding the problem?
Thanks for your help .