Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn T-SQL Querying

You're reading from  Learn T-SQL Querying

Product type Book
Published in May 2019
Publisher Packt
ISBN-13 9781789348811
Pages 484 pages
Edition 1st Edition
Languages
Authors (2):
Pedro Lopes Pedro Lopes
Profile icon Pedro Lopes
Pam Lahoud Pam Lahoud
Profile icon Pam Lahoud
View More author details
Toc

Table of Contents (18) Chapters close

Preface 1. Section 1: Query Processing Fundamentals
2. Anatomy of a Query 3. Understanding Query Processing 4. Mechanics of the Query Optimizer 5. Section 2: Dos and Donts of T-SQL
6. Exploring Query Execution Plans 7. Writing Elegant T-SQL Queries 8. Easily-Identified T-SQL Anti-Patterns 9. Discovering T-SQL Anti-Patterns in Depth 10. Section 3: Assemble Your Query Troubleshooting Toolbox
11. Building Diagnostic Queries Using DMVs and DMFs 12. Building XEvent Profiler Traces 13. Comparative Analysis of Query Plans 14. Tracking Performance History with Query Store 15. Troubleshooting Live Queries 16. Managing Optimizer Changes with the Query Tuning Assistant 17. Other Books You May Enjoy

Avoiding UDF pitfalls

Scalar User-Defined Functions (UDFs) are a very useful T-SQL programming artifact because they allow a specific routine to be reused very easily. However, these seemingly harmless constructs can be detrimental to performance, because the Query Optimizer does not account for any T-SQL logic inside a UDF, and UDFs are executed for every row in the result set, just like a cursor. When using scalar UDFs, there are specific recommendations that apply to UDFs that access system or user data, and recommendations that apply to all UDFs.

An example of a scalar UDF that does not access data was referenced in Chapter 4, Exploring Query Execution Plans, under the Query plan properties of interest section, as shown in the following query:

CREATE FUNCTION ufn_CategorizePrice (@Price money)
RETURNS NVARCHAR(50)
AS
BEGIN
DECLARE @PriceCategory NVARCHAR(50)
IF @Price...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime