Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors.

Similar presentations


Presentation on theme: "Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors."— Presentation transcript:

1 Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors

2 Killing Cursors Techniques to Eradicate/Improve Cursors & Other Loops Within Your Database Code

3

4 Overview Why this session? Why are cursors/loops bad?
Alternative approaches Business Use Case & Refactored Solutions Tips & Considerations

5 Why this session? <Talking> blah blah blah </Talking>

6 In This Demo, I’m using: SQL Server 2016 SSMS v17.4
SQL Sentry Plan Explorer V.3 (FREE!)

7 Why are cursors/loops bad?
Cursors/loops are an imperative approach- you’re telling the engine (compiler,etc) how to do its job T-SQL is DECLARATIVE- tell the engine what you need and it will decide how best to get there. Cursors/loops usually do not utilize set-based approaches in a way that is efficient and will scale There is always another way Discuss the flaw of debating CURSORS VS. LOOPS, point to Aaron Bertrand’s blog post on the subject

8 Alternative Approaches
Easy problems solved with cursors/loops (not covered during this session) Calling existing stored procedures within a loop use bulk INSERTS while utilizing OUTPUT clause to capture identity fields and act using those captured fields string concatenations use FOR XML instead Harder problems often solved with cursors/loops Hierarchical data relationships application of payments in accounting overlapping worklists

9 Business Use Case- Overlapping Priority Driven Worklists: One charge may fit the definition for several of the worklist’s filter definitions, but the one with the highest priority wins W1 W3 W2

10 There is much value to understanding the data

11 While Loop Basic Syntax

12 DEMOS Original Version
Show CURSOR_NonDynamic & CURSOR_Dynamic stored procedures

13 DEATH BY A THOUSAND CUTS
Or not seeing the forest through the trees

14 Refactored V.1 Approach-Basic CTE
Show Worklist_Queries stored procedure ASIDE- What do CTEs give you that derived tables/subqueries do not?

15 Refactored V.1 DEMOS Show CTE_Normal_Dynamic stored procedure

16 Refactored V.2 Approach-Recursive CTE
Default MAXRECURSION is 100 Show PERFECT_WORLD.sql

17 SO MANY RULES! Recursive CTE Rules
From At least one ANCHOR member and one RECURSIVE member UNION ALL must be used between the last ANCHOR and the first RECURSIVE member Date types MUST match exactly between the ANCHOR and RECURSIVE member RECURSIVE member FROM clause may only refer to CTE expression name ONCE SO MANY RULES!

18 Refactored V.2 DEMOS Show CTE_Recursive stored procedure

19 WHILE LOOP using CTE OVER() PARTITION BY() Dynamic SQL
And the WINNER is… WHILE LOOP using CTE OVER() PARTITION BY() Dynamic SQL

20 Why did Recursive CTE not perform?
No unique parent/child key Complex predicates (conditional filters) Recursive CTEs CAN perform well in the right circumstances Discuss anchor member VS several recursive members of the CTE & why the steps weren’t ideal (NULL case had to be added)

21 What is a good use case for Recursive CTE?
Unique parent/child key Simple predicates Smaller data set to operate on whether in a table or a temp table, fields used to step through the data are properly indexed

22 Tips & Considerations Know your data- understanding the data will help you know how to improve upon the processes If you don’t know the data- TEST TEST TEST. You should do this regardless Execution plans are your friend! Get in the habit of looking at them often, even if you don’t understand them very well. That will come with time. If you have to leave a cursor/loop intact, is there any way you can reduce the times it goes through the loop?

23 Additional Resources Fritchey G. (2012) Cursor Cost Analysis. In: SQL Server 2012 Query Performance Tuning. Apress, Berkeley, CA

24 Thank You! linkedin.com/in/cjdatawhisperer medium.com/@SQLanodyne

25 Files Are Huge! https://1drv.ms/u/s!Aimffvkpm1Eauw2N5lb0L7hUWvH-

26 Thank You Sponsors Platinum Gold Silver Bronze Swag Venue


Download ppt "Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors."

Similar presentations


Ads by Google