sql performance 2011/12 joe chang, solidq [email protected]

38
SQL Performance 2011/12 Joe Chang, SolidQ www.qdpma.com http:// sqlblog.com/blogs/joe_chang/default.aspx [email protected]

Upload: nathaniel-hasting

Post on 14-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

SQL Performance 2011/12

Joe Chang, SolidQwww.qdpma.com http://sqlblog.com/blogs/joe_chang/[email protected]

Page 2: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

2011

• Hardware is Powerful & Cheap– CPU (cores), memory, and now IO too!• Quad-core since 2006, 1GHz since 2000

• Is Performance still a concern?– Yes, along with fundamentals

• Modern Performance Strategy– Can handle minor inefficiencies– Identify and circumvent the really bad things

Page 3: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Modern Hardware

• 4-12 cores per processor socket• 16GB DIMM at less than $1K• SSD– Enterprise grade SSD still moderately expensive• Both SLC and MLC

– Consumer SSD – really cheap, $3-4K per TB• Uneven performance characteristics over time

• Desired IO performance: 1-2GB/s, 20-50K IOPS

Page 4: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Hardware Baseline 2011

Entry• 1 Xeon E3 quad-core• 16GB memory

– 4 x 4GB unbuffered ECC

• SSD options:– 2-4 SATA SSDs– 1-2 PCI-E SSDs

Mid-range • 2 Xeon 5600 6-core• 48 – 192GB

– 6 x 8GB to 12 x 16GB

• SSD options– 16+ SATA SSDs– 4-5 PCI-E SSDs

Page 5: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Performance Fundamentals

• Network round-trips– Owner qualified, case correct

• Log write latencies– Sufficiently low to support transaction volume• Not necessarily separate data and log disks

• Normalization – correct data trumps all!• Indexes – a few good ones, and not too many!• SQL – that the optimizer

Page 6: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

What can go wrong?

• With immense hardware resources• And a great database engine– What can go wrong?

• Following a fixed set of rules and procedures– Basic transactions processing should work well– If your process does something unanticipated• Some things can go horribly wrong

Page 7: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Performance Concepts

• Query Optimizer• Execution plan operators– Formula for component operation

• Data distribution statistics – to estimate rows & pages, automatically updated– Rules when estimate not possible

• Stored procedure compile rules– Parameters and variables

Page 8: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Stored Procedure

Basics

Page 9: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Parameters and Variables

• On compile– Parameter values used to for row estimate– Variables – assume unknown value

• Consider effect of skewed distribution

Page 10: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Parameter & Variable

6 rows for value 1

4 rows for value unknown

Consider impact for skewed data distributions

Page 11: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Stored Procedure Compile Options

• WITH RECOMPILE• OPTIMIZE FOR• Plan Guide

• Temp table– KEEP PLAN, KEEPFIXED PLAN

Page 12: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Compile & Execute Time

• Plan reuse desired when– Compile cost is high relative to execute cost

• Recompile desired when– Execute cost is high relative to compile cost

Page 13: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Statistics

Basics

Page 14: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Statistics

• No statistics – table variables• Temp table – statistics auto recompute– 6 row modified, 500 rows, every 20% thereafter

• Statistics sampling– Random page, how to handle skewed distribution?

• Upper and lower bounds– Problems caused by incrementing columns

• Propagation errors

Page 15: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Statistics Recompute

• Scenario: start with accurate statistics• Update column with new values– That did not previously exist

• If fewer than 20% of rows updated– Auto-recompute is not triggered

Page 16: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Sampling

• Default sampling percentage is usually good• Caution: not a random row sample!– Random sampling of page• From nonclustered index if available

• If there is correlation between pages & values– Then serious over estimation possible

Page 17: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Out of range

• Statistics sampling tries to identify lower and upper bound

Page 18: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Bad Execution Plan Examples

Not comprehensive

Page 19: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Scenarios

• Or condition• Multiple optional search arguments• Skewed distributions– 1 business logic for Small and large data sets– Reports for 1 day, 1 week, 1 month, 1 year

• Statistics related problems– Resulting in horrible execution plan

Not comprehensive

Page 20: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

When the Query Optimizer

Does not understand you

Page 21: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Simple OR Conditions

Page 22: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Simple Index Seek

Page 23: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

OR Condition in Join

Page 24: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Alternative – Union

Page 25: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

UNION and UNION ALL

• UNION– Only distinct rows– Sort to eliminate duplicates• Can be expensive for high row counts

• UNION ALL– All rows– No sort to eliminate duplicates

Page 26: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Multiple Optional SARGs

This was suppose to work, but does not

Page 27: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Parameterized SQL

Page 28: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Skew and Range Variation

Page 29: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Statistics Out-of Range

Page 30: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Statistics Out of Range (cont.)

Page 31: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Table Variable

No Statisticsassumes 1 row, 1page Why? No recompiles

Page 32: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Loop Join – Scan Inner Source

Really Bad News

Estimate 1 row

Page 33: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

The Correct Plan

Hash Join forcedwith hint

Estimate 1 row

Page 34: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Loop Join vs. Hash Join

Page 35: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Alternative Plan with Index

Page 36: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com

Temp Table versus CTE

• Consider options– SELECT xxx INTO #Temp– FROM SqlMain Expression

– WITH tmp AS (SELECT xxx FROM Sql)Main Expression

Page 37: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com
Page 38: SQL Performance 2011/12 Joe Chang, SolidQ    jchang6@yahoo.com