There Must Be 15 Ways To Lose Your Cursors… Part 2: Just Put It in a S

  • RBarryYoung

    SSC Guru

    Points: 143327

    Comments posted to this topic are about the item There Must Be 15 Ways To Lose Your Cursors… Part 2: Just Put It in a S

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Bruce W Cassidy

    SSChampion

    Points: 12607

    [font="Verdana"]Aha!

    I have to say I enjoyed this more than the first article, largely because the first article was really just an introduction to the topic.

    I like your use of examples where I can just see a developer writing that sort of code, usually because they don't know better.

    I also like the introduction of a method for converting cursor statements into set-based equivalents. I think as you continue to walk through each of the parts of that method, it will make it easier for people new to thinking and working with sets to apply to their own code. Kudos for that.

    For completeness, will you look at rewriting cursors into just using a loop (without the actual cursor, but otherwise identical)? Because that's another form of code that can also be replaced by set-based code.

    I'm happy to give examples if you need them.

    Well done!

    [/font]

  • Lynn Pettis

    SSC Guru

    Points: 442306

    Very good article. In fact, I'm thinking of printing each of the articles in the series and providing them to my team as required reading.

    The test harness you have created is also well done, and I think that as well can be incorporated in other test environments.

    I'm eagerly awaiting to see what others think of this article.

  • Jeff Moden

    SSC Guru

    Points: 996429

    Nicely done, Barry... I really like the way the story is unfolding in this series... very easy reading, as well. I absolutely agree that you have real life examples because I've seen the same thing not only on these fine forums, but in real production code, as well.

    The bad part about this article is... I've finally learned how to write a Cursor and may have to burn out my mind's-eye to rid myself of the images of RBAR on steriods. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Forum FAQ

  • gserdijn

    Hall of Fame

    Points: 3259

    Well done! Can you tell us the publishing date of Part 3?

    The results of the comparison: no spoilers here 😎

    Original code:

    CpuMs: 8750

    LogRds: 834261

    Elapsed: 9898

    Rewrite:

    CpuMs: 94

    LogRds: 236

    Elapsed: 89


    Dutch Anti-RBAR League

  • Atif-ullah Sheikh

    SSChampion

    Points: 12495

    Another great Article.

    Thanks...

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • RBarryYoung

    SSC Guru

    Points: 143327

    Thanks fro the support Folks! And Jeff, yeah, I had the same problem when writing these article.

    As for part 3, unfortunately medical issues have kept me from completeing it, but I will let you know as soon as I do. In fact these same medical issues are going to keep me off-line most of today, however many of the heavy poster has offered to step-in to fkk the gaps and answer any thecnical questions

    thanks again! ...

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • jwheeler-985942

    SSC Veteran

    Points: 279

    The fact you even have to tell so called developers this stuff is frightening.

  • Samuel Vella

    SSCrazy Eights

    Points: 8055

    jwheeler (4/27/2009)


    The fact you even have to tell so called developers this stuff is frightening.

    Not really and it's something I've seen a few times...

    When someone has a background in VB or C and has been dumped into SQL development then they might be able to write code but they lack the set based thinking which a good DB developer needs (they might not even be aware of being able to return sets depending on how much exposure they're had to databases in their previous life).

    Usually a helping hand with their first few pieces of work will get them thinking the right way from the outset.

    I know from experience when dabbling in ASP .NET that something which I've spent hours and several tens of lines of code trying to get to work can be ripped apart and rewritten as a very simple class by someone who knows what they're doing

  • jwheeler-985942

    SSC Veteran

    Points: 279

    Samuel Vella (4/27/2009)


    jwheeler (4/27/2009)


    The fact you even have to tell so called developers this stuff is frightening.

    Not really and it's something I've seen a few times...

    When someone has a background in VB or C and has been dumped into SQL development then they might be able to write code but they lack the set based thinking which a good DB developer needs (they might not even be aware of being able to return sets depending on how much exposure they're had to databases in their previous life).

    Usually a helping hand with their first few pieces of work will get them thinking the right way from the outset.

    I know from experience when dabbling in ASP .NET that something which I've spent hours and several tens of lines of code trying to get to work can be ripped apart and rewritten as a very simple class by someone who knows what they're doing

    Then surely the manager hasn't done their job by employing the wrong tool for the task?

    If you have database development to be done you either employ a db dev or a C# / VB developer who is also a SQL dev. You don't buy a hammer to put a screw in the wall, surely?

  • Samuel Vella

    SSCrazy Eights

    Points: 8055

    jwheeler (4/27/2009)


    Then surely the manager hasn't done their job by employing the wrong tool for the task?

    If you have database development to be done you either employ a db dev or a C# / VB developer who is also a SQL dev. You don't buy a hammer to put a screw in the wall, surely?

    HR could well have given you the hammer and told you to get on with it

    Internal shuffling is one way of minimising redundancies

  • atoth

    SSC-Addicted

    Points: 452

    Hi

    I still find I need Cursors when Sending out batches of Email notifications from the Server.

    For example:

    1. A scheduled Job selects records of people who need to be informed of some activity that has occurred - relevant data such as their email address, Name, the description of the activity etc is stored in variables using a CURSOR.

    2. Inside the while loop I merge the data from the variables into an HTML Email template, and then send it out using dbMail.

    I would love to replace this with set based handling but I can imagine how...particulary step 2

    Adam

  • Samuel Vella

    SSCrazy Eights

    Points: 8055

    atoth (4/27/2009)


    I still find I need Cursors when Sending out batches of Email notifications from the Server.

    I would love to replace this with set based handling but I can imagine how...particulary step 2

    Adam

    you're performing a procedural not a set based operation and the "per-row" function calls means that you will need to use a cursor.

    Cursors do have their uses and this is one of them.

  • Jeff Moden

    SSC Guru

    Points: 996429

    Samuel Vella (4/27/2009)


    atoth (4/27/2009)


    I still find I need Cursors when Sending out batches of Email notifications from the Server.

    I would love to replace this with set based handling but I can imagine how...particulary step 2

    Adam

    you're performing a procedural not a set based operation and the "per-row" function calls means that you will need to use a cursor.

    Cursors do have their uses and this is one of them.

    Heh... in SQL Server 2005, even that can be done without a cursor. We're just gonna have to wait for it in Barry's series of articles.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Forum FAQ

  • george sibbald

    SSC Guru

    Points: 104200

    nice work barry, you must have slaved other this. You should make a book out of all the chapters once you are done, as Lynn said it would become required reading.

    I particularly like the rules for converting code to set based, useful mental prompts. Also the stats gathering method, although not applicable if using SQL 2000.

    ---------------------------------------------------------------------

Viewing 15 posts - 1 through 15 (of 317 total)

You must be logged in to reply to this topic. Login to reply