Library tutorials & articles
To SP or not to SP in SQL Server
- Introduction
- The Security Argument
- The Performance Argument
- The Maintenance Argument
- The cross-platform tie-in argument
The Performance Argument
The next argument often raised by SP advocates is that SPs offer better performance. Rob says:
When stored procedures are used SQL Server can cache the ‘execution plan' that it uses to execute the SQL vs. having to recalculate the execution plan on each request – would you recompile your business logic class on each request? …probably not.
Note that Rob initially used the word “pre-compile or cache” when describing SPs, which he has edited out of the post, as noted by Rob at the bottom of the entry.
The counter to this is provided by Frans, who quotes SQL Server Books On-Line, as I do here:
SQL Server 2000 and SQL Server version 7.0 incorporate a number of changes to statement processing that extend many of the performance benefits of stored procedures to all SQL statements. SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. A stored procedure is compiled at execution time, like any other Transact-SQL statement. SQL Server 2000 and SQL Server 7.0 retain execution plans for all SQL statements in the procedure cache, not just stored procedure execution plans. The database engine uses an efficient algorithm for comparing new Transact-SQL statements with the Transact-SQL statements of existing execution plans. If the database engine determines that a new Transact-SQL statement matches the Transact-SQL statement of an existing execution plan, it reuses the plan. This reduces the relative performance benefit of precompiling stored procedures by extending execution plan reuse to all SQL statements.
The important thing to note about this is that there is no “compiling” of SPs in the traditional sense. In some environments (Oracle, I am told, and in a number of other databases as well), SPs really are converted to a different programming language (such as C or C++) and compiled to native code. This is not the case in SQL Server, for SPs or ad-hoc SQL. SPs have a slight advantage in that the execution plan can be cached and found a bit more efficiently.
Are there any other performance advantages to SPs? In a number of reasonably rigorous tests, I initially did not see any significant differences in performance. I took a SELECT statement that was pulling from a table using a random ID (generated using the RAND() function in SQL Server) and for a single SELECT, there was no difference between an ad-hoc SELECT statement and an SP. I expected this, so I duplicated the statement a number of times (100 times, in fact) and compared the execution times, and again I saw no difference. Even when connecting to a remote server over the Internet, I saw no difference.
After more thought, I realized that in my stored procedures I am often doing complex work, building up a temporary table and SELECTing the result of the stored procedure. Rather than doing the individual SELECT statements, I next tried a test in which I created a temporary table and ran 100 INSERT statements in which I used a SELECT for the values to insert. Finally, I ran a SELECT against the temporary table, and then dropped the temporary table. In this example, I did see a reasonable performance advantage in using an SP to perform the task, on the order of 10:1 when operating on a SQL Server both across the LAN and over the Internet.
So what are the performance benefits of using SPs? If you have complex work to do with the data, it makes good sense to work as close to the data as possible. If you are doing complicated operations on data that cannot be expressed easily using standard SQL set-based operations, it is possible that SPs will be quicker than looping through results on the client side and doing the calculations. Note that the issue of when to use or not use SPs will change when SQL Server 2005 emerges out of beta, since you will be able to use VB.NET and C# to create stored procedures that are genuinely compiled.
Performance: Some SP advantage when complex operations are involved.
Related articles
Related discussion
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
Research topic in software
by reachsangeethamathew (0 replies)
-
Error Msg Description ?
by morizan (0 replies)
-
How to Change Default exe Icon in C#.net Windows Application
by sonali.terse (2 replies)
-
Job Opening - New Braunfels, Tx for C# Programmer
by kirkkelly (3 replies)
Related podcasts
-
Stack Overflow: Podcast #28
This is the twenty-eighth episode of the StackOverflow podcast, where Joel and Jeff discuss Windows Azure, SQL Server 2008 full text search, Bayesian filtering, porn detection, and project management — among other things. Jeff met the inestimable Joey DeVilla aka Accordion Guy...
Events coming up
-
Nov
19
SQLBits V
Newport, United Kingdom
SQLBits is Europe's largest SQL Server conference, and SQLBits V will be the biggest and best yet. On November 19th we are holding a day of pre-conference seminars; on November 20th we have a pay-to-attend day of SQL Server 2008 and R2 content; and on Saturday November 21st we have our usual free community conference.
Actually there is a much easier way to implement row-based security using joins to a security table that has at least values for the SecurityPrinciple and the KeyValue from the table. A simple join to the table, encapsulated in a view and you can use normal access outside of here (generally this is better done on a domain value so it is not terribly tedious, but either way it is done naturally using SQL) It is what one might do on the front end using programmed logic, but far easier to administrate (since security changes!)
This is not a wild card, it is a weird case of poor management. If a programmer can generate code "on the fly" using client code, it is ignorant to prevent them doing the same thing on the server in code. Obviously there are some major differences in coding procedures versus functional coding but (and here is the great thing) if the proc takes the right parms, and it returns the right data, it can be optimized. Obviously don't wait until your newbie programmer has written 500 procs using cursors, but it doesn't have to be an us vs them mentality.
This is very true. Either method is no better or worse for injection attacks. It just takes some intelligence to deal with it. Check out http://www.sommarskog.se/dynamic_sql.html for more information
The compiling issue is a non-starter. The cost of compilation is so minimal as to not be a real concern unless you are executing thousands of times a second. However, the cost of building a query plan is the real cost. The problem with ad hoc access is that unless you are looking at a single table in your query, the plan will not be parameterized. For example run:
set showplan_text on
go
select customers.customerid, orders.freight
from customers
join orders
on customers.customerid = orders.customerid
where customers.customerid = 'vinet'
|--Nested Loops(Inner Join)
|--Clustered Index Seek(OBJECT:([Northwind].[dbo].[Customers].[PK_Customers]), SEEK:([Customers].[CustomerID]='vinet') ORDERED FORWARD)
|--Bookmark Lookup(BOOKMARK:([Bmk1002]), OBJECT:([Northwind].[dbo].[Orders]))
|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[CustomerID]), SEEK:([Orders].[CustomerID]='vinet') ORDERED FORWARD)
In the plan you will see the literal value 'vinet' in there. This means that this plan is only good if some other user happens to run the exact same query (including spacing and capitalization) with the same literal. Far better to use a procedure because you control the parameter.
Fundamentally this is true. When you need to make a change to a query and it requires a complete recompile of the client code and hence a heavy bit of testing to redeploy to 100 customers (or to 100 users in your organization) it becomes more of a nightmare than it needs to be.
I think you are pretty much right on, though you do get security benefits if you follow through on the entire plan for building the application right, there is a benefit. If you use a proxy account to do security, you are right, the security benefits are far less. Performance is greatly enhanced, but not to a level that you will notice for most applications with smaller numbers of users. As concurrency needs rise however, stored procedures are of great benefit (though SQL Server does a dandy job of assisting the architect who doesnt care.
You are more tied to SQL Server in some ways, but less in others. I am not an expert in any other dialects, but the king of the use standard SQL or you are stupid clearly favors using stored procedures (newsgroup thread: (http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_frm/thread/41b94165a9d7f1ca/840120292a4a1168?lnk=st&q=joe+celko+stored+procedures&rnum=3#840120292a4a1168))
For my original rebuttal to the Bouma article: (http://spaces.msn.com/members/drsql/Blog/cns!1pqLLlQgw9hZKLV1K4eLatKw!273.entry)
Now that argument about maintainability is reduced down to size. It goes: "you can make a lot of changes that break a lot of things - which might be hard to find in the code... blah blah blah." - As noted in the article, you might have to do some code updates and a rebuild anyway - depending on how extensive the DB changes are - even when using straight stored procs.
All the developer has to do is re-run the tool (It connects to the updated DB and updates the objects based on the new schema). Then he does a quick rebuild.
Notes:
1. If changes were made to the DB that have nothing to do with the code, then this rebuild is not necessary.
2. Sometimes satelite DB changes require a code rebuild - usually because the developer grabbed everything from the DB to build the objects, but then didn't use all the objects that were created. The rebuild fixes this automatically, but it's better to tell your tool to ignore those entities (i.e. tables) that it doesn't need in the first place.
3. If stuff in the DB that's being used by the app change - and the objects now fail to build, then since you are using objects and not SQL strings, the build should fail exactly where it needs to be fixed. This kind of build failure speeds up the refactoring process - which would have to occur anyway for the code to take advantage of the "DB enhancements"
Very nice article - and honest! You might want to address the use of these kinds of tools though...
1. You've proven that it takes a little more know-how to write code that prevents sql injection - as noted in the article
2. You've added a bunch of lines of code (which isn't optimised) to refute a simple example that the article was trying to convey.
Thanks for the tutorial, but that wasn't the point being made... It was simply that this is how a developer can really screw up... and all the while thinking "I'm safe, because I'm 'using' a stored procedure"
Now to your point about using SP's only if you are sticking with MSSQL, I think that's a misconception too. If you design your application with the proper separation, then your Data Access Layer should be an abstract pluggable provider. That way, if you want to use ORM on ORACLE instead of SP's along with the SqlClient class then you just write a different provider and your app adapts.
Of course there is overhead in creating the provider model for your DAL, so I would only suggest this there is a chance your app will run under something else.
And the more I think about it, the more I am convinced that changes can also be handled at the DAL level. I always write my DAL as a separate assembly. I always construct the methods to return void, a DataSet or a single native type. Therefore, as long as your signatures remain simple and consistent and you don't return custom entities or SqlClient based types, then you can write a different DAL that accesses your data however you want.
I accept that this is susceptible for the SQL injection attack.
But you have to check the text box string before passing to the stored procedure.
It means that, make sure that there is no illegal character in the text box.
When a stored procedure is called directly from the code on the web server, it DOES prevent SQL injection attacks. When you call a command that uses an EXECUTE statement, you might as well be performing an ad-hoc query. Stored procedures are not meant to be called this way from the code on your web server. Call the stored procedure directly.
Here are some examples that show how SPs can prevent SQL injection attacks.
For the following examples, let's make a few assumptions:
1. The findtitle SP contains the SQL statement SELECT * FROM books WHERE title = @title.
2. A hacker entered 'DROP TABLE books into the textbox.
Your previous example:
strsql = "EXECUTE findtitle '" & textboxtitle.text & "'"
objCmd = New SqlCommand(strSQL, objConn)
In this example, the code would evaluate this way:
strsql = "EXECUTE findtitle ''DROP TABLE books"
Here, the code would execute the SP (which would find no titles equal to an empty string), and then would drop the table books. NOT GOOD.
My suggested way of calling the stored procedure:
string title = String.Empty;
SqlConnection conn = new SqlConnection("Your connection string goes here");
//Calls the stored procedure
SqlCommand cmd = new SqlCommand("findtitle", conn);
//The second and third parameters within the SqlParameter constructor will be dependent on the parameter within the stored procedure, the values supplied below are for example purposes
SqlParameter param = new SqlParameter("findtitle", SqlDbType.VarChar, 50, ParameterDirection.Input, false, ((System.Byte)0), ((System.Byte)0), null, DataRowVersion.Current, textboxtitle.text);
cmd.Parameters.Add(param);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
try
{
while(dr.Read())
{
title = dr["title"].ToString();
}
}
catch (Exception)
{
//Perform error code here
}
finally
{
//Close data reader. Connection will be closed simultaneously because of execute reader parameter
if(dr != null)
{
dr.Close();
}
}
//Write the retrieved value to a label on the form.
lblTitle.Text = title;
In my example above, by calling the stored procedure directly and passing the value of the textbox as a SqlParameter object, the potential SQL injection attack is eliminated. This is because the quote and following SQL statement are viewed as a parameter value instead of T-SQL. The SP would be evaluated this way:
SELECT * FROM books WHERE title = "'DROP TABLE books"
Because there is no title with the value 'DROP TABLE books, you would receive an empty set. The single quote in this example is included in the value to search for. In this example, no hacker code is executed and all tables are intact.
In the previous example, the single quote is evaluated as code, instead of a value, and completes the SELECT SQL statement. Once that statement is completed, the hacker can add any SQL code they want to be executed. In the previous example, the books table is dropped.
So, as you can see from the examples above, SP can prevent SQL injection attacks if executed the way they were intended.
This thread is for discussions of To SP or not to SP in SQL Server.