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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:12:12+00:00 2026-05-27T04:12:12+00:00

I have a monster query that I’m running against a SQL SERVER 2005 database

  • 0

I have a monster query that I’m running against a SQL SERVER 2005 database that is acting very strange. I have two conditions in the WHERE clause of the outermost select, comparing a field to a constant date. When the constant dates are either identical (down to the second) or their date parts are not equal, the query runs in under 2 seconds. When the date parts are the same but the time parts are different, the query takes around 7 minutes to complete. Specifically, having a WHERE clause of

WHERE
  d.date >= '2011-11-07 00:00:00' AND
  d.date <= '2011-11-08 11:59:59'

works well and as expected. Changing the WHERE clause to

WHERE
  d.date >= '2011-11-07 00:00:00' AND
  d.date <= '2011-11-07 11:59:59'

causes the query to take many minutes.

I also noticed that when I turned off the index on the Agent_Hours table that the bad case of having the same dates the same reduces the query time to 25 seconds, still far longer than when they dates are different, but not by as much.

Below is the full query for reference (the WHERE clause in question is at the very end):

SELECT
  s.transaction_id AS 'transaction',
  s.created_on AS transaction_date,
  s.first_name + ' ' + s.Last_Name AS customer_name,
  a.name AS agent_name,
  a.phantom AS phantom,
  a.team AS agent_team,
  a.id AS agent_number,
  h.hours,
  h2.hours_today,
  d.*
FROM
  (SELECT
     agents.first_name + ' ' + agents.last_name AS name,
     agents.id AS id,
     agents.phantom AS phantom,
     transient.value AS team,
     transient.start_date AS team_start_date,
     transient.end_date AS team_end_date
   FROM
       Agents.dbo.Agent_Static AS agents
     JOIN
       Agents.dbo.Agent_Transient AS transient
     ON transient.agent = agents.id
   WHERE
     transient.field = 'team') AS a
  LEFT JOIN Agents.dbo.Agent_Daily AS d
    ON d.agent = a.id
  LEFT JOIN (SELECT
               agent_hours.agent AS agent,
               dates.date AS date,
               CAST(COUNT(*) AS FLOAT) / 4 AS hours
             FROM
                 Agents.dbo.Agent_Hours AS agent_hours
               JOIN
                 (SELECT
                    DISTINCT CONVERT(
                               VARCHAR(10),
                               hour_worked,
                               101)
                               AS date
                  FROM
                    Agents.dbo.Agent_Hours) AS dates
               ON dates.date = CONVERT(
                                 VARCHAR(10),
                                 agent_hours.hour_worked,
                                 101)
             WHERE
               (status = 'Phone' OR
                status = 'Meeting')
             GROUP BY
               agent_hours.agent,
               dates.date) AS h
    ON h.agent = a.id AND
       h.date = d.date
  LEFT JOIN (SELECT
               agent_hours.agent AS agent,
               dates.date AS date,
               CAST(COUNT(*) AS FLOAT) / 4 AS hours_today
             FROM
                 Agents.dbo.Agent_Hours AS agent_hours
               JOIN
                 (SELECT
                    DISTINCT CONVERT(
                               VARCHAR(10),
                               hour_worked,
                               101)
                               AS date
                  FROM
                    Agents.dbo.Agent_Hours) AS dates
               ON dates.date = CONVERT(
                                 VARCHAR(10),
                                 agent_hours.hour_worked,
                                 101)
             WHERE
               (status = 'Phone' OR
                status = 'Meeting') AND
               CONVERT(
                 VARCHAR(10),
                 CAST('11/09/2011 13:01' AS DATETIME),
                 101) = CONVERT(
                          VARCHAR(10),
                          agent_hours.hour_worked,
                          101) AND
               CONVERT(
                 VARCHAR(10),
                 CAST('11/09/2011 13:01' AS DATETIME),
                 114) > CONVERT(
                          VARCHAR(10),
                          agent_hours.hour_worked,
                          114)
             GROUP BY
               agent_hours.agent,
               dates.date) AS h2
    ON h2.agent = a.id AND
       h2.date = d.date
  LEFT JOIN sale_transactions AS s
    ON a.id = s.agent_hermes_id AND
       s.created_on >= a.team_start_date AND
       s.created_on <= a.team_end_date AND
       CONVERT(
         VARCHAR(10),
         d.date,
         101) = CONVERT(
                  VARCHAR(10),
                  s.created_on,
                  101)
  LEFT JOIN sold_phrases AS p
    ON s.Transaction_ID = p.transaction_id
WHERE
  d.date >= '2011-11-07 00:00:00' AND
  d.date <= '2011-11-07 11:59:59'
  • 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-27T04:12:13+00:00Added an answer on May 27, 2026 at 4:12 am

    As a general rule, always post your exact table definition, including all indexes, when asking performance problems in SQL.

    I cannot see any difference between the two cases, but considering your explanation, this is what likely happens: the cardinality estimates for the date range may trigger the index tipping point and you get wildly different execution plans. Such issues are best addressed by using plan guides, see Optimizing Queries in Deployed Applications by Using Plan Guides. You should be able to confirm if the problem is indeed the plan, see Displaying Graphical Execution Plans (SQL Server Management Studio).

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

Sidebar

Related Questions

Is it particularly bad to have a very, very large SQL query with lots
I have a monster legacy query that is in the core of my program,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
I have about two macros (and climbing) in my codebase that look like this:
I'm stuck with a SQL query (for SQL Server). I'm new to SQL and
I have a query that will need to run 28 000 times in a
I really dislike to have monster .git folder (2GB) and I don't care about
I have seen a avatar generator which makes monster creators, but I saw this
We have a dynamic languaging system that doesn't use conventional resource files; fetching resources
I'm creating c++ game server. The server creates many objects monster , and every

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.