I have a non-clustered index on a datetime
column (AdmitDate
) and a varchar
column (Status
) in SQL Server. Now the issue is that I'm filtering the result only on the basis of the datetime
column (no Index on AdmitDate
column alone).
In order for me to utilize the non-clustered index I used a not null condition for the varchar
column (Status
) but in that scenario the execution plan shows "Index Scan".
select ClientName, ID
from PatientVisit
where
(PatientVisit.AdmitDate between '2010-01-01 00:00:00.000' AND '2014-01-31 00:00:00.000' )
AND PatientVisit.Status is not null
-- Index Scan
But if I pass a specific Status value then as expected the excution plan shows Index Seek.
select ClientName, ID
from PatientVisit
where
(PatientVisit.AdmitDate between '2010-01-01 00:00:00.000' AND '2014-01-31 00:00:00.000')
AND PatientVisit.Status = 'ADM'
--Index Seek
Should I use in operator and pass all the possible values for the Status
column to utilize the non-clustered index?
Or is there any other way to utilize the index?
Thanks,
Shubham