A Function by Any Other Name is a Function

560 views

Published on

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
560
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

A Function by Any Other Name is a Function

  1. 1. Performance Impacts ofUser Defined Functions
  2. 2. 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
  3. 3. Functions http://www.flickr.com/photos/protocol/3244887521/
  4. 4. 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/
  5. 5. Goals1. Identify purposes for creating User-Defined Functions2. Discuss the types of User-Defined Functions3. Demonstrate performance impact in selecting different types of functions
  6. 6. 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/
  7. 7. A Function is a Function• Benefits – Consolidation – Modularization – Maintenance – Reuse• Performance – Looking at impact versus gain http://www.flickr.com/photos/vincentsl/3543888150/
  8. 8. A Function is a Function Scalar Functions Multi- statement Inline
  9. 9. A Function is a Function Scalar Functions Multi- statement Inline
  10. 10. Scalar Functions BodyParameter(s) Value Statement(s)
  11. 11. Scalar Functions• Function – Single or multiple statement(s) – Returns single value• Usage – SELECT, WHERE, ORDER BY – CHECK Constraints – Computed columns
  12. 12. Scalar Functions• Cursor like behavior – Linear performance effect• Data Access• Executes on – SELECT – WHERE – ORDER BY http://www.flickr.com/photos/blockpartypress/2625954377/
  13. 13. Scalar DemoDemo 001 – Scalar Functions
  14. 14. 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
  15. 15. 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
  16. 16. A Function is a Function Scalar Functions Multi- statement Inline
  17. 17. Inline Functions Body -Parameter(s) Table Statement
  18. 18. Inline Functions• Returns table parameter – Columns defined in RETURN statement• Parameterized View• DML Operations allowed – INSERT – UPDATE – DELETE• Query-able results
  19. 19. Inline Functions• Limitations – Simple operations – Duplication of access
  20. 20. Joining Functions• APPLY – CROSS = INNER – OUTER = LEFT OUTER• Pass in columns as parameters – Replaces ON clause
  21. 21. Inline DemoDemo 002 – Inline Functions
  22. 22. 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
  23. 23. 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
  24. 24. A Function is a Function Scalar Functions Multi- statement Inline
  25. 25. Multi-Statement PerformanceParameter(s) Function Table Didn’t we already see this?!?
  26. 26. Multi-Statement Performance• Returns table parameter – Columns defined in DDL• Flexibility of Scalar function• Flexibility of Inline function• Query-able results
  27. 27. Multi-Statement DemoDemo 003 – Multi-Statement Functions
  28. 28. 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
  29. 29. 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
  30. 30. Summary• Performance is important – Other factors can be just as important• Know the limitations• Functions have a place
  31. 31. 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
  32. 32. 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.

×