Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hints around the type and order of JOIN operations #13283

Open
hlcianfagna opened this issue Nov 21, 2022 · 3 comments
Open

Allow hints around the type and order of JOIN operations #13283

hlcianfagna opened this issue Nov 21, 2022 · 3 comments
Labels

Comments

@hlcianfagna
Copy link
Contributor

hlcianfagna commented Nov 21, 2022

Problem Statement

[Raising feature request for consideration/refinement following a brief discussion with @seut ]

In some cases, the optimizer may come out with a suboptimal plan for performing JOINs for a query.
There may be many reasons for this, but on an exceptional basis, knowing how the data looks like, it would be useful to be able to request a specific JOIN order or force a hash join.

Possible Solutions

Something along the lines of what other systems do:
PostgreSQL allows hinting NestLoop and MergeJoin with pg_hint_plan.
In MySQL order can be forced using the keyword STRAIGHT_JOIN instead of JOIN.
In MSSQL JOIN can be preceded with LOOP (for nested loops), MERGE, or HASH to request a specific mechanism.
In Oracle the optimizer looks for hints within /* */ comments and there are hints such as USE_NL to request nested loops.

Considered Alternatives

Try rewriting queries with subqueries until obtaining the desired plan, results may change when upgrading CrateDB or changing other factors in the environment.

References:
MS SQL Server Hints https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query?view=sql-server-ver16#force-order

@hlcianfagna
Copy link
Contributor Author

Relates with #10213
if we consider the workaround suggested in https://community.snowflake.com/s/article/Controlling-Join-Order

@hlcianfagna
Copy link
Contributor Author

Regarding the option of using special comments as in pg_hint_plan, a discussion in crate/crash#386 pointed out that https://www.postgresql.org/docs/15/sql-syntax-lexical.html#SQL-SYNTAX-COMMENTS reads:

A comment is removed from the input stream before further syntax analysis and is effectively replaced by whitespace.

@matriv
Copy link
Contributor

matriv commented Jun 21, 2023

I think the "plan" is to use special comments as pg_hint_plan or Oracle:
/*+ PARALLEL(employees 3) */, (notice the + sign) so those shouldn't be removed at parser level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants