Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Stored Procedure vs direct execute SQL

Reply
Thread Tools

Stored Procedure vs direct execute SQL

 
 
=?Utf-8?B?SklNLkgu?=
Guest
Posts: n/a
 
      05-27-2005
Hello,
Is there any difference to between SLQ string in the code and call execute
query and call a stored procedure and execute the query that way concerning
speed, effectiveness, reliability, …?
Thanks,
Jim.

 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      05-27-2005
Stored procedures are compiled, so you should see improvement there. How
much improvement and how noticeable really all depends on a number of
factors.

"JIM.H." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> Is there any difference to between SLQ string in the code and call execute
> query and call a stored procedure and execute the query that way
> concerning
> speed, effectiveness, reliability, .?
> Thanks,
> Jim.
>



 
Reply With Quote
 
 
 
 
Robbe Morris [C# MVP]
Guest
Posts: n/a
 
      05-27-2005
SQL Server caching of queries has reduced the overall performance
gain of procedures versus dynamic sql strings. Procedures
in most cases will still be faster.

They are also "far more" secure than dynamic sql strings.

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net



"JIM.H." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> Is there any difference to between SLQ string in the code and call execute
> query and call a stored procedure and execute the query that way
> concerning
> speed, effectiveness, reliability, .?
> Thanks,
> Jim.
>



 
Reply With Quote
 
clintonG
Guest
Posts: n/a
 
      05-27-2005
Read these for starters...

http://www.windowsitpro.com/Articles...layTab=Article
http://www.codeproject.com/database/hkstoredproc.asp


<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/




"JIM.H." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> Is there any difference to between SLQ string in the code and call execute
> query and call a stored procedure and execute the query that way
> concerning
> speed, effectiveness, reliability, .?
> Thanks,
> Jim.
>



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      05-27-2005
I have to strongly disagree with the two other respondants. The real answer
is that it totally depends. To say that stored procedures are "far more
secure" is extremely misleading. By simply using parameterized values in
your dynamic SQL you've achieved high security. Conversly, by using execute
in your stored procedure it's not at all secure.

As far as performance, it really depends on what you are doing. That SQL
Server caches queries is a very misleading generalization. Complexe queries
are often uncacheable. Additionally, client-side (ASP.Net) caching can
belittle this minor advantage. What's worse, the most efficient search
queries are typically written in one form or another of dynamic sql
(otherwise SQL Server can't take advantage of indexes).

Personally I'm a fan of stored procedures (big time), but blanket statements
are dangerous. Saying that stored procedures are secure dangerously risks
taking the responsability away from the developer. Additionally, different
solutions are better suited for different scenarios. As such, only someone
who doesn't subscribe to blanket statements can make the right decision.

Let's look at a simplified example:

create procedure SearchProperty AS
@ListingType INT --pass 0 to search for all listing types


select listingId, listingName from Property
where (@listingType= 0 OR ListingType = @ListingType)
AND Status = 1


In the above example we need to allow 0 to be passed in to mean any listing.
This will likely result in SQL Server being unable to use an index on the
ListingType column, thus resulting in less that optimal performance. To get
ideal performance, we'd need to if the code:

if @ListingType = 0 BEGIN

select listingId, listingName from Property
WHERE Status = 1

END ELSE BEGIN

select listingId, listingName from Property
WHERE ListingType = @ListingType AND Status = 1

END

Now you are getting somewhere...except that it'll get impossible to maintain
with the addition of just a few more search parameters (and query plan
caching will increasingly become less efficient).

We could use execute:

declare @sql varchar(1024)
set @sql = 'select listingId, listingName from Property WHERE Status = 1'
if @ListingType = 0 BEGIN
SET @sql = @sql + ' AND ListingType = ' + @ListingTypeId
END
exec(@sql)

Looky that, a stored procedure and a HUGE potential for SQL
injection...egads! (yes, the above sproc won't actually work but if
@ListingTypeID was a varchar (say comma-separated values) it would).

This simple, and common, example not only illustrates that stored procedures
aren't necessarily faster and just vulnerable, but that they can also be
harder to maintain.

In short, the correct answer is "it depends" and hopefully as you learn more
you'll learn to use the right solution at the right time

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"JIM.H." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> Is there any difference to between SLQ string in the code and call execute
> query and call a stored procedure and execute the query that way
> concerning
> speed, effectiveness, reliability, .?
> Thanks,
> Jim.
>



 
Reply With Quote
 
PB
Guest
Posts: n/a
 
      05-27-2005
I couldn't agree with you more about blanket statements... implying that
proper research and understanding is in order. But I do have a question
regarding your statement:

<< the most efficient search queries are typically written in one form or
another of dynamic sql (otherwise SQL Server can't take advantage of
indexes).>>

I don't understand this. You seem to be saying that SQL Server can use
indexes *only* when fed dynamic SQL. I don't think that's true. Can you
explain further? I guess it's the "one form or another" part that I don't
get. How many forms of dynamic SQL are there?

Thanks.



"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:(E-Mail Removed)...
>I have to strongly disagree with the two other respondants. The real
>answer is that it totally depends. To say that stored procedures are "far
>more secure" is extremely misleading. By simply using parameterized values
>in your dynamic SQL you've achieved high security. Conversly, by using
>execute in your stored procedure it's not at all secure.
>
> As far as performance, it really depends on what you are doing. That SQL
> Server caches queries is a very misleading generalization. Complexe
> queries are often uncacheable. Additionally, client-side (ASP.Net) caching
> can belittle this minor advantage. What's worse, the most efficient
> search queries are typically written in one form or another of dynamic sql
> (otherwise SQL Server can't take advantage of indexes).
>
> Personally I'm a fan of stored procedures (big time), but blanket
> statements are dangerous. Saying that stored procedures are secure
> dangerously risks taking the responsability away from the developer.
> Additionally, different solutions are better suited for different
> scenarios. As such, only someone who doesn't subscribe to blanket
> statements can make the right decision.
>
> Let's look at a simplified example:
>
> create procedure SearchProperty AS
> @ListingType INT --pass 0 to search for all listing types
>
>
> select listingId, listingName from Property
> where (@listingType= 0 OR ListingType = @ListingType)
> AND Status = 1
>
>
> In the above example we need to allow 0 to be passed in to mean any
> listing. This will likely result in SQL Server being unable to use an
> index on the ListingType column, thus resulting in less that optimal
> performance. To get ideal performance, we'd need to if the code:
>
> if @ListingType = 0 BEGIN
>
> select listingId, listingName from Property
> WHERE Status = 1
>
> END ELSE BEGIN
>
> select listingId, listingName from Property
> WHERE ListingType = @ListingType AND Status = 1
>
> END
>
> Now you are getting somewhere...except that it'll get impossible to
> maintain with the addition of just a few more search parameters (and query
> plan caching will increasingly become less efficient).
>
> We could use execute:
>
> declare @sql varchar(1024)
> set @sql = 'select listingId, listingName from Property WHERE Status = 1'
> if @ListingType = 0 BEGIN
> SET @sql = @sql + ' AND ListingType = ' + @ListingTypeId
> END
> exec(@sql)
>
> Looky that, a stored procedure and a HUGE potential for SQL
> injection...egads! (yes, the above sproc won't actually work but if
> @ListingTypeID was a varchar (say comma-separated values) it would).
>
> This simple, and common, example not only illustrates that stored
> procedures aren't necessarily faster and just vulnerable, but that they
> can also be harder to maintain.
>
> In short, the correct answer is "it depends" and hopefully as you learn
> more you'll learn to use the right solution at the right time
>
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
> "JIM.H." <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello,
>> Is there any difference to between SLQ string in the code and call
>> execute
>> query and call a stored procedure and execute the query that way
>> concerning
>> speed, effectiveness, reliability, .?
>> Thanks,
>> Jim.
>>

>
>



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      05-27-2005
Teehee...I made my own blanket statement but atleast I had the word
"typically" in there.

But I can explain what I was saying in more detail. I was specifically
refering to cases where some search parameters are optional, which in my
experience is almost always a requirement. Without using dynamic sql (I
typically use sp_executesql instead of building it in the DAL but that's
just implementation), you are forced to either write a lot of IF/ELSE (which
is totally unacceptable for maintenance), or you write statements like AND
(@SomeParameter IS NULL OR SomeColumn LIKE @SomeParameter)

Using the above approach is sweet because it lets you avoid huge if/elses
AND also avoids dynamic sql. However, SQL server couldn't take advantage of
any indexes on SomeColumn in the above case. Don't get me wrong, it isn't
necessarily a bad approach, simply one which might have an unknown
side-effects (indexes are largely ignored). With dynamic SQL you're query
would either contain the SomeColumn LIKE @SomeParameter or not (determined
at runtime)...it wouldn't check if @SomeParameter IS NULL and wouldn't need
to OR (which I believe is why an index can't be used).

I can't recommend this article enough if your specific question is about
searches:
http://www.sommarskog.se/dyn-search.html it's totally unbiased and
contains no blanket statements...it'll purely educate you and give you
information necessary to making the right choice.

Hope that cleared some of the muck I said..
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"PB" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I couldn't agree with you more about blanket statements... implying that
>proper research and understanding is in order. But I do have a question
>regarding your statement:
>
> << the most efficient search queries are typically written in one form or
> another of dynamic sql (otherwise SQL Server can't take advantage of
> indexes).>>
>
> I don't understand this. You seem to be saying that SQL Server can use
> indexes *only* when fed dynamic SQL. I don't think that's true. Can you
> explain further? I guess it's the "one form or another" part that I don't
> get. How many forms of dynamic SQL are there?
>
> Thanks.
>
>
>
> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
> wrote in message news:(E-Mail Removed)...
>>I have to strongly disagree with the two other respondants. The real
>>answer is that it totally depends. To say that stored procedures are "far
>>more secure" is extremely misleading. By simply using parameterized
>>values in your dynamic SQL you've achieved high security. Conversly, by
>>using execute in your stored procedure it's not at all secure.
>>
>> As far as performance, it really depends on what you are doing. That SQL
>> Server caches queries is a very misleading generalization. Complexe
>> queries are often uncacheable. Additionally, client-side (ASP.Net)
>> caching can belittle this minor advantage. What's worse, the most
>> efficient search queries are typically written in one form or another of
>> dynamic sql (otherwise SQL Server can't take advantage of indexes).
>>
>> Personally I'm a fan of stored procedures (big time), but blanket
>> statements are dangerous. Saying that stored procedures are secure
>> dangerously risks taking the responsability away from the developer.
>> Additionally, different solutions are better suited for different
>> scenarios. As such, only someone who doesn't subscribe to blanket
>> statements can make the right decision.
>>
>> Let's look at a simplified example:
>>
>> create procedure SearchProperty AS
>> @ListingType INT --pass 0 to search for all listing types
>>
>>
>> select listingId, listingName from Property
>> where (@listingType= 0 OR ListingType = @ListingType)
>> AND Status = 1
>>
>>
>> In the above example we need to allow 0 to be passed in to mean any
>> listing. This will likely result in SQL Server being unable to use an
>> index on the ListingType column, thus resulting in less that optimal
>> performance. To get ideal performance, we'd need to if the code:
>>
>> if @ListingType = 0 BEGIN
>>
>> select listingId, listingName from Property
>> WHERE Status = 1
>>
>> END ELSE BEGIN
>>
>> select listingId, listingName from Property
>> WHERE ListingType = @ListingType AND Status = 1
>>
>> END
>>
>> Now you are getting somewhere...except that it'll get impossible to
>> maintain with the addition of just a few more search parameters (and
>> query plan caching will increasingly become less efficient).
>>
>> We could use execute:
>>
>> declare @sql varchar(1024)
>> set @sql = 'select listingId, listingName from Property WHERE Status = 1'
>> if @ListingType = 0 BEGIN
>> SET @sql = @sql + ' AND ListingType = ' + @ListingTypeId
>> END
>> exec(@sql)
>>
>> Looky that, a stored procedure and a HUGE potential for SQL
>> injection...egads! (yes, the above sproc won't actually work but if
>> @ListingTypeID was a varchar (say comma-separated values) it would).
>>
>> This simple, and common, example not only illustrates that stored
>> procedures aren't necessarily faster and just vulnerable, but that they
>> can also be harder to maintain.
>>
>> In short, the correct answer is "it depends" and hopefully as you learn
>> more you'll learn to use the right solution at the right time
>>
>> Karl
>> --
>> MY ASP.Net tutorials
>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>> annoying)
>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>> come!)
>> "JIM.H." <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Hello,
>>> Is there any difference to between SLQ string in the code and call
>>> execute
>>> query and call a stored procedure and execute the query that way
>>> concerning
>>> speed, effectiveness, reliability, .?
>>> Thanks,
>>> Jim.
>>>

>>
>>

>
>



 
Reply With Quote
 
Robbe Morris [C# MVP]
Guest
Posts: n/a
 
      05-27-2005
He said "direct execute sql".

I took this to be executing an entirely dynamically
create sql string. Use of parameters would not
help in this scenario. You are correct in your
assessment when parameterized command objects
are used.

I also said that SQL Server's caching "reduced"
the overall performance gain as a general statement which
is true.

And, I did say the procedures were still faster
in most cases.


--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net



"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:(E-Mail Removed)...
>I have to strongly disagree with the two other respondants. The real
>answer is that it totally depends. To say that stored procedures are "far
>more secure" is extremely misleading. By simply using parameterized values
>in your dynamic SQL you've achieved high security. Conversly, by using
>execute in your stored procedure it's not at all secure.
>
> As far as performance, it really depends on what you are doing. That SQL
> Server caches queries is a very misleading generalization. Complexe
> queries are often uncacheable. Additionally, client-side (ASP.Net) caching
> can belittle this minor advantage. What's worse, the most efficient
> search queries are typically written in one form or another of dynamic sql
> (otherwise SQL Server can't take advantage of indexes).
>
> Personally I'm a fan of stored procedures (big time), but blanket
> statements are dangerous. Saying that stored procedures are secure
> dangerously risks taking the responsability away from the developer.
> Additionally, different solutions are better suited for different
> scenarios. As such, only someone who doesn't subscribe to blanket
> statements can make the right decision.
>
> Let's look at a simplified example:
>
> create procedure SearchProperty AS
> @ListingType INT --pass 0 to search for all listing types
>
>
> select listingId, listingName from Property
> where (@listingType= 0 OR ListingType = @ListingType)
> AND Status = 1
>
>
> In the above example we need to allow 0 to be passed in to mean any
> listing. This will likely result in SQL Server being unable to use an
> index on the ListingType column, thus resulting in less that optimal
> performance. To get ideal performance, we'd need to if the code:
>
> if @ListingType = 0 BEGIN
>
> select listingId, listingName from Property
> WHERE Status = 1
>
> END ELSE BEGIN
>
> select listingId, listingName from Property
> WHERE ListingType = @ListingType AND Status = 1
>
> END
>
> Now you are getting somewhere...except that it'll get impossible to
> maintain with the addition of just a few more search parameters (and query
> plan caching will increasingly become less efficient).
>
> We could use execute:
>
> declare @sql varchar(1024)
> set @sql = 'select listingId, listingName from Property WHERE Status = 1'
> if @ListingType = 0 BEGIN
> SET @sql = @sql + ' AND ListingType = ' + @ListingTypeId
> END
> exec(@sql)
>
> Looky that, a stored procedure and a HUGE potential for SQL
> injection...egads! (yes, the above sproc won't actually work but if
> @ListingTypeID was a varchar (say comma-separated values) it would).
>
> This simple, and common, example not only illustrates that stored
> procedures aren't necessarily faster and just vulnerable, but that they
> can also be harder to maintain.
>
> In short, the correct answer is "it depends" and hopefully as you learn
> more you'll learn to use the right solution at the right time
>
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
> "JIM.H." <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello,
>> Is there any difference to between SLQ string in the code and call
>> execute
>> query and call a stored procedure and execute the query that way
>> concerning
>> speed, effectiveness, reliability, .?
>> Thanks,
>> Jim.
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'Procedure or function <stored procedure name> has too many arguments specified',,,ARGH! Mike P ASP .Net 0 06-19-2006 01:19 PM
Build and execute stored procedure 'on the fly' iksrazal@terra.com.br Java 1 10-17-2005 09:17 PM
stored procedure takes too much time to execute nicholas ASP .Net 7 08-03-2005 07:20 PM
Help with Code to Execute a Stored Procedure Jeff Thur ASP .Net 2 02-07-2005 08:58 PM
How can I execute a stored procedure in .net? FireListen ASP .Net 1 06-27-2003 01:39 PM



Advertisments