Top Tips for Better TSQL Stored Procedures

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Corporation
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Copyright © 200\8 Quest Software High Performance PL/SQL Guy Harrison Chief Architect, Database Solutions.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to PL/SQL
Connect with life Vinod Kumar M Technology Evangelist | Microsoft
Adding Automated Functionality to Office Applications.
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
Module 8: Monitoring SQL Server for Performance. Overview Why to Monitor SQL Server Performance Monitoring and Tuning Tools for Monitoring SQL Server.
Module 5: Data Access. Overview Introduce database components involved in data access Introduce concepts of Transact -SQL and Procedural SQL as tools.
Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Database Design for DNN Developers Sebastian Leupold.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Version 1.0. MCAD MCSD MCPD Enterprise SQL MCTS MCT Software/Web Development Consultant Cryptography/Digital Signature Consultant SQL Server 2005/2008R2/2012.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Key Concepts About Performance Factors Affecting SQL Performance SQL Performance Tuning Methodologies SQL Performance Tuning Tools 1.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Connect with life Nauzad Kapadia Quartz Systems
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
TSQL Worst Practices Jacob Sebastian, SQL Server MVP
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
Fundamentals of Great SQL Query Performance Matt Wigdahl, ScriptPro LLC.
Common SQL Performance Issues AND HOW TO AVOID OR FIX THEM.
 CONACT UC:  Magnific training   
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
High Performance Functions SQLBits VI. Going backwards is faster than going forwards.
Tuning Transact-SQL Queries
Query Optimization Techniques
MAKE YOUR QUERIES FASTER
Testing and Debugging.
Database Performance Tuning and Query Optimization
Introduction to Execution Plans
Agenda Database Development – Best Practices Why Performance Matters ?
Query Optimization Techniques
Transactions, Locking and Query Optimisation
The PROCESS of Queries John Deardurff
Tally Ho! -- Explore the Varied Uses of Tally Tables
When I Use NOLOCK AND OTHER HINTS
How to Use Parameters Like a Pro
Unit 6 Assignment 2 Chris Boardley.
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
The PROCESS of Queries John Deardurff
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Introduction to Execution Plans
Contents Preface I Introduction Lesson Objectives I-2
When I Use NOLOCK AND OTHER HINTS
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
Diving into Query Execution Plans
Introduction to Execution Plans
Query Optimization Techniques
Controlling Program Flow
Introduction to Execution Plans
Performance Tuning ETL Process
Presentation transcript:

Top Tips for Better TSQL Stored Procedures Grant Fritchey – Red Gate Software

Goal Learn methods for writing TSQL for readability Understand how to write TSQL for performance

Grant Fritchey Product Evangelist for Red Gate Software Twitter: @gfritchey Blog: scarydba.com Email: grant@scarydba.com

Agenda Writing for readability Object Names & Format Comments Error Handling Personal Preferences Writing for performance Data Types Functions in Comparisons Improper Use of Functions The “Run Faster” Switch Inappropriate Use of Query Hints Recompiles Null Values Row by Agonizing Row Nesting Views

Writing for Readability Define a local standard Object names Comments Format Error handling Enforce the standard Document it Code reviews

Object Names & Format Names should be descriptive Procedures should be a phrase Abbreviations should be common (no Ddltbl) Use aliases Clear Common Be consistent A foolish consistency is the hobgoblin of little minds Keyword in that sentence is “foolish”

Comments Do something Most important is describing the action, not documentation Use source control for documentation Self-documenting code is a lie

Error Handling TRY/CATCH Deadlocks Establish local best practices

Personal Preferences CamelCase Uppercase for reserve words and key words Line breaks On SELECT list Between JOIN criteria Between WHERE criteria Use the semicolon Indent After initial SELECT With ON clause After WHERE clause

Writing For Performance Write for your data structures Write for your indexes Write for the query optimizer Avoid common issues: Data types Functions in comparisons Improper use of functions The “run faster” switch Inappropriate Query Hints Recompiles Null Values Row By Agonizing Row Nested Views

Data Types Problem Implicit or explicit data type conversion Indications Scans Slow performance Solution Use appropriate data types

Functions in Comparisons Problem Function on column in WHERE or JOIN criteria Indications Scans Slow performance Solution Don’t run functions on columns Use sargeable functions

Improper Use of Functions Problem Multi-statement user defined functions cause poor performance Indications Zero cost scan operator in exec plan Very slow performance Solution When working with more than a few (~50?) rows, don’t use them When using operations such as JOIN that require statistics, don’t use them

The “Run Faster” Switch Problem Use of the NO_LOCK query hint everywhere, or setting transaction isolation level to READ_UNCOMMITTED Indications Extra rows in result set Missing rows in result set Solution Tune the queries Tune the structures Use snapshot isolation levels

Query Hints Problem Query hints in the code such as FAST n, index hints, JOIN hints Indications Inconsistent performance behavior Bad performance Odd looking execution plans Solution Use of query hints should be exceptional Exceptions are rare, not standard practice

Recompiles Problem Excessive blocking and CPU contention caused by constant or long statement recompiles Indications Recompile events in extended events or trace Blocking High CPU usage Solutions Avoid interleaving DDL & DML Where viable, use table variables

NULL Values Problem Incorrect data returned due to improper use of NULL Indications = and <> instead of IS NULL and IS NOT NULL Solutions Learn to use NULL properly

Row by Agonizing Row Problem Cursors instead of set-based operations WHILE loops instead of set-based operations Multiple statements where 1 will do Indications Extremely slow performance Solutions Eliminate unnecessary row-by-row processing

Nested Views Problem Views within views causes optimizer timeout Indications Incredibly complex query plans for simple queries Very poor performance Solutions Don’t nest views Materialize views

Goal Learn methods for writing TSQL for readability Understand how to write TSQL for performance

Writing for Readability Define a local standard Object names Comments Format Error handling Enforce the standard Document it Code reviews

Writing For Performance Write for your data structures Write for your indexes Write for the query optimizer Avoid common issues: Data types Functions in comparisons Improper use of functions The “run faster” switch Inappropriate Query Hints Recompiles Null Values Row By Agonizing Row Nested Views

Grant Fritchey Product Evangelist for Red Gate Software Twitter: @gfritchey Blog: scarydba.com Email: grant@scarydba.com

Why does… ? When do I… ? Questions? What happens when… ? How would you… ? What happens when… ? Why does… ? When do I… ?