Repository navigation
Conversation
|
Workflow [PR], commit [c1ef61c] Summary: ❌
AI ReviewSummaryThis PR adds Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block Minimum required actions: preserve |
| Specifies which JOIN order algorithms to attempt during query plan optimization. The following algorithms are available: | ||
| - 'greedy' - basic greedy algorithm - works fast but might not produce the best join order | ||
| - 'dpsize' - implements DPsize algorithm currently only for Inner joins - considers all possible join orders and finds the most optimal one but might be slow for queries with many tables and join predicates. | ||
| - 'auto' - uses DPsize for up to 9 relations and greedy for 10 or more relations. |
There was a problem hiding this comment.
Let's switch the default to auto. To do that, we'll also need to add an entry to src/Core/SettingsChangesHistory.cpp.
There was a problem hiding this comment.
Note the existing setting query_plan_optimize_join_order_limit. I think we should retire it in this PR. This can be done by moving it to the "obsolete" section (grep MARK_OBSOLETE in this file).
At the same time, we don't want to hard-code the DPSize --> greedy threshold, this should be controlled by a new setting, e.g. join_order_auto_algorithm_greedy_threshold.
There was a problem hiding this comment.
There should be no such parameter (auto) for the optimizer and then decide based on a threshold ( # rels < 10: pick DPSize otherwise Greedy). In query optimization jargon we call this "fall-of-the-cliff" approach and this is the approach taken e.g., by POSTGRES. If they see that the number of relations is less than 12, they pick DP, otherwise switch to a genetic based opt. alg.
The query optimizer should try to solve the problem exactly, and adaptively tune down the result quality if optimality can no longer be guaranteed, i.e., by means of query graph linearization.
There was a problem hiding this comment.
Fine by me. The auto would basically be a dpsize with cliff. There is a linearization like approach implemented on the layer above the join order optimization algorithms which is controlled by query_plan_optimize_join_order_limit.
There was a problem hiding this comment.
The idea is to try and understand the complexity of the query graph a-priori, but not base the decision on a threshold like query_plan_optimize_join_order_limit. If the number of ccps entails that the problem can be solved exactly (optimally) we do so, if not, we linearize the query graph topology into a chain, so that we can still invoke an optimal algorithm (e.g., DPHyp), as the problem is reduced to a simpler one thus it becomes tractable. As an example we can solve optimally chain queries even up to 100 relations. If we were to work by means of thresholds, we would fall-off the cliff already for joins containing only 10 relations and thus leave a large margin on the optimization potential unharvested.
| : query_graph(std::move(query_graph_)) | ||
| , enabled_algorithms(enabled_algorithms_) | ||
| { | ||
| bool has_auto_algorithm = std::find(enabled_algorithms.begin(), enabled_algorithms.end(), JoinOrderAlgorithm::AUTO) != enabled_algorithms.end(); |
There was a problem hiding this comment.
I'm not sure I understand the change in this ctor. We can probably remove the code and no bad things will happen.
|
|
||
| for (const auto & algorithm : enabled_algorithms) | ||
| { | ||
| chassert(algorithm != JoinOrderAlgorithm::AUTO); |
There was a problem hiding this comment.
Not needed, l. 280 does the trick.
This comment was marked as resolved.
This comment was marked as resolved.
| for (JoinOrderAlgorithm algorithm : optimization_settings.query_plan_optimize_join_order_algorithm) | ||
| { | ||
| if (algorithm == JoinOrderAlgorithm::AUTO) | ||
| algorithm = query_graph.relation_stats.size() > 9 ? JoinOrderAlgorithm::GREEDY : JoinOrderAlgorithm::DPSIZE; |
There was a problem hiding this comment.
auto now becomes the default setting value, but this mapping turns it into a single DPSIZE attempt for every join graph with up to 9 relations. solveDPsize still skips all non-Inner candidates and returns nullptr; with no GREEDY fallback, solve raises EXPERIMENTAL_FEATURE_ERROR. A plain two-table LEFT JOIN under default settings will therefore fail the same way the test at tests/queries/0_stateless/03733_join_order_dp.sql:395 expects for explicit query_plan_optimize_join_order_algorithm = 'dpsize'.
Please make auto keep a GREEDY fallback for cases where DPSIZE cannot produce a plan (for example map up to 9 relations to DPSIZE, GREEDY, or select GREEDY for non-inner/disconnected graphs) before making it the default.
There was a problem hiding this comment.
The bot is right, we need to keep GREEDY for < 9 tables when there are non-inner joins.
rschu1ze
left a comment
There was a problem hiding this comment.
Can you merge from master once more, please?
| DECLARE(JoinOrderAlgorithm, query_plan_optimize_join_order_algorithm, "greedy", R"( | ||
| DECLARE(JoinOrderAlgorithm, query_plan_optimize_join_order_algorithm, "auto", R"( | ||
| Specifies which JOIN order algorithms to attempt during query plan optimization. The following algorithms are available: | ||
| - 'auto' - uses DPsize for up to 9 relations and greedy for 10 or more relations. |
There was a problem hiding this comment.
Let's just say that the choice is made automatically without specifying how:
'auto' - Choose an optimal join order algorithm automatically.
While at it, can you also change the next two lines?
'greedy' - Greedy join order algorithm. Converges fast but might produce a sub-optimal join order. Can be used for queries with many joined tables.
'dpsize' - DPsize join order algorithm. Only for INNER joins. Enumerates all possible join orders (slow) and finds the most optimal one. Recommended only for queries with few joined tables.
| Allow to use hive partitioning with S3Queue/AzureQueue engines | ||
| )", EXPERIMENTAL) \ | ||
| DECLARE(JoinOrderAlgorithm, query_plan_optimize_join_order_algorithm, "greedy", R"( | ||
| DECLARE(JoinOrderAlgorithm, query_plan_optimize_join_order_algorithm, "auto", R"( |
There was a problem hiding this comment.
The new default setting value must also be reflected in src/Core/SettingsChangesHistory.cpp.
| for (JoinOrderAlgorithm algorithm : optimization_settings.query_plan_optimize_join_order_algorithm) | ||
| { | ||
| if (algorithm == JoinOrderAlgorithm::AUTO) | ||
| algorithm = query_graph.relation_stats.size() > 9 ? JoinOrderAlgorithm::GREEDY : JoinOrderAlgorithm::DPSIZE; |
There was a problem hiding this comment.
The bot is right, we need to keep GREEDY for < 9 tables when there are non-inner joins.
| AND T4.D_LookupCode = 'Lookup S' | ||
| SETTINGS query_plan_optimize_join_order_algorithm = 'dpsize'; | ||
|
|
||
| SELECT '========================================='; |
There was a problem hiding this comment.
I think below tests are a bit complex. Maybe each join algorithm can just print a line into the database log file (something like TRACE_DEBUG(log, "Optimizing join order using DPSub")). Then the test can check that this line exists in the logs using system.text_log).
|
Superseeded by #107351. See #99884 (comment) |
|
@fkastrati Thinking about this, I think an |
|
@rschulze My goal/intention was that in the default (
Since this is the intended behavior, then exposing
could still override the default and select a specific algorithm explicitly, e.g.
More generally, Because of this, I would not expect users to choose |
I agree with you. However, as of today, |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Users can now configure ClickHouse to automatically select the join order optimization algorithm based on the number of tables involved.
query_plan_optimize_join_order_algorithm = autowill configure DPsize for up to 9 relations and greedy for 10 or more relations.Documentation entry for user-facing changes