In my SQL Server query I try to get 2 seconds range of data:
DECLARE @runtime AS datetime
SELECT @runtime = '2014-02-15 03:34:17'
SELECT Application FROM commandcip
WHERE
commandname = 'RunTestCase' AND
(createdate BETWEEN DATEADD(s, -1, @runtime) AND DATEADD(s, 1, @runtime))
This command is extremely slow, it takes minutes and the Estimated Subtree Cost based on Performance analyzer is 2800.
On other hand if I compute the range manually, the query is perfectly fast (Estimated Subtree Cost = 0.5, query time < 1 second):
SELECT Application FROM commandcip
WHERE
commandname = 'RunTestCase' AND
createdate BETWEEN '2014-02-15 03:34:16' AND '2014-02-15 03:34:18'
I verified that both commands return correct data. I verified that my DATEADD
commands return correct dates. I also tried to get DATEADD
one step sooner (into separate variables @mindate
, @maxdate
), but it didn't help.
How should I speedup first query without manually computing the range?