Results 1 to 13 of 13

Thread: can it be faster?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Is there a way to make this faster? If I comment out the two cnSQL.Execute statements, it goes lightning fast, but it is extremely slow with them there. Is there a way to tell ADO to update the Executes only once in a while instead of every time I run the execute statement? I am connecting to an SQL Server using ADO.

    Code:
      For x = 1 To ItemCounter
        cnSQL.Execute "INSERT INTO [CompletedFile] (ID) values(" & x & ")"
        
        Do Until iCounter = UBound(NewRecord)
          If NewRecord(iCounter).ContentID = x Then
            iCounter = iCounter + 1
            strSQL = strSQL & "[" & NewRecord(iCounter).ContentName & "]" & "='" & NewRecord(iCounter).ContentValue & "', "
          Else
            lngnewrecord = iCounter + 1
            Exit Do
          End If
        Loop
        strSQL = "UPDATE [CompletedFile] SET " & Left(strSQL, Len(strSQL) - 2) & " WHERE [ID] = " & x
        cnSQL.Execute strSQL
        strSQL = ""
        ProgressBar1.Value = ProgressBar1.Value + 1
        DoEvents
      Next
    Thanks,
    Thai

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Where are your cursors?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    I didn't set it, so it would be the default. is ClientSide or ServerSide cursor faster? Also, what if the SQL Server is on the machine running the program, would it matter at that point?

    Thanks,
    Thai

  4. #4
    New Member
    Join Date
    Aug 2000
    Location
    China
    Posts
    3
    If you are going to add or update a lot of records at a time,you could use the RecordSet object of ADO.
    set the cursorLocation to adUseClient,cursorType to adDynamic,LockType to adLockBatchOptimistic,then you can modify the object,and use the UpdateBatch method to update all the records in a single batch.It should be faster than your current code(not tested ).

    the code goes like this:
    dim rs as new ADODB.recordset
    with rs
    .cursorlocation = aduseclient
    .cursortype = adCursorDynamic
    .LockType = adLockBatchOptimistic
    .ActiveConnection = cn
    .Source = "select * from [Your Table]"
    end with
    rs.open

    for...
    rs.AddNew
    rs!Number = ...
    rs!Update

    while...
    rs!Text = ...
    ...
    rs.Update
    loop
    next...

    rs.UpdateBatch
    rs.close

  5. #5
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Hi Thai, this might help. This batches the operation, so you don't have to communicate with the server on each record, you can perform a batch operation. This should speed things up a bit

    Code:
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim strSQL As String
        Dim lngCounter As Long
    
        'prepare batch sql statement
        For lngCounter = 1 To 100
            strSQL = strSQL & " Insert Into TestTable ([FIELDNAME]) VALUES ('" & lngCounter & "');"
        Next lngCounter
        
        'ok, its prepared, lets connect & execute the statement 1 time
    
        cn.Open "Provider=SQLOLEDB;Data Source=W2KSRV;Integrated Security=SSPI;Initial Catalog=Northwind"
    
        cn.Execute strSQL
    
        '100 records have been inserted, only 1 cn.Execute was necessary
    [Edited by Clunietp on 08-14-2000 at 12:07 PM]

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Cool thanks guys, I tried your way EverJoe and it sped things up a lot. I am going to try your way now Clunietp and see which one is faster. I really appreciate the help!

    Thai

  7. #7
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Please let us know, I'd be interested to see the results

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Well I tried your way also Cluntp, it works but the the slowdown is no longer the sql engine, its building the string. The first few times through the string building loop its very fast, but it slows down tremendously after a while because the string is so big. Any other suggestions?

    Thanks for the help
    Thai

  9. #9
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Maybe its your object...what is: NewRecord(iCounter).ContentID ?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    NewRecord() is a user defined Type that I made, it has ContentID, ContentName, ContentValue, the ID is the unique identifier to update the table, the Name is the field name and the value is the value which can be a number of things (part number, description, unit of measure, etc).

    Thanks,
    Thai

  11. #11
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    does it appear to be the string concatenation that is giving you the slowdown, or is it your UDT?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    I believe it is the string, because it is fast at first, until the string gets exremely big then it slows down.. slower and slower..

    Thanks,
    Thai

  13. #13
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    keep a counter, and execute the SQL string after every 10 or so, then clear the string and start over

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