a function by any other name is a function
DESCRIPTION
TRANSCRIPT
![Page 1: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/1.jpg)
Performance Impacts of User Defined Functions
![Page 2: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/2.jpg)
Introduction
Jason Strate
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/3.jpg)
Functions
http://www.flickr.com/photos/protocol/3244887521/
![Page 4: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/8.jpg)
A Function is a Function
Functions
Scalar
Inline Multi-statement
![Page 9: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/9.jpg)
A Function is a Function
Functions
Scalar
Inline Multi-statement
![Page 10: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/10.jpg)
Scalar Functions
Parameter(s) Body
Statement(s) Value
![Page 11: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/13.jpg)
Scalar Demo
Demo 001 – Scalar Functions
![Page 14: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/16.jpg)
A Function is a Function
Functions
Scalar
Inline Multi-statement
![Page 17: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/17.jpg)
Inline Functions
Parameter(s) Body -
Statement Table
![Page 18: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/19.jpg)
Inline Functions
• Limitations
– Simple operations
– Duplication of access
![Page 20: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/21.jpg)
Inline Demo
Demo 002 – Inline Functions
![Page 22: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/24.jpg)
A Function is a Function
Functions
Scalar
Inline Multi-statement
![Page 25: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/25.jpg)
Multi-Statement Performance
Parameter(s) Function Table
Didn’t we already see
this?!?
![Page 26: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/26.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/27.jpg)
Multi-Statement Demo
Demo 003 – Multi-Statement Functions
![Page 28: A Function by Any Other Name is a Function](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/28.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/29.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/30.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/31.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051513/547b8f0eb4795963098b4ddb/html5/thumbnails/32.jpg)
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