The first thing i would like to say is that this is not exactly what I am trying to achieve. I have dumbed down this query A LOT to get my question across more clearly.
I have a nonclustered index on a table (CallDetail) on two values, TermDate (int) and SourceSystemID (int). To be complete, I will include the precise definition of the index here:
CREATE NONCLUSTERED INDEX [CallDetail_TermDateSourceSystemID] ON [dbo].[CallDetail]
(
[TermDate] ASC,
[SourceSystemID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
The problem that I am having is that when I run two nearly identical queries against this table, I don’t get the same results (not to be confused with result set). The first query runs in less than one second and returns about 10,000 rows. The second query, when executed, continues to run until I cancel it after about 30 minutes.
Query 1 (~1 second):
SELECT
*
FROM
CallDetail
WHERE
CallDetail.TermDate >= 1101221 AND
SourceSystemID = 1
Query 2 (>30 minutes):
DECLARE @TermDate AS INT
SET @TermDate = 1101221
SELECT
*
FROM
CallDetail
WHERE
CallDetail.TermDate >= @TermDate AND
SourceSystemID = 1
Something I would like to note is that the query execution plan tells me to ‘include’ all the columns of this table in the index. I find that to be completely wrong. I would also like to note that if i only select TermDate and SourceSystemID instead of * that I get results in about 1 seconds.
Is there a reason that when using a variable instead of hard coding a value into the where that it is taking so much longer? I am completely stumped on this and any help would be very much appreciated.
Thanks!
Christopher Haws
OK, I reproduced the situation with my query:
After reading up on the post by marc_s, I did this:
And everything was fast again!