The Killing Cursors Cyndi Johnson Senior Software Engineer at AdvancedMD The Killing Cursors
The Killing Cursors Techniques to Eradicate/Improve Cursors & Other Loops Within Your Database Code
Why this session? <Talking> blah blah blah </Talking>
Overview Why this session? Why are cursors/loops bad? Alternative approaches Business Use Case & Refactored Solutions Tips & Considerations
In This Demo, I’m using: SQL Server 2016 SSMS v17.4 SQL Sentry Plan Explorer V.3 (FREE!)
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
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
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
There is much value to understanding the data
While Loop Basic Syntax
DEMOS Original Version Show CURSOR_NonDynamic & CURSOR_Dynamic stored procedures
DEATH BY A THOUSAND CUTS Or not seeing the forest through the trees
Refactored V.1 Approach-Basic CTE Show Worklist_Queries stored procedure ASIDE- What do CTEs give you that derived tables/subqueries do not?
Refactored V.1 DEMOS Show CTE_Normal_Dynamic stored procedure
Refactored V.2 Approach-Recursive CTE Default MAXRECURSION is 100 Show PERFECT_WORLD.sql
SO MANY RULES! Recursive CTE Rules From https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql 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!
Refactored V.2 DEMOS Show CTE_Recursive stored procedure
WHILE LOOP using CTE OVER() PARTITION BY() Dynamic SQL And the WINNER is… WHILE LOOP using CTE OVER() PARTITION BY() Dynamic SQL
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)
What is a good use case for Recursive CTE? https://blogs.msdn.microsoft.com/sqlcat/2011/04/28/optimize-recursive-cte-query/ 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
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?
Additional Resources https://blogs.msdn.microsoft.com/sqlcat/2011/04/28/optimize-recursive-cte-query/ https://www.red-gate.com/simple-talk/sql/performance/the-seven-sins-against-tsql-performance/#six Fritchey G. (2012) Cursor Cost Analysis. In: SQL Server 2012 Query Performance Tuning. Apress, Berkeley, CA https://sqlblog.org/2012/01/26/bad-habits-to-kick-thinking-a-while-loop-isnt-a-cursor
Thank You! linkedin.com/in/cjdatawhisperer medium.com/@SQLanodyne cyndijon1@hotmail.com
https://1drv.ms/u/s!Aimffvkpm1Eau1ghGYIMjTvvq1CC Files Are Huge! https://1drv.ms/u/s!Aimffvkpm1Eau1ghGYIMjTvvq1CC
Thank You Sponsors