Results 1 to 22 of 22

Thread: Very Large Insert

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Very Large Insert

    Hey Guys,

    I have to populate an MS SQL database for a client, he has a database (.dbf) that his store uses and I am writing an app that is reading all of those record and insert them in a MS SQL database for his online store.

    There's around 50 000 record and so far I open his .dbf database, go thru all the record and thought that it would be better to build a file with all my insert in it instead of doing it every loop.

    But when it commes time to send it to the sql database, it takes very long and it looks like it hangs.

    Here's my function that updates the database:

    VB Code:
    1. Private Sub updateWebDatabase()
    2.   Dim sqlStatement As String
    3.   Dim fso As New FileSystemObject
    4.   Dim fle As TextStream
    5.  
    6.   lblProgress.Caption = "Saving to Web Database"
    7.   lblProgress.Refresh
    8.  
    9.   Set fle = fso.OpenTextFile(sqlTxtPath)
    10.  
    11.   sqlStatement = fle.ReadAll
    12.  
    13.   Set fle = Nothing
    14.   Set fso = Nothing
    15.  
    16.   Call openDB("MsSQL")
    17.     Set rs = conn.Execute(sqlStatement)
    18.   Call closeDB
    19. End Sub

    Is there a way to submit my file to the sql database or is there a better way than sending it 50 000 insert's ?

    Thanks a lot

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Very Large Insert

    Have you considered using a DTS package to do the load?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    Quote Originally Posted by DKenny
    Have you considered using a DTS package to do the load?
    DTS ?

    Not very familiar with this, please explain...

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Very Large Insert

    Are you using MS SQLServer?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    Yes, I am, sorry for not specifying in first post.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Very Large Insert

    DTS - Data Trasformation Services is an integrated component in SQLServer and is very useful for bulk loading of data.
    Have a look in BOL, there is an extensive section dedicated to DTS.
    Attached Images Attached Images  
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    The problem is I don't have access to the sql server, what I mean is that the server is on our hosting company server, so I don't think I can use DTS, which is what I would need.

    Heck, I can't even use BULK INSERT's

    Any other ideas ?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Very Large Insert

    Why cant you use the sql BULK INSERT sql statement?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Very Large Insert

    Probably because the "path" to the BULK INSERT file is "server centric" and with a remote server, there is no way for that server to see a "local" file.

    50,000 INSERTS - done one at a time - will be slow. Maybe putting a DOEVENTS in the LOOP that does the INSERT's will make it stop appearing to freeze.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    Lively Member
    Join Date
    Mar 2001
    Location
    Norway
    Posts
    88

    Re: Very Large Insert

    How about doing the update in a seperate thread?

    Check out Multithreaded Programming
    - zen

  11. #11

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    RobDog888: Because I don't have permission and it's soemthing they will look into when they upgrade.

    I had to go to the one by one route, because I need to finish this ASAP, but now I am looking into how I can make it go faster, like using adUserClient for the cursorLocation made it at least twice as fast.

    Any other ideas to make it go faster ? What kind of cursor type ? I never really paid much attention to the cursorType and lockType...

    ZenMan: Thanks, but that's something I should of look into at the beginning, I don't have time anymore

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Very Large Insert

    Some things to try.

    Don't use the Connection.Execute method, use Command.Execute. It will allow you to create a prepared statement, which means the sql is only parsed once and sql server will create an execution plan. I don't think you will gain anything by creating a file of sql statements first.

    Since you are doing an Insert don't create a recordset, use the adExecuteNoRecords option.

    VB Code:
    1. Private Sub updateWebDatabase()
    2.   Dim sqlStatement As String
    3.   Dim cmd as ADODB.Command
    4.  
    5.   Dim fso As New FileSystemObject
    6.   Dim fle As TextStream
    7.  
    8.   lblProgress.Caption = "Saving to Web Database"
    9.   lblProgress.Refresh
    10.  
    11.   Call openDB("MsSQL")
    12.    
    13.   Set cmd = New ADODB.Command
    14.  
    15.   With cmd
    16.      Set .ActiveConnection = conn
    17.      .CommandText = "Insert Into TableName Values(?,?,?,?) 'assumes table contains 4 fields
    18.      .CommandType = adCmdText
    19.      .Prepared = True
    20.      'create a parameter for each field
    21.      .Parameters.Append .CreateParameter("@Field1", adVarChar, adParamInput,30)
    22.      .Parameters.Append .CreateParameter("@Field2", adInteger, adParamInput)
    23. end With
    24.  
    25.   For each record in DBF Table
    26.       cmd.Parameters("@Field1").Value = data from DBF
    27.       '..etc
    28.       cmd.Execute lngRowsAffected,,adExecuteNoRecords
    29.   Next
    End Sub

    It is probably possible to send 2 inserts at once using something like
    .CommandText = "Insert Into TableName Values(?,?,?,?); Insert Into TableName Values(?,?,?,?)"

    This of course means you must double up on the parameters.

    Adding a Begin Trans on the connection object should speed up things as well. Maybe call commit every 1000 records or so.

  13. #13

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    I like that !

    I can do the same with updates, right ? Just need to set two command ? cmdAdd and cmdUpdate...

    Thanks, I will try that.

  14. #14

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    One question bruce,

    What is the lngRowsAffected in the cmd.execute ?

    Thanks

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Very Large Insert

    Bruce - I've never seen that before...

    Do you really just put ?-marks in the INSERT VALUES () and then refer to them as @FIELD1, @FIELD2 and so on??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    Quote Originally Posted by szlamany
    Bruce - I've never seen that before...

    Do you really just put ?-marks in the INSERT VALUES () and then refer to them as @FIELD1, @FIELD2 and so on??
    Looks like it, pretty cool

  17. #17

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    Ok, I made it work, but I do not see a big difference compare to

    Conn.Execute("INSERT INTO...")

    Hmm, I thought it should be faster.

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Very Large Insert

    Are you inserting into an existing table?

    Does that table has indexes and such??

    If so, INSERT into a staging table - an empty table - with no INDEXES.

    That should run faster then one-by-one inserts into a production table.

    Then after you load the staging table do an:

    INSERT INTO REALTABLE SELECT * FROM STAGINGTABLE

    Just a thought...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  19. #19

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    yes, into an existing table.

    I did not created indexes, I created my db in access then use the upsize wizard to put it on my sql db.

    the problem is I will need to to insert and updates, mostly insert for the first time, then it will be mostly updates.

    It's an app to update my client online store database with his store database.

    Thanks for all your help.

  20. #20
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Very Large Insert

    Do you really just put ?-marks in the INSERT VALUES () and then refer to them as @FIELD1, @FIELD2 and so on??
    The ? are considered parameter placeholders. The parameter names ("@Field1" etc.) can be anything you want. I usually use the name of the table column.

    Note that in ADO.NET, the data provider for sql server does not support the ? placeholder but requires named parameters.

  21. #21
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Very Large Insert

    What is the lngRowsAffected in the cmd.execute ?
    This argument will indicate how many records the sql command modified. For inserts it should always be 1. However, for updates it could obviously be any number.

  22. #22

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Re: Very Large Insert

    Quote Originally Posted by brucevde
    This argument will indicate how many records the sql command modified. For inserts it should always be 1. However, for updates it could obviously be any number.
    Thanks, I just omit it since it's optional.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width