Like this presentation? Why not share!

# A Function by Any Other Name is a Function

## by Jason Strate on Apr 08, 2013

• 248 views

### Views

Total Views
248
Views on SlideShare
222
Embed Views
26

Likes
0
Downloads
7
Comments
0

### 1 Embed26

 http://www.jasonstrate.com 26

### Upload Details

Uploaded via SlideShare as Adobe PDF

### Usage Rights

© All Rights Reserved

### Report content

Edit your comment

## A Function by Any Other Name is a FunctionPresentation Transcript

• Performance Impacts ofUser Defined Functions
• IntroductionJason e: jstrate@pragmaticworks.comStrate e: jasonstrate@gmail.com b: www.jasonstrate.com t: StrateSQLResources jasonstrate.com/go/Waits MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
• Functions http://www.flickr.com/photos/protocol/3244887521/
• What Functions? To use functions or not to use• User Defined Functions functions, that’s today’s question. – Scalar – Inline • Aka Table-Valued – Multi-statement • Aka Table-Valued CLR Functions Out of Scope http://www.flickr.com/photos/mshades/3154733910/
• Goals1. Identify purposes for creating User-Defined Functions2. Discuss the types of User-Defined Functions3. 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 Scalar Functions Multi- statement Inline
• A Function is a Function Scalar Functions Multi- statement Inline
• Scalar Functions BodyParameter(s) Value Statement(s)
• 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 DemoDemo 001 – Scalar Functions
• Scalar Performance Basic Function - Basic Function - CPU Basic Function – Duration 9,000 IO12,000 10,000 8,000 9,00010,000 7,000 8,000 6,000 7,000 8,000 5,000 6,000 6,000 5,000 4,000 4,000 3,000 4,000 3,000 2,000 2,000 2,000 1,000 1,000 - - - Native Scalar Native Scalar Native Scalar
• Scalar Performance Data Access - Data Access- Data Access - Duration CPU IO300 250 40000 35000250 200 30000200 25000 150150 20000 100 15000100 10000 5050 5000 0 0 0 1 10 100 1000 10000 1 10 100 1000 10000 1 10 100 1000 10000 Native Scalar Native Scalar Native Scalar
• A Function is a Function Scalar Functions Multi- statement Inline
• Inline Functions Body -Parameter(s) Table Statement
• 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 DemoDemo 002 – Inline Functions
• Inline Performance Basic Function - Duration Basic Function – Basic Function –12,000 CPU IO 9,000 10,00010,000 8,000 9,000 8,000 7,000 8,000 7,000 6,000 6,000 5,000 6,000 5,000 4,000 4,000 4,000 3,000 3,000 2,000 2,000 2,000 1,000 1,000 - - - Native Scalar Inline Native Scalar Inline Native Scalar Inline
• Inline Performance Data Access - Data Access- Data Access - Duration CPU IO300 250 40000 35000250 200 30000200 25000 150150 20000 100 15000100 10000 5050 5000 0 0 0 1 10 100 1000 10000 1 10 100 1000 10000 1 10 100 1000 10000 Native Scalar Inline Native Scalar Inline Native Scalar Inline
• A Function is a Function Scalar Functions Multi- statement Inline
• Multi-Statement PerformanceParameter(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 DemoDemo 003 – Multi-Statement Functions
• Multi-Statement Performance Basic Function - Basic Function - CPU Basic Function – Duration 9,000 IO12,000 10,000 8,000 9,00010,000 7,000 8,000 6,000 7,000 8,000 5,000 6,000 6,000 5,000 4,000 4,000 3,000 4,000 3,000 2,000 2,000 2,000 1,000 1,000 - - - Native Scalar Inline Multi Native Scalar Inline Multi Native Scalar Inline Multi
• Multi-Statement Performance Data Access - Data Access- Data Access - Duration CPU IO1200 1000 140000 900 1200001000 800 700 100000800 600 80000600 500 60000 400400 300 40000 200200 20000 100 0 0 0 1 10 100 1000 10000 1 10 100 1000 10000 1 10 100 1000 10000 Native Scalar Inline Multi Native Scalar Inline Multi 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&qi d=1268026139&sr=8-1
• For more information… Name: Jason Strate Email: jstrate@pragmaticworks.com Blog: www.jasonstrate.com Resource: jasonstrate.com/go/functions Products Services FoundationBI products to covert to a Microsoft BI Speed development through training, and Helping those who do not have theplatform and simplify development on rapid development services from means to get into information technologythe platform. Pragmatic Works. achieve their dreams.