Thursday, July 19, 2012

SQL SERVER – Best Use of EXISTS Keyword (Performance hint)


In below two scripts I have  used the Use of = and IN operator in 1 & 2 Query  and in Third query I have used the Exists Keyword for checking the performance issue and when I seen the Execution Plan and the outcome is that Use the of = operator or IN operator is less better than use of EXISTS Keyword.

Run the below script and See the Execution plan.

USE AdventureWorks
GO

--1) use of IN operator
SELECT * FROM HumanResources.Employee E
WHERE E.ManagerID  in ( SELECT EmpAdd.EmployeeID
FROM HumanResources.EmployeeAddress EmpAdd
WHERE EmpAdd.EmployeeID = E.ManagerID)
GO
--2) use of = operator
SELECT * FROM HumanResources.Employee E
WHERE E.ManagerID  = ( SELECT EmpAdd.EmployeeID
FROM HumanResources.EmployeeAddress EmpAdd
WHERE EmpAdd.EmployeeID = E.ManagerID)
GO
--3) use of exists Keyword
SELECT * FROM HumanResources.Employee E
WHERE EXISTS ( SELECT EmpAdd.EmployeeID
FROM HumanResources.EmployeeAddress EmpAdd
WHERE EmpAdd.EmployeeID = E.ManagerID)
GO

Result: (Snap Shot of Execution Plan)




Like and Share to SQL Integrity Blog

No comments:

Post a Comment

Thank You !!!!