Solved

Stored Procedure is taking longer than raw SQL

Posted on 2012-03-09
7
608 Views
Last Modified: 2012-06-27
Hey Experts,

I am seeking help for a very strange issue I am having with a stored procedure and it's execution time.

I have a store procedure that when run on its own (as a set of queries) runs in about 3-5 minutes.  However, when I execute it as a stored procedure it takes over 30 minutes to run.  This stored procedure is pretty long and hits several views and tables, but no other stored procedures.

Here is the weird part, if I declare new variable that are the same as the ones being passed to the procedure, then assign the new variables to the values of the parameters being passed, it runs in 3-5 minutes again.  I know this last bit is a little confusing, so below is an example.

This is a SQL server running MS SQL 2005.  Also, I have already tried recompiling the stored procedure, and that hasn't helped the problem.  

Example:

Raw Queries (takes 3-5 minutes to run)
DECLARE @VariableA VARCHAR(50)
DECLARE @VariableB VARCHAR(50)
DECLARE @VariableC VARCHAR(50)

SET @VariableA = 'ABC'
SET @VariableB = 'DEF'
SET @VariableC = 'GHI'

SELECT * FROM [FirstSelect] WHERE ColumnA = @VariableA
SELECT * FROM [SecondSelect] WHERE ColumnB = @VariableB
SELECT * FROM [LastSelect] WHERE ColumnC = @VariableC

Open in new window

As Stored Procedure (Takes 30+ minutes to run as a stored procedure)
CREATE PROCEDURE myStoredProcedure
	@VariableA VARCHAR(50) = '0',    
	@VariableB VARCHAR(50) = '0',    
	@VariableC VARCHAR(50) = '0'
AS
	SELECT * FROM [FirstSelect] WHERE ColumnA = @VariableA
	SELECT * FROM [SecondSelect] WHERE ColumnB = @VariableB
	SELECT * FROM [LastSelect] WHERE ColumnC = @VariableC
GO

EXEC myStoredProcedure 
	@VariableA = 'ABC', 
	@VariableB = 'DEF', 
	@VariableC = 'GHI'

Open in new window

Very strange way that makes the stored procedure run faster (3-6 minutes)
CREATE PROCEDURE myStoredProcedure
	@VariableA VARCHAR(50) = '0',    
	@VariableB VARCHAR(50) = '0',    
	@VariableC VARCHAR(50) = '0'
AS
	DECLARE @VariableA1 VARCHAR(50)
	DECLARE @VariableB1 VARCHAR(50)
	DECLARE @VariableC1 VARCHAR(50)

	SET @VariableA1 = @VariableA
	SET @VariableB1 = @VariableB
	SET @VariableC1 = @VariableC

	SELECT * FROM [FirstSelect] WHERE ColumnA = @VariableA1
	SELECT * FROM [SecondSelect] WHERE ColumnB = @VariableB1
	SELECT * FROM [LastSelect] WHERE ColumnC = @VariableC1
GO
EXEC myStoredProcedure 
	@VariableA = 'ABC', 
	@VariableB = 'DEF', 
	@VariableC = 'GHI'
GO

Open in new window

0
Comment
Question by:StarbucksDrinker
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 37702775
Run both versions and compare execution plans.

Then UPDATE STATISTICS and do the same.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 37702853
0
 
LVL 1

Author Comment

by:StarbucksDrinker
ID: 37703000
Isn't UPDATE STATISTICS for tables and indexed views?  Would that need to be run on every table and indexed view that we are hitting in the stored procedure?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37703433
I would try recompiling the procedure every time to avoid using a poorly matching plan from a previous run.


CREATE PROCEDURE myStoredProcedure
      @VariableA VARCHAR(50) = '0',    
      @VariableB VARCHAR(50) = '0',    
      @VariableC VARCHAR(50) = '0'
WITH RECOMPILE
AS
...
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37703438
just prevent parameter sniffing by using variables as in your last example
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37703485
Local variables will often give you a poor plan as well.  They force SQL to use generic estimates for the plan instead of the actual values being passed in.

Recompiling is typically the best option.  If the proc is running 3+ minutes, the recompile time will not be significant.

Thus, as a general rule, you should always try RECOMPILE first.

In certain specific cases, local variables may perform better than recompiling, but that can be detected later.
0
 
LVL 1

Author Closing Comment

by:StarbucksDrinker
ID: 37703701
Awesome, this totally explains the problem!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now