Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Performance - Stored Procedure versus inline

$
0
0

I always assumed that putting code inside a Stored Procedure would result in faster execution that executing in-line with EXEC(). However to my surprise with a little test it does not appear to be the case.

Here is the query (purpose of the query is not important):

DECLARE @client_net_address NVARCHAR(128), @net_address NVARCHAR(128), @hostname NVARCHAR(128),  @text NVARCHAR(2000), @stmt_start INT, @stmt_end INT, @spid int;
set @spid = @@spid;
select @client_net_address = c.client_net_address,  
@net_address = p.net_address,  
@hostname = p.hostname,  
@text = e.text,  
@stmt_start = p.stmt_start,  
@stmt_end = p.stmt_end  
from sys.dm_exec_sql_text((select sql_handle from sys.sysprocesses where spid = @spid)) e,  
sys.dm_exec_connections c, sys.sysprocesses p where p.spid = @spid and c.session_id = @spid;

First I store the text to an NVARCHAR variable and execute inline, i.e.,

declare @exec nvarchar(1024);
set @exec = query text from aboveexec(@exec);

Then I create it as a Stored Procedure, MyProc and execute it like this:

declare @exec nvarchar(1024);
set @exec = 'exec MyProc';
exec(@exec);

Much to my surprise the inline was about 10-15% faster! I always thought that one of the benefits of Stored Procedures was that they were precompiled and so executed faster. Am I missing something?

Thanks.




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>