Solved

Stored Procedure is taking longer than raw SQL

Posted on 2012-03-09
7
626 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 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 61
Record open by another user 6 65
passing parameter in sql procedure 9 65
Email Notifications for SQL 2005 9 36
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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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