I have a webjob project running on .NET Framework with EF Core 3.1. The webjob processes messages from an Azure Service Bus and saves them into an Azure SQL Database.
The problem I have is that the Azure SQL Database generates really bad query plans for the query that EF Core generated. With the generated query plan the execution time is 1-2 minutes. However when I use OPTION (OPTIMIZE FOR UNKNOWN) the execution time drops down to 0.01 - 0.02 minutes.
So now I want to implement the OPTION (OPTIMIZE FOR UNKNOWN) in EF Core. I have found that they added a DbCommandInterceptor in EF Core 3.1 where can you append things to your query: MSDOCS
public class HintCommandInterceptor : DbCommandInterceptor
{
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result)
{
// Manipulate the command text, etc. here...
command.CommandText += " OPTION (OPTIMIZE FOR UNKNOWN)";
return result;
}
}
But it seems like this interceptor will run on every query and I only want it for a specific query. I could implement a seperate DbContext for this interceptor but that doesn't seem like a solid solution. Does anyone have an idea how I could implement this in a correct way?