The Killing Cursors Cyndi Johnson

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.
Relational Database Performance CSCI 6442 Copyright 2013, David C. Roberts, all rights reserved.
T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
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.
Database Design: Solving Problems Before they Start! Ed Pollack Database Administrator CommerceHub.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
SQL Server Statistics and its relationship with Query Optimizer
Effective T-SQL Solutions
Query Optimization Techniques
Enterprise Row Level Security: SQL Server 2016 and Azure SQL DB
Crash course on Better SQL Development
SQL Server 2017 Graph Database Inside-Out
Dynamic SQL: Writing Efficient Queries on the Fly
T-SQL: Simple Changes That Go a Long Way
Solving the Hard Problems
Stop Doing That! Common T-SQL Anti-Patterns
Introduction to Database Systems
Relational Algebra Chapter 4, Part A
SQL Server May Let You Do It, But it Doesn’t Mean You Should
Tapping the Power of Your Historical Data
Third Party Tools for SQL Server
Crash course on Better SQL Development
Save Time & Resources: Job Performance Tuning Strategies
Stop Doing That! Common T-SQL Anti-Patterns
Re-Indexing - The quest of ultimate automation
Overview of Security Investments
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
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
Data Analysis with SQL Window Functions
Cyndi Johnson Senior Software Engineer at AdvancedMD Killing Cursors.
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
EXECUTION PLANS Quick Dive.
NAVIGATING THE MINEFIELD
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
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:

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