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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure Performance Problem

Status
Not open for further replies.

Bobbber

Programmer
Sep 6, 2002
83
TR
Guys,

I have a stored procedure along the following lines :

Code:
Create Procedure ProcedureName

AS

Create Table yyy (
  IDField int,
  Field1 varchar(50),
  Field2 varchar(50),
  Field3 varchar(50),
  Fieldx... 
  etc...
)

Insert into Table yyy (IDField) SELECT ID from tblPPP where Surname='Smith'

UPDATE Table yyy SET Field1=value FROM tblQQQ WHERE tblQQQ.ID=IDField

UPDATE Table yyy SET Field2=value FROM tblRRR WHERE tblRRR.ID=IDField

UPDATE Table yyy SET Field3=value FROM tblSSS WHERE tblSSS.ID=IDField

UPDATE Table yyy SET Fieldx... 

etc...

SELECT * FROM yyy

DROP TABLE yyy

If I run each line of code in Query Analyzer, it takes 1min 40seconds in TOTAL (i.e. adding the times together that it takes to run each line).

But EXECuting the complete code as a stored procedure takes 25 minutes!!!!

Any ideas why?

Thanks!

Bob

 
instead of doing 4 updates do 1
instead of this
Code:
UPDATE Table yyy SET Field1=value FROM tblQQQ WHERE tblQQQ.ID=IDField
UPDATE Table yyy SET Field2=value FROM tblRRR WHERE tblRRR.ID=IDField
UPDATE Table yyy SET Field3=value FROM tblSSS WHERE tblSSS.ID=IDField
UPDATE Table yyy SET Fieldx...
do something like this

Code:
declare @id int,@v1 int,@v2 int,@v3 int.....
select @ID = ID from tblPPP where Surname='Smith'

select @v1 =value FROM tblQQQ WHERE ID=@ID
select @v2 =value FROM tblRRR WHERE ID=@ID
select @v3 =value FROM tblSSS WHERE ID=@ID

UPDATE Table yyy 
SET Field1=@v1,
Field2=@v2,
Field3=@v3 
WHERE ID=@ID

Denis The SQL Menace
SQL blog:
Personal Blog:
 
In addtion to Denis's comments...

I notice that you are creating a 'real' table to use for temporary purposes. You may get better performance if you create a temp table instead.

Change...
Create Table yyy
To
Create Table #yyy

And then change (every where)...
yyy
To
#yyy


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi Denis,

Thanks for the fast reply.

But the table tblPPP contains many surnames of 'smith' and therefore the table yyy is populated with a couple of thousand rows.

This surely only updates one row? And doesn't explain why this is many times slower as a stored procedure than executing each line independently in QA.

Bob

 
George,

Using a temporary table or a "real" one makes little difference to the processing times I'm getting.

Bob

 
Yes - a second or third run of the procedure makes little difference.

Sorry! I know you're trying!

Bob

 
It looks like you are trying to pull information from several tables. The stored procedure returns the data.

Does this query work for you....

Code:
Select	tblPPP.IdField,
		tblQQQ.Value As Field1,
		tblRRR.Value As Field2,
		tblSSS.Value As Field3
From	tblPPP
		Inner Join tblQQQ On tblPPP.IdField = tblQQQ.IdField
		Inner Join tblRRR On tblPPP.IdField = tblRRR.IdField
		Inner Join tblSSS On tblPPP.IdField = tblSSS.IdField
Where   tblPPP.Surname = 'Smith'

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
C'mon guys....

My question is "why it takes longer in a stored procedure than in QA"...

Thanks!

Bob

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top