Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:23:30+00:00 2026-05-30T20:23:30+00:00

I have a SQL query whose execution plan depends on the DateTime parameters that

  • 0

I have a SQL query whose execution plan depends on the DateTime parameters that I provide. I understand that if I have a condition like:

WHERE Date > '2012-02-28' AND Date < '2012-01-01'

then the execution plan will just be the evaluation of a constant, because no result will be returned. But in other cases I get different execution plans that don’t make any sense to me. For example with condition:

WHERE Date >= '2012-02-01 00:00:00' AND Date <= '2012-02-02 22:00:00'

the query uses an index that I added and it is optimized. But if I change the condition to:

WHERE Date >= '2012-02-27 00:00:00' AND Date <= '2012-02-27 22:00:00'

it doesn’t use the index anymore.

I rebuilt all the indexes on this table before running the queries. I can’t find any logic in this. So my question is: how can DateTime parameters affect which execution plan is chosen for a query?

EDIT: Apparently the reason was that I had an additional index on Date only that confused the SQL Server. After removing that index everything worked as expected. But thank you for the ideas. I didn’t offer enough details for you to figure that out. Too many indexes are never good!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:23:31+00:00Added an answer on May 30, 2026 at 8:23 pm

    SQL Server keeps statistics on the table content and builds the query plan based on those statistics.

    For example. When I have a table with 1 million records where 900.000 records are between 2012-02-27 and 2012-02-28 the query governor will do a table scan to read the data. If only 10% of the data has dates between those 2 values it will prefer to use the index you created.

    One issue with the statistics model is that of “outdated statistics”. SQL Server will do an “auto update statistics” whenever more than a certain percentage of the table or more than 500 records + (20% of the data cardinality) have been altered. (see this article).

    So if your statistics are out of date, SQL Server may well come up with incorrect query plans.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL query that takes these parameters: @SearchFor nvarchar(200) = null ,@SearchInLat
Friends, I have a dynamic sql query that I would like to execute and
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not
I have a SQL query that takes a very long time to run on
We have a SQL query that pulls a large number of fields from many
I have a SQL query that is supposed to pull out a record and
I have a linq-to-sql query that I use to fill an object that has
I have a query whose code looks like this: SELECT DocumentID, ComplexSubquery1 ... ComplexSubquery5
On Sql Server 2008, I have a slow-running update query that has been working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.