Monday, April 23, 2012

NOT, AND, OR: the Boolean Operators in T-SQL

The boolean operators in T-SQL are NOT, AND, and OR and are executed in a SQL statement in that order.
  • The NOT operator generally hurts query performance because indexes cannot be used for a WHERE clause when the NOT operator is used *
  • Indexes can be used when the OR operator is specified, but all columns referenced by the OR must be included in an index or none of the indexes are used
  • Performance is often improved when the AND operator is used because the AND operator generally results in a smaller result set
* Just like with the NOT operator, indexes cannot be utilized when a leading wildcard character is used. For example:
SELECT FirstName, LastName 
FROM [Users].[dbo].[Employees]
WHERE LastName LIKE '*ones' 

No comments:

Post a Comment