SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


stored procedure vs individual insert statements


stored procedure vs individual insert statements

Author
Message
mgraulich
mgraulich
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 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



Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25339 Visits: 12464
What happens if you execute those inserts in a batch?
mgraulich
mgraulich
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 Visits: 43
Pardon for my ignorance but what do you mean by batch?



Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25339 Visits: 12464
I mean executing all 3 statements in one go but without wrapping it in a procedure.
Just as you pasted it here.
Jeremy-475548
Jeremy-475548
Right there with Babe
Right there with Babe (729 reputation)Right there with Babe (729 reputation)Right there with Babe (729 reputation)Right there with Babe (729 reputation)Right there with Babe (729 reputation)Right there with Babe (729 reputation)Right there with Babe (729 reputation)Right there with Babe (729 reputation)

Group: General Forum Members
Points: 729 Visits: 906
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!
Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25339 Visits: 12464
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. Smile

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.
Jerome.J.N
Jerome.J.N
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 106
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search