I\'m calling a Table-Valued Function from entity framework and need to be able to add the option (recompile)
to it because the execution plan it picks up is not
Are there other callers of fDE_myquery
outside of your specific usage? And how often does this get called? The issue is not that your SELECT * FROM dbo.fDE_myquery();
is getting a sub-optimal plan, it is that one or more queries inside of fDE_myquery
is getting a sub-optimal plan. Hence, you could just add the OPTION(RECOMPILE)
to one or more queries inside that TVF.
If this TVF is called a lot then this would have a negative impact on performance. That is why I asked about other uses of this TVF: if this is the only, or by far the main, use of this TVF, then it might be well worth it if the bad plans are being picked up frequently.
But if there are several other callers of this TVF that are not experiencing an issue, then putting the RECOMPILE
in the TVF might not be the way to go. Although, in that case you could create a wrapper TVF that encapsulates the SELECT * FROM dbo.fDE_myquery() OPTION (RECOMPILE);
. This would appear to be a more flexible solution :). It would have to be a Multistatment TVF instead of the typically better Inline TVF as I just tried it and the Inline TVF does not seem to appreciate the OPTION
clause, but the Multistatement TVF was fine with it.
EDIT:
Or, if you want to handle this purely in EF, you could simply issue a recompile request with a single line of code:
ctx.context.ExecuteStoreCommand("EXEC sp_recompile 'dbo.fDE_myquery';");
And then do your:
var query = from f in ctx.fDE_myQuery(aBool, anotherBool, StartDate,
EndDate, someInt, moreBool)
select f;