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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:19:35+00:00 2026-05-11T00:19:35+00:00

While developing a new query at work I wrote it and profiled it in

  • 0

While developing a new query at work I wrote it and profiled it in SQL Query Analyzer. The query was performing really good without any table scans but when I encapsulated it within a stored procedure the performance was horrible. When I looked at the execution plan I could see that SQL Server picked a different plan that used a table scan instead of an index seek on TableB (I’ve been forced to obfuscate the table and column names a bit but none of the query logic has changed).

Here’s the query

SELECT          DATEADD(dd, 0, DATEDIFF(dd, 0, TableA.Created)) AS Day,      DATEPART(hh, TableA.Created) AS [Hour],      SUM(TableB.Quantity) AS Quantity,      SUM(TableB.Amount) AS Amount FROM     TableA     INNER JOIN TableB ON TableA.BID = TableB.ID WHERE          (TableA.ShopId = @ShopId) GROUP BY      DATEADD(dd, 0, DATEDIFF(dd, 0, TableA.Created)),      DATEPART(hh, TableA.Created) ORDER BY      DATEPART(hh, TableA.Created) 

When I run the query ‘raw’ I get the following trace stats

 Event Class         Duration  CPU  Reads Writes SQL:StmtCompleted   75        41      7      0 

And when I run the query as a stored proc using the following command

DECLARE @ShopId int SELECT @ShopId = 1 EXEC spStats_GetSalesStatsByHour @ShopId 

I get the following trace stats

 Event Class         Duration  CPU  Reads Writes SQL:StmtCompleted   222       10     48      0 

I also get the same result if I store the query in an nvarchar and execute it using sp_executesql like this (it performs like the sproc)

DECLARE @SQL nvarchar(2000) SET @SQL = 'SELECT DATEADD(dd, ...' exec sp_executesql @SQL 

The stored procedure does not contain anything except for the select statement above. What would cause sql server to pick an inferior execution plan just because the statement is executed as a stored procedure?

We’re currently running on SQL Server 2000

  • 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. 2026-05-11T00:19:36+00:00Added an answer on May 11, 2026 at 12:19 am

    This generally has something to do with parameter sniffing. It can be very frustrating to deal with. Sometimes it can be solved by recompiling the stored procedure, and sometimes you can even use a duplicate variable inside the stored procedure like this:

    alter procedure p_myproc (@p1 int) as declare @p1_copy int; set @p1_copy = @p1; 

    And then use @p1_copy in the query. Seems ridiculous but it works.

    Check my recent question on the same topic:

    Why does the SqlServer optimizer get so confused with parameters?

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

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer ForeColor is the property that controls the color of the… May 11, 2026 at 10:33 am
  • added an answer I have the same problem, and some of my colleagues… May 11, 2026 at 10:33 am
  • added an answer This is surprisingly difficult. Searching by name doesn't work as… May 11, 2026 at 10:33 am

Related Questions

While developing a new query at work I wrote it and profiled it in
I am new to UserControls, and while developing my own control I found a
While developing a C++ application, I had to use a third-party library which produced
My team is moving from Visual SourceSafe to Subversion soon, while developing/supporting a legacy
After working for a while developing games, I've been exposed to both variable frame
I've been developing Web applications for a while now and have dipped my toe
I'm engaged in writing a product using LinqToSql for data-access. While we're rapidly developing,
While developing products, we often need to create proprietary tools to test some of
While developing my app I have come to realize that the majority of my
I've encountered an interesting problem while developing for our legacy XWindows application. For reasons

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.