Close

Results 1 to 6 of 6

Thread: SQL Cursors

  1. #1
    DF MaSter -TheGame-'s Avatar
    Join Date
    Feb 2004
    Location
    USA
    Posts
    86
    Thanks
    0
    Thanked:        0
    Karma Level
    250

    Info SQL Cursors

    Hi guys, I'm working on improving performance in a billing database and am trying to find out if all cursors that are open eventually get closed and de-allocated.

    The one of the processes that i'm working to improve loops on 90000-ish records.. which then loops on data usually using cursors from other tables for each of the 90000 records.

    My question is how can i detect if my cursors are being closed and de-allocated? this database has about 1300 stored procs (somewhere around 200000 lines of SQL code) so its not like is easy to read through the code to make sure they all get closed

    Thanks for any help in advance.

    [Edit] - we are running Microsoft SOL Server 2005 FYI.

  2. #2
    DF VIP Member nitelife's Avatar
    Join Date
    Nov 2002
    Location
    Reading, Berks
    Posts
    1,170
    Thanks
    115
    Thanked:        13
    Karma Level
    337

    Default Re: SQL Cursors

    I'm not a dba, but have dabbled in sql a few times. I found an article here which uses cursors in an example to create custom looping procedures in sql.

    If you look at the code more closely a cursor is declared like this:

    Code:
                declare @local_cursor cursor
                            set @local_cursor = hCForEachObject
               
                open @local_cursor
                fetch @local_cursor into @name
    Later on it's closed like this:

    Code:
    close @local_cursor
    This implies that you need to close cursors explicitly. This is the first example I found, so I suggest doing some research on cursors and seeing whether there are automatically deallocating versions.

  3. #3
    DF MaSter -TheGame-'s Avatar
    Join Date
    Feb 2004
    Location
    USA
    Posts
    86
    Thanks
    0
    Thanked:        0
    Karma Level
    250

    Default Re: SQL Cursors

    thanks for the reply, i'm leaning towards most of the cursors are probably being closed because i think we would see locking issues, i have found some that are not being deallocated though and need to dig into that more.

    But on the bright side i found the major part of bottle neck for the process a briefly described above and with a few modifications to an index and a couple stored procedures, i was able to get a 5+ hour process time to about 25mins!

  4. #4
    DF VIP Member nitelife's Avatar
    Join Date
    Nov 2002
    Location
    Reading, Berks
    Posts
    1,170
    Thanks
    115
    Thanked:        13
    Karma Level
    337

    Default Re: SQL Cursors

    great improvement! It's amazing how much difference optimising some code can be sometimes.

  5. #5
    Argyll's Apprentice TwoPlAnKs's Avatar
    Join Date
    May 2003
    Location
    Aberdeenshire
    Posts
    5,191
    Thanks
    0
    Thanked:        0
    Karma Level
    620

    Default Re: SQL Cursors

    not really sure of an easy way to do it with windows software, short of just installing windows clones of the unix stuff or cygwin

    easy though:
    cat all.sql your.sql code.sql | grep [[CODE TO OPEN A CURSOR]] | wc -l
    cat all.sql your.sql code.sql | grep [[CODE TO CLOSE A CURSOR]] | wc -l

    That returns two numbers. If they don't match, you have a cursor not being closed.

    To find which one, any scripting language would be able to read through the code, pick out the name of every cursor getting opened and find its close statement, then report any that don't have the close statement. Would be pretty easy in perl (which works well in windows)
    "The Net interprets censorship as damage and routes around it." - John Gilmore

  6. #6
    DF VIP Member big man's Avatar
    Join Date
    Jul 2002
    Location
    The big smoke
    Posts
    2,824
    Thanks
    8
    Thanked:        0
    Karma Level
    445

    Default Re: SQL Cursors

    I'm not that familiar with it, but i would have thought you could use SQL Profiler to look for this

    If it's not got a built in function that will do it, it shouldn't be too hard to filter out
    I am a loud man with a very large hat. This means I am in charge

    Never argue with an idiot. They will bring you down to their level, then beat you with experience.

Social Networking Bookmarks

Posting Permissions

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