Solved

Stored Procedure is taking longer than raw SQL

Posted on 2012-03-09
7
629 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL page split per second is high 19 138
Problem with SqlConnection 4 193
Format Date fields 11 63
Database maintenance 36 136
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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