• No results found

Optimizing Procedural Code

N/A
N/A
Protected

Academic year: 2021

Share "Optimizing Procedural Code"

Copied!
12
0
0

Loading.... (view fulltext now)

Full text

(1)

.

Net Rocks!

Visual

 

Studio

 

2012

 

Launch

 

Road

 

Trip

Optimizing

 

Procedural

 

Code

Kimberly

 

L.

 

Tripp

[email protected]

.

Net Rocks!

Visual

 

Studio

 

2012

 

Launch

 

Road

 

Trip

Optimizing

 

Procedural

 

Code

Kimberly

 

L.

 

Tripp

(2)

Kimberly L. Tripp

Kimberly L. Tripp

Consultant/Trainer/Speaker/Writer

President/Founder,

 

SYS

olutions,

 

Inc.

 

e‐mail: [email protected]

blog: http://www.SQLskills.com/blogs/Kimberly

Twitter: @KimberlyLTripp

Author/Instructor for SQL Server Immersion Events

Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM

Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager for SQL 

Server 2008 Jumpstart Content, Author/Speaker at TechEd, SQL Connections, 

SQLPASS, ITForum, Conference Co‐chair for SQL Connections Author of several SQL Server Whitepapers on MSDN/TechNet: 

Partitioning, Snapshot Isolation, Manageability, SQLCLR for DBAs Author/Presenter for more than 25 online webcasts on MSDN 

and TechNet (two series and other individual webcasts)

Co‐author MSPress Title: SQL Server 2008 Internals, the SQL 

Server MVP Project (1 & 2), and SQL Server 2000 High Availability Presenter/Technical Manager for SQL Server HOL DVDs

Consultant/Trainer/Speaker/Writer

President/Founder,

 

SYS

olutions,

 

Inc.

 

e‐mail: [email protected]

blog: http://www.SQLskills.com/blogs/Kimberly

Twitter: @KimberlyLTripp

Author/Instructor for SQL Server Immersion Events

Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM

Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager for SQL 

Server 2008 Jumpstart Content, Author/Speaker at TechEd, SQL Connections, 

SQLPASS, ITForum, Conference Co‐chair for SQL Connections Author of several SQL Server Whitepapers on MSDN/TechNet: 

Partitioning, Snapshot Isolation, Manageability, SQLCLR for DBAs Author/Presenter for more than 25 online webcasts on MSDN 

and TechNet (two series and other individual webcasts)

Co‐author MSPress Title: SQL Server 2008 Internals, the SQL 

Server MVP Project (1 & 2), and SQL Server 2000 High Availability Presenter/Technical Manager for SQL Server HOL DVDs

(3)

SQLskills.com

http://www.SQLskills.com

SQLskills

SQLskills

Team

 

of

 

world

renowned

 

SQL

 

Server

 

experts

Paul

 

S.

 

Randal

 

@PaulRandal

Jonathan

 

Kehayias @SQLPoolBoy

Joseph

 

Sack

 

@JosephSack

Glenn

 

Berry

 

@GlennAlanBerry

Erin

 

Stellato

 

@ErinStellato

Instructor

led

 

training

 

Immersion

 

Events

Online

 

training

 

Pluralsight

(

http://pluralsight.com/

)

 

Consulting:

 

health

 

checks,

 

hardware,

 

performance

Remote

 

DBA

Become

 

a

 

SQLskills Insider

http://www.sqlskills.com/JoinCommunity.asp

Team

 

of

 

world

renowned

 

SQL

 

Server

 

experts

Paul

 

S.

 

Randal

 

@PaulRandal

Jonathan

 

Kehayias @SQLPoolBoy

Joseph

 

Sack

 

@JosephSack

Glenn

 

Berry

 

@GlennAlanBerry

Erin

 

Stellato

 

@ErinStellato

Instructor

led

 

training

 

Immersion

 

Events

Online

 

training

 

Pluralsight

(

http://pluralsight.com/

)

 

Consulting:

 

health

 

checks,

 

hardware,

 

performance

Remote

 

DBA

Become

 

a

 

SQLskills Insider

(4)

Overview

Overview

Back

 

to

 

the

 

basics…

Never

 

say

 

never,

 

never

 

say

 

always…

It

 

depends!

Problem

 

area

 

to

 

highlight

Parameter

 

sniffing

What

 

is

 

it?

When/why

 

does

 

it

 

become

 

problematic?

Recompilation

 

in

 

procedures

What

 

to

 

recompile?

How

 

to

 

effectively

 

use

 

statement

level

 

recompilation

Back

 

to

 

the

 

basics…

Never

 

say

 

never,

 

never

 

say

 

always…

It

 

depends!

Problem

 

area

 

to

 

highlight

Parameter

 

sniffing

What

 

is

 

it?

When/why

 

does

 

it

 

become

 

problematic?

Recompilation

 

in

 

procedures

What

 

to

 

recompile?

(5)

SQLskills.com

http://www.SQLskills.com

Processing

 

Stored

 

Procedures

Processing

 

Stored

 

Procedures

Compiled plan placed in unified plan cache

Reused on subsequent executions parameter sniffing PROBLEMS

Execution

(when a plan  does not  already exist in  cache) Resolution* Optimization Parsing Resolution

Creation

A procedure's plan is NOT saved 

to disk; only metadata is saved 

at procedure creation

Use sys.procedures, sp_

procs, functions and views to 

see metadata

5

“sniffing” runtime 

Compilation

(6)

Procedure Caching

Isn’t

 

That

 

the

 

Point?

Procedure Caching

Isn’t

 

That

 

the

 

Point?

Reusing

 

plans

 

can

 

be

 

good

When

 

different

 

parameters

 

don’t

 

change

 

the

 

optimal

 

plan,

 

then

 

saving

 

and

 

reusing

 

is

 

excellent!

SQL

 

Server

 

saves

 

time

 

in

 

compilation

Reusing

 

plans

 

can

 

be

 

VERY

 

bad

When

 

different

 

parameters

 

wildly

 

change

 

the

 

size

 

of

 

the

 

result

 

set

 

and

 

the

 

optimal

 

plans

 

vary,

 

then

 

reusing

 

the

 

plan

 

can

 

be

 

horribly

 

bad

If

 

indexes

 

are

 

added

 

to

 

base

 

tables,

 

existing

 

plans

 

may

 

not

 

leverage

 

them

Don’t

 

worry,

 

there’s

 

a

 

simple

 

solution

 

here:

EXEC sp_recompile <tablename>

Reusing

 

plans

 

can

 

be

 

good

When

 

different

 

parameters

 

don’t

 

change

 

the

 

optimal

 

plan,

 

then

 

saving

 

and

 

reusing

 

is

 

excellent!

SQL

 

Server

 

saves

 

time

 

in

 

compilation

Reusing

 

plans

 

can

 

be

 

VERY

 

bad

When

 

different

 

parameters

 

wildly

 

change

 

the

 

size

 

of

 

the

 

result

 

set

 

and

 

the

 

optimal

 

plans

 

vary,

 

then

 

reusing

 

the

 

plan

 

can

 

be

 

horribly

 

bad

If

 

indexes

 

are

 

added

 

to

 

base

 

tables,

 

existing

 

plans

 

may

 

not

 

leverage

 

them

Don’t

 

worry,

 

there’s

 

a

 

simple

 

solution

 

here:

(7)

SQLskills.com http://www.SQLskills.com

Recompilation

 

Issues

Recompilation

 

Issues

RECOMPILATION

 

=

 

OPTIMIZATION

OPTIMIZATION

 

=

 

RECOMPILATION

When

 

do

 

you

 

want

 

to

 

recompile?

What

 

options

 

do

 

you

 

have

 

for

 

recompilation

 

– and

 

at

 

what

 

granularity?

How

 

do

 

you

 

know

 

you

 

need

 

to

 

recompile?

Do

 

you

 

want

 

to

 

recompile

 

the

 

entire

 

procedure

 

or

 

only

 

part

 

of

 

it?

Can

 

you

 

test

 

it?

RECOMPILATION

 

=

 

OPTIMIZATION

OPTIMIZATION

 

=

 

RECOMPILATION

When

 

do

 

you

 

want

 

to

 

recompile?

What

 

options

 

do

 

you

 

have

 

for

 

recompilation

 

– and

 

at

 

what

 

granularity?

How

 

do

 

you

 

know

 

you

 

need

 

to

 

recompile?

Do

 

you

 

want

 

to

 

recompile

 

the

 

entire

 

procedure

 

or

 

only

 

part

 

of

 

it?

Can

 

you

 

test

 

it?

(8)

Options

 

for

 

Recompilation

Options

 

for

 

Recompilation

CREATE … WITH RECOMPILE

EXECUTE … WITH RECOMPILE

sp_recompile objname

Statement

level

 

recompilation

The

 

2000+

 

ways

 

(still

 

have

 

benefits)

Dynamic

 

string

 

execution

 

(statement’s

 

plan

 

not

 

stored

 

with

 

procedure’s

 

plan)

Modularized

 

code

 

(reduced

 

sniffing

 

problems

 

with

 

conditional

 

logic/branching)

Better

 

way

 

to

 

change

 

compilation/optimization

2005+:

 

OPTION(RECOMPILE)

2005+:

 

OPTION (OPTIMIZE FOR

(@variable_name = constant, ...) )

CREATE … WITH RECOMPILE

EXECUTE … WITH RECOMPILE

sp_recompile objname

Statement

level

 

recompilation

The

 

2000+

 

ways

 

(still

 

have

 

benefits)

Dynamic

 

string

 

execution

 

(statement’s

 

plan

 

not

 

stored

 

with

 

procedure’s

 

plan)

Modularized

 

code

 

(reduced

 

sniffing

 

problems

 

with

 

conditional

 

logic/branching)

Better

 

way

 

to

 

change

 

compilation/optimization

2005+:

 

OPTION(RECOMPILE)

2005+:

 

OPTION (OPTIMIZE FOR

(9)

SQLskills.com

http://www.SQLskills.com

Statement

Level

 

Recompilation

Statement

Level

 

Recompilation

In

 

2005

 

and

 

2008:

 

“inline”

 

recompilation

 

for

 

statements

OPTION (RECOMPILE)

Excellent when parameters cause the execution plan to widely vary Bad because EVERY execution will recompile – but only for the 

statements

OPTION (OPTIMIZE FOR

(@variable = literal, ...))

Excellent when large majority of executions generate the same 

optimization time

You don’t care that the minority may run slower with a less than optimal 

plan?

2008 only: OPTION (OPTIMIZE FOR UNKNOWN)

Use the “all density” (average) instead of the histogram

In

 

2005

 

and

 

2008:

 

“inline”

 

recompilation

 

for

 

statements

OPTION (RECOMPILE)

Excellent when parameters cause the execution plan to widely vary Bad because EVERY execution will recompile – but only for the 

statements

OPTION (OPTIMIZE FOR

(@variable = literal, ...))

Excellent when large majority of executions generate the same 

optimization time

You don’t care that the minority may run slower with a less than optimal 

plan?

2008 only: OPTION (OPTIMIZE FOR UNKNOWN)

Use the “all density” (average) instead of the histogram

(10)

Recompilation

Recompilation

(11)

SQLskills.com

http://www.SQLskills.com

Stored

 

Procedure

 

Resources

 

Stored

 

Procedure

 

Resources

 

Plan

 

Caching

 

in

 

SQL

 

Server

 

2008

http://msdn.microsoft.com/en

us/library/ee343986.aspx

Batch

 

Compilation,

 

Recompilation,

 

and

 

Plan

 

Caching

 

Issues

 

in

 

SQL

 

Server

 

2005

http://www.microsoft.com/technet/prodtechnol/sql/2005/rec

omp.mspx

PSS

 

SQL

 

Server

 

Engine

 

Blog

http://blogs.msdn.com/psssql/default.aspx

KB

 

243586:

 

Troubleshooting

 

Stored

 

Procedure

 

Recompilation

KB

 

308737:

 

How

 

to

 

identify

 

the

 

cause

 

of

 

recompilation

 

in

 

an

 

SP:Recompile event

 

(SQL

 

Server

 

2000

 

SP2+

 

has

 

6

 

subclass

 

values,

 

SQL

 

Server

 

2005

 

has

 

11

 

subclass

 

values

 

and

 

SQL

 

Server

 

2008

 

RTM

 

has

 

14

 

subclass

 

values)

KB

 

263889:

 

Description

 

of

 

SQL

 

Server

 

blocking

 

caused

 

by

 

compile

 

locks

Plan

 

Caching

 

in

 

SQL

 

Server

 

2008

http://msdn.microsoft.com/en

us/library/ee343986.aspx

Batch

 

Compilation,

 

Recompilation,

 

and

 

Plan

 

Caching

 

Issues

 

in

 

SQL

 

Server

 

2005

http://www.microsoft.com/technet/prodtechnol/sql/2005/rec

omp.mspx

PSS

 

SQL

 

Server

 

Engine

 

Blog

http://blogs.msdn.com/psssql/default.aspx

KB

 

243586:

 

Troubleshooting

 

Stored

 

Procedure

 

Recompilation

KB

 

308737:

 

How

 

to

 

identify

 

the

 

cause

 

of

 

recompilation

 

in

 

an

 

SP:Recompile event

 

(SQL

 

Server

 

2000

 

SP2+

 

has

 

6

 

subclass

 

values,

 

SQL

 

Server

 

2005

 

has

 

11

 

subclass

 

values

 

and

 

SQL

 

Server

 

2008

 

RTM

 

has

 

14

 

subclass

 

values)

KB

 

263889:

 

Description

 

of

 

SQL

 

Server

 

blocking

 

caused

 

by

 

compile

 

locks

(12)

Plan

 

Cache

 

Pollution

 

Resources

Plan

 

Cache

 

Pollution

 

Resources

Blog

 

posts:

Plan

 

cache

 

and

 

optimizing

 

for

 

adhoc workloads

 

Plan

 

cache,

 

adhoc workloads

 

and

 

clearing

 

the

 

single

use

 

plan

 

cache

 

bloat

Clearing

 

the

 

cache

 ‐

are

 

there

 

other

 

options?

Statement

 

execution

 

and

 

why

 

you

 

should

 

use

 

stored

 

procedures

Category: Optimizing Procedural Code

http://www.sqlskills.com/BLOGS/KIMBERLY/category/Opti

mizing

Procedural

Code.aspx

MSDN

 

Article:

 

How

 

Data

 

Access

 

Code

 

Affects

 

Database

 

Performance,

 

Bob

 

Beauchemin

http://msdn.microsoft.com/en

Blog

 

posts:

Plan

 

cache

 

and

 

optimizing

 

for

 

adhoc workloads

 

Plan

 

cache,

 

adhoc workloads

 

and

 

clearing

 

the

 

single

use

 

plan

 

cache

 

bloat

Clearing

 

the

 

cache

 ‐

are

 

there

 

other

 

options?

Statement

 

execution

 

and

 

why

 

you

 

should

 

use

 

stored

 

procedures

Category: Optimizing Procedural Code

http://www.sqlskills.com/BLOGS/KIMBERLY/category/Opti

mizing

Procedural

Code.aspx

MSDN

 

Article:

 

How

 

Data

 

Access

 

Code

 

Affects

 

Database

 

Performance,

 

Bob

 

Beauchemin

References

Related documents

Thus far, NAHR events resulting in deletions, duplications, inversions, or isodicentric chromosomes have been reported only for amplicon pairs located exclusively on the short arm

e'idence and pro&amp;a&amp;ilit&#34; theor&#34;, used to determine /hether e'idence and pro&amp;a&amp;ilit&#34; theor&#34;, used to determine /hether the h&#34;pothesis is

Put differently, the debtor in voluntary surrender and the sequestrating creditor or friendly creditor in compulsory or friendly sequestration respectively, are

8 University of Rhode Island Digital Forensics Center Report Driver Order showing NIST test drivers and SAFE Block XP V1.1... 9 University of Rhode Island Digital Forensics

HP Digital Entertainment Center infrared remote control; HP Digital Entertainment Center Set-up Book; HP Digital Entertainment Center Getting Started Guide; Microsoft Windows

– Large private entities inquire as to debarment status • Government contractors cannot subcontract with.

When we think about this lack of use, we may conclude that the expectations that many teachers have of learners engaging in language learning tasks and activities using mobile

The values from Table 4 show that for the randomly chosen time points, the proposed method- ology for real-time estimation of the parameters and the states obtained a