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 6686141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:08:15+00:00 2026-05-26T05:08:15+00:00

I have a T-SQL statement that I am running against a table with many

  • 0

I have a T-SQL statement that I am running against a table with many rows. I am seeing some strange behavior. Comparing a DateTime column against a precalculated value is slower than comparing each row against a calculation based on the GETDATE() function.

The following SQL takes 8 secs:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO
DECLARE @TimeZoneOffset int = -(DATEPART("HH", GETUTCDATE() - GETDATE()))
DECLARE @LowerTime DATETIME = DATEADD("HH", ABS(@TimeZoneOffset), CONVERT(VARCHAR, GETDATE(), 101) + ' 17:00:00')
SELECT TOP 200 Id, EventDate, Message 
FROM Events WITH (NOLOCK)
WHERE EventDate > @LowerTime
GO

This alternate strangely returns instantly:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO
SELECT TOP 200 Id, EventDate, Message 
FROM Events WITH (NOLOCK)
WHERE EventDate > GETDATE()-1
GO

Why is the second query so much faster?

EDITED: I updated the SQL to accurately reflect other settings I am using

  • 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-26T05:08:16+00:00Added an answer on May 26, 2026 at 5:08 am

    After doing a lot of reading and researching, I’ve discovered the issue here is parameter sniffing. Sql Server attempts to determine how best to use indexes based on the where clause, but in this case it isnt doing a very good job.

    See the examples below :

    Slow version:

    declare @dNow DateTime  
    Select @dNow=GetDate()  
    Select *  
    From response_master_Incident rmi  
    Where rmi.response_date between DateAdd(hh,-2,@dNow) AND @dNow  
    

    Fast version:

    Select *  
    From response_master_Incident rmi  
    Where rmi.response_date between DateAdd(hh,-2,GetDate()) AND GetDate()  
    

    The “Fast” version runs around 10x faster than the slow version. The Response_Date field is indexed and is a DateTime type.

    The solution is to tell Sql Server how best to optimise the query. Modifying the example as follows to include the OPTIMIZE option resulted in it using the same execution plan as the “Fast Version”. The OPTMIZE option here explicitly tells sql server to treat the local @dNow variable as a date (as if declaring it as DateTime wasnt enough :s )

    Care should be taken when doing this however because in more complicated WHERE clauses you could end up making the query perform worse than Sql Server’s own optimisations.

    declare @dNow DateTime
    
    SET @dNow=GetDate()
    
    Select ID, response_date, call_back_phone 
    from response_master_Incident rmi
    where rmi.response_date between DateAdd(hh,-2,@dNow) AND @dNow
    
    -- The optimizer does not know too much about the variable so assumes to should perform a clusterd index scann (on the clustered index ID) - this is slow
    
    -- This hint tells the optimzer that the variable is indeed a datetime in this format (why it does not know that already who knows)
    OPTION(OPTIMIZE FOR (@dNow = '99991231'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long running SQL statement that I want to run, and no
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
I have an sql statement that is supposed to return 2 rows. the first
I have a statement that SELECTs distinct rows from a table with a two
I have a SQL statement in C# (.NET Framework 4 running against SQL Server
I have a sql statement that uses Difference => http://msdn.microsoft.com/en-us/library/ms188753.aspx I do this in
I have an SQL statement that I'm executing through OleDb, the statement is something
I have an sql statement that currently is just returning all the end parent
Let's say I have an SQL statement that's syntactically and semantically correct so it
I have an annoying SQL statement that seem simple but it looks awfull. I

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.