MySQL 9.1.0
Source Code Documentation
join_optimizer.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_JOIN_OPTIMIZER_JOIN_OPTIMIZER_H
25#define SQL_JOIN_OPTIMIZER_JOIN_OPTIMIZER_H
26
27/**
28 @file
29
30 The hypergraph join optimizer takes a query block and decides how to
31 execute it as fast as possible (within a given cost model), based on
32 the idea of expressing the join relations as edges in a hypergraph.
33 (See subgraph_enumeration.h for more details on the core algorithm,
34 or FindBestQueryPlan() for more information on overall execution.)
35
36 It is intended to eventually take over completely from the older join
37 optimizer based on prefix search (sql_planner.cc and related code),
38 and is nearly feature complete, but is currently in the early stages
39 with a very simplistic cost model and certain limitations.
40 The most notable ones are that we do not support:
41
42 - Hints (except STRAIGHT_JOIN).
43 - TRADITIONAL and JSON formats for EXPLAIN (use FORMAT=tree).
44 - UPDATE.
45
46 There are also have many optimization features it does not yet support;
47 among them:
48
49 - Aggregation through a temporary table.
50 - Some range optimizer features (notably MIN/MAX optimization).
51 - Materialization of arbitrary access paths (note that nested loop
52 joins against these can enable a limited form of hash join
53 that preserves ordering on the left side).
54 */
55
56class Query_block;
57class THD;
58struct AccessPath;
59struct JoinHypergraph;
60
61/**
62 The main entry point for the hypergraph join optimizer; takes in a query
63 block and returns an access path to execute it (or nullptr, for error).
64 It works as follows:
65
66 1. Convert the query block from MySQL's Table_ref structures into
67 a hypergraph (see make_join_hypergraph.h).
68 2. Find all legal subplans in the hypergraph, calculate costs for
69 them and create access paths -- if there are multiple ways to make a
70 given subplan (e.g. multiple join types, or joining {t1,t2,t3} can be
71 made through either {t1}-{t2,t3} or {t1,t2}-{t3}), keep only the cheapest
72 one. Filter predicates (from WHERE and pushed-down join conditions)
73 are added as soon down as it is legal, which is usually (but not
74 universally) optimal. The algorithm works so that we always see smaller
75 subplans first and then end at the complete join plan containing all the
76 tables in the query block.
77 3. Add an access path for non-pushable filter predicates.
78 4. Add extra access paths for operations done after the joining,
79 such as ORDER BY, GROUP BY, LIMIT, etc..
80 5. Make access paths for the filters in nodes made by #2
81 (see ExpandFilterAccessPaths()).
82
83 Materializing subqueries need some extra care. (These are typically IN
84 subqueries that for whatever reason could not be rewritten to semijoin,
85 e.g. because they have GROUP BY.) The decision on whether to materialize
86 or not needs to be done cost-based, and depends both on the inner and outer
87 query block, so it needs to be done cost-based. (Materializiation gives
88 a high up-front cost, but each execution is cheaper, so it will depend on
89 how many times we expect to execute the subquery and now expensive it is
90 to run unmaterialized.) Following the flow through the different steps:
91
92 First of all, these go through a stage known as in2exists, rewriting them
93 from e.g.
94
95 WHERE t1_outer.x IN ( SELECT t2.y FROM t2 GROUP BY ... )
96
97 to
98
99 WHERE EXISTS ( SELECT 1 FROM t2 GROUP BY ... HAVING t2.y = t1_outer.x )
100
101 This happens before the join optimizer, and the idea is that the HAVING
102 condition (known as a “created_by_in2exists condition”, possibly in WHERE
103 instead of HAVING) can be attempted pushed down into an index or similar,
104 giving more efficient execution. However, if we want to materialize the
105 subquery, these extra conditions need to be removed before materialization;
106 not only do they give the wrong result, but they can also need to wrong
107 costs and a suboptimal join order.
108
109 Thus, whenever we plan such a subquery, we plan it twice; once as usual,
110 and then a second time with all in2exists conditions removed. This gives
111 EstimateFilterCost() precise cost information for both cases, or at least
112 as precise as the cost model itself is. In the outer query block, we can
113 then weigh the two alternatives against each other when we add a filter
114 with such a subquery; we can choose to materialize it or not, and propose
115 both alternatives as with any other subplan. When we've decided on the
116 final plan, we go through all access paths and actually materialize the
117 subqueries it says to materialize.
118
119 There are lots of places these conditions can show up; to reduce complexity,
120 we only consider materialization in the most common places (filters on
121 base tables, filters after joins, filters from HAVING) -- in particular,
122 we don't bother checking on join conditions. It is never wrong to not
123 materialize a subquery, though it may be suboptimal.
124
125
126 Note that the access path returned by FindBestQueryPlan() is not ready
127 for immediate conversion to iterators; see FinalizePlanForQueryBlock().
128 You may call FindBestQueryPlan() any number of times for a query block,
129 but FinalizePlanForQueryBlock() only once, as finalization generates
130 temporary tables and may rewrite expressions in ways that are incompatible
131 with future planning. The difference is most striking with the planning
132 done twice by in2exists (see above).
133
134 @param thd Thread handle.
135 @param query_block The query block to find a plan for.
136 */
138
139// See comment in .cc file.
141
142// Exposed for unit testing only.
144
147
148#endif // SQL_JOIN_OPTIMIZER_JOIN_OPTIMIZER_H
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1159
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
bool FinalizePlanForQueryBlock(THD *thd, Query_block *query_block)
Definition: finalize_plan.cc:835
void EstimateAggregateCost(AccessPath *path)
AccessPath * FindBestQueryPlan(THD *thd, Query_block *query_block)
The main entry point for the hypergraph join optimizer; takes in a query block and returns an access ...
Definition: join_optimizer.cc:9301
void EstimateMaterializeCost(THD *thd, AccessPath *path)
Definition: cost_model.cc:182
void FindSargablePredicates(THD *thd, JoinHypergraph *graph)
Definition: join_optimizer.cc:8405
static char * path
Definition: mysqldump.cc:149
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:227
A struct containing a join hypergraph of a single query block, encapsulating the constraints given by...
Definition: make_join_hypergraph.h:88
hypergraph::Hypergraph graph
Definition: make_join_hypergraph.h:97
const Query_block * query_block() const
Returns a pointer to the query block that is being planned.
Definition: make_join_hypergraph.h:206