Kevin Kline Director of Engineering Services, SQL Sentry SQL Server MVP since 2003 Twitter, FB, LI: KEKline Blog:
Tuning blog: for free copies of our
ToolsTechniques Process, Patterns & Practices Happy & Helpful DBA!
DBAs: Stodgy Conservative Slow Control Freak Dev’s say “Drats, thwarted again!” The truth? Our job is to act as the guardian of the true corporate IT assets. Developers: Cowboys Reckless Hasty Chaotic DBAs say “Blimey, my database is besmirched!” The truth? Our job is build products that are high quality, fast, and cheap (pick two).
Dev workstations are usually nothing like production: o Build representative data when you can. (Remember auto-update stats). o Build a stats-only database (a.k.a. db clone) for smaller dev environments o Ensure important settings are the same. o Simulate equivalent hardware: DBCC OPTIMIZER_WHATIF.
Warm caching or Cold caching? Measure: o Total elapsed time o Individual statement time o IO load Facilitates automation of execution plans Get to know the DMV sys.dm_exec_query_stats
The steps used by SQL Server to process a query are displayed in an execution plan. o Learn how to compare in SSMS. Better yet, use the free Plan Explorer product. o Learn how to watch in real-time with SQL Profiler or Extended Events. Comparative query performance might mislead: o Should evaluate on comparable data sizes. o Different CPU, disk, caching, etc. o Evaluate the query cost.
Red Flags Query Operators: Lookups, Scans, Spools, Parallelism Operations Other Red Flags: Dissimilar estimated versus actual row counts High physical reads Missing statistics alarms Large sort operations Implicit data type conversions
getord Plan Cache Memory finduser sp_1sp_ What about buffer cache?
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 Execution Load metadata NO In Memory? compile optimize Execute YES ReComp Execute
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)
Cursors are usually unnecessary, but when they are, use the right options. The defaults are heavy-handed and guaranteed to be slow. Blog post:
Leaf pages are the DATA. Non-leaf pages are POINTERS, but are also data. 8K pages. Not always self-healing. Make sure they’re on your important search arguments, a.k.a. “sargs”. Leaf Pages Root Page Level 0 Intermediate Pages Level 1 Level 2
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 -
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:
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
Coding standards. Parameterization. Remember the power of specialized code. More details:
Mimic production as best you can Test harness & testing Read execution plans Get to know the plan cache Avoid unwanted recompiles Use cursors appropriately Create useful and balanced indexes Write code that doesn’t break indexes Temp variables <> pure goodness Prevent cache bloat
PLEASE FILL OUT THE SESSION EVAL! Send questions to me at: Twitter, Facebook, LinkedIn at KEKline Slides at and now at Slideshare.Nethttp://KevinEKline.com/Slides/ IT leadership and soft skills content at THANK YOU!