all groups > sql server programming > december 2004 >
You're in the

sql server programming

group:

queries Vs Stored Procs


queries Vs Stored Procs Nickl
12/31/2004 9:43:02 PM
sql server programming:
I often use a chain of queries to produce a final output. Eg to prepare
aggregate data, or to set up generic queries which are then used in many
other queries to simplify things.
My question is this; how much advantage performance wise will I get if I
make those sub-queries stored procedures and then call them from other stored
procedures? ie, to have a chain of Procs rather than a chain of queries.

Do I get any benefit by starting Procs earlier in the chain (if there is
one), or is it ok to have one proc at the end that references the the next
query, which references the next query etc.
Any help appreciated
Cheers
Re: queries Vs Stored Procs Bob Barrows [MVP]
1/1/2005 8:46:58 AM
[quoted text, click to view]

I would say you are looking at a major performance improvement. Marshalling
data across processes is expensive and time-consuming. Each trip to the
database from your client application incurs that penalty, so it only makes
sense to minimize the number of trips to the database whenever possible.
Also, stored procedures are pre-parsed and compiled, so that is another
operation that is avoided when you switch to using stored procedures.

[quoted text, click to view]

You need to get yourself out of the paradigm of one query/one procedure. You
can put multiple queries into a single stored procedures. This is called
"encapsulation". There is no reason to have a chain of stored procedures
unless some of the queries need to be used by other processes.
[quoted text, click to view]

Again, a single procedure can execute and use the results from several
queries, so tere is no reason to think about creating more than one
procedure. Again, minimizing the trips out of process can only have a
beneficial impact on performance.

Look at this:
http://www.aspfaq.com/show.asp?id=2201

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: queries Vs Stored Procs Jim Tottser
1/1/2005 10:20:52 AM
On Sat, 1 Jan 2005 08:46:58 -0500, "Bob Barrows [MVP]"
[quoted text, click to view]

Bob is exactly right. However, there is an exeption to the many
queries in one stored procedure rule. Any time you need to use a
conditional branching statement to execute different queries you
should put each branch into its own stored procedures. The reason for
this is that the compiler will choose an execution path down only one
branch. Using seperate stored procedures allows SQL Server to create
seperate execution plans for each branch.

Re: queries Vs Stored Procs Bob Barrows [MVP]
1/1/2005 10:30:12 AM
[quoted text, click to view]

Good point.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: queries Vs Stored Procs Erland Sommarskog
1/1/2005 4:00:57 PM
Jim Tottser (jtottser@ptd.net) writes:
[quoted text, click to view]

Nah, this is not the story. As Tibor says, the optimizer creates plans
for all parts of the procedure.

However, the optimizer is into something known as parameter sniffing, which
means that it looks at the values for the input parameters when it builds
the plan the first time. It could that one set of values is bad for a
branch which is not called that first time, but that plan will remain to
the second time when that branch is invoked. One solution to this problem
is to put that branch in a sub-procedure. But this is far from always the
case.

Another reason to break down the procedure in sub-procedures can be
that a very long procedure takes a long time to compile and recompile,
and if recompilations for one reason or another are difficult to avoid,
breaking out code that does not benefit from the recompile may improve
performance.


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

Books Online for SQL Server SP3 at
Re: queries Vs Stored Procs Tibor Karaszi
1/1/2005 5:04:55 PM
[quoted text, click to view]

Just a slight elaboration, in case somebody is interested:

The optimizer doesn't really know or care about the branching in the code. The optimizer sees all
DML statements and also knows the values of the procedure parameters. Based on that, it optimizes
all DML statements, whether or not they later will be executed.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/


[quoted text, click to view]

Re: queries Vs Stored Procs Nickl
1/1/2005 6:55:01 PM
Thank you all for your prompt and erudite responses. I may yet come to love
Stored Procedures.
CHeers
AddThis Social Bookmark Button