SQL Server query with date range in WHERE - execution plan uses clustered PK instance not date column index I have the following query in SQL server:

declare @timestamp_start date = getdate() - 1
declare @timestamp_end date = getdate()

SELECT  
    my_date = a.itemTimestamp
FROM
    my_table a with(nolock)
WHERE 
    (@timestamp_start is null OR a.itemTimestamp &gt;= @timestamp_start)
    AND (@timestamp_end is null OR a.itemTimestamp &lt; @timestamp_end)


It's extremely slow (90s for &lt; 2000 records), because the execution plan uses the clustered index of the PK column for 'my_table':



Why is this happening and how can I optimize the query performance?
What is the datatype of itemTimestamp? Is that column indexed? Is the column nullable?

Assuming the answers are datetime, Yes, and No respectively you might consider

WHERE  a.itemTimestamp &gt;= isnull(@timestamp_start, '17530101')
       AND a.itemTimestamp &lt; isnull(@timestamp_end, '99991231') 


If you were to change to datetime2 and actually store dates before 1753 you would need to change the lower bound condition to 00010101. This also won't bring back dates matching the higher bound of 9999-12-31 - but in the real world this is unlikely to cause a problem.