MySQL 8.0.29
Source Code Documentation
find_contained_subqueries.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef SQL_JOIN_OPTIMIZER_FIND_CONTAINED_SUBQUERIES
24#define SQL_JOIN_OPTIMIZER_FIND_CONTAINED_SUBQUERIES 1
25
26#include "sql/item_subselect.h"
28#include "sql/sql_const.h"
29#include "sql/sql_lex.h"
30
31class Item;
32class THD;
33class Item;
34class Query_block;
35
36// Find out which subqueries are contained in this predicate, if any.
37// (This only counts IN/ALL/ANY subqueries, ie., those that we consider
38// materializing and have not converted to semijoins.) Note that
39// calling this repeatedly can be quite expensive, so many callers will want
40// to cache this information.
41//
42// Func should be on the form void func(ContainedSubquery), or something
43// compatible.
44template <class Func>
45void FindContainedSubqueries(THD *thd, Item *condition,
47 Func &&func) {
49 condition, enum_walk::POSTFIX,
50 [thd, outer_query_block, &func](Item *item) {
51 if (!IsItemInSubSelect(item)) {
52 return false;
53 }
54 Item_in_subselect *item_subs = down_cast<Item_in_subselect *>(item);
55
56 // TODO(sgunders): Respect subquery hints, which can force the
57 // strategy to be materialize.
58 Query_block *query_block = item_subs->unit->first_query_block();
59 const bool materializeable =
60 item_subs->subquery_allows_materialization(thd, query_block,
62 query_block->subquery_strategy(thd) ==
64
65 AccessPath *path = item_subs->unit->root_access_path();
66 if (path == nullptr) {
67 // In rare situations involving IN subqueries on the left side of
68 // other IN subqueries, the query block may not be part of the
69 // parent query block's list of inner query blocks. If so, it has
70 // not been optimized here. Since this is a rare case, we'll just
71 // skip it and assign it zero cost.
72 return false;
73 }
74
75 ContainedSubquery subquery;
76 subquery.row_width = 0;
77 for (const Item *qb_item : query_block->fields) {
78 subquery.row_width += std::min<size_t>(qb_item->max_length, 4096);
79 }
80 subquery.materializable = materializeable;
81 subquery.path = path;
82 func(std::move(subquery));
83 return false;
84 });
85}
86
87#endif // SQL_JOIN_OPTIMIZER_FIND_CONTAINED_SUBQUERIES
Representation of IN subquery predicates of the form "left_expr IN (SELECT ...)".
Definition: item_subselect.h:521
Query_expression * unit
Definition: item_subselect.h:108
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
uint32 max_length
Maximum length of result of evaluating this item, in number of bytes.
Definition: item.h:3286
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1124
Query_block * outer_query_block() const
Definition: sql_lex.h:1202
Subquery_strategy subquery_strategy(const THD *thd) const
Returns which subquery execution strategies can be used for this query block.
Definition: sql_lex.cc:4463
mem_root_deque< Item * > fields
All expressions needed after join and filtering, ie., select list, group by list, having clause,...
Definition: sql_lex.h:1867
Query_block * first_query_block() const
Definition: sql_lex.h:838
AccessPath * root_access_path() const
Definition: sql_lex.h:850
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:945
void FindContainedSubqueries(THD *thd, Item *condition, const Query_block *outer_query_block, Func &&func)
Definition: find_contained_subqueries.h:45
bool subquery_allows_materialization(THD *thd, Query_block *query_block, const Query_block *outer)
Check if the subquery predicate can be executed via materialization.
Definition: sql_resolver.cc:964
bool WalkItem(Item *item, enum_walk walk, T &&functor)
A helper class to give in a functor to Item::walk().
Definition: item.h:3471
bool IsItemInSubSelect(Item *item)
Returns whether the Item is an IN-subselect.
Definition: item_subselect.cc:2591
@ CANDIDATE_FOR_IN2EXISTS_OR_MAT
Candidate for rewriting IN(subquery) to EXISTS, or subquery materialization.
static char * path
Definition: mysqldump.cc:130
File containing constants that can be used throughout the server.
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:188
Definition: relational_expression.h:38
AccessPath * path
Definition: relational_expression.h:39
int row_width
Definition: relational_expression.h:41
bool materializable
Definition: relational_expression.h:40