Question

What kinds of TSQL select statements (i.e. those starting from select) get benefited from compiling and what the advantages is provided by? By compiling I mean hosting select statements inside stored procedures.

I'm aware of other than performance advantages of stored procs (encryption, separation of concerns etc.), but I'm only interested in a performance aspect here.

Here's an example:

select t1.f1, t2.f2 from t1 inner join t2 on t1.pk = t2.fk

does/will the above sql run faster when it's factored as a stored proc rather than a command text?

Was it helpful?

Solution

No, SQL Server optimizes and caches query plans based on the query text at the statement level, so generally, that statement will optimize the same whether in a stored procedure or not.

Now, there are other factors that can affect how that statement will be cached and optimized, and how a stored procedure might exhibit different performance characteristics (and use a different plan) than the same query outside of stored procedure. For example: SET settings. It is beyond the scope of your core question, but Erland's article, Slow in the Application, Fast in SSMS? Understanding Performance Mysteries is a fantastic and enlightening read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top