sql server optimization checklist

29
Grant Fritchey | www.ScaryDBA.com www.ScaryDBA.com SQL Server Optimization Checklist Grant Fritchey Product Evangelist Red Gate Software

Upload: grant-fritchey

Post on 12-Jul-2015

874 views

Category:

Software


4 download

TRANSCRIPT

Page 1: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

www.ScaryDBA.com

SQL Server Optimization

Checklist

Grant FritcheyProduct EvangelistRed Gate Software

Page 2: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Agenda

Server Configuration

SQL Server Administration

Database Design

TSQL Coding

2

Page 3: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Get it Touch

3

scarydba.com

[email protected]

@gfritchey

Page 4: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Test

Test

Test

Test

Test

4

Page 5: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Remember

This is focused on performance tuning

5

Page 6: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Remember

This is focused on performance tuning

6

There are always exceptions

Page 7: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Remember

This is focused on performance tuning

There are always exceptions

7

Your hardware may be DIFFERENT

Page 8: SQL Server Optimization Checklist

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?

Page 9: SQL Server Optimization Checklist

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

Page 10: SQL Server Optimization Checklist

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

Page 11: SQL Server Optimization Checklist

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

Page 12: SQL Server Optimization Checklist

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

Page 13: SQL Server Optimization Checklist

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

Page 14: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Server Configuration

14

Memory CPU Disks

Page 15: SQL Server Optimization Checklist

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

Page 16: SQL Server Optimization Checklist

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

Page 17: SQL Server Optimization Checklist

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

Page 18: SQL Server Optimization Checklist

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

Page 19: SQL Server Optimization Checklist

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

Page 20: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Database Design

Storage

Constraints

Indexes

20

Page 21: SQL Server Optimization Checklist

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

Page 22: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Database Design:

Constraints

Normalize your Data

» Normalization benefits performance

Enforce Constraints

» Foreign keys

» Primary keys

» Unique values

22

Page 23: SQL Server Optimization Checklist

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

Page 24: SQL Server Optimization Checklist

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

Page 25: SQL Server Optimization Checklist

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

Page 26: SQL Server Optimization Checklist

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

Page 27: SQL Server Optimization Checklist

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

Page 28: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Relax

It’s not that bad

Most things can be changed

Help is available

Test everything

28

Page 29: SQL Server Optimization Checklist

Grant Fritchey | www.ScaryDBA.com

Questions?

29