The question has been boiling for quite some time in my head that of the following two stored procedures that could be performed better.
Proc 1
CREATE PROCEDURE GetEmployeeDetails @EmployeeId uniqueidentifier, @IncludeDepartmentInfo bit AS BEGIN SELECT * FROM Employees WHERE Employees.EmployeeId = @EmployeeId IF (@IncludeDepartmentInfo = 1) BEGIN SELECT Departments.* FROM Departments, Employees WHERE Departments.DepartmentId = Employees.DepartmentId AND Employees.EmployeeId = @EmployeeId END END
Proc 2
CREATE PROCEDURE GetEmployeeDetails @EmployeeId uniqueidentifier, @IncludeDepartmentInfo bit AS BEGIN SELECT * FROM Employees WHERE Employees.EmployeeId = @EmployeeId SELECT Departments.* FROM Departments, Employees WHERE Departments.DepartmentId = Employees.DepartmentId AND Employees.EmployeeId = @EmployeeId AND @IncludeDepartmentInfo = 1 END
The only difference between the two is the use of "if statment".
if proc 1 / proc 2 is called with @IncludeDepartmentInfo variables, then from my understanding proc 2 will work better because it will keep the same query plan, regardless of the value of @IncludeDepartmentInfo, whereas proc1 will change the query plan for every call
the answers are actually appericated
PS: this is just a scenario, please do not go to the explicit results of the query, but the essence of the example. I am really special with regard to the result of the query optimizer (in both cases βif and whereβ and their difference), many aspects that, as I know, can affect the performance that I want to avoid in this matter.
source share