John Lam 1997 — 2026

Dynamic SQL vs. sproc performance

Like many age-old arguments in other walks of life (boxers vs. briefs, Coke vs. Pepsi) we developers have the dynamic SQL vs. stored procedure debate. I realize that there has been lots of FUD over this issue, and that there has been a considerable amount of debate on this issue recently.

As a guy who dabbles in low-level bit twiddling stuff from time-to-time, the performance claims are quite interesting to me. The new (as of SQL Server 7.0) cached execution plan optimization in SQL Server looks to me a lot like JIT compilation. If this is, in fact, the case it seems to me that the only overhead that would be associated with dynamic SQL would be:

  1. The amount of bandwidth + time it takes to transmit the dynamic SQL text to the database.
  2. The amount of time it takes to calculate the hash of the dynamic SQL text to look up the cached execution plan.

I can imagine quite a few scenarios where the above overhead would disappear into the noise of the network roundtrip.

What upsets me are the folks who spout forth anecdotal arguments that claim stored procedures have “much better” performance than dynamic SQL. Now, I figure that folks in academia must have done studies on these types of claims. It seems that it’s all the rage in academia to do performance studies on stuff like TCP/IP stacks, so why not dynamic SQL vs. sprocs? Comments?

3 archived reader comments

Rick Childress (2003-12-22)

You could probably count the academic institutions that even acknowledge mssql exists on one hand (Oracle or MySQL on %nix, man. Who needs a GUI anyway?).

Bill Brown (2003-12-24)

The argument against dynamic SQL isn’t so much the performance issues (though many argue that performance is better, understandable given the faint distinction between compiled and JIT code) as the security implications.

If your database allows dynamic SQL, then that means that permissions have to be granted for select, delete, update, and insert to the SQL user. That opens up a whole lot of privilege to the web user and allows for SQL injection attacks if the server-side validation isn’t strict enough.

Stored procedures, on the other hand, require only execute stored procedure permission for the SQL user and thus limit the user to running only those stored procedures already defined in the database. SQL injection attacks can also be thwarted much easier on the stored procedure side because data scrubbing is generally performed there for database integrity (plus there’s more type safety available).

Another reason to use stored procedures is that they can be scheduled and batched at the database level. That’s a great feature since few of the server-side scripting languages (ColdFusion is the only one that I know of) have schedulers to run pages automatically.

There are problems with stored procedures, however. You need someone that knows T-SQL well enough to write all the ones that you’ll need. It’s harder to make the DBAs understand your requirements for dynamically constructing your queries than if you did them yourself. T-SQL isn’t as robust a language as most of the server-side scripting ones and it doesn’t have access to a lot of the page context stuff, so you end up doing a lot of preprocessing and sending of information to the stored procedure—further increasing its complexity.

Bill Brown (2004-01-02)

I forgot one other value in using stored procedures. They encapsulate the data model so that you don’t need to rebuild your dynamic queries if you choose to switch server-side languages. At my work, we use ColdFusion extensively but its days are numbered since we’re moving to ASP.NET eventually. Since both languages have facilities to call stored procedures, we only have to change the calls, we don’t have to rewrite the queries themselves. Such huge platform changes don’t happen often, but such encapsulation is nice when it does happen.