Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > sqlserver server > february 2006 >

sqlserver server : CLR SP Execution


Alistair Harrison
2/28/2006 4:28:22 AM
Hello,

I am currently working on an application which calls some CLR stored
procedures in SQL Server 2005. The first time one of the CLR stored
procedures (it doesn't matter which one) is called it takes a lot longer to
execute. After the first CLR stored procedure has been executed the CLR
stored procedures all execute in a reasonable amount of time.

I have been unable to find any articles explaining the process that occurs
when a CLR stored procedure is called. I am currently assuming the initial
delay is related to the CLR loading the DLL? Is the CLR loaded with SQL
Server or only when needed?

If anyone can point me in the direction of an article to aid my
understanding or can explain the process I would be grateful.

Thanks in advance.

Dan Guzman
3/3/2006 12:00:00 AM
How long of a delay do you experience? Do you get the delay with a trivial
proc like the one below? I get a sub-second response when I execute this
after a fresh SQL Server restart.

public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void MyProc()
{
Microsoft.SqlServer.Server.SqlContext.Pipe.Send("Test message");
}
};

I'm no expert on SQL CLR internals but I can't think of any overhead beyond
the usual object-specific overhead.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Alistair Harrison" <AlistairHarrison@discussions.microsoft.com> wrote in
message news:71A086F6-487F-46FD-B501-A7844F6936E5@microsoft.com...
[quoted text, click to view]

Alistair Harrison
3/6/2006 5:40:32 AM
Dan,

Thanks for your response.

The following are examples of execution times recorded by the Client
Statistics when the query is executed:

Initial execution of my original sp: 1600 to 1700 ms
Subsequent execution: <100 ms

Initial execution of your test sp: 700 to 800 ms
Subsequent execution: <50 ms

Another point I noticed was that if the test sp is executed first followed
by my original sp following a restart the original sp takes around 1200 ms to
execute. This is less than the 1600 to 1700 ms it seems to take when
executing the sp first but is still much greater than the subsequent
execution time.

It might be worth mentioning that my original clr sp executes a couple of
tsql sps using a context connection and then returns some xml as an output
parameter.

Thanks,

Alistair


[quoted text, click to view]
Dan Guzman
3/6/2006 8:41:21 PM
It's likely that subsequent executions are faster simply a result of data
caching. I'd expect execution times to average about 1200ms if you run DBCC
DROPCLEANBUFFERS before each execution. For performance testing, I usually
execute DBCC DROPCLEANBUFFERS and DBCC FREEPROCCACHE before each test.

I don't think the sub-second delay the first time the proc is run is
anything to be concerned about.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Alistair Harrison" <AlistairHarrison@discussions.microsoft.com> wrote in
message news:6A2A987D-E008-440F-867E-0DE4D6A3AA85@microsoft.com...
[quoted text, click to view]

Kent Tegels
3/7/2006 12:00:00 AM
Hello Alistair,

It might be worth remembering that the Assembly (and its dependent assemblies)
still needs to be fetched, verified, JITTed and the app domain created before
the CLR code runs in many cases. That could be why you have a slower initial
startup.

Thank you,
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/

Alistair Harrison
3/7/2006 3:43:41 AM
Dan

Thanks again for your response.

I have retested the two stored procedures running DROPCLEANBUFFERS and
FREEPROCCACHE before each execution. The execution times increase slightly
but are still consistently below 300ms.

I suppose as the longer execution only seems to occur following a restart it
is not too much of a worry just intriguing as to why it seems to occur.

Alistair


[quoted text, click to view]
AddThis Social Bookmark Button