/*******************************************************************//**
Inverts a comparison operator.
@return the equivalent operator when the order of the arguments is switched */ static int
opt_invert_cmp_op( /*==============*/ int op) /*!< in: operator */
{ if (op == '<') { return('>');
} elseif (op == '>') { return('<');
} elseif (op == '=') { return('=');
} elseif (op == PARS_LE_TOKEN) { return(PARS_GE_TOKEN);
} elseif (op == PARS_GE_TOKEN) { return(PARS_LE_TOKEN);
} else { /* TODO: LIKE operator */
ut_error;
}
return(0);
}
/*******************************************************************//**
Checks if the value of an expression can be calculated BEFORE the nth table
in a join is accessed. Ifthis is the case, it can possibly be used in an
index search for the nth table.
@returnTRUEif already determined */ static
ibool
opt_check_exp_determined_before( /*============================*/
que_node_t* exp, /*!< in: expression */
sel_node_t* sel_node, /*!< in: select node */
ulint nth_table) /*!< in: nth table will be accessed */
{
func_node_t* func_node;
sym_node_t* sym_node;
dict_table_t* table;
que_node_t* arg;
ulint i;
ut_ad(exp && sel_node);
if (que_node_get_type(exp) == QUE_NODE_FUNC) {
func_node = static_cast<func_node_t*>(exp);
arg = func_node->args;
while (arg) { if (!opt_check_exp_determined_before(arg, sel_node,
nth_table)) { return(FALSE);
}
/*******************************************************************//**
Looks in a comparison condition if a column value is already restricted by
it BEFORE the nth table is accessed.
@return expression restricting the value of the column, or NULL ifnot known */ static
que_node_t*
opt_look_for_col_in_comparison_before( /*==================================*/
ulint cmp_type, /*!< in: OPT_EQUAL, OPT_COMPARISON */
ulint col_no, /*!< in: column number */
func_node_t* search_cond, /*!< in: comparison condition */
sel_node_t* sel_node, /*!< in: select node */
ulint nth_table, /*!< in: nth table in a join (a query fromasingletableisconsidereda
join of 1 table) */
ulint* op) /*!< out: comparison operator ('=', PARS_GE_TOKEN,...);thisisinverted ifthecolumnappearsontheright
side */
{
sym_node_t* sym_node;
dict_table_t* table;
que_node_t* exp;
que_node_t* arg;
if (opt_check_exp_determined_before(exp, sel_node,
nth_table)) {
*op = ulint(opt_invert_cmp_op(
search_cond->func));
return(exp);
}
}
}
return(NULL);
}
/*******************************************************************//**
Looks in a search condition if a column value is already restricted by the
search condition BEFORE the nth table is accessed. Takes into account that if we will fetch in an ascending order, we cannot utilize an upper limit for
a column value; in a descending order, respectively, a lower limit.
@return expression restricting the value of the column, or NULL ifnot known */ static
que_node_t*
opt_look_for_col_in_cond_before( /*============================*/
ulint cmp_type, /*!< in: OPT_EQUAL, OPT_COMPARISON */
ulint col_no, /*!< in: column number */
func_node_t* search_cond, /*!< in: search condition or NULL */
sel_node_t* sel_node, /*!< in: select node */
ulint nth_table, /*!< in: nth table in a join (a query fromasingletableisconsidereda
join of 1 table) */
ulint* op) /*!< out: comparison operator ('=',
PARS_GE_TOKEN, ... ) */
{
func_node_t* new_cond;
que_node_t* exp;
/*******************************************************************//**
Calculates the goodness for an index according to a select node. The
goodness is 4 times the number of first fields in index whose values we
already know exactly in the query. If we have a comparison condition for
an additional field, 2 point are added. If the index is unique, and we know
all the unique fields for the index we add 1024 points. For a clustered index
we add 1 point.
@return goodness */ static
ulint
opt_calc_index_goodness( /*====================*/
dict_index_t* index, /*!< in: index */
sel_node_t* sel_node, /*!< in: parsed select node */
ulint nth_table, /*!< in: nth table in a join */
que_node_t** index_plan, /*!< in/out: comparison expressions for
this index */
ulint* last_op) /*!< out: last comparison operator, if
goodness > 1 */
{
que_node_t* exp;
ulint goodness;
ulint n_fields;
ulint col_no;
ulint op;
ulint j;
if (!index->is_normal_btree() || !index->is_committed()) { return(0);
}
goodness = 0;
/* Note that as higher level node pointers in the B-tree contain pageaddressesasthelastfield,wemustnotputmorefieldsin thesearchtuplethandict_index_get_n_unique_in_tree(index);see
the note in btr_cur_search_to_nth_level. */
for (j = 0; j < n_fields; j++) { if (UNIV_UNLIKELY(index->fields[j].descending)) { /* The internal InnoDB SQL parser does not
work with indexes that use DESC order. */ return0;
}
col_no = dict_index_get_nth_col_no(index, j);
exp = opt_look_for_col_in_cond_before(
OPT_EQUAL, col_no, static_cast<func_node_t*>(sel_node->search_cond),
sel_node, nth_table, &op); if (exp) { /* The value for this column is exactly known already
at this stage of the join */
if (goodness / 4 >= dict_index_get_n_unique(index)) {
goodness += 1024;
if (dict_index_is_clust(index)) {
goodness += 1024;
}
}
/* We have to test for goodness here, as last_op may not be set */ if (goodness && dict_index_is_clust(index)) {
goodness++;
}
return(goodness);
}
/*******************************************************************//**
Calculates the number of matched fields based on an index goodness.
@return number of exactly or partially matched fields */
UNIV_INLINE
ulint
opt_calc_n_fields_from_goodness( /*============================*/
ulint goodness) /*!< in: goodness */
{ return(((goodness % 1024) + 2) / 4);
}
/*******************************************************************//**
Converts a comparison operator to the corresponding search mode PAGE_CUR_GE,
...
@return search mode */
UNIV_INLINE
page_cur_mode_t
opt_op_to_search_mode( /*==================*/
ibool asc, /*!< in: TRUE if the rows should be fetched in an
ascending order */
ulint op) /*!< in: operator '=', PARS_GE_TOKEN, ... */
{ if (op == '='
|| op == PARS_LIKE_TOKEN_EXACT
|| op == PARS_LIKE_TOKEN_PREFIX
|| op == PARS_LIKE_TOKEN_SUFFIX
|| op == PARS_LIKE_TOKEN_SUBSTR) {
/*******************************************************************//**
Determines if a node is an argument node of a function node.
@returnTRUEif is an argument */ static
ibool
opt_is_arg( /*=======*/
que_node_t* arg_node, /*!< in: possible argument node */
func_node_t* func_node) /*!< in: function node */
{
que_node_t* arg;
arg = func_node->args;
while (arg) { if (arg == arg_node) {
return(TRUE);
}
arg = que_node_get_next(arg);
}
return(FALSE);
}
/*******************************************************************//**
Decides if the fetching of rows should be made in a descending order, and
also checks that the chosen query plan produces a result which satisfies
the order-by. */ static void
opt_check_order_by( /*===============*/
sel_node_t* sel_node) /*!< in: select node; asserts an error iftheplandoesnotagreewiththe
order-by */
{
order_node_t* order_node;
dict_table_t* order_table;
ulint order_col_no;
plan_t* plan;
ulint i;
/* If there is an order-by clause, the first non-exactly matched field intheindexusedforthelasttableinthetablelistshouldbethe columndefinedintheorder-byclause,andforalltheothertables weshouldgetonlyatmostasinglerow,otherwisewecannotpresently
calculate the order-by, as we have no sort utility */
for (i = 0; i < sel_node->n_tables; i++) {
plan = sel_node_get_nth_plan(sel_node, i);
if (i < sel_node->n_tables - 1) {
ut_a(dict_index_get_n_unique(plan->index)
<= plan->n_exact_match);
} else {
ut_a(plan->table == order_table);
/*******************************************************************//**
Optimizes a select. Decides which indexes to tables to use. The tables
are accessed in the order that they were written to the FROM part in the
select statement. */ static void
opt_search_plan_for_table( /*======================*/
sel_node_t* sel_node, /*!< in: parsed select node */
ulint i, /*!< in: this is the ith table */
dict_table_t* table) /*!< in: table */
{
plan_t* plan;
dict_index_t* index;
ulint n_fields;
ulint best_goodness = 0;
ulint best_last_op = 0;
que_node_t* index_plan[256];
que_node_t* best_index_plan[256];
switch (best_last_op) { case'=': case PARS_LIKE_TOKEN_EXACT: case PARS_LIKE_TOKEN_PREFIX: case PARS_LIKE_TOKEN_SUFFIX: case PARS_LIKE_TOKEN_SUBSTR: break; default:
n_fields--;
}
/*******************************************************************//**
Looks at a comparison condition and decides if it can, and need, be tested for
a table AFTER the table has been accessed.
@return OPT_NOT_COND ifnotforthis table, else OPT_END_COND,
OPT_TEST_COND, or OPT_SCROLL_COND, where the last means that the
condition need not be tested, except when scroll cursors are used */ static
ulint
opt_classify_comparison( /*====================*/
sel_node_t* sel_node, /*!< in: select node */
ulint i, /*!< in: ith table in the join */
func_node_t* cond) /*!< in: comparison condition */
{
plan_t* plan;
ulint n_fields;
ulint op;
ulint j;
ut_ad(cond && sel_node);
plan = sel_node_get_nth_plan(sel_node, i);
/* Check if the condition is determined after the ith table has been
accessed, but not after the i - 1:th */
if (!opt_check_exp_determined_before(cond, sel_node, i + 1)) {
return(OPT_NOT_COND);
}
if ((i > 0) && opt_check_exp_determined_before(cond, sel_node, i)) {
return(OPT_NOT_COND);
}
/* If the condition is an exact match condition used in constructing
the search tuple, it is classified as OPT_END_COND */
/* If the condition is an non-exact match condition used in constructingthesearchtuple,itisclassifiedasOPT_SCROLL_COND. Whenthecursorispositioned,andifanon-scrollcursorisused, thereisnoneedtotestthiscondition;ifascrollcursorisused
the testing is necessary when the cursor is reversed. */
if ((n_fields > plan->n_exact_match)
&& opt_is_arg(plan->tuple_exps[n_fields - 1], cond)) {
return(OPT_SCROLL_COND);
}
/* If the condition is a non-exact match condition on the first field inindexforwhichthereisnoexactmatch,anditlimitsthesearch rangefromtheoppositesideofthesearchtuplealreadyBEFOREwe
access the table, it is classified as OPT_END_COND */
if ((dict_index_get_n_fields(plan->index) > plan->n_exact_match)
&& opt_look_for_col_in_comparison_before(
OPT_COMPARISON,
dict_index_get_nth_col_no(plan->index,
plan->n_exact_match),
cond, sel_node, i, &op)) {
/* Otherwise, cond is classified as OPT_TEST_COND */
return(OPT_TEST_COND);
}
/*******************************************************************//**
Recursively looks for test conditions for a table in a join. */ static void
opt_find_test_conds( /*================*/
sel_node_t* sel_node, /*!< in: select node */
ulint i, /*!< in: ith table in the join */
func_node_t* cond) /*!< in: conjunction of search
conditions or NULL */
{
func_node_t* new_cond;
ulint fclass;
plan_t* plan;
if (cond == NULL) {
return;
}
if (cond->func == PARS_AND_TOKEN) {
new_cond = static_cast<func_node_t*>(cond->args);
/*******************************************************************//**
Normalizes a list of comparison conditions so that a column of the table
appears on the left side of the comparison if possible. This is accomplished
by switching the arguments of the operator. */ static void
opt_normalize_cmp_conds( /*====================*/
func_node_t* cond, /*!< in: first in a list of comparison
conditions, or NULL */
dict_table_t* table) /*!< in: table */
{
que_node_t* arg1;
que_node_t* arg2;
sym_node_t* sym_node;
while (cond) {
arg1 = cond->args;
arg2 = que_node_get_next(arg1);
if (que_node_get_type(arg2) == QUE_NODE_SYMBOL) {
sym_node = static_cast<sym_node_t*>(arg2);
if ((sym_node->token_type == SYM_COLUMN)
&& (sym_node->table == table)) {
/* Invert the operator */
cond->func = opt_invert_cmp_op(cond->func);
}
}
cond = UT_LIST_GET_NEXT(cond_list, cond);
}
}
/*******************************************************************//**
Finds out the search condition conjuncts we can, and need, to test as the ith
table in a join is accessed. The search tuple can eliminate the need to test
some conjuncts. */ static void
opt_determine_and_normalize_test_conds( /*===================================*/
sel_node_t* sel_node, /*!< in: select node */
ulint i) /*!< in: ith table in the join */
{
plan_t* plan;
/*******************************************************************//**
Looks for occurrences of the columns of the table in the query subgraph and
adds them to the list of columns if an occurrence of the same column does not
already exist in the list. If the column is already in the list, puts a value
indirection to point to the occurrence in the column list, except if the
column occurrence we are looking at is in the column list, in which case
nothing is done. */ void
opt_find_all_cols( /*==============*/
ibool copy_val, /*!< in: if TRUE, new found columns are
added as columns to copy */
dict_index_t* index, /*!< in: index of the table to use */
sym_node_list_t* col_list, /*!< in: base node of a list where
to add new found columns */
plan_t* plan, /*!< in: plan or NULL */
que_node_t* exp) /*!< in: expression or condition or
NULL */
{
func_node_t* func_node;
que_node_t* arg;
sym_node_t* sym_node;
sym_node_t* col_node;
ulint col_pos;
if (exp == NULL) {
return;
}
if (que_node_get_type(exp) == QUE_NODE_FUNC) {
func_node = static_cast<func_node_t*>(exp);
/*******************************************************************//**
Looks for occurrences of the columns of the table in conditions which are not yet determined AFTER the join operation has fetched a row in the ith
table. The values for these column must be copied to dynamic memory for
later use. */ static void
opt_find_copy_cols( /*===============*/
sel_node_t* sel_node, /*!< in: select node */
ulint i, /*!< in: ith table in the join */
func_node_t* search_cond) /*!< in: search condition or NULL */
{
func_node_t* new_cond;
plan_t* plan;
/*******************************************************************//**
Classifies the table columns according to whether we use the column only while
holding the latch on the page, or whether we have to copy the column value to
dynamic memory. Puts the first occurrence of a column to either list in the
plan node, and puts indirections to later occurrences of the column. */ static void
opt_classify_cols( /*==============*/
sel_node_t* sel_node, /*!< in: select node */
ulint i) /*!< in: ith table in the join */
{
plan_t* plan;
que_node_t* exp;
plan = sel_node_get_nth_plan(sel_node, i);
/* The final value of the following field will depend on the
environment of the select statement: */
/*******************************************************************//**
Fills in the info in plan which is used in accessing a clustered index
record. The columns must already be classified for the plan node. */ static void
opt_clust_access( /*=============*/
sel_node_t* sel_node, /*!< in: select node */
ulint n) /*!< in: nth table in select */
{
plan_t* plan;
dict_table_t* table;
dict_index_t* clust_index;
dict_index_t* index;
mem_heap_t* heap;
ulint n_fields;
ulint pos;
ulint i;
plan = sel_node_get_nth_plan(sel_node, n);
index = plan->index;
/* The final value of the following field depends on the environment
of the select statement: */
plan->no_prefetch = FALSE;
if (dict_index_is_clust(index)) {
plan->clust_map = NULL;
plan->clust_ref = NULL;
for (i = 0; i < n_fields; i++) {
pos = dict_index_get_nth_field_pos(index, clust_index, i);
ut_a(pos != ULINT_UNDEFINED);
/* We optimize here only queries to InnoDB's internal system
tables, and they should not contain column prefix indexes. */
if (dict_is_sys_table(index->table->id)
&& (dict_index_get_nth_field(index, pos)->prefix_len != 0
|| dict_index_get_nth_field(clust_index, i)
->prefix_len != 0)) {
ib::error() << "Error in pars0opt.cc: table "
<< index->table->name
<< " has prefix_len != 0";
}
*(plan->clust_map + i) = pos;
ut_ad(pos != ULINT_UNDEFINED);
}
}
#ifdef UNIV_SQL_DEBUG /** Print info of a query plan.
@param[in,out] sel_node select node */ static void
opt_print_query_plan(
sel_node_t* sel_node); #endif
/*******************************************************************//**
Optimizes a select. Decides which indexes to tables to use. The tables
are accessed in the order that they were written to the FROM part in the
select statement. */ void
opt_search_plan( /*============*/
sel_node_t* sel_node) /*!< in: parsed select node */
{
sym_node_t* table_node;
dict_table_t* table;
order_node_t* order_by;
ulint i;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.