Results 1 to 22 of 22

Thread: [RESOLVED] VB6+SQL Server 2005: Code or DB Optimization

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Resolved [RESOLVED] VB6+SQL Server 2005: Code or DB Optimization

    is there anyway you can help me optimize this code. the slow movement of rs is so visible that it take time to display the other row.

    Code:
    Call opencon
    Set rsbookmark = New ADODB.Recordset
    
    If CLng(txtIDRef) > 0 Then
        rsbookmark.Open "tblsuppliers", conPC, adOpenKeyset, adLockReadOnly
        rsbookmark.Find "pk_supplierid =" & txtIDRef & ""            'find previous bookmark
        
        rsbookmark.MovePrevious
        
        'chk if the next record= BOF
        If rsbookmark.BOF Then
            MsgBox strBOF, vbInformation, strprogname
            GoTo rs_skip
        End If
    
        txtrecpos = "Record " & rsbookmark.AbsolutePosition & " OF " & rsbookmark.RecordCount
        Call search_SC(rsbookmark!pk_supplierid) 'SP Call where pk_supplierid is clustered index
    End If
    
    rs_skip:
    If rsbookmark.State = adStateOpen Then rsbookmark.Close
    Set rsbookmark = Nothing
    Call closecon

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

    Re: VB6+SQL Server 2005: Code or DB Optimization

    Why are you not simply selecting with a WHERE clause and getting the row you want?

    Seems to me that you are loading the entire table and working the recordset on the client side - is that correct?

    *** 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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    yes. but if using WHERE clause will return a filtered row and moving to next or previous row will not be available.

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

    Re: VB6+SQL Server 2005: Code or DB Optimization

    How many rows in the table??

    *** 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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    right now its only 784 but the delay is too visible.

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

    Re: VB6+SQL Server 2005: Code or DB Optimization

    I don't use the technique you are using...

    Aren't you doing the .Open every time they move from record to record??

    *** 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

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    what would you suggest for optimzation

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6+SQL Server 2005: Code or DB Optimization

    Not only that.. but also opening and closing the connection each time too!

    I'd recommend looking at our ADO Tutorial (link in my signature, or via the FAQs in the Database forum) as the code there is organised much better (in terms of speed at least).


    As far as I can see, the routine you posted only needs to contain 3 or 4 relatively simple lines (but for that you use of the connection and recordset needs to be changed), or perhaps even better would be a minor alteration to the SP that search_SC calls.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    it was late last day and i cant reply. anyway, i run through beacons tutorial and i cant find it helpful. i was expecting that there was another FANCY way in moving rows. anyway my coding standard OPEN NOW and CLOSE AFTER - means that every time a move rows call it will:

    1. open connection and recordset
    2. load the entire table
    3. find the last row
    4. move to the other row (next, previous...)
    5. fill form
    6. close recordset and connection

    i would agree with beacons coding standard but suppose you are working with multiple forms (MDI) and a distributed applicaton will you advise to open single connection for the entire application and close only when the application terminated?
    Last edited by jlbantang; Aug 15th, 2007 at 02:16 AM.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB6+SQL Server 2005: Code or DB Optimization

    http://www.vbforums.com/showthread.p...hlight=primary

    Navigate using important fields (eg. primary key and some columns), then load details or other fields later.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    very weird i use the same code i try inserting

    form_load()
    rs.movefirst and rs.movelast
    end sub

    its a daunting response, eventually rs.movenext and rs.moveprevious are perfectly working. even i run the application through lan. really confuse wheres the bottlent neck coming.

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB6+SQL Server 2005: Code or DB Optimization

    Its a matter of volume... transferring 3 columns of data over the network will take less time compared to more columns for the same number of rows.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6+SQL Server 2005: Code or DB Optimization

    You seem to be adding to code that makes it noticeably slower! See the speed issues in bold:
    every time a move rows call it will:

    1. open connection [slow] and recordset [slow]
    2. load the entire table [very slow - but can be appropriate in some situations (not here!)]
    3. find the last row [extremely slow]
    4. move to the other row (next, previous...)
    5. fill form
    6. close recordset and connection [ok in itself, but a nightmare as it causes you to re-do steps 1-3 next time]
    Why not simply do steps 4 and 5?

    If you are desperate to close the recordset & connection at the earliest possible opportunity (I don't know why you would be), then you should at least merge steps 2 3 and 4, so that just the row you want is returned.

    i would agree with beacons coding standard but suppose you are working with multiple forms (MDI) and a distributed applicaton will you advise to open single connection for the entire application and close only when the application terminated?
    If the structure of each of your forms is even vaguely similar to Beacon's tutorial, you should not even consider using the kind of code you have - your code should be like Beacon had it. I have done that for several multi-user systems, and not had speed problems (tho I do avoid returning all rows from a table if possible, and I always specify the columns I want to use).

    Note that for multi-user systems you should avoid a cursor location of Client, as it stops you from seeing other peoples changes, and usually slows things down too.
    really confuse wheres the bottlent neck coming.
    A .MoveLast means that every record in the recordset needs to be sent from the database to your program (then checked to see if is the last one, if not check the next...), rather than simply the record you want being sent.
    Last edited by si_the_geek; Aug 15th, 2007 at 09:37 AM.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    i tried to work on the code by opening the recordset and connection ONCE in form_load() and close when the application terminate. really there was a noticeable difference. but somethings i doubt regarding rows update since these is a multi-user system. will the updates reflect in the OPENED recordset. if not what will be the work around you will recommend.

    btw, does it mean also removing SPs in move rows command. thanks.

    i appreciate your comments.
    Last edited by jlbantang; Aug 15th, 2007 at 10:10 AM.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6+SQL Server 2005: Code or DB Optimization

    Quote Originally Posted by jlbantang
    i tried to work on the code by opening the recordset and connection ONCE in form_load() and close when the application terminate. really there was a noticeable difference.
    What kind of difference? Is it much faster now?
    but somethings i doubt regarding rows update since these is a multi-user system. will the updates reflect in the OPENED recordset. if not what will be the work around you will recommend.
    Depending on the options you used for rs.Open, updates can be seen in open recordsets, see the FAQ article What do the parameters of the recordset.Open method mean? for more details.

    btw, does it mean also removing SPs in move rows command. thanks.
    What does the SP do? (I've never used one in a case like that).



    To be honest tho, probably the best thing to do is step back a bit, and for you to explain (in simple terms, not code) what the form actually does (and/or upload a form for us to see). We can then probably suggest better methods.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    the form actually is a supplier master which allow data read and writes (add,edit,save...). prob comes with the row navigator (basic |< < > >|)

    SP perform the filing of form.

    http://images.cjb.net/4b812.gif

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6+SQL Server 2005: Code or DB Optimization

    It looks to me as if the code from Beacon's ADO Tutorial, and my Further Steps article (links in my signature) would be best for you. It should be noticeably faster.. and if it still needs tweaking, paging the data to a single record would probably improve things.


    I don't understand why you would be using an SP to fill the form.. something like Beacon's FillFields sub should do it. This is more efficient as the data is already in memory (in the current record of the recordset), so there is no point going back to the database to get it again.

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    suppose i will open single rs and con once for the entire operation and someone on the other terminal update the contacts, will it reflect in the open recordset

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB6+SQL Server 2005: Code or DB Optimization

    let me also add this, which will be faster

    1. rs.open SPName
    2. rs.open TableName

    both will have the same cursortype and locktype

  20. #20
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB6+SQL Server 2005: Code or DB Optimization

    Another problem with your design is its not scalable... if the growth of the data within a year is considerable your application will perform worse ... for how many records are you designing? 10K? 50K? In some businesses some tables can have more than a million records ... and when the time comes to address the problem, you'd end up rewriting most of your application rather than doing it right the first time around.

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6+SQL Server 2005: Code or DB Optimization

    Quote Originally Posted by jlbantang
    suppose i will open single rs and con once for the entire operation and someone on the other terminal update the contacts, will it reflect in the open recordset
    If you use the right parameters when opening the recordset, yes it will - see the link I posted above (post #15) for more info.

    et me also add this, which will be faster

    1. rs.open SPName
    2. rs.open TableName

    both will have the same cursortype and locktype
    That obviously depends on what the SP actually does (you said before it fills the form - but unless there is a new trick I don't know about, that is not possible).

    If it just does a "SELECT * FROM TableName", then it will be the faster option.

    If it does similar, but returns fewer columns and/or has a (reasonably simple) Where clause to restrict the rows, it should be much faster.


    Note however that method 2 is terrible anyway - replacing it with an SQL statement (even if it is the equivalent "SELECT * FROM TableName") will be faster, and better in other ways too. For how to do that, see the same link (post #15) again.

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: [RESOLVED] VB6+SQL Server 2005: Code or DB Optimization

    thanks for the contribution. things work fine now.

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