Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn T-SQL Querying

You're reading from   Learn T-SQL Querying A guide to developing efficient and elegant T-SQL code

Arrow left icon
Product type Paperback
Published in May 2019
Publisher Packt
ISBN-13 9781789348811
Length 484 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Pedro Lopes Pedro Lopes
Author Profile Icon Pedro Lopes
Pedro Lopes
Pam Lahoud Pam Lahoud
Author Profile Icon Pam Lahoud
Pam Lahoud
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Section 1: Query Processing Fundamentals FREE CHAPTER
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

Optimizing OR logic

A common query pattern involves the need to express several conditions of which at least one must be true to filter the result set, usually with OR logic. Expressing these OR conditions can have serious performance drawbacks and can often be replaced with other constructs that provide better scalability and performance.

The following example shows a query executed in the AdventureWorks sample database that uses an OR condition in the search predicate. The query lists all rows for a specific product, or where the price is set at a predetermined value:

SELECT ProductID, [UnitPrice], [UnitPriceDiscount],
[UnitPrice] * (1 - [UnitPriceDiscount]) AS FinalUnitPrice,
[UnitPriceDiscount] + 0.10 AS NewUnitPriceDiscount,
[UnitPrice] * (1 - 0.30) AS NewFinalUnitPrice
FROM Sales.SalesOrderDetail
WHERE ProductID = 770
OR UnitPrice = 3399.99
GROUP BY...
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 $19.99/month. Cancel anytime