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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:18:53+00:00 2026-06-09T23:18:53+00:00

I have a view that I can’t seem to fully tune, so I tried

  • 0

I have a view that I can’t seem to fully tune, so I tried converting it to a proc, moving one of my WHERE conditions that I use when querying into a parameter and using the parameter in my derived tables.

View

CREATE VIEW myView
AS
SELECT     i.ItemCode
           , s.StoreID
           , ISNULL(SUM(s.TotalSales)) AS Sales
FROM       Item i
LEFT JOIN  Balance b ON i.ItemCode = b.ItemCode
LEFT JOIN  (SELECT    SUM(Quantity)
                      , StoreID
                      , ItemCode
                      , Date
            FROM      Sales
            GROUP BY  StoreID
                      , ItemCode
                      , Date) AS s    ON i.ItemCode = s.ItemCode
                                      AND s.Data >= COALESCE(b.Date, '01-01-1900)
JOIN
GROUP BY    i.ItemCode
            , s.StoreID

Stored Proc

CREATE PROCEDURE myProc(@StoreID INT)
AS
SELECT     i.ItemCode
           , s.StoreID
           , ISNULL(SUM(s.TotalSales)) AS Sales
FROM       Item i
LEFT JOIN  Balance b ON i.ItemCode = b.ItemCode
LEFT JOIN  (SELECT    SUM(Quantity)
                      , StoreID
                      , ItemCode
                      , Date
            FROM      Sales
            WHERE     StoreID = @StoreID
            GROUP BY  StoreID
                      , ItemCode
                      , Date) AS s    ON i.ItemCode = s.ItemCode
                                      AND s.Data >= COALESCE(b.Date, '01-01-1900)
JOIN
GROUP BY    i.ItemCode
            , s.StoreID

EXEC myProc(100) is much faster than SELECT * FROM myView WHERE StoreID = 100.
Should this be the case?

Note: I know this code may not make perfect sense or run — I’ve tried to simplify it by removing some other JOINs. The only substantial difference in the real code is moving the WHERE into the derived table, which I’ve done here.

Shouldn’t the view take my WHERE into account when performing the derived table queries and be just as fast as the proc?

Thanks for any help!

  • 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-09T23:18:54+00:00Added an answer on June 9, 2026 at 11:18 pm

    The two queries are quite different. The difference is very subtle, but this is the line:

            WHERE     StoreID = @StoreID
    

    The view calculates all the data for all the stores. It then filters the results for a particular store.

    The stored procedure version is only looking at the particular store; it can even make efficient use of an index on StoreId if there is one.

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

Sidebar

Related Questions

I'm using .NET webforms. I have a grid view that can use Eval(Name) in
I have a view that can take several seconds to process a GET request
I have a multi-line text view that can get quite large. When the user
I have a view with a few checkboxes that can be selected or unselected.
I have a system whereby a user can view categories that they've subscribed to
I have a view controller that can fetch many different types of entities from
I have a scroll view that can be scrolled to the sides (only left
Imagine you have a secured site and a View that can be generated in
My main form in my application has a datagrid view that can have 1
I have a view that can be swiped to another page. It has an

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.