Skip to main content
1 of 6
Chris Halcrow
  • 265
  • 1
  • 5
  • 10

Slow running query using date range on non-clustered index column

I have a very slow running query, and I've isolated the problem down to the following SQL.

SELECT
  ...
FROM
    TableA a(nolock) 
    inner join TableB b with(nolock) on (b.Id = a.Id AND b.Date>= isnull(@timestamp_start, '17530101') AND b.Date < isnull(@timestamp_end, '99991231'))
  • a = 2million+ rows

  • b = 2million+ rows

  • b.Id => Index, unique, non-clustered

  • a.Id => Index, unique, non-clustered

  • b.Date => Index, non-unique, non-clustered

This takes several minutes without returning a result even for a single day date range. On my local machine here's what the execution plans looks like:

enter image description here

Why would this query be slow and how can I fix it?

Chris Halcrow
  • 265
  • 1
  • 5
  • 10