Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

stored procedure vs individual insert statements Expand / Collapse
Author
Message
Posted Wednesday, January 9, 2008 11:17 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, October 4, 2009 7:33 AM
Points: 84, Visits: 43
Why is it that if I run the individual insert statements in Example A one at a time, the overall time to run is 20 minutes. If I put all of the insert statements into a stored proc, it takes an hour and a half? Same inserts.

Example A
insert into prod1
select *
from test1

insert into prod2
select *
from test2

insert into test3
select *
from test3

etc




Post #440824
Posted Wednesday, January 9, 2008 3:34 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 5:23 PM
Points: 4,576, Visits: 8,341
What happens if you execute those inserts in a batch?
Post #440929
Posted Wednesday, January 9, 2008 3:41 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, October 4, 2009 7:33 AM
Points: 84, Visits: 43
Pardon for my ignorance but what do you mean by batch?


Post #440933
Posted Wednesday, January 9, 2008 3:54 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 5:23 PM
Points: 4,576, Visits: 8,341
I mean executing all 3 statements in one go but without wrapping it in a procedure.
Just as you pasted it here.
Post #440937
Posted Wednesday, January 9, 2008 8:47 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 11, 2014 12:43 PM
Points: 169, Visits: 814
Is it a typo, or are you really intending on selecting the same table into itself?
I've noticed this on occasion as well (something runs well/as expected in query analyzer, but the moment it is a procedure, performance plummets). Perhaps greater SQL minds than mine have some insight into this problem. These are just my random musings, but I wonder if it has something to do with transaction management/locking and latching. I wonder if wrapping each statement in a begin tran/commit block would help, since it would force out writes to the transaction log.
The other thing is if you are really selecting the same table into itself, depending on its size, consider copying out the records to a temp table, and copying them back in to prevent contention or latching or locking to the table. SQL Gurus, please step in if I've misstated something here, because this area has always been somewhat of a mystery to me.
Hope this at least sparks some interesting thought.
Thanks!
Post #440977
Posted Wednesday, January 9, 2008 9:35 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 5:23 PM
Points: 4,576, Visits: 8,341
Jeremy,
there is no any point in wrapping each statement in a begin tran/commit block because they are transactions anyway.

And I would guess that was a typo.
I really doubt real tables have got such funny names. :)

Regarding this I would say that devil is usually in details.
Posting real statements could be very helpful.
The real reason could be anything we don't see: misuse of parameters, remote calls, even typo in script.
Post #440985
Posted Wednesday, January 9, 2008 9:41 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, August 13, 2013 6:38 AM
Points: 542, Visits: 104
Can u pls paste those queries here so that v can check if they need some fine tuning or u can check if ur tables r indexed properly.
All this while I was under the impression that stored procedures run faster cuz they r pre compiled. Correct me if I'm wrong.
Post #440986
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse