I have a query that should be dynamic in some columns, that is, I get the parameter and in accordance with its value. I decide which column to retrieve in my Where clause. I executed this query using a CASE expression:
(CASE @isArrivalTime WHEN 1 THEN ArrivalTime ELSE PickedupTime END) >= DATEADD(mi, -@TZOffsetInMins , @sTime) AND (CASE @isArrivalTime WHEN 1 THEN ArrivalTime ELSE PickedupTime END) < DATEADD(mi, -@TZOffsetInMins , @fTime)
If @isArrivalTime = 1 , then selected ArrivalTime column else, select the PickedupTime column. I have a clustered index on ArrivalTime and a non-clustered index on PickedupTime .
I noticed that when I use this query (with @isArrivalTime = 1 ), my performance is much worse than when using ArrivalTime .
Perhaps the query optimizer cannot use \ to select the index correctly in this way?
I compared the execution plans, noticed that when I use CASE 32% of the time is spent on index scans, but when I did not use CASE (just used ArrivalTime`), only 3% was wasted on index scans.
Does anyone know the reason for this?
user1271762
source share