Solved

Stored Procedure is taking longer than raw SQL

Posted on 2012-03-09
7
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 69

Expert Comment

by:Scott Pletcher
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 70

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:Scott Pletcher
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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