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

  • Home
  • SEARCH
  • 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 8253157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:46:49+00:00 2026-06-08T00:46:49+00:00

Here is the problem I am having. A table that I am querying has

  • 0

Here is the problem I am having. A table that I am querying has 170 million rows. I wrote a stored procedure to do a simple query about the latest entries into the table. When I write the WHERE clause with a hard-coded string like this:

SELECT top 500 a.field1 , a.field2, a.field3
FROM database..hugeTable a( nolock )
WHERE a.StartTime > '2012-07-17 10:10:35.477'

it is very fast. Less than a second. StartTime is in a non-clustered index.

However, what I really want is this:

DECLARE @fifteenMinutesAgo datetime;
SET @fifteenMinutesAgo = DATEADD(n , -15 , GETDATE());

SELECT top 500 a.field1 , a.field2, a.field3
FROM database..hugeTable a( nolock )
WHERE a.StartTime > @fifteenMinutesAgo

The problem is that when I run the second query, it takes almost 3 minutes!

So I started messing around and searching like crazy. I thought that it might be a datatype problem so I tried various CASTs and CONVERTs and had no success. I tried using OPTIMIZE FOR but realized it would not work on this version of SQL. I checked the datatype of StartTime; it is of type datetime.

Another thing that I tried, which was very weird, was this:

DECLARE @fifteenDaysAgo datetime;
SET @fifteenDaysAgo = DATEADD(d , -15 , GETDATE());

SELECT top 500 a.field1 , a.field2, a.field3
FROM database..hugeTable a( nolock )
WHERE a.StartTime > @fifteenDaysAgo

I changed it from searching for the last 15 minutes, to the last 15 days. Magically it was super-fast again! Less than a second.

This lead me to believe that somehow it is hard for SQL to compare time? It didn’t need to look at the TIME portion of the datetime to see if it fit the compare statement? I don’t know. Here I am grasping at straws.

So my question is, how can I make this reasonably fast and still keep my @fifteenMinutesAgo dynamic?

  • 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-06-08T00:46:52+00:00Added an answer on June 8, 2026 at 12:46 am

    It looks like you have “parameter sniffing” problem with your procedure. WITH RECOMPILE option should help

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

Sidebar

Related Questions

HI Guys, Here I am having problem that How can I post an image
I've been having a strange problem with table cells that I've never seen before.
Here's a sample scenario that illustrates the problem I am having. Here is the
I'm having trouble with a table that has images, and all is great browsing
Im having a problem here. i downloaded jake wharton. He gave an example of
here is the problem I am having with placeholder: I have a repeater and
I'm having a little problem here, I'm new to Actionscript 3, and what I'm
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
I'm having a hard time trying to create a XmlRepository. The problem here 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.