Presentation is loading. Please wait.

Presentation is loading. Please wait.

High Performance Functions SQLBits VI. Going backwards is faster than going forwards.

Similar presentations


Presentation on theme: "High Performance Functions SQLBits VI. Going backwards is faster than going forwards."— Presentation transcript:

1 High Performance Functions SQLBits VI

2 Going backwards is faster than going forwards

3 Simon Sabin Independent SQL Server Consultant and Trainer Independent SQL Server Consultant and Trainer Database design and development, Business Intelligence, Performance tuning and troubleshooting Database design and development, Business Intelligence, Performance tuning and troubleshooting SQL Server since 6.5 SQL Server since 6.5 Email: Simon@SqlKnowHow.com Email: Simon@SqlKnowHow.comSimon@ Blog: http://Sqlblogcasts.com/blogs/simons Blog: http://Sqlblogcasts.com/blogs/simons Twitter: simon_sabin Twitter: simon_sabin

4 Overview TSQL Functions what are they TSQL Functions what are they Why they are evil Why they are evil Solving by going backwards Solving by going backwards Considerations for data access Considerations for data access Is CLR an option Is CLR an option

5 TSQL Functions Logical solution to ensure code reuse Logical solution to ensure code reuse Introduced in SQL 2000 Introduced in SQL 2000 Three different flavours Three different flavours – Scalar Function – Inline Table Valued Function – Multi Statement Table Value function

6 Scalar Functions Not visible in execution plans Not visible in execution plans All too visible in profiler All too visible in profiler Issues with Estimates and Statistics Issues with Estimates and Statistics – Results in nested loop joins Performance Performance – The code is interpreted each call Parallelism not possible Parallelism not possible

7 Solutions Don’t use them in the first place Don’t use them in the first place If you have to then If you have to then Profiler Profiler – Filter by object type 18004 - UDF Bad estimates Bad estimates – Use query hints HASH / MERGE

8 Inline Table Valued Functions What are they? What are they? A function that returns a query A function that returns a query A bit like a parameterised view A bit like a parameterised view Query is consumed into the main query Query is consumed into the main query Resolved down to the base functions Resolved down to the base functions

9 How to? Write a function that returns a TABLE Write a function that returns a TABLE Use whatever parameters you want Use whatever parameters you want Specify you query as the return statement Specify you query as the return statement Using the parameters as required Using the parameters as required In your calling query use a subquery or In your calling query use a subquery or CROSS APPLY/OUTER APPLY CROSS APPLY/OUTER APPLY

10 Whats great IO included in SET STATISTICS IO IO included in SET STATISTICS IO Additional predicates handled Additional predicates handled Performance near to system functions Performance near to system functions No noise in Profiler No noise in Profiler Parallelism capable Parallelism capable Can return multiple values Can return multiple values

11 What about CLR Compiled code Compiled code Can be very simple Can be very simple Even simple solutions are quick Even simple solutions are quick Can be complex Can be complex Complex solutions are generally quicker Complex solutions are generally quicker Don’t be afraid Don’t be afraid

12 Summary Don’t use scalar functions Don’t use scalar functions Convert to inline table valued functions Convert to inline table valued functions Consider using CLR Consider using CLR

13 Then you’ll be happy

14 Q&A Now Now – Just ask Later Later – @Simon_Sabin – Simon@SQLKnowHow.com Simon@SQLKnowHow.com


Download ppt "High Performance Functions SQLBits VI. Going backwards is faster than going forwards."

Similar presentations


Ads by Google