A Function by Any Other Name is a Function

32
Performance Impacts of User Defined Functions

description

 

Transcript of A Function by Any Other Name is a Function

Page 1: A Function by Any Other Name is a Function

Performance Impacts of User Defined Functions

Page 2: A Function by Any Other Name is a Function

Introduction

Jason Strate

e: [email protected]

e: [email protected]

b: www.jasonstrate.com

t: StrateSQL

Resources jasonstrate.com/go/Waits

MAKING BUSINESS INTELLIGENT www.pragmaticworks.com

Page 3: A Function by Any Other Name is a Function

Functions

http://www.flickr.com/photos/protocol/3244887521/

Page 4: A Function by Any Other Name is a Function

What Functions?

• User Defined Functions

– Scalar

– Inline

• Aka Table-Valued

– Multi-statement

• Aka Table-Valued

http://www.flickr.com/photos/mshades/3154733910/

CLR Functions Out of Scope

To use functions or not to use functions, that’s today’s

question.

Page 5: A Function by Any Other Name is a Function

Goals

1. Identify purposes for creating User-Defined Functions

2. Discuss the types of User-Defined Functions

3. Demonstrate performance impact in selecting different types of functions

Page 6: A Function by Any Other Name is a Function

Session Warranty

• Not a deep dive

– Inform those that don’t know

– Arm those that do know

• Not included

– Secret sauce

– Pixie dust

– Bacon

http://www.flickr.com/photos/shawnzam/31302636/

Page 7: A Function by Any Other Name is a Function

A Function is a Function

• Benefits

– Consolidation

– Modularization

– Maintenance

– Reuse

• Performance

– Looking at impact versus gain

http://www.flickr.com/photos/vincentsl/3543888150/

Page 8: A Function by Any Other Name is a Function

A Function is a Function

Functions

Scalar

Inline Multi-statement

Page 9: A Function by Any Other Name is a Function

A Function is a Function

Functions

Scalar

Inline Multi-statement

Page 10: A Function by Any Other Name is a Function

Scalar Functions

Parameter(s) Body

Statement(s) Value

Page 11: A Function by Any Other Name is a Function

Scalar Functions

• Function

– Single or multiple statement(s)

– Returns single value

• Usage

– SELECT, WHERE, ORDER BY

– CHECK Constraints

– Computed columns

Page 12: A Function by Any Other Name is a Function

Scalar Functions

• Cursor like behavior

– Linear performance effect

• Data Access

• Executes on

– SELECT

– WHERE

– ORDER BY

http://www.flickr.com/photos/blockpartypress/2625954377/

Page 13: A Function by Any Other Name is a Function

Scalar Demo

Demo 001 – Scalar Functions

Page 14: A Function by Any Other Name is a Function

Scalar Performance

-

2,000

4,000

6,000

8,000

10,000

12,000

Basic Function - Duration

Native Scalar

-

1,000

2,000

3,000

4,000

5,000

6,000

7,000

8,000

9,000

10,000

Basic Function – IO

Native Scalar

-

1,000

2,000

3,000

4,000

5,000

6,000

7,000

8,000

9,000

Basic Function - CPU

Native Scalar

Page 15: A Function by Any Other Name is a Function

Scalar Performance

0

5000

10000

15000

20000

25000

30000

35000

40000

1 10 100 1000 10000

Data Access - IO

Native Scalar

0

50

100

150

200

250

1 10 100 1000 10000

Data Access- CPU

Native Scalar

0

50

100

150

200

250

300

1 10 100 1000 10000

Data Access - Duration

Native Scalar

Page 16: A Function by Any Other Name is a Function

A Function is a Function

Functions

Scalar

Inline Multi-statement

Page 17: A Function by Any Other Name is a Function

Inline Functions

Parameter(s) Body -

Statement Table

Page 18: A Function by Any Other Name is a Function

Inline Functions

• Returns table parameter

– Columns defined in RETURN statement

• Parameterized View

• DML Operations allowed

– INSERT

– UPDATE

– DELETE

• Query-able results

Page 19: A Function by Any Other Name is a Function

Inline Functions

• Limitations

– Simple operations

– Duplication of access

Page 20: A Function by Any Other Name is a Function

Joining Functions

• APPLY

– CROSS = INNER

– OUTER = LEFT OUTER

• Pass in columns as parameters

– Replaces ON clause

Page 21: A Function by Any Other Name is a Function

Inline Demo

Demo 002 – Inline Functions

Page 22: A Function by Any Other Name is a Function

Inline Performance

-

2,000

4,000

6,000

8,000

10,000

12,000

Basic Function - Duration

Native Scalar Inline

-

1,000

2,000

3,000

4,000

5,000

6,000

7,000

8,000

9,000

10,000

Basic Function – IO

Native Scalar Inline

-

1,000

2,000

3,000

4,000

5,000

6,000

7,000

8,000

9,000

Basic Function – CPU

Native Scalar Inline

Page 23: A Function by Any Other Name is a Function

Inline Performance

0

50

100

150

200

250

300

1 10 100 1000 10000

Data Access - Duration

Native Scalar Inline

0

50

100

150

200

250

1 10 100 1000 10000

Data Access- CPU

Native Scalar Inline

0

5000

10000

15000

20000

25000

30000

35000

40000

1 10 100 1000 10000

Data Access - IO

Native Scalar Inline

Page 24: A Function by Any Other Name is a Function

A Function is a Function

Functions

Scalar

Inline Multi-statement

Page 25: A Function by Any Other Name is a Function

Multi-Statement Performance

Parameter(s) Function Table

Didn’t we already see

this?!?

Page 26: A Function by Any Other Name is a Function

Multi-Statement Performance

• Returns table parameter

– Columns defined in DDL

• Flexibility of Scalar function

• Flexibility of Inline function

• Query-able results

Page 27: A Function by Any Other Name is a Function

Multi-Statement Demo

Demo 003 – Multi-Statement Functions

Page 28: A Function by Any Other Name is a Function

Multi-Statement Performance

-

2,000

4,000

6,000

8,000

10,000

12,000

Basic Function - Duration

Native Scalar Inline Multi

-

1,000

2,000

3,000

4,000

5,000

6,000

7,000

8,000

9,000

10,000

Basic Function – IO

Native Scalar Inline Multi

-

1,000

2,000

3,000

4,000

5,000

6,000

7,000

8,000

9,000

Basic Function - CPU

Native Scalar Inline Multi

Page 29: A Function by Any Other Name is a Function

Multi-Statement Performance

0

200

400

600

800

1000

1200

1 10 100 1000 10000

Data Access - Duration

Native Scalar Inline Multi

0

100

200

300

400

500

600

700

800

900

1000

1 10 100 1000 10000

Data Access- CPU

Native Scalar Inline Multi

0

20000

40000

60000

80000

100000

120000

140000

1 10 100 1000 10000

Data Access - IO

Native Scalar Inline Multi

Page 30: A Function by Any Other Name is a Function

Summary

• Performance is important

– Other factors can be just as important

• Know the limitations

• Functions have a place

Page 31: A Function by Any Other Name is a Function

Resources

• http://msdn.microsoft.com/en-us/library/ms189294.aspx

• http://www.sqlteam.com/article/intro-to-user-defined-functions-updated

• http://www.amazon.com/Transact-SQL-User-Defined-Functions/dp/1556220790/ref=sr_1_1?ie=UTF8&s=books&qid=1268026139&sr=8-1

Page 32: A Function by Any Other Name is a Function

Services Speed development through training, and rapid development services from Pragmatic Works.

Products BI products to covert to a Microsoft BI platform and simplify development on the platform.

Foundation Helping those who do not have the means to get into information technology achieve their dreams.

For more information…

Name: Jason Strate Email: [email protected] Blog: www.jasonstrate.com Resource: jasonstrate.com/go/functions