December 2013
Intermediate to advanced
1872 pages
153h 31m
English
You should use IF EXISTS instead of SELECT COUNT(*) when checking only for the existence of any matching data values and when determining the number of matching rows is not required. IF EXISTS stops the processing of the select query as soon as the first matching row is found, whereas SELECT COUNT(*) continues searching until all matches are found, wasting I/O and CPU cycles. For example, you could replace
if (SELECT count(*) FROM Sales.SalesOrderDetail WHERE ProductID = 324) > 0
with an IF EXISTS check similar to
if EXISTS (SELECT * FROM Sales.SalesOrderDetail WHERE ProductID = 324)