Inserting records from Excel to SQL server slows down terribly on Windows 7

Posted on 2010-11-20
Last Modified: 2012-05-10

I am troubleshooting a VBA script running on 64 bit computer.  The Maco runs in Excel on the same box as the SQL server it is inserting into in both cases.  Both computers in this example have 8 cores and 48 GB of memory.

On Windows XP 64 bit, it inserts 10,000 records in about 5 seconds.  
On Windows 7 64 Bit, it inserts the same 10,000 records in 42 seconds.
Originally, both computers has Excel 2003 and SQL Server 2005.  in an effort to solve the slowdown on the Windows 7 box, we upgraded to Excel 2010 and SQL Server 2008 R2.  It made no measurabe difference.  Regardless of the Excel or the SQL version, the Windows 7 box inserts the records almost 10 times slower in this case. (It seems to run great in every other respect.)  The references in VBA are the same (ADO 2.8 etc.) with the exception of the Excel add in which is 11 on one and 14 on the other.  Again, the version of Excel seems to make no difference.  I put msgboxes after the steps.  All of it is almost instant on both boxes.  It's the loop toward the end after "Getting ready to insert" that takes significantly longer on Windows 7.  

 The code looks like this:

Private Sub CommandButton1_Click()
    Dim cnSQL As ADODB.Connection
    Dim rsXLS As ADODB.Recordset
    Dim fromSQL As String
    Dim toSQL As String
    Dim Index As Integer
    Dim XLSDrive As String
    Dim comADO As ADODB.Command
    Dim rsDB As ADODB.Recordset
    Dim strSQL As String
    Dim strConn As String

    XLSDrive = "C:\Test\D_Input_Daily.xls"
  Set cnSQL = New ADODB.Connection
  strConn = "Driver={SQL Server};Server=(local);Database=TestDB;Trusted_Connection=yes;"
  cnSQL.Open strConn

MsgBox "Opened SQL"

   Set cnXLS = New ADODB.Connection
    cnXLS.Provider = "Microsoft Jet 4.0 OLE DB Provider"
    cnXLS.ConnectionString = "Data Source = " & XLSDrive & "; Extended Properties=Excel 8.0"

MsgBox "Opened Excel"

     fromSQL = "SELECT [Date], [R1D], [PVOL], [P], [MSHS], [PSHS] FROM [Data$C1:H10000]"
    Set rsXLS = New ADODB.Recordset
    rsXLS.Open fromSQL, cnXLS, adOpenStatic, adLockReadOnly, adCmdText
   toSQL = "SELECT top 0 * FROM RTable"
   Set rsDB = New ADODB.Recordset
   rsDB.Open toSQL, cnSQL, adOpenDynamic, adLockBatchOptimistic, adCmdText
MsgBox "Selected Excel Records.  Getting ready to insert"
  If Not rsXLS.EOF Then
    Do While Not rsXLS.EOF
        For Index = 0 To rsXLS.Fields.Count - 1
            rsDB.Fields(rsXLS.Fields(Index).Name).Value = rsXLS.Fields(Index).Value
        Next Index
        rsDB.Fields(1).Value = sCUSIP
End If
   MsgBox "Finished"
End Sub
Question by:mikerich61
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 75

Accepted Solution

Anthony Perkins earned 250 total points
ID: 34181075
While your attempt to speed the process up by using Batch Optimistic is noble, it is somewhat defeated by the fact that the batch is only one row. :)  A better approach would be to load the entire recordset using GetRows, rather than using MoveNext to retrieve every row.

But, have you condidered that it may be time to start using a faster approach then the old ADO "one-row-at-a-time" method:
Perhaps using  linked server and importing the data in directly with a single INSERT statement.  Of course, since there is (to my knowledge) no 64-bit JET driver you will have to drop down to 32-bit mode.  
Another solution you should consider is using SSIS.  
Finally, you can use .NET to load the whole spreadsheet in a table and then pass that table to do a single INSERT statement.

Anything better than what you are currently doing.
LVL 34

Assisted Solution

Norie earned 250 total points
ID: 34183067
Can't you just use OPENROWSET or BULK INSERT?

This is some sample code, but it doesn't deal with anywhere near as many records as you are and the queries are pretty simple.

Here's another example which use the ACE 12.0 driver.

INSERT INTO StaffLevel.dbo.XLImport178
([Account], [Field1])

SELECT [Account], [Field1]

FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0', 'Data Source=C:\MockData\AccountNos.xls;Extended Properties=Excel 12.0')...[tblAccounts$]

That's copied from SSMS - I keep on messing up the concatentation in VBA to get the right syntax.

By the way I these might be linked to linked servers - some of the errors you get when testing certainly seem to day so.:)


Dim cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
    Set cn = New ADODB.Connection
    cn.Open "Provider=SQLOLEDB;Data Source=NORIE-TOSH;" & _
            "Initial Catalog=StaffLevel;Trusted_Connection=yes"

    'Import by using OPENROWSET and SELECT query.
    strSQL = "INSERT INTO XLImport1 ([Account], [Field2]) SELECT [Account], [Field2] FROM " & _
             "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
             "'Excel 8.0;Database=C:\MockData\AccountNos.xls', " & _
             "'SELECT * FROM [tblAccounts$]')"

    Debug.Print strSQL
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
    Debug.Print "Records affected: " & lngRecsAff

Open in new window


Author Closing Comment

ID: 34300996
Thanks for the advice.  There is a bunch of code that exists so I was hoping that the solution would answer the question why SQL on the Windows 7 machine takes 10 times longer.  We will begin working on the code per the suggestions!
LVL 34

Expert Comment

ID: 34301367
I've used SQL Server on Windows Vista and 7, maybe even on Win98.

Never noticed any problems with speed on 7.

Perhaps it's more a network issue than anything?

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question