We are having a problem in our test and dev environments with a function that runs quite slowly at times when called from an .Net Application. When we call this function directly from management studio it works fine.
Here are the differences when they are profiled:
From the Application:
CPU: 906
Reads: 61853
Writes: 0
Duration: 926
From SSMS:
CPU: 15
Reads: 11243
Writes: 0
Duration: 31
Now we have determined that when we recompile the function the performance returns to what we are expecting and the performance profile when run from the application matches that of what we get when we run it from SSMS. It will start slowing down again at what appear to random intervals.
We have not seen this in prod but they may be in part because everything is recompiled there on a weekly basis.
So what might cause this sort of behavior?
Edit -
We finally were able to tackle this and restructuring the varables to deal with parameter sniffing appears to have done the trick...a snippet of what we did here: Thanks for your help.
-- create set of local variables for input parameters - this is to help performance - vis a vis "parameter sniffing"
declare @dtDate_Local datetime
,@vcPriceType_Local varchar(10)
,@iTradingStrategyID_Local int
,@iAccountID_Local int
,@vcSymbol_Local varchar(10)
,@vcTradeSymbol_Local varchar(10)
,@iDerivativeSymbolID_Local int
,@bExcludeZeroPriceTrades_Local bit
declare @dtMaxAggregatedDate smalldatetime
,@iSymbolID int
,@iDerivativePriceTypeID int
select @dtDate_Local = @dtDate
,@vcPriceType_Local = @vcPriceType
,@iTradingStrategyID_Local = @iTradingStrategyID
,@iAccountID_Local = @iAccountID
,@vcSymbol_Local = @vcSymbol
,@vcTradeSymbol_Local = @vcTradeSymbol
,@iDerivativeSymbolID_Local = @iDerivativeSymbolID
,@bExcludeZeroPriceTrades_Local = @bExcludeZeroPriceTrades