Every SQL Programmer Should Know Kevin Kline Director of Engineering Services at SQL Sentry Microsoft MVP since 2003 Facebook, LinkedIn, Twitter at KEKLINE.

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
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
Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Big Data Working with Terabytes in SQL Server Andrew Novick
10 Things Not To Do With SQL SQLBits 7. Some things you shouldn’t do.
Tempdb Parasites Jason Hall-Dir. of Client SQL Sentry Blog-jasonhall.blogs.sqlsentry.net.
SQL Server Stored Procedures Architecture & Performance Victor Isakov MCT, CTT, MSCE, MCDBA
SQL Server 2005 SP2 Israeli SQL Server User Group March 2005 Ami Levin
Kevin Kline, SQL Sentry Director of Engineering Services, Microsoft SQL Server MVP since 2003 Twitter, Facebook, KEKline.
Introduction to Databases Chapter 8: Improving Data Access.
How a little code can help with support.. Chris Barba – Developer at Cimarex Energy Blog:
Kevin Kline, SQL Sentry Director of Engineering Services, Microsoft SQL Server MVP since 2003 Twitter, Facebook, KEKline.
2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.
Atlanta SQL Server Users Group April 10, 2006 Stored Procedure Best Practices Kevin Kline Director of Technology Quest Software.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Copyright © 2006 Quest Software Best Practices for Stored Procedures By Kevin Kline SQL Server MVP.
Top Free Tools for Tuning SQL Statements Kevin Kline & Aaron Bertrand SQL Sentry, Inc.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
BA372 Stored Procedures and Triggers Lab. What needs to be done to change a customer’s credit limit? Who am I? May I? Do it Log it Display A database.
By Shanna Epstein IS 257 September 16, Cnet.com Provides information, tools, and advice to help customers decide what to buy and how to get the.
SQL Dev Tips for Small Workstations How to develop SQL on small dev workstations when prod is huge. Kevin Kline and Aaron Bertrand SQL Sentry.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Connect with life Nauzad Kapadia Quartz Systems
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
SQL SERVER CONFIGURATION OPTIONS AND TRACE FLAG SECRETS Kevin Kline SQL Sentry, Director of Engineering on Twitter, FB, LI Blogs at
October 15-18, 2013 Charlotte, NC Accelerating Database Performance Using Compression Joseph D’Antoni, Solutions Architect Anexinet.
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
Dave LinkedIn
How to kill SQL Server Performance Håkan Winther.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Kevin Kline Director of Engineering Services, SQL Sentry SQL Server MVP since 2003 Twitter, FB, LI: KEKline Blog:
Execution Plans Detail From Zero to Hero İsmail Adar.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQL Server Statistics and its relationship with Query Optimizer
ASE Optdiag Features including dynamic_histogram
Parameter Sniffing in SQL Server Stored Procedures
Stored Procedures – Facts and Myths
Query Tuning without Production Data
UFC #1433 In-Memory tables 2014 vs 2016
Dynamic SQL: Writing Efficient Queries on the Fly
SQL Server Monitoring Overview
Designing Database Solutions for SQL Server
Introduction to SQL Server Management for the Non-DBA
Peeking into the Plan Cache with SQL Server 2008
Introduction to Execution Plans
SQL 2014 In-Memory OLTP What, Why, and How
Query Optimization Techniques
Top Tips for Better TSQL Stored Procedures
When query plans go wrong
Dynamic SQL: Writing Efficient Queries on the Fly
When I Use NOLOCK AND OTHER HINTS
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
In-Memory OLTP for Database Developers
Parameter Sniffing: the Good, the Bad, and the Ugly
Dave Bland LinkedIn SQL Server Execution Plans: How to use them to find performance bottlenecks Dave Bland LinkedIn
Introduction to Execution Plans
Contents Preface I Introduction Lesson Objectives I-2
Parameter Sniffing: the Good,the Bad, and the Ugly
Parameter Sniffing: the Good, the Bad, and the Ugly
Diving into Query Execution Plans
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Using wait stats to determine why my server is slow
Presentation transcript:

Every SQL Programmer Should Know Kevin Kline Director of Engineering Services at SQL Sentry Microsoft MVP since 2003 Facebook, LinkedIn, Twitter at KEKLINE KevinEKline.com, ForITPros.com

Free Plan Explorer download: Free query tuning consultations: Free new ebook (regularly $10) to attendees. Send request to SQL Server educational videos, scripts, and slides: Tuning blog: Monthly eNews tips and tricks: archive.asp archive.asp

Introductions Test & tuning environment 1. Clearing caches Looking for red flags 2. Reading execution plans Query tuning techniques: 8 more specific examples of widespread approaches that lead to poor performance Summary 3

Code to clear the caches*: o CHECKPOINT o DBCC [FreeProcCache | FreeSystemCache | FlushProcInDB( ) ] o DBCC DropCleanBuffers Code to set measurements: o SET STATISTICS [TIME | IO] o SET SHOWPLAN [TEXT | XML] or Graphic Execution Plans Code for Dynamic Management Views (DMV) checks. o System info – sys.dm_os_performance_counters, sys.dm_os_wait_stats o Query info – sys.dm_exec_requests o Index info – sys.dm_db_index_usage_stats, sys.dm_io_virtual_file_stats

Red Flags Query Operators: o Lookups, Scans, Spools, Parallelism Operations Other Red Flags: o Dissimilar estimated versus actual row counts o High physical reads o Missing statistics alarms o Large sort operations o Implicit data type conversions

I don’t always use cursors… o …but when I do, I avoid the default options o Slow and heavy-handed: Global, updateable, dynamic, scrollable o I use LOCAL FAST_FORWARD o May want to test STATIC vs. DYNAMIC, when tempdb is a bottleneck Blog post:

There are lots of ways to find data existing within subsets: IN, EXISTS, JOIN, Apply, subquery Which technique is best? Blog post:

Big differences between a SELECT and a DML statement that effects the same rows. Shouldn’t blindly create every index the Tuning Advisor or execution plan tells you to! Blog post -

8K pages Leaf pages ARE the data. Non-leaf pages are pointers. Leaf Pages Root Page Level 0 Intermediate Pages Level 1 Level 2

Each change to the leaf pages requires all index structures be updated. Leaf Pages Root Page Level 0 Intermediate Pages Level 1 Level 2 Page Split DML Actual place- ment

Execution Load metadata NO In Memory? compile optimize Execute YES ReComp Execute

Expected: Because we request it: CREATE PROC … WITH RECOMPILE or EXEC myproc … WITH RECOMPILE SP_RECOMPILE foo Expected: Plan was aged out of memory Unexpected: Interleaved DDL and DML Unexpected: Big changes since last execution: Schema changes to objects in underlying code New/updated index statistics Sp_configure

CREATE PROC testddldml AS … ; CREATE TABLE #testdml;-- (DDL) INSERT INTO #testdml;-- (DML + RECOMPILE) ALTER TABLE #testdml;-- (DDL) INSERT INTO #testdml;-- (DML + RECOMPILE) DROP TABLE #testdml;-- (DDL)

Usually see it as a one-query-for-all-queries procedure, or even one-proc-for-for-all-transactions procedure: o Where name starts with S, or placed an order this year, or lives in Texas o Insert AND Update AND Delete AND Select Conflicting optional parameters make optimization impossible o OPTION (RECOMPILE) o Dynamic SQL + Optimize for ad hoc workloads o Specialized procedures Better approach? o Specialize and optimize each piece of code to do ONE THING really effectively

I don’t always use dynamic SQL… o …but when I do, I always use sp_executesql o Less fuss with concatenation and implicit/explicit conversions o Better protection against SQL injection (but not for all things) o At worst case, behavior is the same Can promote better plan re-use Encourages strongly typed parameters instead of building up a massive string

SQL Server has to do a lot of extra work / scans when conversion operations are assumed by the SQL programmer. Happens all the time with data types you’d think wouldn’t need it, e.g. between date types and character types. Very useful data type conversion chart at Data type precedence call also have an impact:

Ian Stirk’s Column Mismatch Utility at / / Jonathan Kehayias’ plan cache analyzer at /01/08/finding-implicit-column-conversions-in-the-plan- cache.aspx. /01/08/finding-implicit-column-conversions-in-the-plan- cache.aspx Jonathan Kehayias’ index scan study at queries/implicit-conversion-costs queries/implicit-conversion-costs

Example: pass a comma-separated list of OrderIDs String splitting is expensive, even using CLR Table-valued parameters are typically a better approach

Which are better, temp tables or temp variables? Temp TableTemp Variable Stored in?Tempdb Statistics?YesNo (1 row) Indexs/Keys?Yes1 UK / PK only Truncate?YesNo Recompiles?YesNo Parallelism?YesNo Metadata Overhead? LowLowest Lock Overhead?NormalLowest

Might sound frivolous, but naming schemes are important o Convention is not important; but rather being consistent and logical Story: dbo.UpdateCustomer vs. dbo.Customer_Update Always specify schema when creating, altering, referencing objects o Object resolution works a little bit harder without it o More importantly, it can get the wrong answer o And will often yield multiple copies of the same plan Do not use the sp_ prefix on stored procedures o This has observable overhead, no matter how specific you are

Your dev machine is usually nothing like production o Build representative data when you can o Build a stats-only database when you can’t (a.k.a. a database clone) Will allow you to see plan issues, but not speed o Make sure settings are the same edition Max memory if possible, sp_configure options Logins (and permissions), tempdb settings Parameterization settings, recovery model, compression, snapshot isolation Compatibility level (usually not an issue when working with a restore) Run a full business cycle workload after a restore o Simulate equivalent hardware: DBCC OPTIMIZER_WHATIF o Use Distributed Replay when you can Not perfect, but more realistic than single-threaded trace replay

Test Harness for your SQL Code to ensure stable results. Remember the red flags in SQL code running against SQL Server. Plus 8 more coding techniques: 1.Writing “good” cursors 2.WHERE IN versus WHERE EXISTS 3.The nature of indexes and Optimizing for Reads versus Writes 4.Inadvertent recompiles 5.EXEC versus SP_EXECUTESQL 6.Data types and Implicit conversion 7.Dissimilar execution plans and tracking plan cache bloat 8.Mimicking the production environment More info? Let’s connect! Facebook, LinkedIn, Twitter at KEKLINE. at Blogs at And

Engage with us on social media o I’m thankful for your word of mouth promotions and endorsements! Share your tough SQL tuning problems with us: Download SQL Sentry Plan Explorer for free: Check out our other award winning tools:

It is a turbo button …if you’re ok with inaccuracy There are times it is perfectly valid o Ballpark row counts o Please use session-level setting, not table hint Usually, though, better to use SNAPSHOT or RCSI o But test under heavy load