I have a table on SQL Server with approximately 1 million rows. It has an identifier (PK), status (int), and a datetime column. I also created an index in the datetime column.
Now I have discovered an effect that I do not understand.
SELECT status FROM table WHERE dateTime BETWEEN '2010-01-01T00:00:00' AND '2010-01-02T12:00:00'
This statement returns 3664 rows. It runs for about 150 ms, and the execution plan shows that it performs an index search with a key search.
Now, if I change it as follows (just change the hour from 12 to 13):
SELECT status FROM table WHERE dateTime BETWEEN '2010-01-01T00:00:00' AND '2010-01-02T13:00:00'
This statement returns 3667 rows. It works for about 600 ms, and the billing plan shows that it uses the primary key!
I just don't get it. For 3667 and more lines, it always uses the primary key, even if the search is much faster.
Is there any explanation?
source share