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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:03:20+00:00 2026-06-01T15:03:20+00:00

I am writting a stored procedure that will get data for a sales report.

  • 0

I am writting a stored procedure that will get data for a sales report. Queries are like this:

INSERT INTO @FirstQuery
SELECT t1.*, t2.*
  FROM t1
       LEFT JOIN t2 ON t1.idT1 = t2.idT1
       LEFT JOIN t3 ON t3.idT2 = t2.idT2
 WHERE t1.nonIndexedField = @parameter1
   AND (t2.idT2 IS NULL
          OR
          (@parameter2 = 'XXX' AND t1.indexedField1 = @parameter3)
          OR
          (@parameter2 = 'YYY' AND t3.indexedField1 = @parameter3)
       )

With those results, I then fill a second table variable:

INSERT INTO @SecondQuery
SELECT u1.*, u2.*
  FROM u1
       INNER JOIN u2 ON u2.idU1 = u1.idU1
 WHERE u1.NONindexedField in (SELECT someField FROM @FirstQuery)

As it was being quite slow on QA environment, I watched the execution plans. First I took a look at the Estimated plan. It saw that SecondQuery was taking really long, and I realized that u1.NONindexedField didn’t have an index and was taking an estimated 97% of the total cost.
But then I took a look at the Actual plan, and it said that FirstQuery was takin 100% of total cost. I checked the estimated rows calculated on the estimated plan and it many places it estimated very few rows where the actual plan showed around a hundred thouthand (100K). I thought it was because the field missing an index was in a table with not so many rows (17K), but I created the index anyway. To my surprise, the query time was reduced from 500 to 15 seconds.
So, my question is… why such a difference in execution plans, and how come the actual plan was way off? I know that Estimated plan doesn’t really mean “an estimation of the plan” but rather “a plan with estimated row counts”, but that doesn’t explain the difference, and certanly doesn’t explain why actual plan told me that all the cost was on a query that didn’t need optimizing…

BTW, I compared relative times and the second query indeed takes around 97% of total execution time.

Thanks for reading!

  • 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-01T15:03:22+00:00Added an answer on June 1, 2026 at 3:03 pm

    If the actual plan was way off this could be caused by outdated statistics.

    Have you tried updating statistics on the tables in the query.

    If the statistics are out of date, this can cause the execution plans to be off. With out updated statistics your queries may be very inefficient.

    the syntax for updating statistics is:

    update statistics tablename;
    

    Try updating the statistics and see if you are getting more accurate execution plans.

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

Sidebar

Related Questions

I would like to have a stored procedure that will update values in a
I am writing a stored procedure that will get results from one table then
I am writing a stored procedure that when completed will be used to scan
Let's say I have a stored procedure that will do complicated logic when the
I'm trying to write a stored procedure in SQL Server that will eliminate some
I'm writing a stored procedure that will bring back a lot of project information.
I am having difficulty writing a Stored Procedure that will query a list of
Writing a stored procedure that will have multiple input parameters. The parameters may not
I'm writing a stored procedure that needs to have a lot of conditioning in
i am writing stored procedures in MySQL that return values; CREATE PROCEDURE getCustomerById (id

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.