Unix Technical Forum

sp_executesql vs. stored proc.

This is a discussion on sp_executesql vs. stored proc. within the SQL Server forums, part of the Microsoft SQL Server category; --> Greetings All, currentley there is a heated discussion in my place of work over which method is better/more efficient ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:23 AM
LineVoltageHalogen
 
Posts: n/a
Default sp_executesql vs. stored proc.

Greetings All, currentley there is a heated discussion in my place of
work over which method is better/more efficient for simple selects.

Background:
1.) Simple Application that uses sql server for backened db.
2.) The application is only inserting and selecting data from the db.
3.) The developers want to use sp_executesql for simple selects and
the dba's want to use a stored proc.

>From my reading it seems that sp_executesql has a bit of overhead with

it and it is not as efficient as stored procs.

I would appreciate anyone's input on which would be better for simple
repetitive inserts to the db: Stored Proc, or sp_executesql?

Regards, TFD.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:23 AM
Erland Sommarskog
 
Posts: n/a
Default Re: sp_executesql vs. stored proc.

LineVoltageHalogen (tropicalfruitdrops@yahoo.com) writes:
> Greetings All, currentley there is a heated discussion in my place of
> work over which method is better/more efficient for simple selects.
>
> Background:
> 1.) Simple Application that uses sql server for backened db.
> 2.) The application is only inserting and selecting data from the db.
> 3.) The developers want to use sp_executesql for simple selects and
> the dba's want to use a stored proc.
>
>>From my reading it seems that sp_executesql has a bit of overhead with

> it and it is not as efficient as stored procs.
>
> I would appreciate anyone's input on which would be better for simple
> repetitive inserts to the db: Stored Proc, or sp_executesql?


From a performance perspective, there is not that extreme difference. If
you use sp_executesql, the plan parameterized query will be cached and
reused, just like the query plan for a stored procedure. But the it does
require that the SQL statement is the same. These three will give different
entries in the cache:

SELECT * FROM tbl WHERE col = @par
select * from tbl where col = @par
SELECT * FROM tbl WHERE col = @par

The cache is both space- and case-sensitive.

There is also a certain overhead for looking up the cached query in the
plan, but normally it is neglible.

But there is also the security aspect of things. Stored procedures permits
you to revoke direct access to the tables, and only provide controlled
access through stored procedures.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:23 AM
LineVoltageHalogen
 
Posts: n/a
Default Re: sp_executesql vs. stored proc.


Erland Sommarskog wrote:
> LineVoltageHalogen (tropicalfruitdrops@yahoo.com) writes:
> > Greetings All, currentley there is a heated discussion in my place

of
> > work over which method is better/more efficient for simple selects.
> >
> > Background:
> > 1.) Simple Application that uses sql server for backened db.
> > 2.) The application is only inserting and selecting data from the

db.
> > 3.) The developers want to use sp_executesql for simple selects and
> > the dba's want to use a stored proc.
> >
> >>From my reading it seems that sp_executesql has a bit of overhead

with
> > it and it is not as efficient as stored procs.
> >
> > I would appreciate anyone's input on which would be better for

simple
> > repetitive inserts to the db: Stored Proc, or sp_executesql?

>
> From a performance perspective, there is not that extreme difference.

If
> you use sp_executesql, the plan parameterized query will be cached

and
> reused, just like the query plan for a stored procedure. But the it

does
> require that the SQL statement is the same. These three will give

different
> entries in the cache:
>
> SELECT * FROM tbl WHERE col = @par
> select * from tbl where col = @par
> SELECT * FROM tbl WHERE col = @par
>
> The cache is both space- and case-sensitive.
>
> There is also a certain overhead for looking up the cached query in

the
> plan, but normally it is neglible.
>
> But there is also the security aspect of things. Stored procedures

permits
> you to revoke direct access to the tables, and only provide

controlled
> access through stored procedures.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinf...2000/books.asp


You say it is neglible. However, what if this process runs 50 - 100
thousand times during each load process? EAch of those small neglible
pieces will add up to something measurable?

L

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:23 AM
Erland Sommarskog
 
Posts: n/a
Default Re: sp_executesql vs. stored proc.

LineVoltageHalogen (tropicalfruitdrops@yahoo.com) writes:
> You say it is neglible. However, what if this process runs 50 - 100
> thousand times during each load process? EAch of those small neglible
> pieces will add up to something measurable?


If you really want to know, you would have to benchmark. But say that
you have one single statement in the cache of the type:

INSERT dbo.tbl(col1, col2, ...) VALUES (@par1, @par2, ...)

Then the overhead compared to find the stored procedure load_one_row is the
longer time it takes to compute the hash bucket to find it the cache. I
would guess that is miniscule. Note, though, the dbo part. That may be
important.

And, assume that you have a case-insensitive database, and your beloved
programmer has spelt the procedure Load_one_row. Now you will first get a
cache miss, even if you prefix with dbo, because the cache is case
sensitive. After a lookup in the catalog you will eventually hit the cache
anyway.)

Also stored procedures should be prefixed with dbo. when you call them.
(Provided that they are owned by dbo, that is!)


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com