I would like to get help on optimizing my query below.
I want to have a view that exposes employee data across years. This view has data from employee table which has employeeID, year and employee demographs as columns. I also have a table called testemployees which has EmployeeID and Year. This is a subset of employee table which might or might not have data. What I am trying to accomplish is:
If there is data in testEmployees table my view should fetch employee details only for employees in testEmployees and if there is not data in testEmployees I should have my view have all the data in employee table.
My employee table is really very huge, and even though the query below works it takes a lot of time to fetch this data. Any pointers on how I can improve this query will be greatly appreciated.
Create view dbo.employees(Year, EmployeeID)
as
select * from
employee e, testemployees te
where e.Year = case when((select count(1) from testemployees)>0) then te.Year else e.Year
and e.employeeID = case when((select count(1) from testemployees)> 0) then te.employeeID else e.employeeID
Let me know your thoughts on how to optimize the query. Would using any other kind of joins help?
Thanks in advance.