sql server optimization checklist
TRANSCRIPT
Grant Fritchey | www.ScaryDBA.com
www.ScaryDBA.com
SQL Server Optimization
Checklist
Grant FritcheyProduct EvangelistRed Gate Software
Grant Fritchey | www.ScaryDBA.com
Agenda
Server Configuration
SQL Server Administration
Database Design
TSQL Coding
2
Grant Fritchey | www.ScaryDBA.com
Test
Test
Test
Test
Test
4
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
5
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
6
There are always exceptions
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
7
Your hardware may be DIFFERENT
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
Your hardware may be different
8
Did I mention test?
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
Your hardware may be different
Did I mention test?
9
Incre
mental changes
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
Your hardware may be different
Did I mention test?
Incremental changes
10
onitor your servers
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
Your hardware may be different
Did I mention test?
Incremental changes
Monitor your servers
11
I am NOT a systems expert
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
Your hardware may be different
Did I mention test?
Incremental changes
Monitor your servers
I am not a systems expert
12
You might want to TEST
Grant Fritchey | www.ScaryDBA.com
Remember
This is focused on performance tuning
There are always exceptions
Your hardware may be different
Did I mention test?
Incremental changes
Monitor your servers
I am not a systems expert
You might want to test
13
Grant Fritchey | www.ScaryDBA.com
Server Configuration
14
Memory CPU Disks
Grant Fritchey | www.ScaryDBA.com
SQL Server Configuration:
Memory
>4g enable AWE
» On 2008R2 or less
» On 32-bit systems
Dynamic
» but set a minimum and a maximum value
Optimize for Ad Hoc Workloads
» Enable on all servers
15
Grant Fritchey | www.ScaryDBA.com
SQL Server Configuration:
CPU
Cost Threshold for parallelism
» 5, the default, is too low
» OLTP = 40
» Reporting = 25
Max Degree of Parallelism
» Leave it on, after you change the cost threshold
Consider using Query Governor
» Set a cost limit
16
Grant Fritchey | www.ScaryDBA.com
SQL Server Configuration:
Storage
TEMPDB» Separate from other data when possible» Multiple Files
— Not equal to #of processors— Equal sized files
Compression» Minimum, index compression
More disks is better» But only if you have more controllers
Spend money» Buy faster disks
17
Grant Fritchey | www.ScaryDBA.com
SQL Server
Administration
Maintain Statistics» Enable AUTO_CREATE & AUTO_UPDATE » You must also plan for manual updates» You may need FULL SCAN» Consider ASYNC
Defragment your indexes» Number of pages does matter» You can defrag below 1000 pages» You can’t defrag below 8 pages» Separate defrag & statistics updates
18
Grant Fritchey | www.ScaryDBA.com
SQL Server
Administration 2
Avoid dangerous settings» Disable AUTO_CLOSE» Disable AUTO_SHRINK
Do not use the Profiler GUI on production systems» Create a server-side trace using TSQL scripts
(prior to 2008)» Use extended events
— Consider for 2008 or better— Absolutely for 2012 or better
Set Blocked Process Threshold» Based on your system
19
Grant Fritchey | www.ScaryDBA.com
Database Design
Storage
Constraints
Indexes
20
Grant Fritchey | www.ScaryDBA.com
Database Design: Storage
Separate Log & Data
Multiple File Groups
» Even on a single drive
Turn off auto-grow
» If not possible, used fixed growth, not percentage
Consider In-Memory
» 2014 only
» I/O Latch waits
21
Grant Fritchey | www.ScaryDBA.com
Database Design:
Constraints
Normalize your Data
» Normalization benefits performance
Enforce Constraints
» Foreign keys
» Primary keys
» Unique values
22
Grant Fritchey | www.ScaryDBA.com
Database Design: Indexes -
General
Narrow
» As narrow as possible
Integers
» Believe it or not, they’re better
Choose leading edge carefully
» Most selective column
Covering Indexes are fastest
» Use INCLUDE
23
Grant Fritchey | www.ScaryDBA.com
Database Design: Indexes -
Clustered
Again, Narrow
» As narrow as possible
Design around the clustered index
» Most frequent used path to data
Rebuilding clustered index
» Use DROP_EXISTING
Avoid columns that get updated frequently
» This means all non-clustered indexes are updated too
24
Grant Fritchey | www.ScaryDBA.com
TSQL Coding
Return only the data you need, when you need it
Use stored procedures or parameterized queries
» Encourages plan reuse
» Prevents SQL Injection
Avoid cursors, WHILE, LOOP
» Row By Agonizing Row style processing
Qualify all object names
» Reduces lookup costs
Avoid using ‘sp_*’ for procedure names
» Eliminates additional read to the master database
25
Grant Fritchey | www.ScaryDBA.com
TSQL Coding 2
Avoid nonsargeable conditions
» Meaning functions on columns
» Or, using LIKE ‘%Anything’
Use SET NOCOUNT ON
» Reduces returns on network
Do not nest views
» Or join them. It confuses the optimizer
Don’t use optimizer hints
» More often than not it causes problems
» Especially NOLOCK
26
Grant Fritchey | www.ScaryDBA.com
TSQL Coding 3
Avoid Recompiling Execution Plans
» Stored procedures & parameterized queries
» Table variables instead of temp tables
» ANSI standards on connection settings— And Don’t change them within a query
» Don’t interleave DDL & DML
Adopt best practices for transactions
» Keep scope as short as possible
» Use SET XACT_ABORT
» Consider using READ COMMITTED SNAPSHOT
27
Grant Fritchey | www.ScaryDBA.com
Relax
It’s not that bad
Most things can be changed
Help is available
Test everything
28
Grant Fritchey | www.ScaryDBA.com
Questions?
29