You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An example from #6649 is being able to append OPTION (MAXRECURSION 2000) to the end of a query.
Reacted by Jannik Buschke, Greg Smulko, Daniel Genezini, Anastasiia, Julius R Friedman, Sean Lynch, Jeff Moser, A.J. Ruckman, keatkeat87, Diego Bonura and 111 more
While the idea of integrating arbitrary provider-specific hints in queries seems useful, treating this as some raw SQL to be tacked at the end of the query may be too limited. A specific provider's SQL dialect may require the additional SQL to be integrated somewhere in the middle, etc...
Some arbitrary annotations on the query itself which would be picked up by the provider-specific query SQL generator may cover more cases.
Note from triage: we need to investigate what commonality exists here across relational database systems. For example, where can hints be attached? Is there non-SQL Server value in a query-hint only (i.e. always at the end of the query) solution?
I'm not sure how many things it has which can be tacked on without impacting the shape of the results (in which case they'd be beyond the scope of this issue). Some ideas that come to mind:
ORDER BY ... NULLS { FIRST | LAST }, impacting the sort order.
Locking the selected rows (FOR UPDATE). Users would then be able to do stuff like issue updates in raw SQL, keeping the rows locked until the end of the transaction. It's nice to be able to express the query (i.e. set of rows to lock) with EF Core LINQ.
Seems like ideally providers would be able to define an extension method on the IQueryable (like AsNoTracking()), which would be picked up by the SQL generator.
Reacted by Diego Vega, Fabio Silva, Ethan Resnick, Roman Bezmen, Timo van Zijll Langhout and Mauricio Scheffer
We need to evolve the API as new query hints are introduced (this is presumably a really rare thing)
It's most definitely not a rare thing. Take a look through the docs to see how many got added in every single version.
We also need to keep in mind the parameterization problem. Query hints (most likely) can't be parameterized in SQL; we can use e.g. [NotParameterized] to recompile the query for different hints
Yes in all cases you can't use a parameter, but the twist here is OPTIMIZE FOR ( @variable_name = constant ) which is going to be one of the most heavily used hints (tells the compiler to parameter-sniff against a fixed value). The left side must be parameterized (ie it must be the name of a parameter actually used in the query), the right side must not be parameterized and must be a literal constant. So it's going to take a good bit of gymnastics to make that work. I'm attempting to write this up, seems we need to completely separate out the variables and constants so funcletizer can work just on the variables.
Also note the docs are not perfectly accurate, there are missing hints, and some have incorrect syntax.
global query hints should already be manageable by extending the QueryCompilationContext
I can't see how to get that to work at the moment, because QuerySqlGenerator is a Singleton and has no access to QueryCompilationContext. All it gets is the expression tree, and I note that SelectExpression.Annotationsseems to have been made unusable for mutable Selects for some reason, so can't put it there either.
We need to evolve the API as new query hints are introduced (this is presumably a really rare thing)
It's most definitely not a rare thing. Take a look through the docs to see how many got added in every single version.
Fair enough!
Yes in all cases you can't use a parameter, but the twist here is OPTIMIZE FOR ( @variable_name = constant ) which is going to be one of the most heavily used hints
That's just a hint which specifies a (fixed!) parameter name, and is not parameterization; the latter would be the case if the parameter name in the query hint could itself be sent via a parameter, which is not the case. In other words, in your example above, @variable_name isn't a placeholder referencing a SqlParameter that contains the actual name of the parameter to be optimized as a constant.
I can't see how to get that to work at the moment, because QuerySqlGenerator is a Singleton and has no access to QueryCompilationContext. All it gets is the expression tree, and I note that SelectExpression.Annotations seems to have been made unusable for mutable Selects for some reason, so can't put it there either.
Yeah, a bit more gynmastics is indeed needed at the moment. SQL generation is indeed placed after the SQL cache, which makes things a bit more difficult to pipe through.
@ahdung that's more complicated than it seems, specifically around where the SQL goes (at the end? If so, how does one add table-specific hints in the middle?).
If you just want to add some raw SQL at the end of your query, you can already do that with a query interceptor.
@roji It's a good point. I would consider making table hints go on Set<T> as overload: Set<T>(IEnumerable<ISetHintInterceptor> hints), as well as the ability to apply default interceptors to a set via the DbModelBuilder, and a Set<T>().IgnoreHints() to disable default hints.
Across providers, such as Oracle and MSSQL, the SQL Syntax for table hints is different. In Oracle, a comment containing hints must come after the first keyword in an Oracle statement block. In MSSQL, table hints come after the table alias. However, one common idea they all share is that you can apply multiple hints, hence why the API should take an enumerable of hints. By abstracting the API with an ISetHintInterceptor interface, it allows EFCore consumers operating as ISVs to support multiple database system target deployments to their end-consumers. For example, the same table might need different hints on Oracle and MSSQL due to the differences in how locking patterns behave. For Postgres, it would likely go unused.
In 2018, I put together a summary of query hints across providers, actually in this thread! #6717 (comment)
@iankemp-CapitalOnTap an issue being old doesn't make it more high-priority; if anything, after all the years of its existed this issue has managed to gather far fewer user votes than much younger issues, indicating that it isn't necessarily very important to users.
Reacted by Mario Henrique Lopes Duarte, Martin Meixger, Ilia Burakov and Aurororora
Activity
roji commentedon Oct 7, 2016
While the idea of integrating arbitrary provider-specific hints in queries seems useful, treating this as some raw SQL to be tacked at the end of the query may be too limited. A specific provider's SQL dialect may require the additional SQL to be integrated somewhere in the middle, etc...
Some arbitrary annotations on the query itself which would be picked up by the provider-specific query SQL generator may cover more cases.
[-]Query Hints (append raw SQL, such as OPTIONs)[/-][+]Query Hints (raw SQL, such as OPTIONs)[/+]rowanmiller commentedon Oct 7, 2016
@roji yeah I wasn't intending to limit it to appending text to the end of the query - I removed the word "append" from the title 😄 .
roji commentedon Oct 8, 2016
Ok great :)
[-]Query Hints (raw SQL, such as OPTIONs)[/-][+]Query/Table Hints (raw SQL, such as OPTIONs)[/+]ajcvickers commentedon May 17, 2018
Note from triage: we need to investigate what commonality exists here across relational database systems. For example, where can hints be attached? Is there non-SQL Server value in a query-hint only (i.e. always at the end of the query) solution?
/cc @roji @caleblloyd @ErikEJ
roji commentedon May 18, 2018
Here's the PostgreSQL documentation page on querying.
I'm not sure how many things it has which can be tacked on without impacting the shape of the results (in which case they'd be beyond the scope of this issue). Some ideas that come to mind:
ORDER BY ... NULLS { FIRST | LAST }, impacting the sort order.FOR UPDATE). Users would then be able to do stuff like issue updates in raw SQL, keeping the rows locked until the end of the transaction. It's nice to be able to express the query (i.e. set of rows to lock) with EF Core LINQ.Seems like ideally providers would be able to define an extension method on the IQueryable (like
AsNoTracking()), which would be picked up by the SQL generator.118 remaining items
Charlieface commentedon Jul 13, 2025
@roji
I agree option #2 is IMO much better. But:
It's most definitely not a rare thing. Take a look through the docs to see how many got added in every single version.
Yes in all cases you can't use a parameter, but the twist here is
OPTIMIZE FOR ( @variable_name = constant )which is going to be one of the most heavily used hints (tells the compiler to parameter-sniff against a fixed value). The left side must be parameterized (ie it must be the name of a parameter actually used in the query), the right side must not be parameterized and must be a literal constant. So it's going to take a good bit of gymnastics to make that work. I'm attempting to write this up, seems we need to completely separate out the variables and constants so funcletizer can work just on the variables.Also note the docs are not perfectly accurate, there are missing hints, and some have incorrect syntax.
I can't see how to get that to work at the moment, because
QuerySqlGeneratoris a Singleton and has no access toQueryCompilationContext. All it gets is the expression tree, and I note thatSelectExpression.Annotationsseems to have been made unusable for mutable Selects for some reason, so can't put it there either.roji commentedon Jul 14, 2025
Fair enough!
That's just a hint which specifies a (fixed!) parameter name, and is not parameterization; the latter would be the case if the parameter name in the query hint could itself be sent via a parameter, which is not the case. In other words, in your example above,
@variable_nameisn't a placeholder referencing a SqlParameter that contains the actual name of the parameter to be optimized as a constant.Yeah, a bit more gynmastics is indeed needed at the moment. SQL generation is indeed placed after the SQL cache, which makes things a bit more difficult to pipe through.
ahdung commentedon Sep 10, 2025
please provide
WithRawSqlfirstroji commentedon Sep 10, 2025
@ahdung that's more complicated than it seems, specifically around where the SQL goes (at the end? If so, how does one add table-specific hints in the middle?).
If you just want to add some raw SQL at the end of your query, you can already do that with a query interceptor.
jzabroski commentedon Sep 10, 2025
@roji It's a good point. I would consider making table hints go on
Set<T>as overload:Set<T>(IEnumerable<ISetHintInterceptor> hints), as well as the ability to apply default interceptors to a set via the DbModelBuilder, and aSet<T>().IgnoreHints()to disable default hints.Across providers, such as Oracle and MSSQL, the SQL Syntax for table hints is different. In Oracle, a comment containing hints must come after the first keyword in an Oracle statement block. In MSSQL, table hints come after the table alias. However, one common idea they all share is that you can apply multiple hints, hence why the API should take an enumerable of hints. By abstracting the API with an ISetHintInterceptor interface, it allows EFCore consumers operating as ISVs to support multiple database system target deployments to their end-consumers. For example, the same table might need different hints on Oracle and MSSQL due to the differences in how locking patterns behave. For Postgres, it would likely go unused.
In 2018, I put together a summary of query hints across providers, actually in this thread! #6717 (comment)
iankemp-CapitalOnTap commentedon Oct 20, 2025
Only another 9 years and this issue will be old enough to have a pint.
roji commentedon Oct 20, 2025
@iankemp-CapitalOnTap an issue being old doesn't make it more high-priority; if anything, after all the years of its existed this issue has managed to gather far fewer user votes than much younger issues, indicating that it isn't necessarily very important to users.