Upload
jason-strate
View
555
Download
2
Embed Size (px)
DESCRIPTION
Citation preview
Performance Impacts of User Defined Functions
Introduction
Jason Strate
b: www.jasonstrate.com
t: StrateSQL
Resources jasonstrate.com/go/Waits
MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
Functions
http://www.flickr.com/photos/protocol/3244887521/
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.
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
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/
A Function is a Function
• Benefits
– Consolidation
– Modularization
– Maintenance
– Reuse
• Performance
– Looking at impact versus gain
http://www.flickr.com/photos/vincentsl/3543888150/
A Function is a Function
Functions
Scalar
Inline Multi-statement
A Function is a Function
Functions
Scalar
Inline Multi-statement
Scalar Functions
Parameter(s) Body
Statement(s) Value
Scalar Functions
• Function
– Single or multiple statement(s)
– Returns single value
• Usage
– SELECT, WHERE, ORDER BY
– CHECK Constraints
– Computed columns
Scalar Functions
• Cursor like behavior
– Linear performance effect
• Data Access
• Executes on
– SELECT
– WHERE
– ORDER BY
http://www.flickr.com/photos/blockpartypress/2625954377/
Scalar Demo
Demo 001 – Scalar Functions
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
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
A Function is a Function
Functions
Scalar
Inline Multi-statement
Inline Functions
Parameter(s) Body -
Statement Table
Inline Functions
• Returns table parameter
– Columns defined in RETURN statement
• Parameterized View
• DML Operations allowed
– INSERT
– UPDATE
– DELETE
• Query-able results
Inline Functions
• Limitations
– Simple operations
– Duplication of access
Joining Functions
• APPLY
– CROSS = INNER
– OUTER = LEFT OUTER
• Pass in columns as parameters
– Replaces ON clause
Inline Demo
Demo 002 – Inline Functions
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
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
A Function is a Function
Functions
Scalar
Inline Multi-statement
Multi-Statement Performance
Parameter(s) Function Table
Didn’t we already see
this?!?
Multi-Statement Performance
• Returns table parameter
– Columns defined in DDL
• Flexibility of Scalar function
• Flexibility of Inline function
• Query-able results
Multi-Statement Demo
Demo 003 – Multi-Statement Functions
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
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
Summary
• Performance is important
– Other factors can be just as important
• Know the limitations
• Functions have a place
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
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