Your query will be unlikely to use your current index due to the SELECT *
selecting all columns in the table.
To be a covering index, an index must include all columns in the where clause, the select etc.
BUT it will only use an index if it is selective enough i.e. it filters the resulting data to a small enough subset of rows.
You should examine the actual execution plan (and make sure your statistics are up to date).
What are Covering Indexes and Covered Queries in SQL Server?
Your query should use the following index (but always check the actual plan):
CREATE INDEX IX_CreateDate_Location_I_EmpName_EmpID
ON EmployeeDetails (CreateDate)
INCLUDE (Location, EmpName, EmpID)
[Note: assuming your primary key column is the clustered index (it is by default if unspecified), you don't have to explicitly include EmpId
in the INCLUDE
clause.]
If you want an index that can be used by queries that also filter by Location
as well as CreateDate
, create this index:
CREATE INDEX IX_CreateDate_Location_I_EmpName_EmpID
ON EmployeeDetails (CreateDate, Location)
INCLUDE (EmpName, EmpID)
An index might be used where the leading column of the index, 'CreateDate'
, is used but not if the query does not include the leading column. For instance, this query won't be able to use either of the indexes I have defined (in the majority of cases):
SELECT *
FROM EmployeeDetails
WHERE Location = 'blah'