Advertisement

03.19.2006 at 09:44AM PST, ID: 21779905
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

Query as StoredProc taking 60x as long as query executed directly

Asked by zacbell in MS SQL Server

Tags: , , ,

When I run a sequence of queries directly (in SQL Query Analyzer against Sql Server 2k), it runs in about 40s. When I take that EXACT same sequence of queries and slap them into a StoredProc, it takes 40min. The statement at the end of the sequence is responsible for the long execution, and is an insert into a temp table. I tried googling for other users' experiences with this problem, and found something similar here (http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21750204.html). However, the suggestions have not helped with this query.

The execution plans are different when running directly vs. as a StoredProc. When direct, the execution plan shows a hash join and parallelism, the StoredProc uses a nested loop join. I have added a HASH join hint to the stored proc, and the performance improves quite a bit, but is still 10x worse than running the sequence directly. I have tried many things, including setting maxdop, clearing the proc cache, executing with recompile, using a table variable instead of a temp table (actually gave worse performance since the temp table benefits from a clustered index for subsequent operations), breaking the insert into multiple exclusive steps -- none of these have improved the performance of the storedproc.

Here is the specific statement that is taking "forever" when executed within a storedproc. Note that the VWMarketPriceHistory item is a view on a set of two partitioned tables. Even if I separate the insert into two inserts on the paritioned tables individually, the performance is the same as using this view.

Any ideas?

create table #Price (
      MarketDt smalldatetime not null,
      PkgProgId int not null,
      Coupon float not null,
      Price float not null,
      primary key clustered (MarketDt, PkgProgId, Coupon)
)



insert #Price
      select
            s.MarketDt,
            s.PkgProgId,
            s.Coupon,
            Price = max(s.MarketPrice)
      from Position p
      join VWMarketPriceHistory s
            on p.PkgProgId = s.PkgProgId
            and p.Coupon = s.Coupon
      where datediff(m, s.MarketDt, s.SettlementDt) between 0 and 1
        and s.MarketDt <= '3/17/06'
      group by
            s.MarketDt,
            s.PkgProgId,
            s.Coupon
Start Free Trial
[+][-]03.19.2006 at 10:00AM PST, ID: 16230450

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2006 at 10:17AM PST, ID: 16230495

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2006 at 11:29AM PST, ID: 16230771

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2006 at 01:11AM PST, ID: 16233885

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2006 at 02:45AM PST, ID: 16234206

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2006 at 07:22AM PST, ID: 16236138

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2006 at 07:29AM PST, ID: 16236243

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.23.2006 at 08:32PM PST, ID: 16277464

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.31.2006 at 03:44PM PST, ID: 16347440

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 14-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04.04.2006 at 05:08AM PDT, ID: 16369933

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: long, queries, sql, taking
Sign Up Now!
Solution Provided By: GranMod
Participating Experts: 4
Solution Grade: B
 
 
 
Loading Advertisement...
20081112-EE-VQP-43