32
Vote

Enable a mechanism to provide query hints

description

Neither the Linq extensions to C# nor Entity Framework provides any way to pass a query hint to the underlying data provider. For database programming, this is a fundamental shortcoming of the Microsoft development stack. There is no way, on a per-query basis to pass a simple locking hint (NOLOCK, HOLDLOCK, etc.).
The only workaround is to use TransactionScope objects to fence specific code boundaries, which essentially executes SET ISOLATION statements on the connection. But there is no way, for example, to specify READPAST or FORCESEEK query options, which are essential to managing lock contention in high-volume systems. Obviously, this also means there is no way to provide index hints.

Short Term: Add a monadic With() function to Entity Framework to transmit query hints to Sql Server:
                            (from e in Db.ExtCompany.With(QueryHint.ReadPast) where e.Company == companyId select e).Any();

                                       Longer Term: Add a “with” keyword to C# for use in Linq to supply a query-provider-specific hint to the underlying provider:
                            (from e in Db.ExtCompany with ReadPast, ForceSeek where e.Company == companyId select e).Any();

file attachments

comments

davidlars99 wrote Feb 7, 2013 at 3:12 PM

It would be fantastic to add support for query/table hints. Our team has to shy away from EF because of this limitation..

RoMiller wrote Feb 20, 2013 at 5:22 PM

EF Team Triage: We agree that this would be a good scenario to enable. Taking into account where we are in the EF6 release along with the size and the impact of this feature our team is not planning to implement it in EF6. Therefore, we are moving it to the Future release to reconsider in the next release.

ZBo wrote May 10, 2013 at 5:21 PM

There are two work-arounds.

1) Use TransactionScope objects to execute SET ISOLATION statements on the connection.

2) Create a view and apply the join hints to the FROM clause inside the view. Example ViewWrapperTest.SQL attached.

sporty81 wrote Jan 1 at 7:27 PM

This is a huge shortcoming for sure.

brebok wrote Tue at 1:02 AM

We are having an issue where SQL Server parameter sniffing is causing for inefficient query plans to be used to execute certain queries. It would be great to have an API in Entity Framework to specify OPTION (OPTIMIZE FOR(@param1 = <val_1>)) or OPTION(OPTIMIZE FOR UNKNOWN).

Without it, I am considering a TVF wrapper over a view with the OPTIMIZE statement or modifying the EF query so that it contains the filter value directly in the generated SQL query (that is not replaced with a SQL bind variable). The latter solution defeats the purpose and benefits of EF query compilation and SQL server query compilation.