17.07.2015 Views

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Reusing T-SQL Code……SQL Server Execution Times:CPU time = 109 ms, elapsed time = 82 ms.Not using any functions at all……SQL Server Execution Times:CPU time = 32 ms, elapsed time = 52 ms.Listing 5-24: The performance of the query using our scalar UDF is dramaticallyslower than the performance of other equivalent queries.I am not saying that using inline UDFs never incurs any performance penalties; blanketstatements do not belong in database programming, and we always need to consider theperformance of each particular case separately. However, in many cases, inline UDFsperform very well.Multi-statement Table-valued UDFsBesides scalar and inline UDFs, there are multi-statement table-valued UDFs. I willnot discuss or benchmark them here, because I feel I've already proved the point thatwe need to consider performance when we refactor code. However, it's worth notingthat, in general, while inline UDFs tend to be "performance neutral," scalar and multistatementones tend to hurt performance if not used carefully, and should be rigorouslytested and benchmarked. Be especially wary of using a multi-statement table-valuedUDF in an APPLY, since that may force the optimizer to re-execute the UDF for eachrow in the table the UDF is applied against.If you are interested in learning about different flavors of UDF, I encourage you toread Books Online and Itzik Ben Gan's T-SQL <strong>Programming</strong> book (www.amazon.co.uk/Inside-Microsoft-Server-2008-Pro-Developer/dp/0735626022/).151

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!