Skip to content

Query hints (raw SQL, such as OPTIONs) #6717

Description

@rowanmiller

An example from #6649 is being able to append OPTION (MAXRECURSION 2000) to the end of a query.

Activity

  1. added this to the Backlog milestone on Oct 7, 2016
  2. roji commented on Oct 7, 2016

    @roji
    Member

    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.

  3. changed the title [-]Query Hints (append raw SQL, such as OPTIONs)[/-] [+]Query Hints (raw SQL, such as OPTIONs)[/+] on Oct 7, 2016
  4. rowanmiller commented on Oct 7, 2016

    @rowanmiller
    ContributorAuthor

    @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 😄 .

  5. roji commented on Oct 8, 2016

    @roji
    Member

    Ok great :)

  6. changed the title [-]Query Hints (raw SQL, such as OPTIONs)[/-] [+]Query/Table Hints (raw SQL, such as OPTIONs)[/+] on May 4, 2018
  7. ajcvickers commented on May 17, 2018

    @ajcvickers
    Contributor

    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

  8. roji commented on May 18, 2018

    @roji
    Member

    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.
    • 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.

  9. 118 remaining items

  10. added theissue type on Jun 15, 2025
  11. marked Enable ForceIndex extension #16034 as a duplicate of this issue on Jun 16, 2025
  12. marked Lock row Mysql #14538 as a duplicate of this issue on Jun 16, 2025
  13. marked NOLOCK #11897 as a duplicate of this issue on Jun 16, 2025
  14. Charlieface commented on Jul 13, 2025

    @Charlieface

    @roji
    I agree option #2 is IMO much better. But:

    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.Annotations seems to have been made unusable for mutable Selects for some reason, so can't put it there either.

  15. roji commented on Jul 14, 2025

    @roji
    Member

    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.

  16. ahdung commented on Sep 10, 2025

    @ahdung

    please provide WithRawSql first

  17. roji commented on Sep 10, 2025

    @roji
    Member

    @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.

  18. jzabroski commented on Sep 10, 2025

    @jzabroski

    @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)

  19. iankemp-CapitalOnTap commented on Oct 20, 2025

    @iankemp-CapitalOnTap

    Only another 9 years and this issue will be old enough to have a pint.

  20. roji commented on Oct 20, 2025

    @roji
    Member

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions