| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file pars/pars0opt.cc |
| 29 |
Simple SQL optimizer |
| 30 |
|
| 31 |
Created 12/21/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "pars0opt.h" |
| 35 |
|
| 36 |
#include <stddef.h> |
| 37 |
|
| 38 |
#include "dict0boot.h" |
| 39 |
#include "dict0dict.h" |
| 40 |
#include "dict0mem.h" |
| 41 |
#include "lock0lock.h" |
| 42 |
#include "pars0grm.h" |
| 43 |
#include "pars0pars.h" |
| 44 |
#include "que0que.h" |
| 45 |
#include "row0ins.h" |
| 46 |
#include "row0sel.h" |
| 47 |
#include "row0upd.h" |
| 48 |
|
| 49 |
#define OPT_EQUAL 1 /* comparison by = */ |
| 50 |
#define OPT_COMPARISON 2 /* comparison by <, >, <=, or >= */ |
| 51 |
|
| 52 |
#define OPT_NOT_COND 1 |
| 53 |
#define OPT_END_COND 2 |
| 54 |
#define OPT_TEST_COND 3 |
| 55 |
#define OPT_SCROLL_COND 4 |
| 56 |
|
| 57 |
/** Inverts a comparison operator. |
| 58 |
@return the equivalent operator when the order of the arguments is switched */ |
| 59 |
static int opt_invert_cmp_op(int op) /*!< in: operator */ |
| 60 |
{ |
| 61 |
if (op == '<') { |
| 62 |
return ('>'); |
| 63 |
} else if (op == '>') { |
| 64 |
return ('<'); |
| 65 |
} else if (op == '=') { |
| 66 |
return ('='); |
| 67 |
} else if (op == PARS_LE_TOKEN) { |
| 68 |
return (PARS_GE_TOKEN); |
| 69 |
} else if (op == PARS_GE_TOKEN) { |
| 70 |
return (PARS_LE_TOKEN); |
| 71 |
} else { |
| 72 |
/* TODO: LIKE operator */ |
| 73 |
ut_error; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** Checks if the value of an expression can be calculated BEFORE the nth table |
| 78 |
in a join is accessed. If this is the case, it can possibly be used in an |
| 79 |
index search for the nth table. |
| 80 |
@return true if already determined */ |
| 81 |
static bool opt_check_exp_determined_before( |
| 82 |
que_node_t *exp, /*!< in: expression */ |
| 83 |
sel_node_t *sel_node, /*!< in: select node */ |
| 84 |
ulint nth_table) /*!< in: nth table will be accessed */ |
| 85 |
{ |
| 86 |
func_node_t *func_node; |
| 87 |
sym_node_t *sym_node; |
| 88 |
dict_table_t *table; |
| 89 |
que_node_t *arg; |
| 90 |
ulint i; |
| 91 |
|
| 92 |
ut_ad(exp && sel_node); |
| 93 |
|
| 94 |
if (que_node_get_type(exp) == QUE_NODE_FUNC) { |
| 95 |
func_node = static_cast<func_node_t *>(exp); |
| 96 |
|
| 97 |
arg = func_node->args; |
| 98 |
|
| 99 |
while (arg) { |
| 100 |
if (!opt_check_exp_determined_before(arg, sel_node, nth_table)) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
arg = que_node_get_next(arg); |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
ut_a(que_node_get_type(exp) == QUE_NODE_SYMBOL); |
| 111 |
|
| 112 |
sym_node = static_cast<sym_node_t *>(exp); |
| 113 |
|
| 114 |
if (sym_node->token_type != SYM_COLUMN) { |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
for (i = 0; i < nth_table; i++) { |
| 119 |
table = sel_node_get_nth_plan(sel_node, i)->table; |
| 120 |
|
| 121 |
if (sym_node->table == table) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
/** Looks in a comparison condition if a column value is already restricted by |
| 130 |
it BEFORE the nth table is accessed. |
| 131 |
@return expression restricting the value of the column, or NULL if not known */ |
| 132 |
static que_node_t *opt_look_for_col_in_comparison_before( |
| 133 |
ulint cmp_type, /*!< in: OPT_EQUAL, OPT_COMPARISON */ |
| 134 |
ulint col_no, /*!< in: column number */ |
| 135 |
func_node_t *search_cond, /*!< in: comparison condition */ |
| 136 |
sel_node_t *sel_node, /*!< in: select node */ |
| 137 |
ulint nth_table, /*!< in: nth table in a join (a query |
| 138 |
from a single table is considered a |
| 139 |
join of 1 table) */ |
| 140 |
ulint *op) /*!< out: comparison operator ('=', |
| 141 |
PARS_GE_TOKEN, ... ); this is inverted |
| 142 |
if the column appears on the right |
| 143 |
side */ |
| 144 |
{ |
| 145 |
sym_node_t *sym_node; |
| 146 |
dict_table_t *table; |
| 147 |
que_node_t *exp; |
| 148 |
que_node_t *arg; |
| 149 |
|
| 150 |
ut_ad(search_cond); |
| 151 |
|
| 152 |
ut_a((search_cond->func == '<') || (search_cond->func == '>') || |
| 153 |
(search_cond->func == '=') || (search_cond->func == PARS_GE_TOKEN) || |
| 154 |
(search_cond->func == PARS_LE_TOKEN) || |
| 155 |
(search_cond->func == PARS_LIKE_TOKEN_EXACT) || |
| 156 |
(search_cond->func == PARS_LIKE_TOKEN_PREFIX) || |
| 157 |
(search_cond->func == PARS_LIKE_TOKEN_SUFFIX) || |
| 158 |
(search_cond->func == PARS_LIKE_TOKEN_SUBSTR)); |
| 159 |
|
| 160 |
table = sel_node_get_nth_plan(sel_node, nth_table)->table; |
| 161 |
|
| 162 |
if ((cmp_type == OPT_EQUAL) && (search_cond->func != '=') && |
| 163 |
(search_cond->func != PARS_LIKE_TOKEN_EXACT) && |
| 164 |
(search_cond->func != PARS_LIKE_TOKEN_PREFIX)) { |
| 165 |
return (nullptr); |
| 166 |
|
| 167 |
} else if ((cmp_type == OPT_COMPARISON) && (search_cond->func != '<') && |
| 168 |
(search_cond->func != '>') && |
| 169 |
(search_cond->func != PARS_GE_TOKEN) && |
| 170 |
(search_cond->func != PARS_LE_TOKEN) && |
| 171 |
(search_cond->func != PARS_LIKE_TOKEN_PREFIX) && |
| 172 |
(search_cond->func != PARS_LIKE_TOKEN_SUFFIX)) { |
| 173 |
return (nullptr); |
| 174 |
} |
| 175 |
|
| 176 |
arg = search_cond->args; |
| 177 |
|
| 178 |
if (que_node_get_type(arg) == QUE_NODE_SYMBOL) { |
| 179 |
sym_node = static_cast<sym_node_t *>(arg); |
| 180 |
|
| 181 |
if ((sym_node->token_type == SYM_COLUMN) && (sym_node->table == table) && |
| 182 |
(sym_node->col_no == col_no)) { |
| 183 |
/* sym_node contains the desired column id */ |
| 184 |
|
| 185 |
/* Check if the expression on the right side of the |
| 186 |
operator is already determined */ |
| 187 |
|
| 188 |
exp = que_node_get_next(arg); |
| 189 |
|
| 190 |
if (opt_check_exp_determined_before(exp, sel_node, nth_table)) { |
| 191 |
*op = search_cond->func; |
| 192 |
|
| 193 |
return (exp); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
exp = search_cond->args; |
| 199 |
arg = que_node_get_next(arg); |
| 200 |
|
| 201 |
if (que_node_get_type(arg) == QUE_NODE_SYMBOL) { |
| 202 |
sym_node = static_cast<sym_node_t *>(arg); |
| 203 |
|
| 204 |
if ((sym_node->token_type == SYM_COLUMN) && (sym_node->table == table) && |
| 205 |
(sym_node->col_no == col_no)) { |
| 206 |
if (opt_check_exp_determined_before(exp, sel_node, nth_table)) { |
| 207 |
*op = opt_invert_cmp_op(search_cond->func); |
| 208 |
|
| 209 |
return (exp); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return (nullptr); |
| 215 |
} |
| 216 |
|
| 217 |
/** Looks in a search condition if a column value is already restricted by the |
| 218 |
search condition BEFORE the nth table is accessed. Takes into account that |
| 219 |
if we will fetch in an ascending order, we cannot utilize an upper limit for |
| 220 |
a column value; in a descending order, respectively, a lower limit. |
| 221 |
@return expression restricting the value of the column, or NULL if not known */ |
| 222 |
static que_node_t *opt_look_for_col_in_cond_before( |
| 223 |
ulint cmp_type, /*!< in: OPT_EQUAL, OPT_COMPARISON */ |
| 224 |
ulint col_no, /*!< in: column number */ |
| 225 |
func_node_t *search_cond, /*!< in: search condition or NULL */ |
| 226 |
sel_node_t *sel_node, /*!< in: select node */ |
| 227 |
ulint nth_table, /*!< in: nth table in a join (a query |
| 228 |
from a single table is considered a |
| 229 |
join of 1 table) */ |
| 230 |
ulint *op) /*!< out: comparison operator ('=', |
| 231 |
PARS_GE_TOKEN, ... ) */ |
| 232 |
{ |
| 233 |
func_node_t *new_cond; |
| 234 |
que_node_t *exp; |
| 235 |
|
| 236 |
if (search_cond == nullptr) { |
| 237 |
return (nullptr); |
| 238 |
} |
| 239 |
|
| 240 |
ut_a(que_node_get_type(search_cond) == QUE_NODE_FUNC); |
| 241 |
ut_a(search_cond->func != PARS_OR_TOKEN); |
| 242 |
ut_a(search_cond->func != PARS_NOT_TOKEN); |
| 243 |
|
| 244 |
if (search_cond->func == PARS_AND_TOKEN) { |
| 245 |
new_cond = static_cast<func_node_t *>(search_cond->args); |
| 246 |
|
| 247 |
exp = opt_look_for_col_in_cond_before(cmp_type, col_no, new_cond, sel_node, |
| 248 |
nth_table, op); |
| 249 |
if (exp) { |
| 250 |
return (exp); |
| 251 |
} |
| 252 |
|
| 253 |
new_cond = static_cast<func_node_t *>(que_node_get_next(new_cond)); |
| 254 |
|
| 255 |
exp = opt_look_for_col_in_cond_before(cmp_type, col_no, new_cond, sel_node, |
| 256 |
nth_table, op); |
| 257 |
return (exp); |
| 258 |
} |
| 259 |
|
| 260 |
exp = opt_look_for_col_in_comparison_before(cmp_type, col_no, search_cond, |
| 261 |
sel_node, nth_table, op); |
| 262 |
if (exp == nullptr) { |
| 263 |
return (nullptr); |
| 264 |
} |
| 265 |
|
| 266 |
/* If we will fetch in an ascending order, we cannot utilize an upper |
| 267 |
limit for a column value; in a descending order, respectively, a lower |
| 268 |
limit */ |
| 269 |
|
| 270 |
if (sel_node->asc && ((*op == '<') || (*op == PARS_LE_TOKEN))) { |
| 271 |
return (nullptr); |
| 272 |
|
| 273 |
} else if (!sel_node->asc && ((*op == '>') || (*op == PARS_GE_TOKEN))) { |
| 274 |
return (nullptr); |
| 275 |
} |
| 276 |
|
| 277 |
return (exp); |
| 278 |
} |
| 279 |
|
| 280 |
/** Calculates the goodness for an index according to a select node. The |
| 281 |
goodness is 4 times the number of first fields in index whose values we |
| 282 |
already know exactly in the query. If we have a comparison condition for |
| 283 |
an additional field, 2 point are added. If the index is unique, and we know |
| 284 |
all the unique fields for the index we add 1024 points. For a clustered index |
| 285 |
we add 1 point. |
| 286 |
@return goodness */ |
| 287 |
static ulint opt_calc_index_goodness( |
| 288 |
dict_index_t *index, /*!< in: index */ |
| 289 |
sel_node_t *sel_node, /*!< in: parsed select node */ |
| 290 |
ulint nth_table, /*!< in: nth table in a join */ |
| 291 |
que_node_t **index_plan, /*!< in/out: comparison expressions for |
| 292 |
this index */ |
| 293 |
ulint *last_op) /*!< out: last comparison operator, if |
| 294 |
goodness > 1 */ |
| 295 |
{ |
| 296 |
que_node_t *exp; |
| 297 |
ulint goodness; |
| 298 |
ulint n_fields; |
| 299 |
ulint col_no; |
| 300 |
ulint op; |
| 301 |
ulint j; |
| 302 |
|
| 303 |
/* At least for now we don't support using FTS indexes, or |
| 304 |
virtual index for queries done through InnoDB's own SQL parser. */ |
| 305 |
if (dict_index_is_online_ddl(index) || (index->type & DICT_FTS) || |
| 306 |
dict_index_has_virtual(index)) { |
| 307 |
return (0); |
| 308 |
} |
| 309 |
|
| 310 |
goodness = 0; |
| 311 |
|
| 312 |
/* Note that as higher level node pointers in the B-tree contain |
| 313 |
page addresses as the last field, we must not put more fields in |
| 314 |
the search tuple than dict_index_get_n_unique_in_tree(index); see |
| 315 |
the note in btr_cur_search_to_nth_level. */ |
| 316 |
|
| 317 |
n_fields = dict_index_get_n_unique_in_tree(index); |
| 318 |
|
| 319 |
for (j = 0; j < n_fields; j++) { |
| 320 |
if (!index->get_field(j)->is_ascending) { |
| 321 |
/* The internal InnoDB SQL parser does not |
| 322 |
work on indexes with descending order. */ |
| 323 |
return (0); |
| 324 |
} |
| 325 |
|
| 326 |
col_no = index->get_col_no(j); |
| 327 |
|
| 328 |
exp = opt_look_for_col_in_cond_before( |
| 329 |
OPT_EQUAL, col_no, static_cast<func_node_t *>(sel_node->search_cond), |
| 330 |
sel_node, nth_table, &op); |
| 331 |
if (exp) { |
| 332 |
/* The value for this column is exactly known already |
| 333 |
at this stage of the join */ |
| 334 |
|
| 335 |
index_plan[j] = exp; |
| 336 |
*last_op = op; |
| 337 |
goodness += 4; |
| 338 |
} else { |
| 339 |
/* Look for non-equality comparisons */ |
| 340 |
|
| 341 |
exp = opt_look_for_col_in_cond_before( |
| 342 |
OPT_COMPARISON, col_no, |
| 343 |
static_cast<func_node_t *>(sel_node->search_cond), sel_node, |
| 344 |
nth_table, &op); |
| 345 |
if (exp) { |
| 346 |
index_plan[j] = exp; |
| 347 |
*last_op = op; |
| 348 |
goodness += 2; |
| 349 |
} |
| 350 |
|
| 351 |
break; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
if (goodness >= 4 * dict_index_get_n_unique(index)) { |
| 356 |
goodness += 1024; |
| 357 |
|
| 358 |
if (index->is_clustered()) { |
| 359 |
goodness += 1024; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/* We have to test for goodness here, as last_op may not be set */ |
| 364 |
if (goodness && index->is_clustered()) { |
| 365 |
goodness++; |
| 366 |
} |
| 367 |
|
| 368 |
return (goodness); |
| 369 |
} |
| 370 |
|
| 371 |
/** Calculates the number of matched fields based on an index goodness. |
| 372 |
@return number of exactly or partially matched fields */ |
| 373 |
static inline ulint opt_calc_n_fields_from_goodness( |
| 374 |
ulint goodness) /*!< in: goodness */ |
| 375 |
{ |
| 376 |
return (((goodness % 1024) + 2) / 4); |
| 377 |
} |
| 378 |
|
| 379 |
/** Converts a comparison operator to the corresponding search mode PAGE_CUR_GE, |
| 380 |
... |
| 381 |
@return search mode */ |
| 382 |
static inline page_cur_mode_t opt_op_to_search_mode( |
| 383 |
bool asc, /*!< in: true if the rows should be fetched in an |
| 384 |
ascending order */ |
| 385 |
ulint op) /*!< in: operator '=', PARS_GE_TOKEN, ... */ |
| 386 |
{ |
| 387 |
if (op == '=' || op == PARS_LIKE_TOKEN_EXACT || |
| 388 |
op == PARS_LIKE_TOKEN_PREFIX || op == PARS_LIKE_TOKEN_SUFFIX || |
| 389 |
op == PARS_LIKE_TOKEN_SUBSTR) { |
| 390 |
if (asc) { |
| 391 |
return (PAGE_CUR_GE); |
| 392 |
} else { |
| 393 |
return (PAGE_CUR_LE); |
| 394 |
} |
| 395 |
} else if (op == '<') { |
| 396 |
ut_a(!asc); |
| 397 |
return (PAGE_CUR_L); |
| 398 |
} else if (op == '>') { |
| 399 |
ut_a(asc); |
| 400 |
return (PAGE_CUR_G); |
| 401 |
} else if (op == PARS_GE_TOKEN) { |
| 402 |
ut_a(asc); |
| 403 |
return (PAGE_CUR_GE); |
| 404 |
} else if (op == PARS_LE_TOKEN) { |
| 405 |
ut_a(!asc); |
| 406 |
return (PAGE_CUR_LE); |
| 407 |
} else { |
| 408 |
ut_error; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/** Determines if a node is an argument node of a function node. |
| 413 |
@return true if is an argument */ |
| 414 |
static bool opt_is_arg(que_node_t *arg_node, /*!< in: possible argument node */ |
| 415 |
func_node_t *func_node) /*!< in: function node */ |
| 416 |
{ |
| 417 |
que_node_t *arg; |
| 418 |
|
| 419 |
arg = func_node->args; |
| 420 |
|
| 421 |
while (arg) { |
| 422 |
if (arg == arg_node) { |
| 423 |
return true; |
| 424 |
} |
| 425 |
|
| 426 |
arg = que_node_get_next(arg); |
| 427 |
} |
| 428 |
|
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
/** Decides if the fetching of rows should be made in a descending order, and |
| 433 |
also checks that the chosen query plan produces a result which satisfies |
| 434 |
the order-by. */ |
| 435 |
static void opt_check_order_by( |
| 436 |
sel_node_t *sel_node) /*!< in: select node; asserts an error |
| 437 |
if the plan does not agree with the |
| 438 |
order-by */ |
| 439 |
{ |
| 440 |
order_node_t *order_node; |
| 441 |
dict_table_t *order_table; |
| 442 |
ulint order_col_no; |
| 443 |
plan_t *plan; |
| 444 |
ulint i; |
| 445 |
|
| 446 |
if (!sel_node->order_by) { |
| 447 |
return; |
| 448 |
} |
| 449 |
|
| 450 |
order_node = sel_node->order_by; |
| 451 |
order_col_no = order_node->column->col_no; |
| 452 |
order_table = order_node->column->table; |
| 453 |
|
| 454 |
/* If there is an order-by clause, the first non-exactly matched field |
| 455 |
in the index used for the last table in the table list should be the |
| 456 |
column defined in the order-by clause, and for all the other tables |
| 457 |
we should get only at most a single row, otherwise we cannot presently |
| 458 |
calculate the order-by, as we have no sort utility */ |
| 459 |
|
| 460 |
for (i = 0; i < sel_node->n_tables; i++) { |
| 461 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 462 |
|
| 463 |
if (i < sel_node->n_tables - 1) { |
| 464 |
ut_a(dict_index_get_n_unique(plan->index) <= plan->n_exact_match); |
| 465 |
} else { |
| 466 |
ut_a(plan->table == order_table); |
| 467 |
|
| 468 |
ut_a((dict_index_get_n_unique(plan->index) <= plan->n_exact_match) || |
| 469 |
(plan->index->get_col_no(plan->n_exact_match) == order_col_no)); |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/** Optimizes a select. Decides which indexes to tables to use. The tables |
| 475 |
are accessed in the order that they were written to the FROM part in the |
| 476 |
select statement. */ |
| 477 |
static void opt_search_plan_for_table( |
| 478 |
sel_node_t *sel_node, /*!< in: parsed select node */ |
| 479 |
ulint i, /*!< in: this is the ith table */ |
| 480 |
dict_table_t *table) /*!< in: table */ |
| 481 |
{ |
| 482 |
plan_t *plan; |
| 483 |
dict_index_t *index; |
| 484 |
dict_index_t *best_index; |
| 485 |
ulint n_fields; |
| 486 |
ulint goodness; |
| 487 |
ulint last_op = 75946965; /* Eliminate a Purify |
| 488 |
warning */ |
| 489 |
ulint best_goodness; |
| 490 |
ulint best_last_op = 0; /* remove warning */ |
| 491 |
que_node_t *index_plan[256]; |
| 492 |
que_node_t *best_index_plan[256]; |
| 493 |
|
| 494 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 495 |
|
| 496 |
plan->table = table; |
| 497 |
plan->asc = sel_node->asc; |
| 498 |
plan->pcur_is_open = false; |
| 499 |
plan->cursor_at_end = false; |
| 500 |
|
| 501 |
/* Calculate goodness for each index of the table */ |
| 502 |
|
| 503 |
index = table->first_index(); |
| 504 |
best_index = index; /* Eliminate compiler warning */ |
| 505 |
best_goodness = 0; |
| 506 |
|
| 507 |
do { |
| 508 |
goodness = |
| 509 |
opt_calc_index_goodness(index, sel_node, i, index_plan, &last_op); |
| 510 |
if (goodness > best_goodness) { |
| 511 |
best_index = index; |
| 512 |
best_goodness = goodness; |
| 513 |
n_fields = opt_calc_n_fields_from_goodness(goodness); |
| 514 |
|
| 515 |
ut_memcpy(best_index_plan, index_plan, n_fields * sizeof(void *)); |
| 516 |
best_last_op = last_op; |
| 517 |
} |
| 518 |
|
| 519 |
dict_table_next_uncorrupted_index(index); |
| 520 |
} while (index); |
| 521 |
|
| 522 |
plan->index = best_index; |
| 523 |
|
| 524 |
n_fields = opt_calc_n_fields_from_goodness(best_goodness); |
| 525 |
|
| 526 |
if (n_fields == 0) { |
| 527 |
plan->tuple = nullptr; |
| 528 |
plan->n_exact_match = 0; |
| 529 |
} else { |
| 530 |
plan->tuple = dtuple_create(pars_sym_tab_global->heap, n_fields); |
| 531 |
dict_index_copy_types(plan->tuple, plan->index, n_fields); |
| 532 |
|
| 533 |
plan->tuple_exps = static_cast<que_node_t **>( |
| 534 |
mem_heap_alloc(pars_sym_tab_global->heap, n_fields * sizeof(void *))); |
| 535 |
|
| 536 |
ut_memcpy(plan->tuple_exps, best_index_plan, n_fields * sizeof(void *)); |
| 537 |
if (best_last_op == '=' || best_last_op == PARS_LIKE_TOKEN_EXACT || |
| 538 |
best_last_op == PARS_LIKE_TOKEN_PREFIX || |
| 539 |
best_last_op == PARS_LIKE_TOKEN_SUFFIX || |
| 540 |
best_last_op == PARS_LIKE_TOKEN_SUBSTR) { |
| 541 |
plan->n_exact_match = n_fields; |
| 542 |
} else { |
| 543 |
plan->n_exact_match = n_fields - 1; |
| 544 |
} |
| 545 |
|
| 546 |
plan->mode = opt_op_to_search_mode(sel_node->asc, best_last_op); |
| 547 |
} |
| 548 |
|
| 549 |
if (best_index->is_clustered() && |
| 550 |
(plan->n_exact_match >= dict_index_get_n_unique(best_index))) { |
| 551 |
plan->unique_search = true; |
| 552 |
} else { |
| 553 |
plan->unique_search = false; |
| 554 |
} |
| 555 |
|
| 556 |
plan->old_vers_heap = nullptr; |
| 557 |
|
| 558 |
plan->pcur.init(); |
| 559 |
plan->clust_pcur.init(); |
| 560 |
} |
| 561 |
|
| 562 |
/** Looks at a comparison condition and decides if it can, and need, be tested |
| 563 |
for a table AFTER the table has been accessed. |
| 564 |
@return OPT_NOT_COND if not for this table, else OPT_END_COND, |
| 565 |
OPT_TEST_COND, or OPT_SCROLL_COND, where the last means that the |
| 566 |
condition need not be tested, except when scroll cursors are used */ |
| 567 |
static ulint opt_classify_comparison( |
| 568 |
sel_node_t *sel_node, /*!< in: select node */ |
| 569 |
ulint i, /*!< in: ith table in the join */ |
| 570 |
func_node_t *cond) /*!< in: comparison condition */ |
| 571 |
{ |
| 572 |
plan_t *plan; |
| 573 |
ulint n_fields; |
| 574 |
ulint op; |
| 575 |
ulint j; |
| 576 |
|
| 577 |
ut_ad(cond && sel_node); |
| 578 |
|
| 579 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 580 |
|
| 581 |
/* Check if the condition is determined after the ith table has been |
| 582 |
accessed, but not after the i - 1:th */ |
| 583 |
|
| 584 |
if (!opt_check_exp_determined_before(cond, sel_node, i + 1)) { |
| 585 |
return (OPT_NOT_COND); |
| 586 |
} |
| 587 |
|
| 588 |
if ((i > 0) && opt_check_exp_determined_before(cond, sel_node, i)) { |
| 589 |
return (OPT_NOT_COND); |
| 590 |
} |
| 591 |
|
| 592 |
/* If the condition is an exact match condition used in constructing |
| 593 |
the search tuple, it is classified as OPT_END_COND */ |
| 594 |
|
| 595 |
if (plan->tuple) { |
| 596 |
n_fields = dtuple_get_n_fields(plan->tuple); |
| 597 |
} else { |
| 598 |
n_fields = 0; |
| 599 |
} |
| 600 |
|
| 601 |
for (j = 0; j < plan->n_exact_match; j++) { |
| 602 |
if (opt_is_arg(plan->tuple_exps[j], cond)) { |
| 603 |
return (OPT_END_COND); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
/* If the condition is an non-exact match condition used in |
| 608 |
constructing the search tuple, it is classified as OPT_SCROLL_COND. |
| 609 |
When the cursor is positioned, and if a non-scroll cursor is used, |
| 610 |
there is no need to test this condition; if a scroll cursor is used |
| 611 |
the testing is necessary when the cursor is reversed. */ |
| 612 |
|
| 613 |
if ((n_fields > plan->n_exact_match) && |
| 614 |
opt_is_arg(plan->tuple_exps[n_fields - 1], cond)) { |
| 615 |
return (OPT_SCROLL_COND); |
| 616 |
} |
| 617 |
|
| 618 |
/* If the condition is a non-exact match condition on the first field |
| 619 |
in index for which there is no exact match, and it limits the search |
| 620 |
range from the opposite side of the search tuple already BEFORE we |
| 621 |
access the table, it is classified as OPT_END_COND */ |
| 622 |
|
| 623 |
if ((dict_index_get_n_fields(plan->index) > plan->n_exact_match) && |
| 624 |
opt_look_for_col_in_comparison_before( |
| 625 |
OPT_COMPARISON, plan->index->get_col_no(plan->n_exact_match), cond, |
| 626 |
sel_node, i, &op)) { |
| 627 |
if (sel_node->asc && ((op == '<') || (op == PARS_LE_TOKEN))) { |
| 628 |
return (OPT_END_COND); |
| 629 |
} |
| 630 |
|
| 631 |
if (!sel_node->asc && ((op == '>') || (op == PARS_GE_TOKEN))) { |
| 632 |
return (OPT_END_COND); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
/* Otherwise, cond is classified as OPT_TEST_COND */ |
| 637 |
|
| 638 |
return (OPT_TEST_COND); |
| 639 |
} |
| 640 |
|
| 641 |
/** Recursively looks for test conditions for a table in a join. */ |
| 642 |
static void opt_find_test_conds(sel_node_t *sel_node, /*!< in: select node */ |
| 643 |
ulint i, /*!< in: ith table in the join */ |
| 644 |
func_node_t *cond) /*!< in: conjunction of |
| 645 |
search conditions or NULL */ |
| 646 |
{ |
| 647 |
func_node_t *new_cond; |
| 648 |
ulint fclass; |
| 649 |
plan_t *plan; |
| 650 |
|
| 651 |
if (cond == nullptr) { |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
if (cond->func == PARS_AND_TOKEN) { |
| 656 |
new_cond = static_cast<func_node_t *>(cond->args); |
| 657 |
|
| 658 |
opt_find_test_conds(sel_node, i, new_cond); |
| 659 |
|
| 660 |
new_cond = static_cast<func_node_t *>(que_node_get_next(new_cond)); |
| 661 |
|
| 662 |
opt_find_test_conds(sel_node, i, new_cond); |
| 663 |
|
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 668 |
|
| 669 |
fclass = opt_classify_comparison(sel_node, i, cond); |
| 670 |
|
| 671 |
if (fclass == OPT_END_COND) { |
| 672 |
UT_LIST_ADD_LAST(plan->end_conds, cond); |
| 673 |
|
| 674 |
} else if (fclass == OPT_TEST_COND) { |
| 675 |
UT_LIST_ADD_LAST(plan->other_conds, cond); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** Normalizes a list of comparison conditions so that a column of the table |
| 680 |
appears on the left side of the comparison if possible. This is accomplished |
| 681 |
by switching the arguments of the operator. */ |
| 682 |
static void opt_normalize_cmp_conds( |
| 683 |
func_node_t *cond, /*!< in: first in a list of comparison |
| 684 |
conditions, or NULL */ |
| 685 |
dict_table_t *table) /*!< in: table */ |
| 686 |
{ |
| 687 |
que_node_t *arg1; |
| 688 |
que_node_t *arg2; |
| 689 |
sym_node_t *sym_node; |
| 690 |
|
| 691 |
while (cond) { |
| 692 |
arg1 = cond->args; |
| 693 |
arg2 = que_node_get_next(arg1); |
| 694 |
|
| 695 |
if (que_node_get_type(arg2) == QUE_NODE_SYMBOL) { |
| 696 |
sym_node = static_cast<sym_node_t *>(arg2); |
| 697 |
|
| 698 |
if ((sym_node->token_type == SYM_COLUMN) && (sym_node->table == table)) { |
| 699 |
/* Switch the order of the arguments */ |
| 700 |
|
| 701 |
cond->args = arg2; |
| 702 |
que_node_list_add_last(nullptr, arg2); |
| 703 |
que_node_list_add_last(arg2, arg1); |
| 704 |
|
| 705 |
/* Invert the operator */ |
| 706 |
cond->func = opt_invert_cmp_op(cond->func); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
cond = UT_LIST_GET_NEXT(cond_list, cond); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
/** Finds out the search condition conjuncts we can, and need, to test as the |
| 715 |
ith table in a join is accessed. The search tuple can eliminate the need to |
| 716 |
test some conjuncts. */ |
| 717 |
static void opt_determine_and_normalize_test_conds( |
| 718 |
sel_node_t *sel_node, /*!< in: select node */ |
| 719 |
ulint i) /*!< in: ith table in the join */ |
| 720 |
{ |
| 721 |
plan_t *plan; |
| 722 |
|
| 723 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 724 |
|
| 725 |
UT_LIST_INIT(plan->end_conds); |
| 726 |
UT_LIST_INIT(plan->other_conds); |
| 727 |
|
| 728 |
/* Recursively go through the conjuncts and classify them */ |
| 729 |
|
| 730 |
opt_find_test_conds(sel_node, i, |
| 731 |
static_cast<func_node_t *>(sel_node->search_cond)); |
| 732 |
|
| 733 |
opt_normalize_cmp_conds(UT_LIST_GET_FIRST(plan->end_conds), plan->table); |
| 734 |
|
| 735 |
ut_a(UT_LIST_GET_LEN(plan->end_conds) >= plan->n_exact_match); |
| 736 |
} |
| 737 |
|
| 738 |
/** Looks for occurrences of the columns of the table in the query subgraph and |
| 739 |
adds them to the list of columns if an occurrence of the same column does not |
| 740 |
already exist in the list. If the column is already in the list, puts a value |
| 741 |
indirection to point to the occurrence in the column list, except if the |
| 742 |
column occurrence we are looking at is in the column list, in which case |
| 743 |
nothing is done. */ |
| 744 |
void opt_find_all_cols( |
| 745 |
bool copy_val, /*!< in: if true, new found columns are |
| 746 |
added as columns to copy */ |
| 747 |
dict_index_t *index, /*!< in: index of the table to use */ |
| 748 |
sym_node_list_t *col_list, /*!< in: base node of a list where |
| 749 |
to add new found columns */ |
| 750 |
plan_t *plan, /*!< in: plan or NULL */ |
| 751 |
que_node_t *exp) /*!< in: expression or condition or |
| 752 |
NULL */ |
| 753 |
{ |
| 754 |
func_node_t *func_node; |
| 755 |
que_node_t *arg; |
| 756 |
sym_node_t *sym_node; |
| 757 |
ulint col_pos; |
| 758 |
|
| 759 |
if (exp == nullptr) { |
| 760 |
return; |
| 761 |
} |
| 762 |
|
| 763 |
if (que_node_get_type(exp) == QUE_NODE_FUNC) { |
| 764 |
func_node = static_cast<func_node_t *>(exp); |
| 765 |
|
| 766 |
for (arg = func_node->args; arg != nullptr; arg = que_node_get_next(arg)) { |
| 767 |
opt_find_all_cols(copy_val, index, col_list, plan, arg); |
| 768 |
} |
| 769 |
|
| 770 |
return; |
| 771 |
} |
| 772 |
|
| 773 |
ut_a(que_node_get_type(exp) == QUE_NODE_SYMBOL); |
| 774 |
|
| 775 |
sym_node = static_cast<sym_node_t *>(exp); |
| 776 |
|
| 777 |
if (sym_node->token_type != SYM_COLUMN) { |
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
if (sym_node->table != index->table) { |
| 782 |
return; |
| 783 |
} |
| 784 |
|
| 785 |
/* Look for an occurrence of the same column in the plan column |
| 786 |
list */ |
| 787 |
|
| 788 |
for (auto col_node : *col_list) { |
| 789 |
if (col_node->col_no == sym_node->col_no) { |
| 790 |
if (col_node == sym_node) { |
| 791 |
/* sym_node was already in a list: do |
| 792 |
nothing */ |
| 793 |
|
| 794 |
return; |
| 795 |
} |
| 796 |
|
| 797 |
/* Put an indirection */ |
| 798 |
sym_node->indirection = col_node; |
| 799 |
sym_node->alias = col_node; |
| 800 |
|
| 801 |
return; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
/* The same column did not occur in the list: add it */ |
| 806 |
|
| 807 |
UT_LIST_ADD_LAST(*col_list, sym_node); |
| 808 |
|
| 809 |
sym_node->copy_val = copy_val; |
| 810 |
|
| 811 |
/* Fill in the field_no fields in sym_node */ |
| 812 |
|
| 813 |
sym_node->field_nos[SYM_CLUST_FIELD_NO] = |
| 814 |
index->table->first_index()->get_col_pos(sym_node->col_no); |
| 815 |
if (!index->is_clustered()) { |
| 816 |
ut_a(plan); |
| 817 |
|
| 818 |
col_pos = index->get_col_pos(sym_node->col_no); |
| 819 |
|
| 820 |
if (col_pos == ULINT_UNDEFINED) { |
| 821 |
plan->must_get_clust = true; |
| 822 |
} |
| 823 |
|
| 824 |
sym_node->field_nos[SYM_SEC_FIELD_NO] = col_pos; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
/** Looks for occurrences of the columns of the table in conditions which are |
| 829 |
not yet determined AFTER the join operation has fetched a row in the ith |
| 830 |
table. The values for these column must be copied to dynamic memory for |
| 831 |
later use. */ |
| 832 |
static void opt_find_copy_cols( |
| 833 |
sel_node_t *sel_node, /*!< in: select node */ |
| 834 |
ulint i, /*!< in: ith table in the join */ |
| 835 |
func_node_t *search_cond) /*!< in: search condition or NULL */ |
| 836 |
{ |
| 837 |
func_node_t *new_cond; |
| 838 |
plan_t *plan; |
| 839 |
|
| 840 |
if (search_cond == nullptr) { |
| 841 |
return; |
| 842 |
} |
| 843 |
|
| 844 |
ut_ad(que_node_get_type(search_cond) == QUE_NODE_FUNC); |
| 845 |
|
| 846 |
if (search_cond->func == PARS_AND_TOKEN) { |
| 847 |
new_cond = static_cast<func_node_t *>(search_cond->args); |
| 848 |
|
| 849 |
opt_find_copy_cols(sel_node, i, new_cond); |
| 850 |
|
| 851 |
new_cond = static_cast<func_node_t *>(que_node_get_next(new_cond)); |
| 852 |
|
| 853 |
opt_find_copy_cols(sel_node, i, new_cond); |
| 854 |
|
| 855 |
return; |
| 856 |
} |
| 857 |
|
| 858 |
if (!opt_check_exp_determined_before(search_cond, sel_node, i + 1)) { |
| 859 |
/* Any ith table columns occurring in search_cond should be |
| 860 |
copied, as this condition cannot be tested already on the |
| 861 |
fetch from the ith table */ |
| 862 |
|
| 863 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 864 |
|
| 865 |
opt_find_all_cols(true, plan->index, &(plan->columns), plan, search_cond); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
/** Classifies the table columns according to whether we use the column only |
| 870 |
while holding the latch on the page, or whether we have to copy the column |
| 871 |
value to dynamic memory. Puts the first occurrence of a column to either list |
| 872 |
in the plan node, and puts indirections to later occurrences of the column. */ |
| 873 |
static void opt_classify_cols(sel_node_t *sel_node, /*!< in: select node */ |
| 874 |
ulint i) /*!< in: ith table in the join */ |
| 875 |
{ |
| 876 |
plan_t *plan; |
| 877 |
que_node_t *exp; |
| 878 |
|
| 879 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 880 |
|
| 881 |
/* The final value of the following field will depend on the |
| 882 |
environment of the select statement: */ |
| 883 |
|
| 884 |
plan->must_get_clust = false; |
| 885 |
|
| 886 |
UT_LIST_INIT(plan->columns); |
| 887 |
|
| 888 |
/* All select list columns should be copied: therefore true as the |
| 889 |
first argument */ |
| 890 |
|
| 891 |
for (exp = sel_node->select_list; exp != nullptr; |
| 892 |
exp = que_node_get_next(exp)) { |
| 893 |
opt_find_all_cols(true, plan->index, &(plan->columns), plan, exp); |
| 894 |
} |
| 895 |
|
| 896 |
opt_find_copy_cols(sel_node, i, |
| 897 |
static_cast<func_node_t *>(sel_node->search_cond)); |
| 898 |
|
| 899 |
/* All remaining columns in the search condition are temporary |
| 900 |
columns: therefore false */ |
| 901 |
|
| 902 |
opt_find_all_cols(false, plan->index, &plan->columns, plan, |
| 903 |
static_cast<func_node_t *>(sel_node->search_cond)); |
| 904 |
} |
| 905 |
|
| 906 |
/** Fills in the info in plan which is used in accessing a clustered index |
| 907 |
record. The columns must already be classified for the plan node. */ |
| 908 |
static void opt_clust_access(sel_node_t *sel_node, /*!< in: select node */ |
| 909 |
ulint n) /*!< in: nth table in select */ |
| 910 |
{ |
| 911 |
plan_t *plan; |
| 912 |
dict_table_t *table; |
| 913 |
dict_index_t *clust_index; |
| 914 |
dict_index_t *index; |
| 915 |
mem_heap_t *heap; |
| 916 |
ulint n_fields; |
| 917 |
ulint pos; |
| 918 |
ulint i; |
| 919 |
|
| 920 |
plan = sel_node_get_nth_plan(sel_node, n); |
| 921 |
|
| 922 |
index = plan->index; |
| 923 |
|
| 924 |
/* The final value of the following field depends on the environment |
| 925 |
of the select statement: */ |
| 926 |
|
| 927 |
plan->no_prefetch = false; |
| 928 |
|
| 929 |
if (index->is_clustered()) { |
| 930 |
plan->clust_map = nullptr; |
| 931 |
plan->clust_ref = nullptr; |
| 932 |
|
| 933 |
return; |
| 934 |
} |
| 935 |
|
| 936 |
table = index->table; |
| 937 |
|
| 938 |
clust_index = table->first_index(); |
| 939 |
|
| 940 |
n_fields = dict_index_get_n_unique(clust_index); |
| 941 |
|
| 942 |
heap = pars_sym_tab_global->heap; |
| 943 |
|
| 944 |
plan->clust_ref = dtuple_create(heap, n_fields); |
| 945 |
|
| 946 |
dict_index_copy_types(plan->clust_ref, clust_index, n_fields); |
| 947 |
|
| 948 |
plan->clust_map = |
| 949 |
static_cast<ulint *>(mem_heap_alloc(heap, n_fields * sizeof(ulint))); |
| 950 |
|
| 951 |
for (i = 0; i < n_fields; i++) { |
| 952 |
pos = dict_index_get_nth_field_pos(index, clust_index, i); |
| 953 |
|
| 954 |
ut_a(pos != ULINT_UNDEFINED); |
| 955 |
|
| 956 |
/* We optimize here only queries to InnoDB's internal system |
| 957 |
tables, and they should not contain column prefix indexes. */ |
| 958 |
|
| 959 |
if (dict_is_old_sys_table(index->table->id) && |
| 960 |
(index->get_field(pos)->prefix_len != 0 || |
| 961 |
clust_index->get_field(i)->prefix_len != 0)) { |
| 962 |
ib::error(ER_IB_MSG_916) << "Error in pars0opt.cc: table " |
| 963 |
<< index->table->name << " has prefix_len != 0"; |
| 964 |
} |
| 965 |
|
| 966 |
*(plan->clust_map + i) = pos; |
| 967 |
|
| 968 |
ut_ad(pos != ULINT_UNDEFINED); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
#ifdef UNIV_SQL_DEBUG |
| 973 |
/** Print info of a query plan. |
| 974 |
@param[in,out] sel_node select node */ |
| 975 |
static void opt_print_query_plan(sel_node_t *sel_node); |
| 976 |
#endif |
| 977 |
|
| 978 |
/** Optimizes a select. Decides which indexes to tables to use. The tables |
| 979 |
are accessed in the order that they were written to the FROM part in the |
| 980 |
select statement. */ |
| 981 |
void opt_search_plan(sel_node_t *sel_node) /*!< in: parsed select node */ |
| 982 |
{ |
| 983 |
sym_node_t *table_node; |
| 984 |
dict_table_t *table; |
| 985 |
order_node_t *order_by; |
| 986 |
ulint i; |
| 987 |
|
| 988 |
sel_node->plans = static_cast<plan_t *>(mem_heap_alloc( |
| 989 |
pars_sym_tab_global->heap, sel_node->n_tables * sizeof(plan_t))); |
| 990 |
|
| 991 |
/* Analyze the search condition to find out what we know at each |
| 992 |
join stage about the conditions that the columns of a table should |
| 993 |
satisfy */ |
| 994 |
|
| 995 |
table_node = sel_node->table_list; |
| 996 |
|
| 997 |
if (sel_node->order_by == nullptr) { |
| 998 |
sel_node->asc = true; |
| 999 |
} else { |
| 1000 |
order_by = sel_node->order_by; |
| 1001 |
|
| 1002 |
sel_node->asc = order_by->asc; |
| 1003 |
} |
| 1004 |
|
| 1005 |
for (i = 0; i < sel_node->n_tables; i++) { |
| 1006 |
table = table_node->table; |
| 1007 |
|
| 1008 |
/* Choose index through which to access the table */ |
| 1009 |
|
| 1010 |
opt_search_plan_for_table(sel_node, i, table); |
| 1011 |
|
| 1012 |
/* Determine the search condition conjuncts we can test at |
| 1013 |
this table; normalize the end conditions */ |
| 1014 |
|
| 1015 |
opt_determine_and_normalize_test_conds(sel_node, i); |
| 1016 |
|
| 1017 |
table_node = static_cast<sym_node_t *>(que_node_get_next(table_node)); |
| 1018 |
} |
| 1019 |
|
| 1020 |
table_node = sel_node->table_list; |
| 1021 |
|
| 1022 |
for (i = 0; i < sel_node->n_tables; i++) { |
| 1023 |
/* Classify the table columns into those we only need to access |
| 1024 |
but not copy, and to those we must copy to dynamic memory */ |
| 1025 |
|
| 1026 |
opt_classify_cols(sel_node, i); |
| 1027 |
|
| 1028 |
/* Calculate possible info for accessing the clustered index |
| 1029 |
record */ |
| 1030 |
|
| 1031 |
opt_clust_access(sel_node, i); |
| 1032 |
|
| 1033 |
table_node = static_cast<sym_node_t *>(que_node_get_next(table_node)); |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* Check that the plan obeys a possible order-by clause: if not, |
| 1037 |
an assertion error occurs */ |
| 1038 |
|
| 1039 |
opt_check_order_by(sel_node); |
| 1040 |
|
| 1041 |
#ifdef UNIV_SQL_DEBUG |
| 1042 |
opt_print_query_plan(sel_node); |
| 1043 |
#endif |
| 1044 |
} |
| 1045 |
|
| 1046 |
#ifdef UNIV_SQL_DEBUG |
| 1047 |
/** Print info of a query plan. |
| 1048 |
@param[in,out] sel_node select node */ |
| 1049 |
static void opt_print_query_plan(sel_node_t *sel_node) { |
| 1050 |
plan_t *plan; |
| 1051 |
ulint n_fields; |
| 1052 |
ulint i; |
| 1053 |
|
| 1054 |
fputs("QUERY PLAN FOR A SELECT NODE\n", stderr); |
| 1055 |
|
| 1056 |
fputs(sel_node->asc ? "Asc. search; " : "Desc. search; ", stderr); |
| 1057 |
|
| 1058 |
if (sel_node->set_x_locks) { |
| 1059 |
fputs("sets row x-locks; ", stderr); |
| 1060 |
ut_a(sel_node->row_lock_mode == LOCK_X); |
| 1061 |
ut_a(!sel_node->consistent_read); |
| 1062 |
} else if (sel_node->consistent_read) { |
| 1063 |
fputs("consistent read; ", stderr); |
| 1064 |
} else { |
| 1065 |
ut_a(sel_node->row_lock_mode == LOCK_S); |
| 1066 |
fputs("sets row s-locks; ", stderr); |
| 1067 |
} |
| 1068 |
|
| 1069 |
putc('\n', stderr); |
| 1070 |
|
| 1071 |
for (i = 0; i < sel_node->n_tables; i++) { |
| 1072 |
plan = sel_node_get_nth_plan(sel_node, i); |
| 1073 |
|
| 1074 |
if (plan->tuple) { |
| 1075 |
n_fields = dtuple_get_n_fields(plan->tuple); |
| 1076 |
} else { |
| 1077 |
n_fields = 0; |
| 1078 |
} |
| 1079 |
|
| 1080 |
fprintf(stderr, |
| 1081 |
"Index %s of table %s" |
| 1082 |
"; exact m. %lu, match %lu, end conds %lu\n", |
| 1083 |
plan->index->name(), plan->index->table_name, |
| 1084 |
(unsigned long)plan->n_exact_match, (unsigned long)n_fields, |
| 1085 |
(unsigned long)UT_LIST_GET_LEN(plan->end_conds)); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
#endif /* UNIV_SQL_DEBUG */ |