Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 ad hoc query 176x faster than stored procedure?!?

Author  Topic 

aclarke
Posting Yak Master

133 Posts

Posted - 2002-05-25 : 02:24:47
Hi. I have a stored procedure that takes 7-9 seconds to run. If I run the same code as an ad hoc query it runs almost immediately. I just made a Coldfusion page where I called the stored procedure and immediately below it ran the same SQL outside the procedure. The SP call took 8266ms and the query took 47ms.

Any ideas as to why this might be? I've recompiled the SP thinking that would help but it didn't. Here's the query, and thanks...


CREATE PROCEDURE usp_Backend_getPossibleParentSKUsForSKU

@subordinateSKUID integer,
@when datetime

AS

if (isDate(@when) = 0) set @when = getDate()

SELECT Subcategory.SubcategoryName,
SKU.SKUID as parentSKUID, SKU.SKU as parentSKU, SKU.Color

FROM SubCategory left join SKU on Subcategory.subcategoryID = SKU.SubcategoryID

where subcategory.Subordinate = 0
and subcategory.time_validFrom <= @when and subcategory.time_validTo >= @when
and SKU.time_validFrom <= @when and SKU.time_validTo >= @when
and subcategory.manufacturerID in (
select manufacturerID
from subcategory left join SKU on subcategory.subcategoryID = SKU.subcategoryID
where SKU.SKUID = @subordinateSKUID
and subcategory.time_validFrom <= @when and subcategory.time_validTo >= @when
and SKU.time_validFrom <= @when and SKU.time_validTo >= @when
)
and SKU.SKUID not in (select distinct parentSKUID from subordinate where subordinateSKUID = @subordinateSKUID and time_validFrom <= @when and time_validTo >= @when)

order by subcategory.subcategoryName, SKU.color, SKU.SKU

GO


nr
SQLTeam MVY

12543 Posts

Posted - 2002-05-25 : 04:14:11
Almost certainly because the ad hoc query is getting a better execution plan than the SP. They should be the same but sometimes they aren't.
Check that you have the latest service pack as sometimes this fixes it.
Otherwise try adding hints or restructuring the query.
You could also use a temp table to split the query up.

==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -