Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query vs Stored Proc

Status
Not open for further replies.

Bonediggler1

Technical User
Jul 2, 2008
156
US
Hi-

Why would a query using three CTEs take 30 seconds to run, and the same code as a SP takes 30 minutes?

The only difference is passing the parameter to the SP rather than declaring/setting it in code. The parameter is as follows:

DECLARE @ACCTGID INT
SET @ACCTGID = 201009

Thank you!!

 
How is the variable declared in your SP?

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 

CREATE PROCEDURE [BLAH].[sp_BLAH1]
@ACCTGID INT
AS
SET NOCOUNT ON;

ETC ETC
 
It is probably a [google]parameter sniffing[/google] issue.

Try adding WITH RECOMPILE to the stored procedure declaration to see if that solves the problem.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 

It's been running 5 minutes (query is 30 seconds) already with the WITH RECOMPILE addition.

I changed the CTE arrangement into temp tables and execution time is down to 50 seconds down from 30 minutes.

Wonder why that is?

Thanks for the help anyway. I didn't now about that WITH RECOMPILE feature...may come in handy in the future.
 
I've never been a fan of CTE's. Have you tried using a derived table approach? That usually gives better performance than temp tables. Not always, but usually.

Can you show the whole query?

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top