Query Tuning Fundamentals
Vern Rabe Portland, OR independent consultant, trainer MCTA, MCDBA, MCSE, MCITP VP Oregon PASS chapter Working with/focusing on SQL Server since 1993, starting with SQL Server 4.21a
What We’ll Cover The Optimizer Tools Query Writing Suggestions Stored Procedures Triggers Views Indexes
The Optimizer What is it? What does it do? Not all plans considered (Good enough) Timeout? Statistics> Auto update statistics Auto create statistics
Tools What Resource (usually) has Most Performance Impact? I/O SET STATISTICS> SET STATISTICS IO ON SET STATISTICS TIME ON Execution Plan Estimated vs. Actual> Join types: Merge/Nested Loop/Hash Match Each has its place Execution Logging> “home grown”
Query Writing Suggestions Think set (avoid RBAR) Sometimes “chunk” NOLOCK (READUNCOMMITTED)> Avoid SELECT * in production code Unnecessary bits across the wire Plans often not as efficient> UNION vs. UNION ALL> Always include schema name SET NOCOUNT ON
Query Writing Suggestions SARGable> Watch out for Implicit Conversion> Avoid leading wild card comparisons Avoid user defined scalar functions in predicates> Limit number of JOINs DISTINCT Only UPDATE if something changed>
Stored Procedures Why stored procedures? Parameter Sniffing> Code modularity Pre-compiled Parameter Sniffing> WITH RECOMPILE OPTION (RECOMPILE) Branch Do not name a stored procedure “sp_”…
Triggers Can cause performance issues Try to use CONSTRAINT first Performance issues obfuscated Triggers code is not in Estimated Execution Plan>
Views Views per se are not a performance problem Abuse of views ARE a performance concern Views of views – too many joins and obfuscated Indexed (materialized) views
Not sure what should go here Thanks. Questions? Not sure what should go here vern@rabedata.com @VernRabe Vern Rabe
Thanks Questions? Contact information vern@rabedata.com LinkedIn www.linkedin.com/pub/vern-rabe/a/ba3/980 Resources Join operators http://www.madeira.co.il/loop-hash-and-merge-join-types/