MySQL 8.4.0
Source Code Documentation
opt_hints.cc File Reference
#include "sql/opt_hints.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "my_table_map.h"
#include "mysql/strings/m_ctype.h"
#include "mysql/udf_registration_types.h"
#include "mysqld_error.h"
#include "sql/derror.h"
#include "sql/error_handler.h"
#include "sql/item.h"
#include "sql/item_subselect.h"
#include "sql/key.h"
#include "sql/mysqld.h"
#include "sql/nested_join.h"
#include "sql/parse_tree_hints.h"
#include "sql/set_var.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_select.h"
#include "sql/table.h"
#include "string_with_len.h"

Classes

class  Join_order_hint_handler
 Auxiliary class is used to save/restore table dependencies. More...
 

Functions

int cmp_lex_string (const LEX_CSTRING &s, const LEX_CSTRING &t, const CHARSET_INFO *cs)
 
static void print_join_order_warn (THD *thd, opt_hints_enum type, const Hint_param_table *hint_table)
 Print warning about unresolved table for join order hints. More...
 
static bool compare_table_name (const Hint_param_table *hint_table, const Table_ref *table)
 Function compares hint table name and Table_ref table name. More...
 
static table_map get_other_dep (opt_hints_enum type, table_map hint_tab_map, table_map table_map)
 Function returns dependencies used for updating table dependencies depending on hint type. More...
 
static void update_nested_join_deps (JOIN *join, const JOIN_TAB *hint_tab, table_map hint_tab_map)
 Function updates dependencies for nested joins. More...
 
static bool set_join_hint_deps (JOIN *join, const Hint_param_table_list *hint_table_list, opt_hints_enum type)
 Function resolves hint tables, checks and sets table dependencies according to the hint. More...
 
bool is_compound_hint (opt_hints_enum type_arg)
 
static void print_hint_from_var (const THD *thd, String *str, set_var *var)
 Function prints hint using the info from set_var variable. More...
 
static void print_hint_specified (String *str, const std::string &sys_var_name, Item *sys_var_value)
 Function prints hint as it is specified. More...
 
static bool get_hint_state (Opt_hints *hint, Opt_hints *parent_hint, opt_hints_enum type_arg, bool *ret_val)
 Function returns hint value depending on the specified hint level. More...
 
bool hint_key_state (const THD *thd, const Table_ref *table, uint keyno, opt_hints_enum type_arg, uint optimizer_switch)
 Returns key hint value if hint is specified, returns optimizer switch value if hint is not specified. More...
 
bool hint_table_state (const THD *thd, const Table_ref *table_list, opt_hints_enum type_arg, uint optimizer_switch)
 Returns table hint value if hint is specified, returns optimizer switch value if hint is not specified. More...
 
void append_table_name (const THD *thd, String *str, const LEX_CSTRING *qb_name, const LEX_CSTRING *table_name)
 Append table and query block name. More...
 
bool compound_hint_key_enabled (const TABLE *table, uint keyno, opt_hints_enum type_arg)
 Returns true if compound hint state is on with or without specified keys, otherwise returns false. More...
 
bool idx_merge_hint_state (THD *thd, const TABLE *table, bool *use_cheapest_index_merge)
 Returns true if index merge hint state is on otherwise returns false. More...
 

Variables

struct st_opt_hint_info opt_hint_info []
 Information about hints. More...
 
const LEX_CSTRING sys_qb_prefix = {"select#", 7}
 Prefix for system generated query block name. More...
 

Function Documentation

◆ append_table_name()

void append_table_name ( const THD thd,
String str,
const LEX_CSTRING qb_name,
const LEX_CSTRING table_name 
)

Append table and query block name.

Parameters
thdpointer to THD object
strpointer to String object
qb_namepointer to query block name, may be null
table_namepointer to table name

◆ cmp_lex_string()

int cmp_lex_string ( const LEX_CSTRING s,
const LEX_CSTRING t,
const CHARSET_INFO cs 
)

◆ compare_table_name()

static bool compare_table_name ( const Hint_param_table hint_table,
const Table_ref table 
)
static

Function compares hint table name and Table_ref table name.

Query block name is taken into account.

Parameters
hint_tablehint table name
tablepointer to Table_ref object
Returns
false if table names are equal, true otherwise.

◆ compound_hint_key_enabled()

bool compound_hint_key_enabled ( const TABLE table,
uint  keyno,
opt_hints_enum  type_arg 
)

Returns true if compound hint state is on with or without specified keys, otherwise returns false.

If compound hint state is on and hint is specified without indexes, function returns 'true' for any 'keyno' argument. If hint specified with indexes, function returns true only for appropriate 'keyno' index.

Parameters
tablePointer to TABLE object
keynoKey number
type_argHint type
Returns
true if compound hint state is on with or without specified keys, otherwise returns false.

◆ get_hint_state()

static bool get_hint_state ( Opt_hints hint,
Opt_hints parent_hint,
opt_hints_enum  type_arg,
bool *  ret_val 
)
static

Function returns hint value depending on the specified hint level.

If hint is specified on current level, current level hint value is returned, otherwise parent level hint is checked.

Parameters
hintPointer to the hint object
parent_hintPointer to the parent hint object, should never be NULL
type_arghint type
[out]ret_valhint value depending on what hint level is used
Returns
true if hint is specified, false otherwise

◆ get_other_dep()

static table_map get_other_dep ( opt_hints_enum  type,
table_map  hint_tab_map,
table_map  table_map 
)
static

Function returns dependencies used for updating table dependencies depending on hint type.

Parameters
typehint type
hint_tab_maphint table map
table_maptable map
Returns
table dependencies.

◆ hint_key_state()

bool hint_key_state ( const THD thd,
const Table_ref table,
uint  keyno,
opt_hints_enum  type_arg,
uint  optimizer_switch 
)

Returns key hint value if hint is specified, returns optimizer switch value if hint is not specified.

Parameters
thdPointer to THD object
tablePointer to Table_ref object
keynoKey number
type_argHint type
optimizer_switchOptimizer switch flag
Returns
key hint value if hint is specified, otherwise optimizer switch value.

◆ hint_table_state()

bool hint_table_state ( const THD thd,
const Table_ref table,
opt_hints_enum  type_arg,
uint  optimizer_switch 
)

Returns table hint value if hint is specified, returns optimizer switch value if hint is not specified.

Parameters
thdPointer to THD object
tablePointer to Table_ref object
type_argHint type
optimizer_switchOptimizer switch flag
Returns
table hint value if hint is specified, otherwise optimizer switch value.

◆ idx_merge_hint_state()

bool idx_merge_hint_state ( THD thd,
const TABLE table,
bool *  use_cheapest_index_merge 
)

Returns true if index merge hint state is on otherwise returns false.

Parameters
thdThread handler
tablePointer to TABLE object
use_cheapest_index_mergeIN/OUT Returns true if INDEX_MERGE hint is used without any specified key.
Returns
true if index merge hint state is on otherwise returns false.

◆ is_compound_hint()

bool is_compound_hint ( opt_hints_enum  type_arg)

◆ print_hint_from_var()

static void print_hint_from_var ( const THD thd,
String str,
set_var var 
)
static

Function prints hint using the info from set_var variable.

Parameters
thdThread handle
strPointer to string object
varPointer to set_var object

◆ print_hint_specified()

static void print_hint_specified ( String str,
const std::string &  sys_var_name,
Item sys_var_value 
)
static

Function prints hint as it is specified.

Parameters
strPointer to string object
sys_var_nameVariable name
sys_var_valueVariable value

◆ print_join_order_warn()

static void print_join_order_warn ( THD thd,
opt_hints_enum  type,
const Hint_param_table hint_table 
)
static

Print warning about unresolved table for join order hints.

Parameters
thdpointer to THD object
typehint type
hint_tabletable name

◆ set_join_hint_deps()

static bool set_join_hint_deps ( JOIN join,
const Hint_param_table_list hint_table_list,
opt_hints_enum  type 
)
static

Function resolves hint tables, checks and sets table dependencies according to the hint.

If the hint is ignored due to circular table dependencies, original dependencies are restored.

Parameters
joinpointer to JOIN object
hint_table_listhint table list
typehint type
Returns
false if hint is applied, true otherwise.

◆ update_nested_join_deps()

static void update_nested_join_deps ( JOIN join,
const JOIN_TAB hint_tab,
table_map  hint_tab_map 
)
static

Function updates dependencies for nested joins.

If table specified in the hint belongs to nested join, we need to update dependencies of all tables of the nested join with the same dependency as for the hint table. It is also necessary to update all tables of the nested joins this table is part of.

Parameters
joinpointer to JOIN object
hint_tabpointer to JOIN_TAB object
hint_tab_mapmap of the tables, specified in the hint

Variable Documentation

◆ opt_hint_info

struct st_opt_hint_info opt_hint_info[]
Initial value:
= {
{"BKA", true, true, false},
{"BNL", true, true, false},
{"ICP", true, true, false},
{"MRR", true, true, false},
{"NO_RANGE_OPTIMIZATION", true, true, false},
{"MAX_EXECUTION_TIME", false, false, false},
{"QB_NAME", false, false, false},
{"SEMIJOIN", false, false, false},
{"SUBQUERY", false, false, false},
{"MERGE", true, true, false},
{"JOIN_PREFIX", false, false, true},
{"JOIN_SUFFIX", false, false, true},
{"JOIN_ORDER", false, false, true},
{"JOIN_FIXED_ORDER", false, true, false},
{"INDEX_MERGE", false, false, false},
{"RESOURCE_GROUP", false, false, false},
{"SKIP_SCAN", false, false, false},
{"HASH_JOIN", true, true, false},
{"INDEX", false, false, false},
{"JOIN_INDEX", false, false, false},
{"GROUP_INDEX", false, false, false},
{"ORDER_INDEX", false, false, false},
{"DERIVED_CONDITION_PUSHDOWN", true, true, false},
{nullptr, false, false, false}}

Information about hints.

Should be synchronized with opt_hints_enum enum.

Note: Hint name depends on hint state. 'NO_' prefix is added if appropriate hint state bit(see Opt_hints_map::hints) is not set. Depending on 'switch_state_arg' argument in 'parse tree object' constructors(see parse_tree_hints.[h,cc]) implementor can control wishful form of the hint name.

◆ sys_qb_prefix

const LEX_CSTRING sys_qb_prefix = {"select#", 7}

Prefix for system generated query block name.

Used in information warning in EXPLAIN oputput.