|  | Posted by Erland Sommarskog on 05/27/05 23:46 
(EggsAckley@Last.com) writes:> Can you (or anyone) suggest general guidelines for improving the
 > performance and/or efficiency of stored procedures under SqlServer
 > 2000? Again, I very much appreciate any guidance.
 
 The question is a bit open-ended. But here are some general points
 from the top of my head:
 
 o  Try as much as possible to avoid iterative solution, and use set-
 based solutions.
 
 o  When using temp tables, create them as the first executable statement
 in the procedure. (DECLARE @local is not an executable statement.) If
 you create temp tables in the middle of it all, you will get a
 recompile.
 
 o  Share your graces between temp tables and table variables. Sometimes
 ons is right, and sometimes the other. My general suggestion is that
 you start with a temp table, but if you find that you get performance
 problems because of recompiles, switch to temp tables. (Keep in mind
 that those recompiles can just as well be life-savers!)
 
 o  And while it's sometimes it's a good idea to keep a temp table/table
 variable for storage of intermediate results, it can also sometimes
 be more effecient with one big query from hell that does it all in
 one statement.
 
 o  Don't do this:
 CREATE PROCEDURE some_sp ... @startdate = NULL, .... AS
 IF @startdate IS NULL
 SELECT @startdate = convert(char(8), getdate(), 112)
 since SQL Server sniffs the parameter value, it will build query
 plans assuming that @startdate is NULL. It's better to copy to
 a local variable, of which SQL Server makes no assumption at all
 about the value. Even more effecient is to move processing to an
 inner procedure once all defaults have been filled in.
 
 o  Microsoft recommends that you always use two-part notation, for
 instance "SELECT ... FROM dbl.tbl" and not "SELECT ... FROM tbl",
 and claims this is more effecient. I claim that if the procedure
 is owned by dbo, there should be no difference, and if there is,
 that's a bug. One of these days, I will have to benchmark it.
 Anyway, since MS recommends it, I thought I should mention it.
 
 If you have particular issues you want to dicsuss, you are welcome.
 
 --
 Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
 
 Books Online for SQL Server SP3 at
 http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
 [Back to original message] |