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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:30:01+00:00 2026-06-18T12:30:01+00:00

I never questioned any efficiency difference between the following two scripts: DateKey is type

  • 0

I never questioned any efficiency difference between the following two scripts:

DateKey is type INT

1.

DECLARE @StartDate  INT = 20130101,
        @EndDate    INT = 20130201

SELECT  UserAccountKey,
        income_LT = SUM(ISNULL(income,0.0)) 
INTO    #x
FROM    WH.dbo.xxx x
WHERE   x.DateKey > = @StartDate
        AND x.DateKey < @EndDate
GROUP BY    UserAccountKey

Execution for the above is:

enter image description here

2.

SELECT  UserAccountKey,
        income_LT = SUM(ISNULL(income,0.0)) 
INTO    #x
FROM    WH.dbo.xxx x
WHERE   x.DateKey > = 20130101
        AND x.DateKey < 20130201
GROUP BY    UserAccountKey

Here is the execution plan for number 2:
enter image description here

1. is a lot faster (2 seconds compared to 80 seconds) – is this as expected? Why?

  • 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-18T12:30:02+00:00Added an answer on June 18, 2026 at 12:30 pm

    On the first query it uses variables. The value of these is not known at compilation time so it produces a plan based on generic estimates. On the second one it compiles a plan based on the actual parameter values.

    The fact that the generic guesses work out better than the plan where it knows the specific values indicates that probably your statistics need updating.

    Likely last time they were updated few if any rows matched the WHERE DateKey > = 20130101 AND DateKey < 20130201 predicate but now many do.

    This is a common issue with ascending date columns

    See also this question and answers on the dba site

    Edit This can be seen in the plan here

    enter image description here

    The thickness of the lines indicate the number of rows. The very thin line to the left of the compute scalar shows the estimated number of rows (actual row counts often aren’t shown for compute scalars for the reasons here). The very thick lines into the compute scalar and out of the sort represent the actual number of rows. The two are clearly very different.

    As well as choosing an inappropriate plan (serial with nested loops join) this poor estimate also means that the sort spilled to disc as the query requested an insufficient memory grant (shown by the warning triangle).

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

Sidebar

Related Questions

Whenever any question is asked, and a reference text is needed, I never see
I have a quick pro-con question for you. I've never done any reporting within
I have the following code in C++ here: #include <iostream> int main(int argc, const
As my first language and as completely taught from other's example I never questioned
Sort of a two part question: Is there any theoretical regular expression that will
I don't know why I never asked myself that questioned the last years before,
I have asked several questions on the buddypress forum but I never get any
Is there any way with CSS to target all inputs based on their type?
a couple of questions (never done javascript before). Below is part of the what
This question was asked earlier by someone else but never answered: MKAnnotation - Map

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.