Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors.

Slides:



Advertisements
Similar presentations
Understanding SQL Server Query Execution Plans
Advertisements

Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Module 7 Designing Queries for Optimal Performance.
Clarity Educational Community Clarity Educational Community Creating and Tuning SQL Queries that Engage Users.
T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
Thinking in Sets and SQL Query Logical Processing.
There’s a particular style to it… Rob Hatton
Overview of Security Investments in SQL Server 2016 and Azure SQL Database Jamey Johnston 1/15/2016Security Investments in SQL Server 2016 and Azure SQL.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Mastering Master Data Services Presented By: Jeff Prom BI Data Architect Bridgepoint Education MCTS - Business Intelligence, Admin, Developer.
Carlos Bossy Quanta Intelligence SQL Server MCTS, MCITP BI CBIP, Data Mining Real-time Data Warehouse and Reporting Solutions.
Database Design: Solving Problems Before they Start! Ed Pollack Database Administrator CommerceHub.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
SQL Server Statistics and its relationship with Query Optimizer
Effective T-SQL Solutions
Using Common Table Expressions
Reverse Engineering The Database
Query Optimization Techniques
Enterprise Row Level Security: SQL Server 2016 and Azure SQL DB
Crash course on Better SQL Development
Dynamic SQL: Writing Efficient Queries on the Fly
T-SQL: Simple Changes That Go a Long Way
Solving the Hard Problems
Introduction to Database Systems
Reading Execution Plans Successfully
Four Rules For Columnstore Query Performance
Relational Algebra Chapter 4, Part A
Beginner Table Partitioning
The Ins and Outs of Indexes
Third Party Tools for SQL Server
Crash course on Better SQL Development
Save Time & Resources: Job Performance Tuning Strategies
Re-Indexing - The quest of ultimate automation
Overview of Security Investments
The Killing Cursors Cyndi Johnson
Query Optimization Techniques
Execution Plans Demystified
Stop Wasting Time & Resources: Performance Tune Your Jobs
Advanced SQL: Views & Triggers
Dynamic SQL: Writing Efficient Queries on the Fly
I Learnded SQL And So Can You!
Tally Ho! -- Explore the Varied Uses of Tally Tables
SQL Server Query Plans Journeyman and Beyond
Crash course on Better SQL Development
Microsoft SQL Server 2014 for Oracle DBAs Module 7
Please thank our sponsors!
Crash course on Better SQL Development
T-SQL gotchas and power-ups
Save Time & Resources: Job Performance Tuning Strategies
The Hidden Mysteries of Common Table Expressions
Database Projects and Source Control (TFS)
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Four Rules For Columnstore Query Performance
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Diving into Query Execution Plans
Chapter 11 Managing Databases with SQL Server 2000
Crash course on Better SQL Development
Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors.
SQL Server Query Design and Optimization Recommendations
Query Optimization Techniques
Isolation Levels Understanding Transaction Temper Tantrums
Database Projects and Source Control (TFS)
Efficient and Effective coding of stored procedures
All about Indexes Gail Shaw.
Improving the Performance of Functions
XML? What’s this doing in my database? Adam Koehler
Presentation transcript:

Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors

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

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

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

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

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

Thank You Sponsors Platinum Gold Silver Bronze Swag Venue