00001 /* Copyright (C) 2000-2004 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 00018 /* mysql_select and join optimization */ 00019 00020 #ifdef USE_PRAGMA_IMPLEMENTATION 00021 #pragma implementation // gcc: Class implementation 00022 #endif 00023 00024 #include "mysql_priv.h" 00025 #include "sql_select.h" 00026 #include "sql_cursor.h" 00027 00028 #include <m_ctype.h> 00029 #include <hash.h> 00030 #include <ft_global.h> 00031 00032 const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref", 00033 "MAYBE_REF","ALL","range","index","fulltext", 00034 "ref_or_null","unique_subquery","index_subquery", 00035 "index_merge" 00036 }; 00037 00038 static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array); 00039 static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds, 00040 DYNAMIC_ARRAY *keyuse); 00041 static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse, 00042 JOIN_TAB *join_tab, 00043 uint tables, COND *conds, 00044 COND_EQUAL *cond_equal, 00045 table_map table_map, SELECT_LEX *select_lex); 00046 static int sort_keyuse(KEYUSE *a,KEYUSE *b); 00047 static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key); 00048 static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse, 00049 table_map used_tables); 00050 static void choose_plan(JOIN *join,table_map join_tables); 00051 00052 static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd, 00053 table_map remaining_tables, uint idx, 00054 double record_count, double read_time); 00055 static void optimize_straight_join(JOIN *join, table_map join_tables); 00056 static void greedy_search(JOIN *join, table_map remaining_tables, 00057 uint depth, uint prune_level); 00058 static void best_extension_by_limited_search(JOIN *join, 00059 table_map remaining_tables, 00060 uint idx, double record_count, 00061 double read_time, uint depth, 00062 uint prune_level); 00063 static uint determine_search_depth(JOIN* join); 00064 static int join_tab_cmp(const void* ptr1, const void* ptr2); 00065 static int join_tab_cmp_straight(const void* ptr1, const void* ptr2); 00066 /* 00067 TODO: 'find_best' is here only temporarily until 'greedy_search' is 00068 tested and approved. 00069 */ 00070 static void find_best(JOIN *join,table_map rest_tables,uint index, 00071 double record_count,double read_time); 00072 static uint cache_record_length(JOIN *join,uint index); 00073 static double prev_record_reads(JOIN *join,table_map found_ref); 00074 static bool get_best_combination(JOIN *join); 00075 static store_key *get_store_key(THD *thd, 00076 KEYUSE *keyuse, table_map used_tables, 00077 KEY_PART_INFO *key_part, char *key_buff, 00078 uint maybe_null); 00079 static bool make_simple_join(JOIN *join,TABLE *tmp_table); 00080 static void make_outerjoin_info(JOIN *join); 00081 static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item); 00082 static void make_join_readinfo(JOIN *join,uint options); 00083 static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables); 00084 static void update_depend_map(JOIN *join); 00085 static void update_depend_map(JOIN *join, ORDER *order); 00086 static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond, 00087 bool change_list, bool *simple_order); 00088 static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables, 00089 List<Item> &fields, bool send_row, 00090 uint select_options, const char *info, 00091 Item *having); 00092 static COND *build_equal_items(THD *thd, COND *cond, 00093 COND_EQUAL *inherited, 00094 List<TABLE_LIST> *join_list, 00095 COND_EQUAL **cond_equal_ref); 00096 static COND* substitute_for_best_equal_field(COND *cond, 00097 COND_EQUAL *cond_equal, 00098 void *table_join_idx); 00099 static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, 00100 COND *conds, bool top); 00101 static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next); 00102 static void restore_prev_nj_state(JOIN_TAB *last); 00103 static void reset_nj_counters(List<TABLE_LIST> *join_list); 00104 static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 00105 uint first_unused); 00106 00107 static COND *optimize_cond(JOIN *join, COND *conds, 00108 List<TABLE_LIST> *join_list, 00109 Item::cond_result *cond_value); 00110 static bool resolve_nested_join (TABLE_LIST *table); 00111 static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item); 00112 static bool open_tmp_table(TABLE *table); 00113 static bool create_myisam_tmp_table(TABLE *table,TMP_TABLE_PARAM *param, 00114 ulong options); 00115 static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table, 00116 Procedure *proc); 00117 00118 static enum_nested_loop_state 00119 evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, 00120 int error, my_bool *report_error); 00121 static enum_nested_loop_state 00122 evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab); 00123 static enum_nested_loop_state 00124 flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last); 00125 static enum_nested_loop_state 00126 end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); 00127 static enum_nested_loop_state 00128 end_send_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); 00129 static enum_nested_loop_state 00130 end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); 00131 static enum_nested_loop_state 00132 end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); 00133 static enum_nested_loop_state 00134 end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); 00135 static enum_nested_loop_state 00136 end_write_group(JOIN *join, JOIN_TAB *join_tab, bool end_of_records); 00137 00138 static int test_if_group_changed(List<Cached_item> &list); 00139 static int join_read_const_table(JOIN_TAB *tab, POSITION *pos); 00140 static int join_read_system(JOIN_TAB *tab); 00141 static int join_read_const(JOIN_TAB *tab); 00142 static int join_read_key(JOIN_TAB *tab); 00143 static int join_read_always_key(JOIN_TAB *tab); 00144 static int join_read_last_key(JOIN_TAB *tab); 00145 static int join_no_more_records(READ_RECORD *info); 00146 static int join_read_next(READ_RECORD *info); 00147 static int join_init_quick_read_record(JOIN_TAB *tab); 00148 static int test_if_quick_select(JOIN_TAB *tab); 00149 static int join_init_read_record(JOIN_TAB *tab); 00150 static int join_read_first(JOIN_TAB *tab); 00151 static int join_read_next(READ_RECORD *info); 00152 static int join_read_next_same(READ_RECORD *info); 00153 static int join_read_last(JOIN_TAB *tab); 00154 static int join_read_prev_same(READ_RECORD *info); 00155 static int join_read_prev(READ_RECORD *info); 00156 static int join_ft_read_first(JOIN_TAB *tab); 00157 static int join_ft_read_next(READ_RECORD *info); 00158 static int join_read_always_key_or_null(JOIN_TAB *tab); 00159 static int join_read_next_same_or_null(READ_RECORD *info); 00160 static COND *make_cond_for_table(COND *cond,table_map table, 00161 table_map used_table); 00162 static Item* part_of_refkey(TABLE *form,Field *field); 00163 uint find_shortest_key(TABLE *table, const key_map *usable_keys); 00164 static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order, 00165 ha_rows select_limit, bool no_changes); 00166 static bool list_contains_unique_index(TABLE *table, 00167 bool (*find_func) (Field *, void *), void *data); 00168 static bool find_field_in_item_list (Field *field, void *data); 00169 static bool find_field_in_order_list (Field *field, void *data); 00170 static int create_sort_index(THD *thd, JOIN *join, ORDER *order, 00171 ha_rows filesort_limit, ha_rows select_limit); 00172 static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields, 00173 Item *having); 00174 static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field, 00175 ulong offset,Item *having); 00176 static int remove_dup_with_hash_index(THD *thd,TABLE *table, 00177 uint field_count, Field **first_field, 00178 00179 ulong key_length,Item *having); 00180 static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count); 00181 static ulong used_blob_length(CACHE_FIELD **ptr); 00182 static bool store_record_in_cache(JOIN_CACHE *cache); 00183 static void reset_cache_read(JOIN_CACHE *cache); 00184 static void reset_cache_write(JOIN_CACHE *cache); 00185 static void read_cached_record(JOIN_TAB *tab); 00186 static bool cmp_buffer_with_ref(JOIN_TAB *tab); 00187 static bool setup_new_fields(THD *thd, List<Item> &fields, 00188 List<Item> &all_fields, ORDER *new_order); 00189 static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array, 00190 ORDER *order, List<Item> &fields, 00191 bool *all_order_by_fields_used); 00192 static bool test_if_subpart(ORDER *a,ORDER *b); 00193 static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables); 00194 static void calc_group_buffer(JOIN *join,ORDER *group); 00195 static bool make_group_fields(JOIN *main_join, JOIN *curr_join); 00196 static bool alloc_group_fields(JOIN *join,ORDER *group); 00197 // Create list for using with tempory table 00198 static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array, 00199 List<Item> &new_list1, 00200 List<Item> &new_list2, 00201 uint elements, List<Item> &items); 00202 // Create list for using with tempory table 00203 static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array, 00204 List<Item> &new_list1, 00205 List<Item> &new_list2, 00206 uint elements, List<Item> &items); 00207 static void init_tmptable_sum_functions(Item_sum **func); 00208 static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table); 00209 static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end); 00210 static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab); 00211 static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr); 00212 static bool init_sum_functions(Item_sum **func, Item_sum **end); 00213 static bool update_sum_func(Item_sum **func); 00214 static void select_describe(JOIN *join, bool need_tmp_table,bool need_order, 00215 bool distinct, const char *message=NullS); 00216 static Item *remove_additional_cond(Item* conds); 00217 static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab); 00218 00219 00220 /* 00221 This handles SELECT with and without UNION 00222 */ 00223 00224 bool handle_select(THD *thd, LEX *lex, select_result *result, 00225 ulong setup_tables_done_option) 00226 { 00227 bool res; 00228 register SELECT_LEX *select_lex = &lex->select_lex; 00229 DBUG_ENTER("handle_select"); 00230 00231 if (select_lex->next_select() || select_lex->master_unit()->fake_select_lex) 00232 res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option); 00233 else 00234 { 00235 SELECT_LEX_UNIT *unit= &lex->unit; 00236 unit->set_limit(unit->global_parameters); 00237 /* 00238 'options' of mysql_select will be set in JOIN, as far as JOIN for 00239 every PS/SP execution new, we will not need reset this flag if 00240 setup_tables_done_option changed for next rexecution 00241 */ 00242 res= mysql_select(thd, &select_lex->ref_pointer_array, 00243 (TABLE_LIST*) select_lex->table_list.first, 00244 select_lex->with_wild, select_lex->item_list, 00245 select_lex->where, 00246 select_lex->order_list.elements + 00247 select_lex->group_list.elements, 00248 (ORDER*) select_lex->order_list.first, 00249 (ORDER*) select_lex->group_list.first, 00250 select_lex->having, 00251 (ORDER*) lex->proc_list.first, 00252 select_lex->options | thd->options | 00253 setup_tables_done_option, 00254 result, unit, select_lex); 00255 } 00256 DBUG_PRINT("info",("res: %d report_error: %d", res, 00257 thd->net.report_error)); 00258 res|= thd->net.report_error; 00259 if (unlikely(res)) 00260 { 00261 /* If we had a another error reported earlier then this will be ignored */ 00262 result->send_error(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR)); 00263 result->abort(); 00264 } 00265 DBUG_RETURN(res); 00266 } 00267 00268 00269 /* 00270 Function to setup clauses without sum functions 00271 */ 00272 inline int setup_without_group(THD *thd, Item **ref_pointer_array, 00273 TABLE_LIST *tables, 00274 TABLE_LIST *leaves, 00275 List<Item> &fields, 00276 List<Item> &all_fields, 00277 COND **conds, 00278 ORDER *order, 00279 ORDER *group, bool *hidden_group_fields) 00280 { 00281 int res; 00282 nesting_map save_allow_sum_func=thd->lex->allow_sum_func ; 00283 DBUG_ENTER("setup_without_group"); 00284 00285 thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level); 00286 res= setup_conds(thd, tables, leaves, conds); 00287 00288 thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level; 00289 res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields, 00290 order); 00291 thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level); 00292 res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields, 00293 group, hidden_group_fields); 00294 thd->lex->allow_sum_func= save_allow_sum_func; 00295 DBUG_RETURN(res); 00296 } 00297 00298 /***************************************************************************** 00299 Check fields, find best join, do the select and output fields. 00300 mysql_select assumes that all tables are already opened 00301 *****************************************************************************/ 00302 00303 /* 00304 Prepare of whole select (including sub queries in future). 00305 return -1 on error 00306 0 on success 00307 */ 00308 int 00309 JOIN::prepare(Item ***rref_pointer_array, 00310 TABLE_LIST *tables_init, 00311 uint wild_num, COND *conds_init, uint og_num, 00312 ORDER *order_init, ORDER *group_init, 00313 Item *having_init, 00314 ORDER *proc_param_init, SELECT_LEX *select_lex_arg, 00315 SELECT_LEX_UNIT *unit_arg) 00316 { 00317 DBUG_ENTER("JOIN::prepare"); 00318 00319 // to prevent double initialization on EXPLAIN 00320 if (optimized) 00321 DBUG_RETURN(0); 00322 00323 conds= conds_init; 00324 order= order_init; 00325 group_list= group_init; 00326 having= having_init; 00327 proc_param= proc_param_init; 00328 tables_list= tables_init; 00329 select_lex= select_lex_arg; 00330 select_lex->join= this; 00331 join_list= &select_lex->top_join_list; 00332 union_part= (unit_arg->first_select()->next_select() != 0); 00333 00334 /* 00335 If we have already executed SELECT, then it have not sense to prevent 00336 its table from update (see unique_table()) 00337 */ 00338 if (thd->derived_tables_processing) 00339 select_lex->exclude_from_table_unique_test= TRUE; 00340 00341 /* Check that all tables, fields, conds and order are ok */ 00342 00343 if ((!(select_options & OPTION_SETUP_TABLES_DONE) && 00344 setup_tables_and_check_access(thd, &select_lex->context, join_list, 00345 tables_list, 00346 &select_lex->leaf_tables, FALSE, 00347 SELECT_ACL)) || 00348 setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) || 00349 select_lex->setup_ref_array(thd, og_num) || 00350 setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ, 00351 &all_fields, 1) || 00352 setup_without_group(thd, (*rref_pointer_array), tables_list, 00353 select_lex->leaf_tables, fields_list, 00354 all_fields, &conds, order, group_list, 00355 &hidden_group_fields)) 00356 DBUG_RETURN(-1); /* purecov: inspected */ 00357 00358 ref_pointer_array= *rref_pointer_array; 00359 00360 if (having) 00361 { 00362 nesting_map save_allow_sum_func= thd->lex->allow_sum_func; 00363 thd->where="having clause"; 00364 thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level; 00365 select_lex->having_fix_field= 1; 00366 bool having_fix_rc= (!having->fixed && 00367 (having->fix_fields(thd, &having) || 00368 having->check_cols(1))); 00369 select_lex->having_fix_field= 0; 00370 if (having_fix_rc || thd->net.report_error) 00371 DBUG_RETURN(-1); /* purecov: inspected */ 00372 thd->lex->allow_sum_func= save_allow_sum_func; 00373 } 00374 00375 if (!thd->lex->view_prepare_mode) 00376 { 00377 Item_subselect *subselect; 00378 /* Is it subselect? */ 00379 if ((subselect= select_lex->master_unit()->item)) 00380 { 00381 Item_subselect::trans_res res; 00382 if ((res= subselect->select_transformer(this)) != 00383 Item_subselect::RES_OK) 00384 { 00385 select_lex->fix_prepare_information(thd, &conds); 00386 DBUG_RETURN((res == Item_subselect::RES_ERROR)); 00387 } 00388 } 00389 } 00390 00391 if (having && having->with_sum_func) 00392 having->split_sum_func2(thd, ref_pointer_array, all_fields, 00393 &having, TRUE); 00394 if (select_lex->inner_sum_func_list) 00395 { 00396 Item_sum *end=select_lex->inner_sum_func_list; 00397 Item_sum *item_sum= end; 00398 do 00399 { 00400 item_sum= item_sum->next; 00401 item_sum->split_sum_func2(thd, ref_pointer_array, 00402 all_fields, item_sum->ref_by, FALSE); 00403 } while (item_sum != end); 00404 } 00405 00406 if (setup_ftfuncs(select_lex)) /* should be after having->fix_fields */ 00407 DBUG_RETURN(-1); 00408 00409 00410 /* 00411 Check if one one uses a not constant column with group functions 00412 and no GROUP BY. 00413 TODO: Add check of calculation of GROUP functions and fields: 00414 SELECT COUNT(*)+table.col1 from table1; 00415 */ 00416 { 00417 if (!group_list) 00418 { 00419 uint flag=0; 00420 List_iterator_fast<Item> it(fields_list); 00421 Item *item; 00422 while ((item= it++)) 00423 { 00424 if (item->with_sum_func) 00425 flag|=1; 00426 else if (!(flag & 2) && !item->const_during_execution()) 00427 flag|=2; 00428 } 00429 if (flag == 3) 00430 { 00431 my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS, 00432 ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0)); 00433 DBUG_RETURN(-1); 00434 } 00435 } 00436 TABLE_LIST *table_ptr; 00437 for (table_ptr= select_lex->leaf_tables; 00438 table_ptr; 00439 table_ptr= table_ptr->next_leaf) 00440 tables++; 00441 } 00442 { 00443 /* Caclulate the number of groups */ 00444 send_group_parts= 0; 00445 for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next) 00446 send_group_parts++; 00447 } 00448 00449 procedure= setup_procedure(thd, proc_param, result, fields_list, &error); 00450 if (error) 00451 goto err; /* purecov: inspected */ 00452 if (procedure) 00453 { 00454 if (setup_new_fields(thd, fields_list, all_fields, 00455 procedure->param_fields)) 00456 goto err; /* purecov: inspected */ 00457 if (procedure->group) 00458 { 00459 if (!test_if_subpart(procedure->group,group_list)) 00460 { /* purecov: inspected */ 00461 my_message(ER_DIFF_GROUPS_PROC, ER(ER_DIFF_GROUPS_PROC), 00462 MYF(0)); /* purecov: inspected */ 00463 goto err; /* purecov: inspected */ 00464 } 00465 } 00466 if (order && (procedure->flags & PROC_NO_SORT)) 00467 { /* purecov: inspected */ 00468 my_message(ER_ORDER_WITH_PROC, ER(ER_ORDER_WITH_PROC), 00469 MYF(0)); /* purecov: inspected */ 00470 goto err; /* purecov: inspected */ 00471 } 00472 } 00473 00474 /* Init join struct */ 00475 count_field_types(&tmp_table_param, all_fields, 0); 00476 ref_pointer_array_size= all_fields.elements*sizeof(Item*); 00477 this->group= group_list != 0; 00478 unit= unit_arg; 00479 00480 #ifdef RESTRICTED_GROUP 00481 if (sum_func_count && !group_list && (func_count || field_count)) 00482 { 00483 my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0)); 00484 goto err; 00485 } 00486 #endif 00487 if (!procedure && result && result->prepare(fields_list, unit_arg)) 00488 goto err; /* purecov: inspected */ 00489 00490 if (select_lex->olap == ROLLUP_TYPE && rollup_init()) 00491 goto err; 00492 if (alloc_func_list()) 00493 goto err; 00494 00495 select_lex->fix_prepare_information(thd, &conds); 00496 DBUG_RETURN(0); // All OK 00497 00498 err: 00499 delete procedure; /* purecov: inspected */ 00500 procedure= 0; 00501 DBUG_RETURN(-1); /* purecov: inspected */ 00502 } 00503 00504 /* 00505 test if it is known for optimisation IN subquery 00506 00507 SYNOPSYS 00508 JOIN::test_in_subselect 00509 where - pointer for variable in which conditions should be 00510 stored if subquery is known 00511 00512 RETURN 00513 1 - known 00514 0 - unknown 00515 */ 00516 00517 bool JOIN::test_in_subselect(Item **where) 00518 { 00519 if (conds->type() == Item::FUNC_ITEM && 00520 ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC && 00521 ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM && 00522 ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM) 00523 { 00524 join_tab->info= "Using index"; 00525 *where= 0; 00526 return 1; 00527 } 00528 if (conds->type() == Item::COND_ITEM && 00529 ((class Item_func *)this->conds)->functype() == 00530 Item_func::COND_AND_FUNC) 00531 { 00532 if ((*where= remove_additional_cond(conds))) 00533 join_tab->info= "Using index; Using where"; 00534 else 00535 join_tab->info= "Using index"; 00536 return 1; 00537 } 00538 return 0; 00539 } 00540 00541 00542 /* 00543 global select optimisation. 00544 return 0 - success 00545 1 - error 00546 error code saved in field 'error' 00547 */ 00548 00549 int 00550 JOIN::optimize() 00551 { 00552 DBUG_ENTER("JOIN::optimize"); 00553 // to prevent double initialization on EXPLAIN 00554 if (optimized) 00555 DBUG_RETURN(0); 00556 optimized= 1; 00557 00558 row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR : 00559 unit->select_limit_cnt); 00560 /* select_limit is used to decide if we are likely to scan the whole table */ 00561 select_limit= unit->select_limit_cnt; 00562 if (having || (select_options & OPTION_FOUND_ROWS)) 00563 select_limit= HA_POS_ERROR; 00564 do_send_rows = (unit->select_limit_cnt) ? 1 : 0; 00565 // Ignore errors of execution if option IGNORE present 00566 if (thd->lex->ignore) 00567 thd->lex->current_select->no_error= 1; 00568 #ifdef HAVE_REF_TO_FIELDS // Not done yet 00569 /* Add HAVING to WHERE if possible */ 00570 if (having && !group_list && !sum_func_count) 00571 { 00572 if (!conds) 00573 { 00574 conds= having; 00575 having= 0; 00576 } 00577 else if ((conds=new Item_cond_and(conds,having))) 00578 { 00579 /* 00580 Item_cond_and can't be fixed after creation, so we do not check 00581 conds->fixed 00582 */ 00583 conds->fix_fields(thd, &conds); 00584 conds->change_ref_to_fields(thd, tables_list); 00585 conds->top_level_item(); 00586 having= 0; 00587 } 00588 } 00589 #endif 00590 SELECT_LEX *sel= thd->lex->current_select; 00591 if (sel->first_cond_optimization) 00592 { 00593 /* 00594 The following code will allocate the new items in a permanent 00595 MEMROOT for prepared statements and stored procedures. 00596 */ 00597 00598 Query_arena *arena= thd->stmt_arena, backup; 00599 if (arena->is_conventional()) 00600 arena= 0; // For easier test 00601 else 00602 thd->set_n_backup_active_arena(arena, &backup); 00603 00604 sel->first_cond_optimization= 0; 00605 00606 /* Convert all outer joins to inner joins if possible */ 00607 conds= simplify_joins(this, join_list, conds, TRUE); 00608 build_bitmap_for_nested_joins(join_list, 0); 00609 00610 sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0; 00611 sel->prep_having= having ? having->copy_andor_structure(thd) : 0; 00612 00613 if (arena) 00614 thd->restore_active_arena(arena, &backup); 00615 } 00616 00617 conds= optimize_cond(this, conds, join_list, &cond_value); 00618 if (thd->net.report_error) 00619 { 00620 error= 1; 00621 DBUG_PRINT("error",("Error from optimize_cond")); 00622 DBUG_RETURN(1); 00623 } 00624 00625 { 00626 Item::cond_result having_value; 00627 having= optimize_cond(this, having, join_list, &having_value); 00628 if (thd->net.report_error) 00629 { 00630 error= 1; 00631 DBUG_PRINT("error",("Error from optimize_cond")); 00632 DBUG_RETURN(1); 00633 } 00634 00635 if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 00636 (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS))) 00637 { /* Impossible cond */ 00638 DBUG_PRINT("info", (having_value == Item::COND_FALSE ? 00639 "Impossible HAVING" : "Impossible WHERE")); 00640 zero_result_cause= having_value == Item::COND_FALSE ? 00641 "Impossible HAVING" : "Impossible WHERE"; 00642 error= 0; 00643 DBUG_RETURN(0); 00644 } 00645 } 00646 00647 #ifdef WITH_PARTITION_STORAGE_ENGINE 00648 { 00649 TABLE_LIST *tbl; 00650 for (tbl= select_lex->leaf_tables; tbl; tbl= tbl->next_leaf) 00651 { 00652 /* 00653 If tbl->embedding!=NULL that means that this table is in the inner 00654 part of the nested outer join, and we can't do partition pruning 00655 (TODO: check if this limitation can be lifted) 00656 */ 00657 if (!tbl->embedding) 00658 { 00659 Item *prune_cond= tbl->on_expr? tbl->on_expr : conds; 00660 tbl->table->no_partitions_used= prune_partitions(thd, tbl->table, 00661 prune_cond); 00662 } 00663 } 00664 } 00665 #endif 00666 00667 /* Optimize count(*), min() and max() */ 00668 if (tables_list && tmp_table_param.sum_func_count && ! group_list) 00669 { 00670 int res; 00671 /* 00672 opt_sum_query() returns -1 if no rows match to the WHERE conditions, 00673 or 1 if all items were resolved, or 0, or an error number HA_ERR_... 00674 */ 00675 if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds))) 00676 { 00677 if (res > 1) 00678 { 00679 DBUG_PRINT("error",("Error from opt_sum_query")); 00680 DBUG_RETURN(1); 00681 } 00682 if (res < 0) 00683 { 00684 DBUG_PRINT("info",("No matching min/max row")); 00685 zero_result_cause= "No matching min/max row"; 00686 error=0; 00687 DBUG_RETURN(0); 00688 } 00689 DBUG_PRINT("info",("Select tables optimized away")); 00690 zero_result_cause= "Select tables optimized away"; 00691 tables_list= 0; // All tables resolved 00692 /* 00693 Extract all table-independent conditions and replace the WHERE 00694 clause with them. All other conditions were computed by opt_sum_query 00695 and the MIN/MAX/COUNT function(s) have been replaced by constants, 00696 so there is no need to compute the whole WHERE clause again. 00697 Notice that make_cond_for_table() will always succeed to remove all 00698 computed conditions, because opt_sum_query() is applicable only to 00699 conjunctions. 00700 Preserve conditions for EXPLAIN. 00701 */ 00702 if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED)) 00703 { 00704 COND *table_independent_conds= 00705 make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0); 00706 DBUG_EXECUTE("where", 00707 print_where(table_independent_conds, 00708 "where after opt_sum_query()");); 00709 conds= table_independent_conds; 00710 } 00711 } 00712 } 00713 if (!tables_list) 00714 { 00715 DBUG_PRINT("info",("No tables")); 00716 error= 0; 00717 DBUG_RETURN(0); 00718 } 00719 error= -1; // Error is sent to client 00720 sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables); 00721 00722 /* Calculate how to do the join */ 00723 thd->proc_info= "statistics"; 00724 if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) || 00725 thd->is_fatal_error) 00726 { 00727 DBUG_PRINT("error",("Error: make_join_statistics() failed")); 00728 DBUG_RETURN(1); 00729 } 00730 00731 /* Remove distinct if only const tables */ 00732 select_distinct= select_distinct && (const_tables != tables); 00733 thd->proc_info= "preparing"; 00734 if (result->initialize_tables(this)) 00735 { 00736 DBUG_PRINT("error",("Error: initialize_tables() failed")); 00737 DBUG_RETURN(1); // error == -1 00738 } 00739 if (const_table_map != found_const_table_map && 00740 !(select_options & SELECT_DESCRIBE) && 00741 (!conds || 00742 !(conds->used_tables() & RAND_TABLE_BIT) || 00743 select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT 00744 { 00745 zero_result_cause= "no matching row in const table"; 00746 DBUG_PRINT("error",("Error: %s", zero_result_cause)); 00747 error= 0; 00748 DBUG_RETURN(0); 00749 } 00750 if (!(thd->options & OPTION_BIG_SELECTS) && 00751 best_read > (double) thd->variables.max_join_size && 00752 !(select_options & SELECT_DESCRIBE)) 00753 { /* purecov: inspected */ 00754 my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0)); 00755 error= -1; 00756 DBUG_RETURN(1); 00757 } 00758 if (const_tables && !thd->locked_tables && 00759 !(select_options & SELECT_NO_UNLOCK)) 00760 mysql_unlock_some_tables(thd, table, const_tables); 00761 if (!conds && outer_join) 00762 { 00763 /* Handle the case where we have an OUTER JOIN without a WHERE */ 00764 conds=new Item_int((longlong) 1,1); // Always true 00765 } 00766 select= make_select(*table, const_table_map, 00767 const_table_map, conds, 1, &error); 00768 if (error) 00769 { /* purecov: inspected */ 00770 error= -1; /* purecov: inspected */ 00771 DBUG_PRINT("error",("Error: make_select() failed")); 00772 DBUG_RETURN(1); 00773 } 00774 00775 reset_nj_counters(join_list); 00776 make_outerjoin_info(this); 00777 00778 /* 00779 Among the equal fields belonging to the same multiple equality 00780 choose the one that is to be retrieved first and substitute 00781 all references to these in where condition for a reference for 00782 the selected field. 00783 */ 00784 if (conds) 00785 { 00786 conds= substitute_for_best_equal_field(conds, cond_equal, map2table); 00787 conds->update_used_tables(); 00788 DBUG_EXECUTE("where", print_where(conds, "after substitute_best_equal");); 00789 } 00790 /* 00791 Permorm the the optimization on fields evaluation mentioned above 00792 for all on expressions. 00793 */ 00794 for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++) 00795 { 00796 if (*tab->on_expr_ref) 00797 { 00798 *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref, 00799 tab->cond_equal, 00800 map2table); 00801 (*tab->on_expr_ref)->update_used_tables(); 00802 } 00803 } 00804 00805 if (make_join_select(this, select, conds)) 00806 { 00807 zero_result_cause= 00808 "Impossible WHERE noticed after reading const tables"; 00809 DBUG_RETURN(0); // error == 0 00810 } 00811 00812 error= -1; /* if goto err */ 00813 00814 /* Optimize distinct away if possible */ 00815 { 00816 ORDER *org_order= order; 00817 order=remove_const(this, order,conds,1, &simple_order); 00818 /* 00819 If we are using ORDER BY NULL or ORDER BY const_expression, 00820 return result in any order (even if we are using a GROUP BY) 00821 */ 00822 if (!order && org_order) 00823 skip_sort_order= 1; 00824 } 00825 if (group_list || tmp_table_param.sum_func_count) 00826 { 00827 if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE) 00828 select_distinct=0; 00829 } 00830 else if (select_distinct && tables - const_tables == 1) 00831 { 00832 /* 00833 We are only using one table. In this case we change DISTINCT to a 00834 GROUP BY query if: 00835 - The GROUP BY can be done through indexes (no sort) and the ORDER 00836 BY only uses selected fields. 00837 (In this case we can later optimize away GROUP BY and ORDER BY) 00838 - We are scanning the whole table without LIMIT 00839 This can happen if: 00840 - We are using CALC_FOUND_ROWS 00841 - We are using an ORDER BY that can't be optimized away. 00842 00843 We don't want to use this optimization when we are using LIMIT 00844 because in this case we can just create a temporary table that 00845 holds LIMIT rows and stop when this table is full. 00846 */ 00847 JOIN_TAB *tab= &join_tab[const_tables]; 00848 bool all_order_fields_used; 00849 if (order) 00850 skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1); 00851 if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array, 00852 order, fields_list, 00853 &all_order_fields_used))) 00854 { 00855 bool skip_group= (skip_sort_order && 00856 test_if_skip_sort_order(tab, group_list, select_limit, 00857 1) != 0); 00858 if ((skip_group && all_order_fields_used) || 00859 select_limit == HA_POS_ERROR || 00860 (order && !skip_sort_order)) 00861 { 00862 /* Change DISTINCT to GROUP BY */ 00863 select_distinct= 0; 00864 no_order= !order; 00865 if (all_order_fields_used) 00866 { 00867 if (order && skip_sort_order) 00868 { 00869 /* 00870 Force MySQL to read the table in sorted order to get result in 00871 ORDER BY order. 00872 */ 00873 tmp_table_param.quick_group=0; 00874 } 00875 order=0; 00876 } 00877 group=1; // For end_write_group 00878 } 00879 else 00880 group_list= 0; 00881 } 00882 else if (thd->is_fatal_error) // End of memory 00883 DBUG_RETURN(1); 00884 } 00885 simple_group= 0; 00886 { 00887 ORDER *old_group_list; 00888 group_list= remove_const(this, (old_group_list= group_list), conds, 00889 rollup.state == ROLLUP::STATE_NONE, 00890 &simple_group); 00891 if (old_group_list && !group_list) 00892 select_distinct= 0; 00893 } 00894 /* 00895 Check if we can optimize away GROUP BY/DISTINCT. 00896 We can do that if there are no aggregate functions and the 00897 fields in DISTINCT clause (if present) and/or columns in GROUP BY 00898 (if present) contain direct references to all key parts of 00899 an unique index (in whatever order). 00900 Note that the unique keys for DISTINCT and GROUP BY should not 00901 be the same (as long as they are unique). 00902 00903 The FROM clause must contain a single non-constant table. 00904 */ 00905 if (tables - const_tables == 1 && (group_list || select_distinct) && 00906 !tmp_table_param.sum_func_count && 00907 (!join_tab[const_tables].select || 00908 !join_tab[const_tables].select->quick || 00909 join_tab[const_tables].select->quick->get_type() != 00910 QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)) 00911 { 00912 if (group_list && 00913 list_contains_unique_index(join_tab[const_tables].table, 00914 find_field_in_order_list, 00915 (void *) group_list)) 00916 { 00917 group_list= 0; 00918 group= 0; 00919 } 00920 if (select_distinct && 00921 list_contains_unique_index(join_tab[const_tables].table, 00922 find_field_in_item_list, 00923 (void *) &fields_list)) 00924 { 00925 select_distinct= 0; 00926 } 00927 } 00928 if (!group_list && group) 00929 { 00930 order=0; // The output has only one row 00931 simple_order=1; 00932 select_distinct= 0; // No need in distinct for 1 row 00933 } 00934 00935 calc_group_buffer(this, group_list); 00936 send_group_parts= tmp_table_param.group_parts; /* Save org parts */ 00937 if (procedure && procedure->group) 00938 { 00939 group_list= procedure->group= remove_const(this, procedure->group, conds, 00940 1, &simple_group); 00941 calc_group_buffer(this, group_list); 00942 } 00943 00944 if (test_if_subpart(group_list, order) || 00945 (!group_list && tmp_table_param.sum_func_count)) 00946 order=0; 00947 00948 // Can't use sort on head table if using row cache 00949 if (full_join) 00950 { 00951 if (group_list) 00952 simple_group=0; 00953 if (order) 00954 simple_order=0; 00955 } 00956 00957 /* 00958 Check if we need to create a temporary table. 00959 This has to be done if all tables are not already read (const tables) 00960 and one of the following conditions holds: 00961 - We are using DISTINCT (simple distinct's are already optimized away) 00962 - We are using an ORDER BY or GROUP BY on fields not in the first table 00963 - We are using different ORDER BY and GROUP BY orders 00964 - The user wants us to buffer the result. 00965 */ 00966 need_tmp= (const_tables != tables && 00967 ((select_distinct || !simple_order || !simple_group) || 00968 (group_list && order) || 00969 test(select_options & OPTION_BUFFER_RESULT))); 00970 00971 // No cache for MATCH 00972 make_join_readinfo(this, 00973 (select_options & (SELECT_DESCRIBE | 00974 SELECT_NO_JOIN_CACHE)) | 00975 (select_lex->ftfunc_list->elements ? 00976 SELECT_NO_JOIN_CACHE : 0)); 00977 00978 /* Perform FULLTEXT search before all regular searches */ 00979 if (!(select_options & SELECT_DESCRIBE)) 00980 init_ftfuncs(thd, select_lex, test(order)); 00981 00982 /* 00983 is this simple IN subquery? 00984 */ 00985 if (!group_list && !order && 00986 unit->item && unit->item->substype() == Item_subselect::IN_SUBS && 00987 tables == 1 && conds && 00988 !unit->first_select()->next_select()) 00989 { 00990 if (!having) 00991 { 00992 Item *where= 0; 00993 if (join_tab[0].type == JT_EQ_REF && 00994 join_tab[0].ref.items[0]->name == in_left_expr_name) 00995 { 00996 if (test_in_subselect(&where)) 00997 { 00998 join_tab[0].type= JT_UNIQUE_SUBQUERY; 00999 error= 0; 01000 DBUG_RETURN(unit->item-> 01001 change_engine(new 01002 subselect_uniquesubquery_engine(thd, 01003 join_tab, 01004 unit->item, 01005 where))); 01006 } 01007 } 01008 else if (join_tab[0].type == JT_REF && 01009 join_tab[0].ref.items[0]->name == in_left_expr_name) 01010 { 01011 if (test_in_subselect(&where)) 01012 { 01013 join_tab[0].type= JT_INDEX_SUBQUERY; 01014 error= 0; 01015 DBUG_RETURN(unit->item-> 01016 change_engine(new 01017 subselect_indexsubquery_engine(thd, 01018 join_tab, 01019 unit->item, 01020 where, 01021 0))); 01022 } 01023 } 01024 } else if (join_tab[0].type == JT_REF_OR_NULL && 01025 join_tab[0].ref.items[0]->name == in_left_expr_name && 01026 having->type() == Item::FUNC_ITEM && 01027 ((Item_func *) having)->functype() == 01028 Item_func::ISNOTNULLTEST_FUNC) 01029 { 01030 join_tab[0].type= JT_INDEX_SUBQUERY; 01031 error= 0; 01032 01033 if ((conds= remove_additional_cond(conds))) 01034 join_tab->info= "Using index; Using where"; 01035 else 01036 join_tab->info= "Using index"; 01037 01038 DBUG_RETURN(unit->item-> 01039 change_engine(new subselect_indexsubquery_engine(thd, 01040 join_tab, 01041 unit->item, 01042 conds, 01043 1))); 01044 } 01045 01046 } 01047 /* 01048 Need to tell handlers that to play it safe, it should fetch all 01049 columns of the primary key of the tables: this is because MySQL may 01050 build row pointers for the rows, and for all columns of the primary key 01051 the read set has not necessarily been set by the server code. 01052 */ 01053 if (need_tmp || select_distinct || group_list || order) 01054 { 01055 for (uint i = const_tables; i < tables; i++) 01056 join_tab[i].table->prepare_for_position(); 01057 } 01058 01059 DBUG_EXECUTE("info",TEST_join(this);); 01060 01061 if (const_tables != tables) 01062 { 01063 /* 01064 Because filesort always does a full table scan or a quick range scan 01065 we must add the removed reference to the select for the table. 01066 We only need to do this when we have a simple_order or simple_group 01067 as in other cases the join is done before the sort. 01068 */ 01069 if ((order || group_list) && 01070 join_tab[const_tables].type != JT_ALL && 01071 join_tab[const_tables].type != JT_FT && 01072 join_tab[const_tables].type != JT_REF_OR_NULL && 01073 (order && simple_order || group_list && simple_group)) 01074 { 01075 if (add_ref_to_table_cond(thd,&join_tab[const_tables])) 01076 DBUG_RETURN(1); 01077 } 01078 01079 if (!(select_options & SELECT_BIG_RESULT) && 01080 ((group_list && 01081 (!simple_group || 01082 !test_if_skip_sort_order(&join_tab[const_tables], group_list, 01083 unit->select_limit_cnt, 0))) || 01084 select_distinct) && 01085 tmp_table_param.quick_group && !procedure) 01086 { 01087 need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort 01088 } 01089 if (order) 01090 { 01091 /* 01092 Force using of tmp table if sorting by a SP or UDF function due to 01093 their expensive and probably non-deterministic nature. 01094 */ 01095 for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next) 01096 { 01097 Item *item= *tmp_order->item; 01098 if (item->walk(&Item::is_expensive_processor, 0, (byte*)0)) 01099 { 01100 /* Force tmp table without sort */ 01101 need_tmp=1; simple_order=simple_group=0; 01102 break; 01103 } 01104 } 01105 } 01106 } 01107 01108 tmp_having= having; 01109 if (select_options & SELECT_DESCRIBE) 01110 { 01111 error= 0; 01112 DBUG_RETURN(0); 01113 } 01114 having= 0; 01115 01116 /* 01117 The loose index scan access method guarantees that all grouping or 01118 duplicate row elimination (for distinct) is already performed 01119 during data retrieval, and that all MIN/MAX functions are already 01120 computed for each group. Thus all MIN/MAX functions should be 01121 treated as regular functions, and there is no need to perform 01122 grouping in the main execution loop. 01123 Notice that currently loose index scan is applicable only for 01124 single table queries, thus it is sufficient to test only the first 01125 join_tab element of the plan for its access method. 01126 */ 01127 if (join_tab->is_using_loose_index_scan()) 01128 tmp_table_param.precomputed_group_by= TRUE; 01129 01130 /* Create a tmp table if distinct or if the sort is too complicated */ 01131 if (need_tmp) 01132 { 01133 DBUG_PRINT("info",("Creating tmp table")); 01134 thd->proc_info="Creating tmp table"; 01135 01136 init_items_ref_array(); 01137 01138 tmp_table_param.hidden_field_count= (all_fields.elements - 01139 fields_list.elements); 01140 if (!(exec_tmp_table1= 01141 create_tmp_table(thd, &tmp_table_param, all_fields, 01142 ((!simple_group && !procedure && 01143 !(test_flags & TEST_NO_KEY_GROUP)) ? 01144 group_list : (ORDER*) 0), 01145 group_list ? 0 : select_distinct, 01146 group_list && simple_group, 01147 select_options, 01148 (order == 0 || skip_sort_order || 01149 test(select_options & OPTION_BUFFER_RESULT)) ? 01150 select_limit : HA_POS_ERROR, 01151 (char *) ""))) 01152 DBUG_RETURN(1); 01153 01154 /* 01155 We don't have to store rows in temp table that doesn't match HAVING if: 01156 - we are sorting the table and writing complete group rows to the 01157 temp table. 01158 - We are using DISTINCT without resolving the distinct as a GROUP BY 01159 on all columns. 01160 01161 If having is not handled here, it will be checked before the row 01162 is sent to the client. 01163 */ 01164 if (tmp_having && 01165 (sort_and_group || (exec_tmp_table1->distinct && !group_list))) 01166 having= tmp_having; 01167 01168 /* if group or order on first table, sort first */ 01169 if (group_list && simple_group) 01170 { 01171 DBUG_PRINT("info",("Sorting for group")); 01172 thd->proc_info="Sorting for group"; 01173 if (create_sort_index(thd, this, group_list, 01174 HA_POS_ERROR, HA_POS_ERROR) || 01175 alloc_group_fields(this, group_list) || 01176 make_sum_func_list(all_fields, fields_list, 1) || 01177 setup_sum_funcs(thd, sum_funcs)) 01178 DBUG_RETURN(1); 01179 group_list=0; 01180 } 01181 else 01182 { 01183 if (make_sum_func_list(all_fields, fields_list, 0) || 01184 setup_sum_funcs(thd, sum_funcs)) 01185 DBUG_RETURN(1); 01186 if (!group_list && ! exec_tmp_table1->distinct && order && simple_order) 01187 { 01188 DBUG_PRINT("info",("Sorting for order")); 01189 thd->proc_info="Sorting for order"; 01190 if (create_sort_index(thd, this, order, 01191 HA_POS_ERROR, HA_POS_ERROR)) 01192 DBUG_RETURN(1); 01193 order=0; 01194 } 01195 } 01196 01197 /* 01198 Optimize distinct when used on some of the tables 01199 SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b 01200 In this case we can stop scanning t2 when we have found one t1.a 01201 */ 01202 01203 if (exec_tmp_table1->distinct) 01204 { 01205 table_map used_tables= thd->used_tables; 01206 JOIN_TAB *last_join_tab= join_tab+tables-1; 01207 do 01208 { 01209 if (used_tables & last_join_tab->table->map) 01210 break; 01211 last_join_tab->not_used_in_distinct=1; 01212 } while (last_join_tab-- != join_tab); 01213 /* Optimize "select distinct b from t1 order by key_part_1 limit #" */ 01214 if (order && skip_sort_order) 01215 { 01216 /* Should always succeed */ 01217 if (test_if_skip_sort_order(&join_tab[const_tables], 01218 order, unit->select_limit_cnt, 0)) 01219 order=0; 01220 } 01221 } 01222 01223 if (select_lex->uncacheable && !is_top_level_join()) 01224 { 01225 /* If this join belongs to an uncacheable subquery */ 01226 if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN)))) 01227 DBUG_RETURN(-1); 01228 error= 0; // Ensure that tmp_join.error= 0 01229 restore_tmp(); 01230 } 01231 } 01232 01233 error= 0; 01234 DBUG_RETURN(0); 01235 } 01236 01237 01238 /* 01239 Restore values in temporary join 01240 */ 01241 void JOIN::restore_tmp() 01242 { 01243 memcpy(tmp_join, this, (size_t) sizeof(JOIN)); 01244 } 01245 01246 01247 int 01248 JOIN::reinit() 01249 { 01250 DBUG_ENTER("JOIN::reinit"); 01251 01252 unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ? 01253 select_lex->offset_limit->val_uint() : 01254 ULL(0)); 01255 01256 first_record= 0; 01257 01258 if (exec_tmp_table1) 01259 { 01260 exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE); 01261 exec_tmp_table1->file->delete_all_rows(); 01262 free_io_cache(exec_tmp_table1); 01263 filesort_free_buffers(exec_tmp_table1); 01264 } 01265 if (exec_tmp_table2) 01266 { 01267 exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE); 01268 exec_tmp_table2->file->delete_all_rows(); 01269 free_io_cache(exec_tmp_table2); 01270 filesort_free_buffers(exec_tmp_table2); 01271 } 01272 if (items0) 01273 set_items_ref_array(items0); 01274 01275 if (join_tab_save) 01276 memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables); 01277 01278 if (tmp_join) 01279 restore_tmp(); 01280 01281 /* Reset of sum functions */ 01282 if (sum_funcs) 01283 { 01284 Item_sum *func, **func_ptr= sum_funcs; 01285 while ((func= *(func_ptr++))) 01286 func->clear(); 01287 } 01288 01289 DBUG_RETURN(0); 01290 } 01291 01292 01293 bool 01294 JOIN::save_join_tab() 01295 { 01296 if (!join_tab_save && select_lex->master_unit()->uncacheable) 01297 { 01298 if (!(join_tab_save= (JOIN_TAB*)thd->memdup((gptr) join_tab, 01299 sizeof(JOIN_TAB) * tables))) 01300 return 1; 01301 } 01302 return 0; 01303 } 01304 01305 01306 /* 01307 Exec select 01308 */ 01309 void 01310 JOIN::exec() 01311 { 01312 List<Item> *columns_list= &fields_list; 01313 int tmp_error; 01314 DBUG_ENTER("JOIN::exec"); 01315 01316 error= 0; 01317 if (procedure) 01318 { 01319 procedure_fields_list= fields_list; 01320 if (procedure->change_columns(procedure_fields_list) || 01321 result->prepare(procedure_fields_list, unit)) 01322 { 01323 thd->limit_found_rows= thd->examined_row_count= 0; 01324 DBUG_VOID_RETURN; 01325 } 01326 columns_list= &procedure_fields_list; 01327 } 01328 (void) result->prepare2(); // Currently, this cannot fail. 01329 01330 if (!tables_list) 01331 { // Only test of functions 01332 if (select_options & SELECT_DESCRIBE) 01333 select_describe(this, FALSE, FALSE, FALSE, 01334 (zero_result_cause?zero_result_cause:"No tables used")); 01335 else 01336 { 01337 result->send_fields(*columns_list, 01338 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF); 01339 /* 01340 We have to test for 'conds' here as the WHERE may not be constant 01341 even if we don't have any tables for prepared statements or if 01342 conds uses something like 'rand()'. 01343 */ 01344 if (cond_value != Item::COND_FALSE && 01345 (!conds || conds->val_int()) && 01346 (!having || having->val_int())) 01347 { 01348 if (do_send_rows && 01349 (procedure ? (procedure->send_row(procedure_fields_list) || 01350 procedure->end_of_records()) : result->send_data(fields_list))) 01351 error= 1; 01352 else 01353 { 01354 error= (int) result->send_eof(); 01355 send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : 01356 thd->sent_row_count); 01357 } 01358 } 01359 else 01360 { 01361 error=(int) result->send_eof(); 01362 send_records= 0; 01363 } 01364 } 01365 /* Single select (without union) always returns 0 or 1 row */ 01366 thd->limit_found_rows= send_records; 01367 thd->examined_row_count= 0; 01368 DBUG_VOID_RETURN; 01369 } 01370 thd->limit_found_rows= thd->examined_row_count= 0; 01371 01372 if (zero_result_cause) 01373 { 01374 (void) return_zero_rows(this, result, select_lex->leaf_tables, 01375 *columns_list, 01376 send_row_on_empty_set(), 01377 select_options, 01378 zero_result_cause, 01379 having); 01380 DBUG_VOID_RETURN; 01381 } 01382 01383 if (select_options & SELECT_DESCRIBE) 01384 { 01385 /* 01386 Check if we managed to optimize ORDER BY away and don't use temporary 01387 table to resolve ORDER BY: in that case, we only may need to do 01388 filesort for GROUP BY. 01389 */ 01390 if (!order && !no_order && (!skip_sort_order || !need_tmp)) 01391 { 01392 /* 01393 Reset 'order' to 'group_list' and reinit variables describing 01394 'order' 01395 */ 01396 order= group_list; 01397 simple_order= simple_group; 01398 skip_sort_order= 0; 01399 } 01400 if (order && 01401 (const_tables == tables || 01402 ((simple_order || skip_sort_order) && 01403 test_if_skip_sort_order(&join_tab[const_tables], order, 01404 select_limit, 0)))) 01405 order=0; 01406 having= tmp_having; 01407 select_describe(this, need_tmp, 01408 order != 0 && !skip_sort_order, 01409 select_distinct); 01410 DBUG_VOID_RETURN; 01411 } 01412 01413 JOIN *curr_join= this; 01414 List<Item> *curr_all_fields= &all_fields; 01415 List<Item> *curr_fields_list= &fields_list; 01416 TABLE *curr_tmp_table= 0; 01417 01418 if ((curr_join->select_lex->options & OPTION_SCHEMA_TABLE) && 01419 get_schema_tables_result(curr_join)) 01420 { 01421 DBUG_VOID_RETURN; 01422 } 01423 01424 /* Create a tmp table if distinct or if the sort is too complicated */ 01425 if (need_tmp) 01426 { 01427 if (tmp_join) 01428 { 01429 /* 01430 We are in a non cacheable sub query. Get the saved join structure 01431 after optimization. 01432 (curr_join may have been modified during last exection and we need 01433 to reset it) 01434 */ 01435 curr_join= tmp_join; 01436 } 01437 curr_tmp_table= exec_tmp_table1; 01438 01439 /* Copy data to the temporary table */ 01440 thd->proc_info= "Copying to tmp table"; 01441 DBUG_PRINT("info", ("%s", thd->proc_info)); 01442 if (!curr_join->sort_and_group && 01443 curr_join->const_tables != curr_join->tables) 01444 curr_join->join_tab[curr_join->const_tables].sorted= 0; 01445 if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table, 0))) 01446 { 01447 error= tmp_error; 01448 DBUG_VOID_RETURN; 01449 } 01450 curr_tmp_table->file->info(HA_STATUS_VARIABLE); 01451 01452 if (curr_join->having) 01453 curr_join->having= curr_join->tmp_having= 0; // Allready done 01454 01455 /* Change sum_fields reference to calculated fields in tmp_table */ 01456 curr_join->all_fields= *curr_all_fields; 01457 if (!items1) 01458 { 01459 items1= items0 + all_fields.elements; 01460 if (sort_and_group || curr_tmp_table->group) 01461 { 01462 if (change_to_use_tmp_fields(thd, items1, 01463 tmp_fields_list1, tmp_all_fields1, 01464 fields_list.elements, all_fields)) 01465 DBUG_VOID_RETURN; 01466 } 01467 else 01468 { 01469 if (change_refs_to_tmp_fields(thd, items1, 01470 tmp_fields_list1, tmp_all_fields1, 01471 fields_list.elements, all_fields)) 01472 DBUG_VOID_RETURN; 01473 } 01474 curr_join->tmp_all_fields1= tmp_all_fields1; 01475 curr_join->tmp_fields_list1= tmp_fields_list1; 01476 curr_join->items1= items1; 01477 } 01478 curr_all_fields= &tmp_all_fields1; 01479 curr_fields_list= &tmp_fields_list1; 01480 curr_join->set_items_ref_array(items1); 01481 01482 if (sort_and_group || curr_tmp_table->group) 01483 { 01484 curr_join->tmp_table_param.field_count+= 01485 curr_join->tmp_table_param.sum_func_count+ 01486 curr_join->tmp_table_param.func_count; 01487 curr_join->tmp_table_param.sum_func_count= 01488 curr_join->tmp_table_param.func_count= 0; 01489 } 01490 else 01491 { 01492 curr_join->tmp_table_param.field_count+= 01493 curr_join->tmp_table_param.func_count; 01494 curr_join->tmp_table_param.func_count= 0; 01495 } 01496 01497 // procedure can't be used inside subselect => we do nothing special for it 01498 if (procedure) 01499 procedure->update_refs(); 01500 01501 if (curr_tmp_table->group) 01502 { // Already grouped 01503 if (!curr_join->order && !curr_join->no_order && !skip_sort_order) 01504 curr_join->order= curr_join->group_list; /* order by group */ 01505 curr_join->group_list= 0; 01506 } 01507 01508 /* 01509 If we have different sort & group then we must sort the data by group 01510 and copy it to another tmp table 01511 This code is also used if we are using distinct something 01512 we haven't been able to store in the temporary table yet 01513 like SEC_TO_TIME(SUM(...)). 01514 */ 01515 01516 if (curr_join->group_list && (!test_if_subpart(curr_join->group_list, 01517 curr_join->order) || 01518 curr_join->select_distinct) || 01519 (curr_join->select_distinct && 01520 curr_join->tmp_table_param.using_indirect_summary_function)) 01521 { /* Must copy to another table */ 01522 DBUG_PRINT("info",("Creating group table")); 01523 01524 /* Free first data from old join */ 01525 curr_join->join_free(); 01526 if (make_simple_join(curr_join, curr_tmp_table)) 01527 DBUG_VOID_RETURN; 01528 calc_group_buffer(curr_join, group_list); 01529 count_field_types(&curr_join->tmp_table_param, 01530 curr_join->tmp_all_fields1, 01531 curr_join->select_distinct && !curr_join->group_list); 01532 curr_join->tmp_table_param.hidden_field_count= 01533 (curr_join->tmp_all_fields1.elements- 01534 curr_join->tmp_fields_list1.elements); 01535 01536 01537 if (exec_tmp_table2) 01538 curr_tmp_table= exec_tmp_table2; 01539 else 01540 { 01541 /* group data to new table */ 01542 01543 /* 01544 If the access method is loose index scan then all MIN/MAX 01545 functions are precomputed, and should be treated as regular 01546 functions. See extended comment in JOIN::exec. 01547 */ 01548 if (curr_join->join_tab->is_using_loose_index_scan()) 01549 curr_join->tmp_table_param.precomputed_group_by= TRUE; 01550 01551 if (!(curr_tmp_table= 01552 exec_tmp_table2= create_tmp_table(thd, 01553 &curr_join->tmp_table_param, 01554 *curr_all_fields, 01555 (ORDER*) 0, 01556 curr_join->select_distinct && 01557 !curr_join->group_list, 01558 1, curr_join->select_options, 01559 HA_POS_ERROR, 01560 (char *) ""))) 01561 DBUG_VOID_RETURN; 01562 curr_join->exec_tmp_table2= exec_tmp_table2; 01563 } 01564 if (curr_join->group_list) 01565 { 01566 thd->proc_info= "Creating sort index"; 01567 if (curr_join->join_tab == join_tab && save_join_tab()) 01568 { 01569 DBUG_VOID_RETURN; 01570 } 01571 if (create_sort_index(thd, curr_join, curr_join->group_list, 01572 HA_POS_ERROR, HA_POS_ERROR) || 01573 make_group_fields(this, curr_join)) 01574 { 01575 DBUG_VOID_RETURN; 01576 } 01577 } 01578 01579 thd->proc_info="Copying to group table"; 01580 DBUG_PRINT("info", ("%s", thd->proc_info)); 01581 tmp_error= -1; 01582 if (curr_join != this) 01583 { 01584 if (sum_funcs2) 01585 { 01586 curr_join->sum_funcs= sum_funcs2; 01587 curr_join->sum_funcs_end= sum_funcs_end2; 01588 } 01589 else 01590 { 01591 curr_join->alloc_func_list(); 01592 sum_funcs2= curr_join->sum_funcs; 01593 sum_funcs_end2= curr_join->sum_funcs_end; 01594 } 01595 } 01596 if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 01597 1, TRUE)) 01598 DBUG_VOID_RETURN; 01599 curr_join->group_list= 0; 01600 if (!curr_join->sort_and_group && 01601 curr_join->const_tables != curr_join->tables) 01602 curr_join->join_tab[curr_join->const_tables].sorted= 0; 01603 if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) || 01604 (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table, 01605 0))) 01606 { 01607 error= tmp_error; 01608 DBUG_VOID_RETURN; 01609 } 01610 end_read_record(&curr_join->join_tab->read_record); 01611 curr_join->const_tables= curr_join->tables; // Mark free for cleanup() 01612 curr_join->join_tab[0].table= 0; // Table is freed 01613 01614 // No sum funcs anymore 01615 if (!items2) 01616 { 01617 items2= items1 + all_fields.elements; 01618 if (change_to_use_tmp_fields(thd, items2, 01619 tmp_fields_list2, tmp_all_fields2, 01620 fields_list.elements, tmp_all_fields1)) 01621 DBUG_VOID_RETURN; 01622 curr_join->tmp_fields_list2= tmp_fields_list2; 01623 curr_join->tmp_all_fields2= tmp_all_fields2; 01624 } 01625 curr_fields_list= &curr_join->tmp_fields_list2; 01626 curr_all_fields= &curr_join->tmp_all_fields2; 01627 curr_join->set_items_ref_array(items2); 01628 curr_join->tmp_table_param.field_count+= 01629 curr_join->tmp_table_param.sum_func_count; 01630 curr_join->tmp_table_param.sum_func_count= 0; 01631 } 01632 if (curr_tmp_table->distinct) 01633 curr_join->select_distinct=0; /* Each row is unique */ 01634 01635 curr_join->join_free(); /* Free quick selects */ 01636 if (curr_join->select_distinct && ! curr_join->group_list) 01637 { 01638 thd->proc_info="Removing duplicates"; 01639 if (curr_join->tmp_having) 01640 curr_join->tmp_having->update_used_tables(); 01641 if (remove_duplicates(curr_join, curr_tmp_table, 01642 *curr_fields_list, curr_join->tmp_having)) 01643 DBUG_VOID_RETURN; 01644 curr_join->tmp_having=0; 01645 curr_join->select_distinct=0; 01646 } 01647 curr_tmp_table->reginfo.lock_type= TL_UNLOCK; 01648 if (make_simple_join(curr_join, curr_tmp_table)) 01649 DBUG_VOID_RETURN; 01650 calc_group_buffer(curr_join, curr_join->group_list); 01651 count_field_types(&curr_join->tmp_table_param, *curr_all_fields, 0); 01652 01653 } 01654 if (procedure) 01655 count_field_types(&curr_join->tmp_table_param, *curr_all_fields, 0); 01656 01657 if (curr_join->group || curr_join->tmp_table_param.sum_func_count || 01658 (procedure && (procedure->flags & PROC_GROUP))) 01659 { 01660 if (make_group_fields(this, curr_join)) 01661 { 01662 DBUG_VOID_RETURN; 01663 } 01664 if (!items3) 01665 { 01666 if (!items0) 01667 init_items_ref_array(); 01668 items3= ref_pointer_array + (all_fields.elements*4); 01669 setup_copy_fields(thd, &curr_join->tmp_table_param, 01670 items3, tmp_fields_list3, tmp_all_fields3, 01671 curr_fields_list->elements, *curr_all_fields); 01672 tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs; 01673 tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field; 01674 tmp_table_param.save_copy_field_end= 01675 curr_join->tmp_table_param.copy_field_end; 01676 curr_join->tmp_all_fields3= tmp_all_fields3; 01677 curr_join->tmp_fields_list3= tmp_fields_list3; 01678 } 01679 else 01680 { 01681 curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs; 01682 curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field; 01683 curr_join->tmp_table_param.copy_field_end= 01684 tmp_table_param.save_copy_field_end; 01685 } 01686 curr_fields_list= &tmp_fields_list3; 01687 curr_all_fields= &tmp_all_fields3; 01688 curr_join->set_items_ref_array(items3); 01689 01690 if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 01691 1, TRUE) || 01692 setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) || 01693 thd->is_fatal_error) 01694 DBUG_VOID_RETURN; 01695 } 01696 if (curr_join->group_list || curr_join->order) 01697 { 01698 DBUG_PRINT("info",("Sorting for send_fields")); 01699 thd->proc_info="Sorting result"; 01700 /* If we have already done the group, add HAVING to sorted table */ 01701 if (curr_join->tmp_having && ! curr_join->group_list && 01702 ! curr_join->sort_and_group) 01703 { 01704 // Some tables may have been const 01705 curr_join->tmp_having->update_used_tables(); 01706 JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables]; 01707 table_map used_tables= (curr_join->const_table_map | 01708 curr_table->table->map); 01709 01710 Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, 01711 used_tables, 01712 used_tables); 01713 if (sort_table_cond) 01714 { 01715 if (!curr_table->select) 01716 if (!(curr_table->select= new SQL_SELECT)) 01717 DBUG_VOID_RETURN; 01718 if (!curr_table->select->cond) 01719 curr_table->select->cond= sort_table_cond; 01720 else // This should never happen 01721 { 01722 if (!(curr_table->select->cond= 01723 new Item_cond_and(curr_table->select->cond, 01724 sort_table_cond))) 01725 DBUG_VOID_RETURN; 01726 /* 01727 Item_cond_and do not need fix_fields for execution, its parameters 01728 are fixed or do not need fix_fields, too 01729 */ 01730 curr_table->select->cond->quick_fix_field(); 01731 } 01732 curr_table->select_cond= curr_table->select->cond; 01733 curr_table->select_cond->top_level_item(); 01734 DBUG_EXECUTE("where",print_where(curr_table->select->cond, 01735 "select and having");); 01736 curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having, 01737 ~ (table_map) 0, 01738 ~used_tables); 01739 DBUG_EXECUTE("where",print_where(curr_join->tmp_having, 01740 "having after sort");); 01741 } 01742 } 01743 { 01744 if (group) 01745 curr_join->select_limit= HA_POS_ERROR; 01746 else 01747 { 01748 /* 01749 We can abort sorting after thd->select_limit rows if we there is no 01750 WHERE clause for any tables after the sorted one. 01751 */ 01752 JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1]; 01753 JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables]; 01754 for (; curr_table < end_table ; curr_table++) 01755 { 01756 /* 01757 table->keyuse is set in the case there was an original WHERE clause 01758 on the table that was optimized away. 01759 */ 01760 if (curr_table->select_cond || 01761 (curr_table->keyuse && !curr_table->first_inner)) 01762 { 01763 /* We have to sort all rows */ 01764 curr_join->select_limit= HA_POS_ERROR; 01765 break; 01766 } 01767 } 01768 } 01769 if (curr_join->join_tab == join_tab && save_join_tab()) 01770 { 01771 DBUG_VOID_RETURN; 01772 } 01773 /* 01774 Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser 01775 chose FILESORT to be faster than INDEX SCAN or there is no 01776 suitable index present. 01777 Note, that create_sort_index calls test_if_skip_sort_order and may 01778 finally replace sorting with index scan if there is a LIMIT clause in 01779 the query. XXX: it's never shown in EXPLAIN! 01780 OPTION_FOUND_ROWS supersedes LIMIT and is taken into account. 01781 */ 01782 if (create_sort_index(thd, curr_join, 01783 curr_join->group_list ? 01784 curr_join->group_list : curr_join->order, 01785 curr_join->select_limit, 01786 (select_options & OPTION_FOUND_ROWS ? 01787 HA_POS_ERROR : unit->select_limit_cnt))) 01788 DBUG_VOID_RETURN; 01789 if (curr_join->const_tables != curr_join->tables && 01790 !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache) 01791 { 01792 /* 01793 If no IO cache exists for the first table then we are using an 01794 INDEX SCAN and no filesort. Thus we should not remove the sorted 01795 attribute on the INDEX SCAN. 01796 */ 01797 skip_sort_order= 1; 01798 } 01799 } 01800 } 01801 /* XXX: When can we have here thd->net.report_error not zero? */ 01802 if (thd->net.report_error) 01803 { 01804 error= thd->net.report_error; 01805 DBUG_VOID_RETURN; 01806 } 01807 curr_join->having= curr_join->tmp_having; 01808 curr_join->fields= curr_fields_list; 01809 curr_join->procedure= procedure; 01810 01811 if (is_top_level_join() && thd->cursor && tables != const_tables) 01812 { 01813 /* 01814 We are here if this is JOIN::exec for the last select of the main unit 01815 and the client requested to open a cursor. 01816 We check that not all tables are constant because this case is not 01817 handled by do_select() separately, and this case is not implemented 01818 for cursors yet. 01819 */ 01820 DBUG_ASSERT(error == 0); 01821 /* 01822 curr_join is used only for reusable joins - that is, 01823 to perform SELECT for each outer row (like in subselects). 01824 This join is main, so we know for sure that curr_join == join. 01825 */ 01826 DBUG_ASSERT(curr_join == this); 01827 /* Open cursor for the last join sweep */ 01828 error= thd->cursor->open(this); 01829 } 01830 else 01831 { 01832 thd->proc_info="Sending data"; 01833 DBUG_PRINT("info", ("%s", thd->proc_info)); 01834 result->send_fields((procedure ? curr_join->procedure_fields_list : 01835 *curr_fields_list), 01836 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF); 01837 error= do_select(curr_join, curr_fields_list, NULL, procedure); 01838 thd->limit_found_rows= curr_join->send_records; 01839 thd->examined_row_count= curr_join->examined_rows; 01840 } 01841 01842 DBUG_VOID_RETURN; 01843 } 01844 01845 01846 /* 01847 Clean up join. Return error that hold JOIN. 01848 */ 01849 01850 int 01851 JOIN::destroy() 01852 { 01853 DBUG_ENTER("JOIN::destroy"); 01854 select_lex->join= 0; 01855 01856 if (tmp_join) 01857 { 01858 if (join_tab != tmp_join->join_tab) 01859 { 01860 JOIN_TAB *tab, *end; 01861 for (tab= join_tab, end= tab+tables ; tab != end ; tab++) 01862 tab->cleanup(); 01863 } 01864 tmp_join->tmp_join= 0; 01865 tmp_table_param.copy_field=0; 01866 DBUG_RETURN(tmp_join->destroy()); 01867 } 01868 cond_equal= 0; 01869 01870 cleanup(1); 01871 if (exec_tmp_table1) 01872 free_tmp_table(thd, exec_tmp_table1); 01873 if (exec_tmp_table2) 01874 free_tmp_table(thd, exec_tmp_table2); 01875 delete select; 01876 delete_dynamic(&keyuse); 01877 delete procedure; 01878 DBUG_RETURN(error); 01879 } 01880 01881 /* 01882 An entry point to single-unit select (a select without UNION). 01883 01884 SYNOPSIS 01885 mysql_select() 01886 01887 thd thread handler 01888 rref_pointer_array a reference to ref_pointer_array of 01889 the top-level select_lex for this query 01890 tables list of all tables used in this query. 01891 The tables have been pre-opened. 01892 wild_num number of wildcards used in the top level 01893 select of this query. 01894 For example statement 01895 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2; 01896 has 3 wildcards. 01897 fields list of items in SELECT list of the top-level 01898 select 01899 e.g. SELECT a, b, c FROM t1 will have Item_field 01900 for a, b and c in this list. 01901 conds top level item of an expression representing 01902 WHERE clause of the top level select 01903 og_num total number of ORDER BY and GROUP BY clauses 01904 arguments 01905 order linked list of ORDER BY agruments 01906 group linked list of GROUP BY arguments 01907 having top level item of HAVING expression 01908 proc_param list of PROCEDUREs 01909 select_options select options (BIG_RESULT, etc) 01910 result an instance of result set handling class. 01911 This object is responsible for send result 01912 set rows to the client or inserting them 01913 into a table. 01914 select_lex the only SELECT_LEX of this query 01915 unit top-level UNIT of this query 01916 UNIT is an artificial object created by the parser 01917 for every SELECT clause. 01918 e.g. SELECT * FROM t1 WHERE a1 IN (SELECT * FROM t2) 01919 has 2 unions. 01920 01921 RETURN VALUE 01922 FALSE success 01923 TRUE an error 01924 */ 01925 01926 bool 01927 mysql_select(THD *thd, Item ***rref_pointer_array, 01928 TABLE_LIST *tables, uint wild_num, List<Item> &fields, 01929 COND *conds, uint og_num, ORDER *order, ORDER *group, 01930 Item *having, ORDER *proc_param, ulong select_options, 01931 select_result *result, SELECT_LEX_UNIT *unit, 01932 SELECT_LEX *select_lex) 01933 { 01934 bool err; 01935 bool free_join= 1; 01936 DBUG_ENTER("mysql_select"); 01937 01938 select_lex->context.resolve_in_select_list= TRUE; 01939 JOIN *join; 01940 if (select_lex->join != 0) 01941 { 01942 join= select_lex->join; 01943 /* 01944 is it single SELECT in derived table, called in derived table 01945 creation 01946 */ 01947 if (select_lex->linkage != DERIVED_TABLE_TYPE || 01948 (select_options & SELECT_DESCRIBE)) 01949 { 01950 if (select_lex->linkage != GLOBAL_OPTIONS_TYPE) 01951 { 01952 //here is EXPLAIN of subselect or derived table 01953 if (join->change_result(result)) 01954 { 01955 DBUG_RETURN(TRUE); 01956 } 01957 } 01958 else 01959 { 01960 if (err= join->prepare(rref_pointer_array, tables, wild_num, 01961 conds, og_num, order, group, having, proc_param, 01962 select_lex, unit)) 01963 { 01964 goto err; 01965 } 01966 } 01967 } 01968 free_join= 0; 01969 join->select_options= select_options; 01970 } 01971 else 01972 { 01973 if (!(join= new JOIN(thd, fields, select_options, result))) 01974 DBUG_RETURN(TRUE); 01975 thd->proc_info="init"; 01976 thd->used_tables=0; // Updated by setup_fields 01977 if (err= join->prepare(rref_pointer_array, tables, wild_num, 01978 conds, og_num, order, group, having, proc_param, 01979 select_lex, unit)) 01980 { 01981 goto err; 01982 } 01983 } 01984 01985 if ((err= join->optimize())) 01986 { 01987 goto err; // 1 01988 } 01989 01990 if (thd->lex->describe & DESCRIBE_EXTENDED) 01991 { 01992 join->conds_history= join->conds; 01993 join->having_history= (join->having?join->having:join->tmp_having); 01994 } 01995 01996 if (thd->net.report_error) 01997 goto err; 01998 01999 join->exec(); 02000 02001 if (thd->cursor && thd->cursor->is_open()) 02002 { 02003 /* 02004 A cursor was opened for the last sweep in exec(). 02005 We are here only if this is mysql_select for top-level SELECT_LEX_UNIT 02006 and there were no error. 02007 */ 02008 free_join= 0; 02009 } 02010 02011 if (thd->lex->describe & DESCRIBE_EXTENDED) 02012 { 02013 select_lex->where= join->conds_history; 02014 select_lex->having= join->having_history; 02015 } 02016 02017 err: 02018 if (free_join) 02019 { 02020 thd->proc_info="end"; 02021 err|= select_lex->cleanup(); 02022 DBUG_RETURN(err || thd->net.report_error); 02023 } 02024 DBUG_RETURN(join->error); 02025 } 02026 02027 /***************************************************************************** 02028 Create JOIN_TABS, make a guess about the table types, 02029 Approximate how many records will be used in each table 02030 *****************************************************************************/ 02031 02032 static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select, 02033 TABLE *table, 02034 const key_map *keys,ha_rows limit) 02035 { 02036 int error; 02037 DBUG_ENTER("get_quick_record_count"); 02038 if (select) 02039 { 02040 select->head=table; 02041 table->reginfo.impossible_range=0; 02042 if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0, 02043 limit, 0)) == 1) 02044 DBUG_RETURN(select->quick->records); 02045 if (error == -1) 02046 { 02047 table->reginfo.impossible_range=1; 02048 DBUG_RETURN(0); 02049 } 02050 DBUG_PRINT("warning",("Couldn't use record count on const keypart")); 02051 } 02052 DBUG_RETURN(HA_POS_ERROR); /* This shouldn't happend */ 02053 } 02054 02055 02056 /* 02057 Calculate the best possible join and initialize the join structure 02058 02059 RETURN VALUES 02060 0 ok 02061 1 Fatal error 02062 */ 02063 02064 static bool 02065 make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds, 02066 DYNAMIC_ARRAY *keyuse_array) 02067 { 02068 int error; 02069 TABLE *table; 02070 uint i,table_count,const_count,key; 02071 table_map found_const_table_map, all_table_map, found_ref, refs; 02072 key_map const_ref, eq_part; 02073 TABLE **table_vector; 02074 JOIN_TAB *stat,*stat_end,*s,**stat_ref; 02075 KEYUSE *keyuse,*start_keyuse; 02076 table_map outer_join=0; 02077 JOIN_TAB *stat_vector[MAX_TABLES+1]; 02078 DBUG_ENTER("make_join_statistics"); 02079 02080 table_count=join->tables; 02081 stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count); 02082 stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES); 02083 table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2)); 02084 if (!stat || !stat_ref || !table_vector) 02085 DBUG_RETURN(1); // Eom /* purecov: inspected */ 02086 02087 join->best_ref=stat_vector; 02088 02089 stat_end=stat+table_count; 02090 found_const_table_map= all_table_map=0; 02091 const_count=0; 02092 02093 for (s= stat, i= 0; 02094 tables; 02095 s++, tables= tables->next_leaf, i++) 02096 { 02097 TABLE_LIST *embedding= tables->embedding; 02098 stat_vector[i]=s; 02099 s->keys.init(); 02100 s->const_keys.init(); 02101 s->checked_keys.init(); 02102 s->needed_reg.init(); 02103 table_vector[i]=s->table=table=tables->table; 02104 table->pos_in_table_list= tables; 02105 table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);// record count 02106 table->quick_keys.clear_all(); 02107 table->reginfo.join_tab=s; 02108 table->reginfo.not_exists_optimize=0; 02109 bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys); 02110 all_table_map|= table->map; 02111 s->join=join; 02112 s->info=0; // For describe 02113 02114 s->dependent= tables->dep_tables; 02115 s->key_dependent= 0; 02116 if (tables->schema_table) 02117 table->file->stats.records= 2; 02118 table->quick_condition_rows= table->file->records(); 02119 02120 s->on_expr_ref= &tables->on_expr; 02121 if (*s->on_expr_ref) 02122 { 02123 /* s is the only inner table of an outer join */ 02124 #ifdef WITH_PARTITION_STORAGE_ENGINE 02125 if ((!table->file->stats.records || table->no_partitions_used) && !embedding) 02126 #else 02127 if (!table->file->stats.records && !embedding) 02128 #endif 02129 { // Empty table 02130 s->dependent= 0; // Ignore LEFT JOIN depend. 02131 set_position(join,const_count++,s,(KEYUSE*) 0); 02132 continue; 02133 } 02134 outer_join|= table->map; 02135 s->embedding_map= 0; 02136 for (;embedding; embedding= embedding->embedding) 02137 s->embedding_map|= embedding->nested_join->nj_map; 02138 continue; 02139 } 02140 if (embedding) 02141 { 02142 /* s belongs to a nested join, maybe to several embedded joins */ 02143 s->embedding_map= 0; 02144 do 02145 { 02146 NESTED_JOIN *nested_join= embedding->nested_join; 02147 s->embedding_map|=nested_join->nj_map; 02148 s->dependent|= embedding->dep_tables; 02149 embedding= embedding->embedding; 02150 outer_join|= nested_join->used_tables; 02151 } 02152 while (embedding); 02153 continue; 02154 } 02155 #ifdef WITH_PARTITION_STORAGE_ENGINE 02156 bool no_partitions_used= table->no_partitions_used; 02157 #else 02158 const bool no_partitions_used= FALSE; 02159 #endif 02160 if ((table->s->system || table->file->stats.records <= 1 || 02161 no_partitions_used) && 02162 !s->dependent && 02163 (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && 02164 !table->fulltext_searched) 02165 { 02166 set_position(join,const_count++,s,(KEYUSE*) 0); 02167 } 02168 } 02169 stat_vector[i]=0; 02170 join->outer_join=outer_join; 02171 02172 if (join->outer_join) 02173 { 02174 /* 02175 Build transitive closure for relation 'to be dependent on'. 02176 This will speed up the plan search for many cases with outer joins, 02177 as well as allow us to catch illegal cross references/ 02178 Warshall's algorithm is used to build the transitive closure. 02179 As we use bitmaps to represent the relation the complexity 02180 of the algorithm is O((number of tables)^2). 02181 */ 02182 for (i= 0, s= stat ; i < table_count ; i++, s++) 02183 { 02184 for (uint j= 0 ; j < table_count ; j++) 02185 { 02186 table= stat[j].table; 02187 if (s->dependent & table->map) 02188 s->dependent |= table->reginfo.join_tab->dependent; 02189 } 02190 if (s->dependent) 02191 s->table->maybe_null= 1; 02192 } 02193 /* Catch illegal cross references for outer joins */ 02194 for (i= 0, s= stat ; i < table_count ; i++, s++) 02195 { 02196 if (s->dependent & s->table->map) 02197 { 02198 join->tables=0; // Don't use join->table 02199 my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0)); 02200 DBUG_RETURN(1); 02201 } 02202 s->key_dependent= s->dependent; 02203 } 02204 } 02205 02206 if (conds || outer_join) 02207 if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables, 02208 conds, join->cond_equal, 02209 ~outer_join, join->select_lex)) 02210 DBUG_RETURN(1); 02211 02212 /* Read tables with 0 or 1 rows (system tables) */ 02213 join->const_table_map= 0; 02214 02215 for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count; 02216 p_pos < p_end ; 02217 p_pos++) 02218 { 02219 int tmp; 02220 s= p_pos->table; 02221 s->type=JT_SYSTEM; 02222 join->const_table_map|=s->table->map; 02223 if ((tmp=join_read_const_table(s, p_pos))) 02224 { 02225 if (tmp > 0) 02226 DBUG_RETURN(1); // Fatal error 02227 } 02228 else 02229 found_const_table_map|= s->table->map; 02230 } 02231 02232 /* loop until no more const tables are found */ 02233 int ref_changed; 02234 do 02235 { 02236 ref_changed = 0; 02237 found_ref=0; 02238 02239 /* 02240 We only have to loop from stat_vector + const_count as 02241 set_position() will move all const_tables first in stat_vector 02242 */ 02243 02244 for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++) 02245 { 02246 table=s->table; 02247 if (s->dependent) // If dependent on some table 02248 { 02249 // All dep. must be constants 02250 if (s->dependent & ~(found_const_table_map)) 02251 continue; 02252 if (table->file->stats.records <= 1L && 02253 (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && 02254 !table->pos_in_table_list->embedding) 02255 { // system table 02256 int tmp= 0; 02257 s->type=JT_SYSTEM; 02258 join->const_table_map|=table->map; 02259 set_position(join,const_count++,s,(KEYUSE*) 0); 02260 if ((tmp= join_read_const_table(s, join->positions+const_count-1))) 02261 { 02262 if (tmp > 0) 02263 DBUG_RETURN(1); // Fatal error 02264 } 02265 else 02266 found_const_table_map|= table->map; 02267 continue; 02268 } 02269 } 02270 /* check if table can be read by key or table only uses const refs */ 02271 if ((keyuse=s->keyuse)) 02272 { 02273 s->type= JT_REF; 02274 while (keyuse->table == table) 02275 { 02276 start_keyuse=keyuse; 02277 key=keyuse->key; 02278 s->keys.set_bit(key); // QQ: remove this ? 02279 02280 refs=0; 02281 const_ref.clear_all(); 02282 eq_part.clear_all(); 02283 do 02284 { 02285 if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize) 02286 { 02287 if (!((~found_const_table_map) & keyuse->used_tables)) 02288 const_ref.set_bit(keyuse->keypart); 02289 else 02290 refs|=keyuse->used_tables; 02291 eq_part.set_bit(keyuse->keypart); 02292 } 02293 keyuse++; 02294 } while (keyuse->table == table && keyuse->key == key); 02295 02296 if (eq_part.is_prefix(table->key_info[key].key_parts) && 02297 ((table->key_info[key].flags & (HA_NOSAME | HA_END_SPACE_KEY)) == 02298 HA_NOSAME) && 02299 !table->fulltext_searched && 02300 !table->pos_in_table_list->embedding) 02301 { 02302 if (const_ref == eq_part) 02303 { // Found everything for ref. 02304 int tmp; 02305 ref_changed = 1; 02306 s->type= JT_CONST; 02307 join->const_table_map|=table->map; 02308 set_position(join,const_count++,s,start_keyuse); 02309 if (create_ref_for_key(join, s, start_keyuse, 02310 found_const_table_map)) 02311 DBUG_RETURN(1); 02312 if ((tmp=join_read_const_table(s, 02313 join->positions+const_count-1))) 02314 { 02315 if (tmp > 0) 02316 DBUG_RETURN(1); // Fatal error 02317 } 02318 else 02319 found_const_table_map|= table->map; 02320 break; 02321 } 02322 else 02323 found_ref|= refs; // Table is const if all refs are const 02324 } 02325 } 02326 } 02327 } 02328 } while (join->const_table_map & found_ref && ref_changed); 02329 02330 /* Calc how many (possible) matched records in each table */ 02331 02332 for (s=stat ; s < stat_end ; s++) 02333 { 02334 if (s->type == JT_SYSTEM || s->type == JT_CONST) 02335 { 02336 /* Only one matching row */ 02337 s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0; 02338 continue; 02339 } 02340 /* Approximate found rows and time to read them */ 02341 s->found_records=s->records=s->table->file->stats.records; 02342 s->read_time=(ha_rows) s->table->file->scan_time(); 02343 02344 /* 02345 Set a max range of how many seeks we can expect when using keys 02346 This is can't be to high as otherwise we are likely to use 02347 table scan. 02348 */ 02349 s->worst_seeks= min((double) s->found_records / 10, 02350 (double) s->read_time*3); 02351 if (s->worst_seeks < 2.0) // Fix for small tables 02352 s->worst_seeks=2.0; 02353 02354 /* 02355 Add to stat->const_keys those indexes for which all group fields or 02356 all select distinct fields participate in one index. 02357 */ 02358 add_group_and_distinct_keys(join, s); 02359 02360 if (!s->const_keys.is_clear_all() && 02361 !s->table->pos_in_table_list->embedding) 02362 { 02363 ha_rows records; 02364 SQL_SELECT *select; 02365 select= make_select(s->table, found_const_table_map, 02366 found_const_table_map, 02367 *s->on_expr_ref ? *s->on_expr_ref : conds, 02368 1, &error); 02369 if (!select) 02370 DBUG_RETURN(1); 02371 records= get_quick_record_count(join->thd, select, s->table, 02372 &s->const_keys, join->row_limit); 02373 s->quick=select->quick; 02374 s->needed_reg=select->needed_reg; 02375 select->quick=0; 02376 if (records == 0 && s->table->reginfo.impossible_range) 02377 { 02378 /* 02379 Impossible WHERE or ON expression 02380 In case of ON, we mark that the we match one empty NULL row. 02381 In case of WHERE, don't set found_const_table_map to get the 02382 caller to abort with a zero row result. 02383 */ 02384 join->const_table_map|= s->table->map; 02385 set_position(join,const_count++,s,(KEYUSE*) 0); 02386 s->type= JT_CONST; 02387 if (*s->on_expr_ref) 02388 { 02389 /* Generate empty row */ 02390 s->info= "Impossible ON condition"; 02391 found_const_table_map|= s->table->map; 02392 s->type= JT_CONST; 02393 mark_as_null_row(s->table); // All fields are NULL 02394 } 02395 } 02396 if (records != HA_POS_ERROR) 02397 { 02398 s->found_records=records; 02399 s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0); 02400 } 02401 delete select; 02402 } 02403 } 02404 02405 join->join_tab=stat; 02406 join->map2table=stat_ref; 02407 join->table= join->all_tables=table_vector; 02408 join->const_tables=const_count; 02409 join->found_const_table_map=found_const_table_map; 02410 02411 /* Find an optimal join order of the non-constant tables. */ 02412 if (join->const_tables != join->tables) 02413 { 02414 optimize_keyuse(join, keyuse_array); 02415 choose_plan(join, all_table_map & ~join->const_table_map); 02416 } 02417 else 02418 { 02419 memcpy((gptr) join->best_positions,(gptr) join->positions, 02420 sizeof(POSITION)*join->const_tables); 02421 join->best_read=1.0; 02422 } 02423 /* Generate an execution plan from the found optimal join order. */ 02424 DBUG_RETURN(join->thd->killed || get_best_combination(join)); 02425 } 02426 02427 02428 /***************************************************************************** 02429 Check with keys are used and with tables references with tables 02430 Updates in stat: 02431 keys Bitmap of all used keys 02432 const_keys Bitmap of all keys with may be used with quick_select 02433 keyuse Pointer to possible keys 02434 *****************************************************************************/ 02435 02436 typedef struct key_field_t { // Used when finding key fields 02437 Field *field; 02438 Item *val; // May be empty if diff constant 02439 uint level; 02440 uint optimize; 02441 bool eq_func; 02442 /* 02443 If true, the condition this struct represents will not be satisfied 02444 when val IS NULL. 02445 */ 02446 bool null_rejecting; 02447 } KEY_FIELD; 02448 02449 /* Values in optimize */ 02450 #define KEY_OPTIMIZE_EXISTS 1 02451 #define KEY_OPTIMIZE_REF_OR_NULL 2 02452 02453 /* 02454 Merge new key definitions to old ones, remove those not used in both 02455 02456 This is called for OR between different levels 02457 02458 To be able to do 'ref_or_null' we merge a comparison of a column 02459 and 'column IS NULL' to one test. This is useful for sub select queries 02460 that are internally transformed to something like: 02461 02462 SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 02463 02464 KEY_FIELD::null_rejecting is processed as follows: 02465 result has null_rejecting=true if it is set for both ORed references. 02466 for example: 02467 (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true 02468 (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false 02469 */ 02470 02471 static KEY_FIELD * 02472 merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end, 02473 uint and_level) 02474 { 02475 if (start == new_fields) 02476 return start; // Impossible or 02477 if (new_fields == end) 02478 return start; // No new fields, skip all 02479 02480 KEY_FIELD *first_free=new_fields; 02481 02482 /* Mark all found fields in old array */ 02483 for (; new_fields != end ; new_fields++) 02484 { 02485 for (KEY_FIELD *old=start ; old != first_free ; old++) 02486 { 02487 if (old->field == new_fields->field) 02488 { 02489 /* 02490 NOTE: below const_item() call really works as "!used_tables()", i.e. 02491 it can return FALSE where it is feasible to make it return TRUE. 02492 02493 The cause is as follows: Some of the tables are already known to be 02494 const tables (the detection code is in make_join_statistics(), 02495 above the update_ref_and_keys() call), but we didn't propagate 02496 information about this: TABLE::const_table is not set to TRUE, and 02497 Item::update_used_tables() hasn't been called for each item. 02498 The result of this is that we're missing some 'ref' accesses. 02499 TODO: OptimizerTeam: Fix this 02500 */ 02501 if (!new_fields->val->const_item()) 02502 { 02503 /* 02504 If the value matches, we can use the key reference. 02505 If not, we keep it until we have examined all new values 02506 */ 02507 if (old->val->eq(new_fields->val, old->field->binary())) 02508 { 02509 old->level= and_level; 02510 old->optimize= ((old->optimize & new_fields->optimize & 02511 KEY_OPTIMIZE_EXISTS) | 02512 ((old->optimize | new_fields->optimize) & 02513 KEY_OPTIMIZE_REF_OR_NULL)); 02514 old->null_rejecting= (old->null_rejecting && 02515 new_fields->null_rejecting); 02516 } 02517 } 02518 else if (old->eq_func && new_fields->eq_func && 02519 old->val->eq(new_fields->val, old->field->binary())) 02520 02521 { 02522 old->level= and_level; 02523 old->optimize= ((old->optimize & new_fields->optimize & 02524 KEY_OPTIMIZE_EXISTS) | 02525 ((old->optimize | new_fields->optimize) & 02526 KEY_OPTIMIZE_REF_OR_NULL)); 02527 old->null_rejecting= (old->null_rejecting && 02528 new_fields->null_rejecting); 02529 } 02530 else if (old->eq_func && new_fields->eq_func && 02531 ((old->val->const_item() && old->val->is_null()) || 02532 new_fields->val->is_null())) 02533 { 02534 /* field = expression OR field IS NULL */ 02535 old->level= and_level; 02536 old->optimize= KEY_OPTIMIZE_REF_OR_NULL; 02537 /* 02538 Remember the NOT NULL value unless the value does not depend 02539 on other tables. 02540 */ 02541 if (!old->val->used_tables() && old->val->is_null()) 02542 old->val= new_fields->val; 02543 /* The referred expression can be NULL: */ 02544 old->null_rejecting= 0; 02545 } 02546 else 02547 { 02548 /* 02549 We are comparing two different const. In this case we can't 02550 use a key-lookup on this so it's better to remove the value 02551 and let the range optimzier handle it 02552 */ 02553 if (old == --first_free) // If last item 02554 break; 02555 *old= *first_free; // Remove old value 02556 old--; // Retry this value 02557 } 02558 } 02559 } 02560 } 02561 /* Remove all not used items */ 02562 for (KEY_FIELD *old=start ; old != first_free ;) 02563 { 02564 if (old->level != and_level) 02565 { // Not used in all levels 02566 if (old == --first_free) 02567 break; 02568 *old= *first_free; // Remove old value 02569 continue; 02570 } 02571 old++; 02572 } 02573 return first_free; 02574 } 02575 02576 02577 /* 02578 Add a possible key to array of possible keys if it's usable as a key 02579 02580 SYNPOSIS 02581 add_key_field() 02582 key_fields Pointer to add key, if usable 02583 and_level And level, to be stored in KEY_FIELD 02584 cond Condition predicate 02585 field Field used in comparision 02586 eq_func True if we used =, <=> or IS NULL 02587 value Value used for comparison with field 02588 usable_tables Tables which can be used for key optimization 02589 02590 NOTES 02591 If we are doing a NOT NULL comparison on a NOT NULL field in a outer join 02592 table, we store this to be able to do not exists optimization later. 02593 02594 RETURN 02595 *key_fields is incremented if we stored a key in the array 02596 */ 02597 02598 static void 02599 add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond, 02600 Field *field, bool eq_func, Item **value, uint num_values, 02601 table_map usable_tables) 02602 { 02603 uint exists_optimize= 0; 02604 if (!(field->flags & PART_KEY_FLAG)) 02605 { 02606 // Don't remove column IS NULL on a LEFT JOIN table 02607 if (!eq_func || (*value)->type() != Item::NULL_ITEM || 02608 !field->table->maybe_null || field->null_ptr) 02609 return; // Not a key. Skip it 02610 exists_optimize= KEY_OPTIMIZE_EXISTS; 02611 DBUG_ASSERT(num_values == 1); 02612 } 02613 else 02614 { 02615 table_map used_tables=0; 02616 bool optimizable=0; 02617 for (uint i=0; i<num_values; i++) 02618 { 02619 used_tables|=(value[i])->used_tables(); 02620 if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT))) 02621 optimizable=1; 02622 } 02623 if (!optimizable) 02624 return; 02625 if (!(usable_tables & field->table->map)) 02626 { 02627 if (!eq_func || (*value)->type() != Item::NULL_ITEM || 02628 !field->table->maybe_null || field->null_ptr) 02629 return; // Can't use left join optimize 02630 exists_optimize= KEY_OPTIMIZE_EXISTS; 02631 } 02632 else 02633 { 02634 JOIN_TAB *stat=field->table->reginfo.join_tab; 02635 key_map possible_keys=field->key_start; 02636 possible_keys.intersect(field->table->keys_in_use_for_query); 02637 stat[0].keys.merge(possible_keys); // Add possible keys 02638 02639 /* 02640 Save the following cases: 02641 Field op constant 02642 Field LIKE constant where constant doesn't start with a wildcard 02643 Field = field2 where field2 is in a different table 02644 Field op formula 02645 Field IS NULL 02646 Field IS NOT NULL 02647 Field BETWEEN ... 02648 Field IN ... 02649 */ 02650 stat[0].key_dependent|=used_tables; 02651 02652 bool is_const=1; 02653 for (uint i=0; i<num_values; i++) 02654 { 02655 if (!(is_const&= value[i]->const_item())) 02656 break; 02657 } 02658 if (is_const) 02659 stat[0].const_keys.merge(possible_keys); 02660 /* 02661 We can't always use indexes when comparing a string index to a 02662 number. cmp_type() is checked to allow compare of dates to numbers. 02663 eq_func is NEVER true when num_values > 1 02664 */ 02665 if (!eq_func) 02666 { 02667 /* 02668 Additional optimization: if we're processing 02669 "t.key BETWEEN c1 AND c1" then proceed as if we were processing 02670 "t.key = c1". 02671 TODO: This is a very limited fix. A more generic fix is possible. 02672 There are 2 options: 02673 A) Make equality propagation code be able to handle BETWEEN 02674 (including cases like t1.key BETWEEN t2.key AND t3.key) 02675 B) Make range optimizer to infer additional "t.key = c" equalities 02676 and use them in equality propagation process (see details in 02677 OptimizerKBAndTodo) 02678 */ 02679 if ((cond->functype() != Item_func::BETWEEN) || 02680 ((Item_func_between*) cond)->negated || 02681 !value[0]->eq(value[1], field->binary())) 02682 return; 02683 eq_func= TRUE; 02684 } 02685 02686 if (field->result_type() == STRING_RESULT) 02687 { 02688 if ((*value)->result_type() != STRING_RESULT) 02689 { 02690 if (field->cmp_type() != (*value)->result_type()) 02691 return; 02692 } 02693 else 02694 { 02695 /* 02696 We can't use indexes if the effective collation 02697 of the operation differ from the field collation. 02698 02699 We also cannot use index on a text column, as the column may 02700 contain 'x' 'x\t' 'x ' and 'read_next_same' will stop after 02701 'x' when searching for WHERE col='x ' 02702 */ 02703 if (field->cmp_type() == STRING_RESULT && 02704 (((Field_str*)field)->charset() != cond->compare_collation() || 02705 ((*value)->type() != Item::NULL_ITEM && 02706 (field->flags & BLOB_FLAG) && !field->binary()))) 02707 return; 02708 } 02709 } 02710 } 02711 } 02712 /* 02713 For the moment eq_func is always true. This slot is reserved for future 02714 extensions where we want to remembers other things than just eq comparisons 02715 */ 02716 DBUG_ASSERT(eq_func); 02717 /* Store possible eq field */ 02718 (*key_fields)->field= field; 02719 (*key_fields)->eq_func= eq_func; 02720 (*key_fields)->val= *value; 02721 (*key_fields)->level= and_level; 02722 (*key_fields)->optimize= exists_optimize; 02723 /* 02724 If the condition has form "tbl.keypart = othertbl.field" and 02725 othertbl.field can be NULL, there will be no matches if othertbl.field 02726 has NULL value. 02727 We use null_rejecting in add_not_null_conds() to add 02728 'othertbl.field IS NOT NULL' to tab->select_cond. 02729 */ 02730 (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC) && 02731 ((*value)->type() == Item::FIELD_ITEM) && 02732 ((Item_field*)*value)->field->maybe_null()); 02733 (*key_fields)++; 02734 } 02735 02736 /* 02737 Add possible keys to array of possible keys originated from a simple predicate 02738 02739 SYNPOSIS 02740 add_key_equal_fields() 02741 key_fields Pointer to add key, if usable 02742 and_level And level, to be stored in KEY_FIELD 02743 cond Condition predicate 02744 field Field used in comparision 02745 eq_func True if we used =, <=> or IS NULL 02746 value Value used for comparison with field 02747 Is NULL for BETWEEN and IN 02748 usable_tables Tables which can be used for key optimization 02749 02750 NOTES 02751 If field items f1 and f2 belong to the same multiple equality and 02752 a key is added for f1, the the same key is added for f2. 02753 02754 RETURN 02755 *key_fields is incremented if we stored a key in the array 02756 */ 02757 02758 static void 02759 add_key_equal_fields(KEY_FIELD **key_fields, uint and_level, 02760 Item_func *cond, Item_field *field_item, 02761 bool eq_func, Item **val, 02762 uint num_values, table_map usable_tables) 02763 { 02764 Field *field= field_item->field; 02765 add_key_field(key_fields, and_level, cond, field, 02766 eq_func, val, num_values, usable_tables); 02767 Item_equal *item_equal= field_item->item_equal; 02768 if (item_equal) 02769 { 02770 /* 02771 Add to the set of possible key values every substitution of 02772 the field for an equal field included into item_equal 02773 */ 02774 Item_equal_iterator it(*item_equal); 02775 Item_field *item; 02776 while ((item= it++)) 02777 { 02778 if (!field->eq(item->field)) 02779 { 02780 add_key_field(key_fields, and_level, cond, item->field, 02781 eq_func, val, num_values, usable_tables); 02782 } 02783 } 02784 } 02785 } 02786 02787 static void 02788 add_key_fields(KEY_FIELD **key_fields,uint *and_level, 02789 COND *cond, table_map usable_tables) 02790 { 02791 if (cond->type() == Item_func::COND_ITEM) 02792 { 02793 List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); 02794 KEY_FIELD *org_key_fields= *key_fields; 02795 02796 if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) 02797 { 02798 Item *item; 02799 while ((item=li++)) 02800 add_key_fields(key_fields,and_level,item,usable_tables); 02801 for (; org_key_fields != *key_fields ; org_key_fields++) 02802 org_key_fields->level= *and_level; 02803 } 02804 else 02805 { 02806 (*and_level)++; 02807 add_key_fields(key_fields,and_level,li++,usable_tables); 02808 Item *item; 02809 while ((item=li++)) 02810 { 02811 KEY_FIELD *start_key_fields= *key_fields; 02812 (*and_level)++; 02813 add_key_fields(key_fields,and_level,item,usable_tables); 02814 *key_fields=merge_key_fields(org_key_fields,start_key_fields, 02815 *key_fields,++(*and_level)); 02816 } 02817 } 02818 return; 02819 } 02820 /* If item is of type 'field op field/constant' add it to key_fields */ 02821 02822 if (cond->type() != Item::FUNC_ITEM) 02823 return; 02824 Item_func *cond_func= (Item_func*) cond; 02825 switch (cond_func->select_optimize()) { 02826 case Item_func::OPTIMIZE_NONE: 02827 break; 02828 case Item_func::OPTIMIZE_KEY: 02829 { 02830 // BETWEEN, IN, NE 02831 if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM && 02832 !(cond_func->used_tables() & OUTER_REF_TABLE_BIT)) 02833 { 02834 Item **values= cond_func->arguments()+1; 02835 if (cond_func->functype() == Item_func::NE_FUNC && 02836 cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM && 02837 !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT)) 02838 values--; 02839 DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC || 02840 cond_func->argument_count() != 2); 02841 add_key_equal_fields(key_fields, *and_level, cond_func, 02842 (Item_field*) (cond_func->key_item()->real_item()), 02843 0, values, 02844 cond_func->argument_count()-1, 02845 usable_tables); 02846 } 02847 break; 02848 } 02849 case Item_func::OPTIMIZE_OP: 02850 { 02851 bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC || 02852 cond_func->functype() == Item_func::EQUAL_FUNC); 02853 02854 if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM && 02855 !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT)) 02856 { 02857 add_key_equal_fields(key_fields, *and_level, cond_func, 02858 (Item_field*) (cond_func->arguments()[0])->real_item(), 02859 equal_func, 02860 cond_func->arguments()+1, 1, usable_tables); 02861 } 02862 if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM && 02863 cond_func->functype() != Item_func::LIKE_FUNC && 02864 !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT)) 02865 { 02866 add_key_equal_fields(key_fields, *and_level, cond_func, 02867 (Item_field*) (cond_func->arguments()[1])->real_item(), 02868 equal_func, 02869 cond_func->arguments(),1,usable_tables); 02870 } 02871 break; 02872 } 02873 case Item_func::OPTIMIZE_NULL: 02874 /* column_name IS [NOT] NULL */ 02875 if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM && 02876 !(cond_func->used_tables() & OUTER_REF_TABLE_BIT)) 02877 { 02878 Item *tmp=new Item_null; 02879 if (unlikely(!tmp)) // Should never be true 02880 return; 02881 add_key_equal_fields(key_fields, *and_level, cond_func, 02882 (Item_field*) (cond_func->arguments()[0])->real_item(), 02883 cond_func->functype() == Item_func::ISNULL_FUNC, 02884 &tmp, 1, usable_tables); 02885 } 02886 break; 02887 case Item_func::OPTIMIZE_EQUAL: 02888 Item_equal *item_equal= (Item_equal *) cond; 02889 Item *const_item= item_equal->get_const(); 02890 Item_equal_iterator it(*item_equal); 02891 Item_field *item; 02892 if (const_item) 02893 { 02894 /* 02895 For each field field1 from item_equal consider the equality 02896 field1=const_item as a condition allowing an index access of the table 02897 with field1 by the keys value of field1. 02898 */ 02899 while ((item= it++)) 02900 { 02901 add_key_field(key_fields, *and_level, cond_func, item->field, 02902 TRUE, &const_item, 1, usable_tables); 02903 } 02904 } 02905 else 02906 { 02907 /* 02908 Consider all pairs of different fields included into item_equal. 02909 For each of them (field1, field1) consider the equality 02910 field1=field2 as a condition allowing an index access of the table 02911 with field1 by the keys value of field2. 02912 */ 02913 Item_equal_iterator fi(*item_equal); 02914 while ((item= fi++)) 02915 { 02916 Field *field= item->field; 02917 while ((item= it++)) 02918 { 02919 if (!field->eq(item->field)) 02920 { 02921 add_key_field(key_fields, *and_level, cond_func, field, 02922 TRUE, (Item **) &item, 1, usable_tables); 02923 } 02924 } 02925 it.rewind(); 02926 } 02927 } 02928 break; 02929 } 02930 } 02931 02932 /* 02933 Add all keys with uses 'field' for some keypart 02934 If field->and_level != and_level then only mark key_part as const_part 02935 */ 02936 02937 static uint 02938 max_part_bit(key_part_map bits) 02939 { 02940 uint found; 02941 for (found=0; bits & 1 ; found++,bits>>=1) ; 02942 return found; 02943 } 02944 02945 static void 02946 add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field) 02947 { 02948 Field *field=key_field->field; 02949 TABLE *form= field->table; 02950 KEYUSE keyuse; 02951 02952 if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS)) 02953 { 02954 for (uint key=0 ; key < form->s->keys ; key++) 02955 { 02956 if (!(form->keys_in_use_for_query.is_set(key))) 02957 continue; 02958 if (form->key_info[key].flags & HA_FULLTEXT) 02959 continue; // ToDo: ft-keys in non-ft queries. SerG 02960 02961 uint key_parts= (uint) form->key_info[key].key_parts; 02962 for (uint part=0 ; part < key_parts ; part++) 02963 { 02964 if (field->eq(form->key_info[key].key_part[part].field)) 02965 { 02966 keyuse.table= field->table; 02967 keyuse.val = key_field->val; 02968 keyuse.key = key; 02969 keyuse.keypart=part; 02970 keyuse.keypart_map= (key_part_map) 1 << part; 02971 keyuse.used_tables=key_field->val->used_tables(); 02972 keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL; 02973 keyuse.null_rejecting= key_field->null_rejecting; 02974 VOID(insert_dynamic(keyuse_array,(gptr) &keyuse)); 02975 } 02976 } 02977 } 02978 } 02979 } 02980 02981 02982 #define FT_KEYPART (MAX_REF_PARTS+10) 02983 02984 static void 02985 add_ft_keys(DYNAMIC_ARRAY *keyuse_array, 02986 JOIN_TAB *stat,COND *cond,table_map usable_tables) 02987 { 02988 Item_func_match *cond_func=NULL; 02989 02990 if (!cond) 02991 return; 02992 02993 if (cond->type() == Item::FUNC_ITEM) 02994 { 02995 Item_func *func=(Item_func *)cond; 02996 Item_func::Functype functype= func->functype(); 02997 if (functype == Item_func::FT_FUNC) 02998 cond_func=(Item_func_match *)cond; 02999 else if (func->arg_count == 2) 03000 { 03001 Item_func *arg0=(Item_func *)(func->arguments()[0]), 03002 *arg1=(Item_func *)(func->arguments()[1]); 03003 if (arg1->const_item() && 03004 ((functype == Item_func::GE_FUNC && arg1->val_real() > 0) || 03005 (functype == Item_func::GT_FUNC && arg1->val_real() >=0)) && 03006 arg0->type() == Item::FUNC_ITEM && 03007 arg0->functype() == Item_func::FT_FUNC) 03008 cond_func=(Item_func_match *) arg0; 03009 else if (arg0->const_item() && 03010 ((functype == Item_func::LE_FUNC && arg0->val_real() > 0) || 03011 (functype == Item_func::LT_FUNC && arg0->val_real() >=0)) && 03012 arg1->type() == Item::FUNC_ITEM && 03013 arg1->functype() == Item_func::FT_FUNC) 03014 cond_func=(Item_func_match *) arg1; 03015 } 03016 } 03017 else if (cond->type() == Item::COND_ITEM) 03018 { 03019 List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); 03020 03021 if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) 03022 { 03023 Item *item; 03024 while ((item=li++)) 03025 add_ft_keys(keyuse_array,stat,item,usable_tables); 03026 } 03027 } 03028 03029 if (!cond_func || cond_func->key == NO_SUCH_KEY || 03030 !(usable_tables & cond_func->table->map)) 03031 return; 03032 03033 KEYUSE keyuse; 03034 keyuse.table= cond_func->table; 03035 keyuse.val = cond_func; 03036 keyuse.key = cond_func->key; 03037 keyuse.keypart= FT_KEYPART; 03038 keyuse.used_tables=cond_func->key_item()->used_tables(); 03039 keyuse.optimize= 0; 03040 keyuse.keypart_map= 0; 03041 VOID(insert_dynamic(keyuse_array,(gptr) &keyuse)); 03042 } 03043 03044 03045 static int 03046 sort_keyuse(KEYUSE *a,KEYUSE *b) 03047 { 03048 int res; 03049 if (a->table->tablenr != b->table->tablenr) 03050 return (int) (a->table->tablenr - b->table->tablenr); 03051 if (a->key != b->key) 03052 return (int) (a->key - b->key); 03053 if (a->keypart != b->keypart) 03054 return (int) (a->keypart - b->keypart); 03055 // Place const values before other ones 03056 if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) - 03057 test((b->used_tables & ~OUTER_REF_TABLE_BIT)))) 03058 return res; 03059 /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */ 03060 return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) - 03061 (b->optimize & KEY_OPTIMIZE_REF_OR_NULL)); 03062 } 03063 03064 03065 /* 03066 Add to KEY_FIELD array all 'ref' access candidates within nested join 03067 03068 SYNPOSIS 03069 add_key_fields_for_nj() 03070 nested_join_table IN Nested join pseudo-table to process 03071 end INOUT End of the key field array 03072 and_level INOUT And-level 03073 03074 DESCRIPTION 03075 This function populates KEY_FIELD array with entries generated from the 03076 ON condition of the given nested join, and does the same for nested joins 03077 contained within this nested join. 03078 03079 NOTES 03080 We can add accesses to the tables that are direct children of this nested 03081 join (1), and are not inner tables w.r.t their neighbours (2). 03082 03083 Example for #1 (outer brackets pair denotes nested join this function is 03084 invoked for): 03085 ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond 03086 Example for #2: 03087 ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond 03088 In examples 1-2 for condition cond, we can add 'ref' access candidates to 03089 t1 only. 03090 Example #3: 03091 ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond 03092 Here we can add 'ref' access candidates for t1 and t2, but not for t3. 03093 */ 03094 03095 static void add_key_fields_for_nj(TABLE_LIST *nested_join_table, 03096 KEY_FIELD **end, uint *and_level) 03097 { 03098 List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list); 03099 table_map tables= 0; 03100 TABLE_LIST *table; 03101 DBUG_ASSERT(nested_join_table->nested_join); 03102 03103 while ((table= li++)) 03104 { 03105 if (table->nested_join) 03106 add_key_fields_for_nj(table, end, and_level); 03107 else 03108 if (!table->on_expr) 03109 tables |= table->table->map; 03110 } 03111 add_key_fields(end, and_level, nested_join_table->on_expr, tables); 03112 } 03113 03114 03115 /* 03116 Update keyuse array with all possible keys we can use to fetch rows 03117 03118 SYNOPSIS 03119 update_ref_and_keys() 03120 thd 03121 keyuse OUT Put here ordered array of KEYUSE structures 03122 join_tab Array in tablenr_order 03123 tables Number of tables in join 03124 cond WHERE condition (note that the function analyzes 03125 join_tab[i]->on_expr too) 03126 normal_tables tables not inner w.r.t some outer join (ones for which 03127 we can make ref access based the WHERE clause) 03128 select_lex current SELECT 03129 03130 RETURN 03131 0 - OK 03132 1 - Out of memory. 03133 */ 03134 03135 static bool 03136 update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab, 03137 uint tables, COND *cond, COND_EQUAL *cond_equal, 03138 table_map normal_tables, SELECT_LEX *select_lex) 03139 { 03140 uint and_level,i,found_eq_constant; 03141 KEY_FIELD *key_fields, *end, *field; 03142 uint m= 1; 03143 03144 if (cond_equal && cond_equal->max_members) 03145 m= cond_equal->max_members; 03146 03147 if (!(key_fields=(KEY_FIELD*) 03148 thd->alloc(sizeof(key_fields[0])* 03149 (thd->lex->current_select->cond_count+1)*2*m))) 03150 return TRUE; /* purecov: inspected */ 03151 and_level= 0; 03152 field= end= key_fields; 03153 if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64)) 03154 return TRUE; 03155 if (cond) 03156 { 03157 add_key_fields(&end,&and_level,cond,normal_tables); 03158 for (; field != end ; field++) 03159 { 03160 add_key_part(keyuse,field); 03161 /* Mark that we can optimize LEFT JOIN */ 03162 if (field->val->type() == Item::NULL_ITEM && 03163 !field->field->real_maybe_null()) 03164 field->field->table->reginfo.not_exists_optimize=1; 03165 } 03166 } 03167 for (i=0 ; i < tables ; i++) 03168 { 03169 /* 03170 Block the creation of keys for inner tables of outer joins. 03171 Here only the outer joins that can not be converted to 03172 inner joins are left and all nests that can be eliminated 03173 are flattened. 03174 In the future when we introduce conditional accesses 03175 for inner tables in outer joins these keys will be taken 03176 into account as well. 03177 */ 03178 if (*join_tab[i].on_expr_ref) 03179 add_key_fields(&end,&and_level,*join_tab[i].on_expr_ref, 03180 join_tab[i].table->map); 03181 } 03182 03183 /* Process ON conditions for the nested joins */ 03184 { 03185 List_iterator<TABLE_LIST> li(*join_tab->join->join_list); 03186 TABLE_LIST *table; 03187 while ((table= li++)) 03188 { 03189 if (table->nested_join) 03190 add_key_fields_for_nj(table, &end, &and_level); 03191 } 03192 } 03193 03194 /* fill keyuse with found key parts */ 03195 for ( ; field != end ; field++) 03196 add_key_part(keyuse,field); 03197 03198 if (select_lex->ftfunc_list->elements) 03199 { 03200 add_ft_keys(keyuse,join_tab,cond,normal_tables); 03201 } 03202 03203 /* 03204 Sort the array of possible keys and remove the following key parts: 03205 - ref if there is a keypart which is a ref and a const. 03206 (e.g. if there is a key(a,b) and the clause is a=3 and b=7 and b=t2.d, 03207 then we skip the key part corresponding to b=t2.d) 03208 - keyparts without previous keyparts 03209 (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is 03210 used in the query, we drop the partial key parts from consideration). 03211 Special treatment for ft-keys. 03212 */ 03213 if (keyuse->elements) 03214 { 03215 KEYUSE end,*prev,*save_pos,*use; 03216 03217 qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE), 03218 (qsort_cmp) sort_keyuse); 03219 03220 bzero((char*) &end,sizeof(end)); /* Add for easy testing */ 03221 VOID(insert_dynamic(keyuse,(gptr) &end)); 03222 03223 use=save_pos=dynamic_element(keyuse,0,KEYUSE*); 03224 prev=&end; 03225 found_eq_constant=0; 03226 for (i=0 ; i < keyuse->elements-1 ; i++,use++) 03227 { 03228 if (!use->used_tables) 03229 use->table->const_key_parts[use->key]|= use->keypart_map; 03230 if (use->keypart != FT_KEYPART) 03231 { 03232 if (use->key == prev->key && use->table == prev->table) 03233 { 03234 if (prev->keypart+1 < use->keypart || 03235 prev->keypart == use->keypart && found_eq_constant) 03236 continue; /* remove */ 03237 } 03238 else if (use->keypart != 0) // First found must be 0 03239 continue; 03240 } 03241 03242 *save_pos= *use; 03243 prev=use; 03244 found_eq_constant= !use->used_tables; 03245 /* Save ptr to first use */ 03246 if (!use->table->reginfo.join_tab->keyuse) 03247 use->table->reginfo.join_tab->keyuse=save_pos; 03248 use->table->reginfo.join_tab->checked_keys.set_bit(use->key); 03249 save_pos++; 03250 } 03251 i=(uint) (save_pos-(KEYUSE*) keyuse->buffer); 03252 VOID(set_dynamic(keyuse,(gptr) &end,i)); 03253 keyuse->elements=i; 03254 } 03255 return FALSE; 03256 } 03257 03258 /* 03259 Update some values in keyuse for faster choose_plan() loop 03260 */ 03261 03262 static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array) 03263 { 03264 KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*); 03265 03266 for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++) 03267 { 03268 table_map map; 03269 /* 03270 If we find a ref, assume this table matches a proportional 03271 part of this table. 03272 For example 100 records matching a table with 5000 records 03273 gives 5000/100 = 50 records per key 03274 Constant tables are ignored. 03275 To avoid bad matches, we don't make ref_table_rows less than 100. 03276 */ 03277 keyuse->ref_table_rows= ~(ha_rows) 0; // If no ref 03278 if (keyuse->used_tables & 03279 (map= (keyuse->used_tables & ~join->const_table_map & 03280 ~OUTER_REF_TABLE_BIT))) 03281 { 03282 uint tablenr; 03283 for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ; 03284 if (map == 1) // Only one table 03285 { 03286 TABLE *tmp_table=join->all_tables[tablenr]; 03287 keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100); 03288 } 03289 } 03290 /* 03291 Outer reference (external field) is constant for single executing 03292 of subquery 03293 */ 03294 if (keyuse->used_tables == OUTER_REF_TABLE_BIT) 03295 keyuse->ref_table_rows= 1; 03296 } 03297 } 03298 03299 03300 /* 03301 Discover the indexes that can be used for GROUP BY or DISTINCT queries. 03302 03303 SYNOPSIS 03304 add_group_and_distinct_keys() 03305 join 03306 join_tab 03307 03308 DESCRIPTION 03309 If the query has a GROUP BY clause, find all indexes that contain all 03310 GROUP BY fields, and add those indexes to join->const_keys. 03311 If the query has a DISTINCT clause, find all indexes that contain all 03312 SELECT fields, and add those indexes to join->const_keys. 03313 This allows later on such queries to be processed by a 03314 QUICK_GROUP_MIN_MAX_SELECT. 03315 03316 RETURN 03317 None 03318 */ 03319 03320 static void 03321 add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab) 03322 { 03323 List<Item_field> indexed_fields; 03324 List_iterator<Item_field> indexed_fields_it(indexed_fields); 03325 ORDER *cur_group; 03326 Item_field *cur_item; 03327 key_map possible_keys(0); 03328 03329 if (join->group_list) 03330 { /* Collect all query fields referenced in the GROUP clause. */ 03331 for (cur_group= join->group_list; cur_group; cur_group= cur_group->next) 03332 (*cur_group->item)->walk(&Item::collect_item_field_processor, 0, 03333 (byte*) &indexed_fields); 03334 } 03335 else if (join->select_distinct) 03336 { /* Collect all query fields referenced in the SELECT clause. */ 03337 List<Item> &select_items= join->fields_list; 03338 List_iterator<Item> select_items_it(select_items); 03339 Item *item; 03340 while ((item= select_items_it++)) 03341 item->walk(&Item::collect_item_field_processor, 0, 03342 (byte*) &indexed_fields); 03343 } 03344 else 03345 return; 03346 03347 if (indexed_fields.elements == 0) 03348 return; 03349 03350 /* Intersect the keys of all group fields. */ 03351 cur_item= indexed_fields_it++; 03352 possible_keys.merge(cur_item->field->part_of_key); 03353 while ((cur_item= indexed_fields_it++)) 03354 { 03355 possible_keys.intersect(cur_item->field->part_of_key); 03356 } 03357 03358 if (!possible_keys.is_clear_all()) 03359 join_tab->const_keys.merge(possible_keys); 03360 } 03361 03362 03363 /***************************************************************************** 03364 Go through all combinations of not marked tables and find the one 03365 which uses least records 03366 *****************************************************************************/ 03367 03368 /* Save const tables first as used tables */ 03369 03370 static void 03371 set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key) 03372 { 03373 join->positions[idx].table= table; 03374 join->positions[idx].key=key; 03375 join->positions[idx].records_read=1.0; /* This is a const table */ 03376 03377 /* Move the const table as down as possible in best_ref */ 03378 JOIN_TAB **pos=join->best_ref+idx+1; 03379 JOIN_TAB *next=join->best_ref[idx]; 03380 for (;next != table ; pos++) 03381 { 03382 JOIN_TAB *tmp=pos[0]; 03383 pos[0]=next; 03384 next=tmp; 03385 } 03386 join->best_ref[idx]=table; 03387 } 03388 03389 03390 /* 03391 Find the best access path for an extension of a partial execution plan and 03392 add this path to the plan. 03393 03394 SYNOPSIS 03395 best_access_path() 03396 join pointer to the structure providing all context info 03397 for the query 03398 s the table to be joined by the function 03399 thd thread for the connection that submitted the query 03400 remaining_tables set of tables not included into the partial plan yet 03401 idx the length of the partial plan 03402 record_count estimate for the number of records returned by the partial 03403 plan 03404 read_time the cost of the partial plan 03405 03406 DESCRIPTION 03407 The function finds the best access path to table 's' from the passed 03408 partial plan where an access path is the general term for any means to 03409 access the data in 's'. An access path may use either an index or a scan, 03410 whichever is cheaper. The input partial plan is passed via the array 03411 'join->positions' of length 'idx'. The chosen access method for 's' and its 03412 cost are stored in 'join->positions[idx]'. 03413 03414 RETURN 03415 None 03416 */ 03417 03418 static void 03419 best_access_path(JOIN *join, 03420 JOIN_TAB *s, 03421 THD *thd, 03422 table_map remaining_tables, 03423 uint idx, 03424 double record_count, 03425 double read_time) 03426 { 03427 KEYUSE *best_key= 0; 03428 uint best_max_key_part= 0; 03429 my_bool found_constraint= 0; 03430 double best= DBL_MAX; 03431 double best_time= DBL_MAX; 03432 double records= DBL_MAX; 03433 double tmp; 03434 ha_rows rec; 03435 03436 DBUG_ENTER("best_access_path"); 03437 03438 if (s->keyuse) 03439 { /* Use key if possible */ 03440 TABLE *table= s->table; 03441 KEYUSE *keyuse,*start_key=0; 03442 double best_records= DBL_MAX; 03443 uint max_key_part=0; 03444 03445 /* Test how we can use keys */ 03446 rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key 03447 for (keyuse=s->keyuse ; keyuse->table == table ;) 03448 { 03449 key_part_map found_part= 0; 03450 table_map found_ref= 0; 03451 uint key= keyuse->key; 03452 KEY *keyinfo= table->key_info+key; 03453 bool ft_key= (keyuse->keypart == FT_KEYPART); 03454 /* Bitmap of keyparts where the ref access is over 'keypart=const': */ 03455 key_part_map const_part= 0; 03456 /* The or-null keypart in ref-or-null access: */ 03457 key_part_map ref_or_null_part= 0; 03458 03459 /* Calculate how many key segments of the current key we can use */ 03460 start_key= keyuse; 03461 do 03462 { /* for each keypart */ 03463 uint keypart= keyuse->keypart; 03464 table_map best_part_found_ref= 0; 03465 double best_prev_record_reads= DBL_MAX; 03466 do 03467 { 03468 if (!(remaining_tables & keyuse->used_tables) && 03469 !(ref_or_null_part && (keyuse->optimize & 03470 KEY_OPTIMIZE_REF_OR_NULL))) 03471 { 03472 found_part|= keyuse->keypart_map; 03473 if (!(keyuse->used_tables & ~join->const_table_map)) 03474 const_part|= keyuse->keypart_map; 03475 double tmp= prev_record_reads(join, (found_ref | 03476 keyuse->used_tables)); 03477 if (tmp < best_prev_record_reads) 03478 { 03479 best_part_found_ref= keyuse->used_tables; 03480 best_prev_record_reads= tmp; 03481 } 03482 if (rec > keyuse->ref_table_rows) 03483 rec= keyuse->ref_table_rows; 03484 /* 03485 If there is one 'key_column IS NULL' expression, we can 03486 use this ref_or_null optimisation of this field 03487 */ 03488 if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) 03489 ref_or_null_part |= keyuse->keypart_map; 03490 } 03491 keyuse++; 03492 } while (keyuse->table == table && keyuse->key == key && 03493 keyuse->keypart == keypart); 03494 found_ref|= best_part_found_ref; 03495 } while (keyuse->table == table && keyuse->key == key); 03496 03497 /* 03498 Assume that that each key matches a proportional part of table. 03499 */ 03500 if (!found_part && !ft_key) 03501 continue; // Nothing usable found 03502 03503 if (rec < MATCHING_ROWS_IN_OTHER_TABLE) 03504 rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables 03505 03506 /* 03507 ft-keys require special treatment 03508 */ 03509 if (ft_key) 03510 { 03511 /* 03512 Really, there should be records=0.0 (yes!) 03513 but 1.0 would be probably safer 03514 */ 03515 tmp= prev_record_reads(join, found_ref); 03516 records= 1.0; 03517 } 03518 else 03519 { 03520 found_constraint= 1; 03521 /* 03522 Check if we found full key 03523 */ 03524 if (found_part == PREV_BITS(uint,keyinfo->key_parts) && 03525 !ref_or_null_part) 03526 { /* use eq key */ 03527 max_key_part= (uint) ~0; 03528 if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME) 03529 { 03530 tmp = prev_record_reads(join, found_ref); 03531 records=1.0; 03532 } 03533 else 03534 { 03535 if (!found_ref) 03536 { /* We found a const key */ 03537 /* 03538 ReuseRangeEstimateForRef-1: 03539 We get here if we've found a ref(const) (c_i are constants): 03540 "(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond] 03541 03542 If range optimizer was able to construct a "range" 03543 access on this index, then its condition "quick_cond" was 03544 eqivalent to ref_const_cond (*), and we can re-use E(#rows) 03545 from the range optimizer. 03546 03547 Proof of (*): By properties of range and ref optimizers 03548 quick_cond will be equal or tighther than ref_const_cond. 03549 ref_const_cond already covers "smallest" possible interval - 03550 a singlepoint interval over all keyparts. Therefore, 03551 quick_cond is equivalent to ref_const_cond (if it was an 03552 empty interval we wouldn't have got here). 03553 */ 03554 if (table->quick_keys.is_set(key)) 03555 records= (double) table->quick_rows[key]; 03556 else 03557 { 03558 /* quick_range couldn't use key! */ 03559 records= (double) s->records/rec; 03560 } 03561 } 03562 else 03563 { 03564 if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1])) 03565 { /* Prefer longer keys */ 03566 records= 03567 ((double) s->records / (double) rec * 03568 (1.0 + 03569 ((double) (table->s->max_key_length-keyinfo->key_length) / 03570 (double) table->s->max_key_length))); 03571 if (records < 2.0) 03572 records=2.0; /* Can't be as good as a unique */ 03573 } 03574 /* 03575 ReuseRangeEstimateForRef-2: We get here if we could not reuse 03576 E(#rows) from range optimizer. Make another try: 03577 03578 If range optimizer produced E(#rows) for a prefix of the ref 03579 access we're considering, and that E(#rows) is lower then our 03580 current estimate, make an adjustment. The criteria of when we 03581 can make an adjustment is a special case of the criteria used 03582 in ReuseRangeEstimateForRef-3. 03583 */ 03584 if (table->quick_keys.is_set(key) && 03585 const_part & (1 << table->quick_key_parts[key]) && 03586 table->quick_n_ranges[key] == 1 && 03587 records > (double) table->quick_rows[key]) 03588 { 03589 records= (double) table->quick_rows[key]; 03590 } 03591 } 03592 /* Limit the number of matched rows */ 03593 tmp= records; 03594 set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key); 03595 if (table->used_keys.is_set(key)) 03596 { 03597 /* we can use only index tree */ 03598 uint keys_per_block= table->file->stats.block_size/2/ 03599 (keyinfo->key_length+table->file->ref_length)+1; 03600 tmp= record_count*(tmp+keys_per_block-1)/keys_per_block; 03601 } 03602 else 03603 tmp= record_count*min(tmp,s->worst_seeks); 03604 } 03605 } 03606 else 03607 { 03608 /* 03609 Use as much key-parts as possible and a uniq key is better 03610 than a not unique key 03611 Set tmp to (previous record count) * (records / combination) 03612 */ 03613 if ((found_part & 1) && 03614 (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) || 03615 found_part == PREV_BITS(uint,keyinfo->key_parts))) 03616 { 03617 max_key_part= max_part_bit(found_part); 03618 /* 03619 ReuseRangeEstimateForRef-3: 03620 We're now considering a ref[or_null] access via 03621 (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR 03622 (same-as-above but with one cond replaced 03623 with "t.keypart_i IS NULL")] (**) 03624 03625 Try re-using E(#rows) from "range" optimizer: 03626 We can do so if "range" optimizer used the same intervals as 03627 in (**). The intervals used by range optimizer may be not 03628 available at this point (as "range" access might have choosen to 03629 create quick select over another index), so we can't compare 03630 them to (**). We'll make indirect judgements instead. 03631 The sufficient conditions for re-use are: 03632 (C1) All e_i in (**) are constants, i.e. found_ref==FALSE. (if 03633 this is not satisfied we have no way to know which ranges 03634 will be actually scanned by 'ref' until we execute the 03635 join) 03636 (C2) max #key parts in 'range' access == K == max_key_part (this 03637 is apparently a necessary requirement) 03638 03639 We also have a property that "range optimizer produces equal or 03640 tighter set of scan intervals than ref(const) optimizer". Each 03641 of the intervals in (**) are "tightest possible" intervals when 03642 one limits itself to using keyparts 1..K (which we do in #2). 03643 From here it follows that range access used either one, or 03644 both of the (I1) and (I2) intervals: 03645 03646 (t.keypart1=c1 AND ... AND t.keypartK=eK) (I1) 03647 (same-as-above but with one cond replaced 03648 with "t.keypart_i IS NULL") (I2) 03649 03650 The remaining part is to exclude the situation where range 03651 optimizer used one interval while we're considering 03652 ref-or-null and looking for estimate for two intervals. This 03653 is done by last limitation: 03654 03655 (C3) "range optimizer used (have ref_or_null?2:1) intervals" 03656 */ 03657 if (table->quick_keys.is_set(key) && !found_ref && //(C1) 03658 table->quick_key_parts[key] == max_key_part && //(C2) 03659 table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3) 03660 { 03661 tmp= records= (double) table->quick_rows[key]; 03662 } 03663 else 03664 { 03665 /* Check if we have statistic about the distribution */ 03666 if ((records= keyinfo->rec_per_key[max_key_part-1])) 03667 tmp= records; 03668 else 03669 { 03670 /* 03671 Assume that the first key part matches 1% of the file 03672 and that the whole key matches 10 (duplicates) or 1 03673 (unique) records. 03674 Assume also that more key matches proportionally more 03675 records 03676 This gives the formula: 03677 records = (x * (b-a) + a*c-b)/(c-1) 03678 03679 b = records matched by whole key 03680 a = records matched by first key part (1% of all records?) 03681 c = number of key parts in key 03682 x = used key parts (1 <= x <= c) 03683 */ 03684 double rec_per_key; 03685 if (!(rec_per_key=(double) 03686 keyinfo->rec_per_key[keyinfo->key_parts-1])) 03687 rec_per_key=(double) s->records/rec+1; 03688 03689 if (!s->records) 03690 tmp = 0; 03691 else if (rec_per_key/(double) s->records >= 0.01) 03692 tmp = rec_per_key; 03693 else 03694 { 03695 double a=s->records*0.01; 03696 if (keyinfo->key_parts > 1) 03697 tmp= (max_key_part * (rec_per_key - a) + 03698 a*keyinfo->key_parts - rec_per_key)/ 03699 (keyinfo->key_parts-1); 03700 else 03701 tmp= a; 03702 set_if_bigger(tmp,1.0); 03703 } 03704 records = (ulong) tmp; 03705 } 03706 03707 if (ref_or_null_part) 03708 { 03709 /* We need to do two key searches to find key */ 03710 tmp *= 2.0; 03711 records *= 2.0; 03712 } 03713 03714 /* 03715 ReuseRangeEstimateForRef-4: We get here if we could not reuse 03716 E(#rows) from range optimizer. Make another try: 03717 03718 If range optimizer produced E(#rows) for a prefix of the ref 03719 access we're considering, and that E(#rows) is lower then our 03720 current estimate, make the adjustment. 03721 03722 The decision whether we can re-use the estimate from the range 03723 optimizer is the same as in ReuseRangeEstimateForRef-3, 03724 applied to first table->quick_key_parts[key] key parts. 03725 */ 03726 if (table->quick_keys.is_set(key) && 03727 table->quick_key_parts[key] <= max_key_part && 03728 const_part & (1 << table->quick_key_parts[key]) && 03729 table->quick_n_ranges[key] == 1 + test(ref_or_null_part & 03730 const_part) && 03731 records > (double) table->quick_rows[key]) 03732 { 03733 tmp= records= (double) table->quick_rows[key]; 03734 } 03735 } 03736 03737 /* Limit the number of matched rows */ 03738 set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key); 03739 if (table->used_keys.is_set(key)) 03740 { 03741 /* we can use only index tree */ 03742 uint keys_per_block= table->file->stats.block_size/2/ 03743 (keyinfo->key_length+table->file->ref_length)+1; 03744 tmp= record_count*(tmp+keys_per_block-1)/keys_per_block; 03745 } 03746 else 03747 tmp= record_count*min(tmp,s->worst_seeks); 03748 } 03749 else 03750 tmp= best_time; // Do nothing 03751 } 03752 } /* not ft_key */ 03753 if (tmp < best_time - records/(double) TIME_FOR_COMPARE) 03754 { 03755 best_time= tmp + records/(double) TIME_FOR_COMPARE; 03756 best= tmp; 03757 best_records= records; 03758 best_key= start_key; 03759 best_max_key_part= max_key_part; 03760 } 03761 } 03762 records= best_records; 03763 } 03764 03765 /* 03766 Don't test table scan if it can't be better. 03767 Prefer key lookup if we would use the same key for scanning. 03768 03769 Don't do a table scan on InnoDB tables, if we can read the used 03770 parts of the row from any of the used index. 03771 This is because table scans uses index and we would not win 03772 anything by using a table scan. 03773 03774 A word for word translation of the below if-statement in psergey's 03775 understanding: we check if we should use table scan if: 03776 (1) The found 'ref' access produces more records than a table scan 03777 (or index scan, or quick select), or 'ref' is more expensive than 03778 any of them. 03779 (2) This doesn't hold: the best way to perform table scan is to to perform 03780 'range' access using index IDX, and the best way to perform 'ref' 03781 access is to use the same index IDX, with the same or more key parts. 03782 (note: it is not clear how this rule is/should be extended to 03783 index_merge quick selects) 03784 (3) See above note about InnoDB. 03785 (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access 03786 path, but there is no quick select) 03787 If the condition in the above brackets holds, then the only possible 03788 "table scan" access method is ALL/index (there is no quick select). 03789 Since we have a 'ref' access path, and FORCE INDEX instructs us to 03790 choose it over ALL/index, there is no need to consider a full table 03791 scan. 03792 */ 03793 if ((records >= s->found_records || best > s->read_time) && // (1) 03794 !(s->quick && best_key && s->quick->index == best_key->key && // (2) 03795 best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2) 03796 !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3) 03797 ! s->table->used_keys.is_clear_all() && best_key) && // (3) 03798 !(s->table->force_index && best_key && !s->quick)) // (4) 03799 { // Check full join 03800 ha_rows rnd_records= s->found_records; 03801 /* 03802 If there is a filtering condition on the table (i.e. ref analyzer found 03803 at least one "table.keyXpartY= exprZ", where exprZ refers only to tables 03804 preceding this table in the join order we're now considering), then 03805 assume that 25% of the rows will be filtered out by this condition. 03806 03807 This heuristic is supposed to force tables used in exprZ to be before 03808 this table in join order. 03809 */ 03810 if (found_constraint) 03811 rnd_records-= rnd_records/4; 03812 03813 /* 03814 If applicable, get a more accurate estimate. Don't use the two 03815 heuristics at once. 03816 */ 03817 if (s->table->quick_condition_rows != s->found_records) 03818 rnd_records= s->table->quick_condition_rows; 03819 03820 /* 03821 Range optimizer never proposes a RANGE if it isn't better 03822 than FULL: so if RANGE is present, it's always preferred to FULL. 03823 Here we estimate its cost. 03824 */ 03825 if (s->quick) 03826 { 03827 /* 03828 For each record we: 03829 - read record range through 'quick' 03830 - skip rows which does not satisfy WHERE constraints 03831 TODO: 03832 We take into account possible use of join cache for ALL/index 03833 access (see first else-branch below), but we don't take it into 03834 account here for range/index_merge access. Find out why this is so. 03835 */ 03836 tmp= record_count * 03837 (s->quick->read_time + 03838 (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE); 03839 } 03840 else 03841 { 03842 /* Estimate cost of reading table. */ 03843 tmp= s->table->file->scan_time(); 03844 if (s->table->map & join->outer_join) // Can't use join cache 03845 { 03846 /* 03847 For each record we have to: 03848 - read the whole table record 03849 - skip rows which does not satisfy join condition 03850 */ 03851 tmp= record_count * 03852 (tmp + 03853 (s->records - rnd_records)/(double) TIME_FOR_COMPARE); 03854 } 03855 else 03856 { 03857 /* We read the table as many times as join buffer becomes full. */ 03858 tmp*= (1.0 + floor((double) cache_record_length(join,idx) * 03859 record_count / 03860 (double) thd->variables.join_buff_size)); 03861 /* 03862 We don't make full cartesian product between rows in the scanned 03863 table and existing records because we skip all rows from the 03864 scanned table, which does not satisfy join condition when 03865 we read the table (see flush_cached_records for details). Here we 03866 take into account cost to read and skip these records. 03867 */ 03868 tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE; 03869 } 03870 } 03871 03872 /* 03873 We estimate the cost of evaluating WHERE clause for found records 03874 as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus 03875 tmp give us total cost of using TABLE SCAN 03876 */ 03877 if (best == DBL_MAX || 03878 (tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records < 03879 best + record_count/(double) TIME_FOR_COMPARE*records)) 03880 { 03881 /* 03882 If the table has a range (s->quick is set) make_join_select() 03883 will ensure that this will be used 03884 */ 03885 best= tmp; 03886 records= rows2double(rnd_records); 03887 best_key= 0; 03888 } 03889 } 03890 03891 /* Update the cost information for the current partial plan */ 03892 join->positions[idx].records_read= records; 03893 join->positions[idx].read_time= best; 03894 join->positions[idx].key= best_key; 03895 join->positions[idx].table= s; 03896 03897 if (!best_key && 03898 idx == join->const_tables && 03899 s->table == join->sort_by_table && 03900 join->unit->select_limit_cnt >= records) 03901 join->sort_by_table= (TABLE*) 1; // Must use temporary table 03902 03903 DBUG_VOID_RETURN; 03904 } 03905 03906 03907 /* 03908 Selects and invokes a search strategy for an optimal query plan. 03909 03910 SYNOPSIS 03911 choose_plan() 03912 join pointer to the structure providing all context info for 03913 the query 03914 join_tables set of the tables in the query 03915 03916 DESCRIPTION 03917 The function checks user-configurable parameters that control the search 03918 strategy for an optimal plan, selects the search method and then invokes 03919 it. Each specific optimization procedure stores the final optimal plan in 03920 the array 'join->best_positions', and the cost of the plan in 03921 'join->best_read'. 03922 03923 RETURN 03924 None 03925 */ 03926 03927 static void 03928 choose_plan(JOIN *join, table_map join_tables) 03929 { 03930 uint search_depth= join->thd->variables.optimizer_search_depth; 03931 uint prune_level= join->thd->variables.optimizer_prune_level; 03932 bool straight_join= join->select_options & SELECT_STRAIGHT_JOIN; 03933 DBUG_ENTER("choose_plan"); 03934 03935 join->cur_embedding_map= 0; 03936 reset_nj_counters(join->join_list); 03937 /* 03938 if (SELECT_STRAIGHT_JOIN option is set) 03939 reorder tables so dependent tables come after tables they depend 03940 on, otherwise keep tables in the order they were specified in the query 03941 else 03942 Apply heuristic: pre-sort all access plans with respect to the number of 03943 records accessed. 03944 */ 03945 qsort(join->best_ref + join->const_tables, join->tables - join->const_tables, 03946 sizeof(JOIN_TAB*), straight_join?join_tab_cmp_straight:join_tab_cmp); 03947 03948 if (straight_join) 03949 { 03950 optimize_straight_join(join, join_tables); 03951 } 03952 else 03953 { 03954 if (search_depth == MAX_TABLES+2) 03955 { /* 03956 TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before 03957 the greedy version. Will be removed when greedy_search is approved. 03958 */ 03959 join->best_read= DBL_MAX; 03960 find_best(join, join_tables, join->const_tables, 1.0, 0.0); 03961 } 03962 else 03963 { 03964 if (search_depth == 0) 03965 /* Automatically determine a reasonable value for 'search_depth' */ 03966 search_depth= determine_search_depth(join); 03967 greedy_search(join, join_tables, search_depth, prune_level); 03968 } 03969 } 03970 03971 /* 03972 Store the cost of this query into a user variable 03973 */ 03974 join->thd->status_var.last_query_cost= join->best_read; 03975 DBUG_VOID_RETURN; 03976 } 03977 03978 03979 /* 03980 Compare two JOIN_TAB objects based on the number of accessed records. 03981 03982 SYNOPSIS 03983 join_tab_cmp() 03984 ptr1 pointer to first JOIN_TAB object 03985 ptr2 pointer to second JOIN_TAB object 03986 03987 RETURN 03988 1 if first is bigger 03989 -1 if second is bigger 03990 0 if equal 03991 */ 03992 03993 static int 03994 join_tab_cmp(const void* ptr1, const void* ptr2) 03995 { 03996 JOIN_TAB *jt1= *(JOIN_TAB**) ptr1; 03997 JOIN_TAB *jt2= *(JOIN_TAB**) ptr2; 03998 03999 if (jt1->dependent & jt2->table->map) 04000 return 1; 04001 if (jt2->dependent & jt1->table->map) 04002 return -1; 04003 if (jt1->found_records > jt2->found_records) 04004 return 1; 04005 if (jt1->found_records < jt2->found_records) 04006 return -1; 04007 return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0); 04008 } 04009 04010 04011 /* 04012 Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN. 04013 */ 04014 04015 static int 04016 join_tab_cmp_straight(const void* ptr1, const void* ptr2) 04017 { 04018 JOIN_TAB *jt1= *(JOIN_TAB**) ptr1; 04019 JOIN_TAB *jt2= *(JOIN_TAB**) ptr2; 04020 04021 if (jt1->dependent & jt2->table->map) 04022 return 1; 04023 if (jt2->dependent & jt1->table->map) 04024 return -1; 04025 return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0); 04026 } 04027 04028 /* 04029 Heuristic procedure to automatically guess a reasonable degree of 04030 exhaustiveness for the greedy search procedure. 04031 04032 SYNOPSIS 04033 determine_search_depth() 04034 join pointer to the structure providing all context info for the query 04035 04036 DESCRIPTION 04037 The procedure estimates the optimization time and selects a search depth 04038 big enough to result in a near-optimal QEP, that doesn't take too long to 04039 find. If the number of tables in the query exceeds some constant, then 04040 search_depth is set to this constant. 04041 04042 NOTES 04043 This is an extremely simplistic implementation that serves as a stub for a 04044 more advanced analysis of the join. Ideally the search depth should be 04045 determined by learning from previous query optimizations, because it will 04046 depend on the CPU power (and other factors). 04047 04048 RETURN 04049 A positive integer that specifies the search depth (and thus the 04050 exhaustiveness) of the depth-first search algorithm used by 04051 'greedy_search'. 04052 */ 04053 04054 static uint 04055 determine_search_depth(JOIN *join) 04056 { 04057 uint table_count= join->tables - join->const_tables; 04058 uint search_depth; 04059 /* TODO: this value should be determined dynamically, based on statistics: */ 04060 uint max_tables_for_exhaustive_opt= 7; 04061 04062 if (table_count <= max_tables_for_exhaustive_opt) 04063 search_depth= table_count+1; // use exhaustive for small number of tables 04064 else 04065 /* 04066 TODO: this value could be determined by some mapping of the form: 04067 depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE] 04068 */ 04069 search_depth= max_tables_for_exhaustive_opt; // use greedy search 04070 04071 return search_depth; 04072 } 04073 04074 04075 /* 04076 Select the best ways to access the tables in a query without reordering them. 04077 04078 SYNOPSIS 04079 optimize_straight_join() 04080 join pointer to the structure providing all context info for 04081 the query 04082 join_tables set of the tables in the query 04083 04084 DESCRIPTION 04085 Find the best access paths for each query table and compute their costs 04086 according to their order in the array 'join->best_ref' (thus without 04087 reordering the join tables). The function calls sequentially 04088 'best_access_path' for each table in the query to select the best table 04089 access method. The final optimal plan is stored in the array 04090 'join->best_positions', and the corresponding cost in 'join->best_read'. 04091 04092 NOTES 04093 This function can be applied to: 04094 - queries with STRAIGHT_JOIN 04095 - internally to compute the cost of an arbitrary QEP 04096 Thus 'optimize_straight_join' can be used at any stage of the query 04097 optimization process to finalize a QEP as it is. 04098 04099 RETURN 04100 None 04101 */ 04102 04103 static void 04104 optimize_straight_join(JOIN *join, table_map join_tables) 04105 { 04106 JOIN_TAB *s; 04107 uint idx= join->const_tables; 04108 double record_count= 1.0; 04109 double read_time= 0.0; 04110 04111 for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++) 04112 { 04113 /* Find the best access method from 's' to the current partial plan */ 04114 best_access_path(join, s, join->thd, join_tables, idx, 04115 record_count, read_time); 04116 /* compute the cost of the new plan extended with 's' */ 04117 record_count*= join->positions[idx].records_read; 04118 read_time+= join->positions[idx].read_time; 04119 join_tables&= ~(s->table->map); 04120 ++idx; 04121 } 04122 04123 read_time+= record_count / (double) TIME_FOR_COMPARE; 04124 if (join->sort_by_table && 04125 join->sort_by_table != join->positions[join->const_tables].table->table) 04126 read_time+= record_count; // We have to make a temp table 04127 memcpy((gptr) join->best_positions, (gptr) join->positions, 04128 sizeof(POSITION)*idx); 04129 join->best_read= read_time; 04130 } 04131 04132 04133 /* 04134 Find a good, possibly optimal, query execution plan (QEP) by a greedy search. 04135 04136 SYNOPSIS 04137 join pointer to the structure providing all context info 04138 for the query 04139 remaining_tables set of tables not included into the partial plan yet 04140 search_depth controlls the exhaustiveness of the search 04141 prune_level the pruning heuristics that should be applied during 04142 search 04143 04144 DESCRIPTION 04145 The search procedure uses a hybrid greedy/exhaustive search with controlled 04146 exhaustiveness. The search is performed in N = card(remaining_tables) 04147 steps. Each step evaluates how promising is each of the unoptimized tables, 04148 selects the most promising table, and extends the current partial QEP with 04149 that table. Currenly the most 'promising' table is the one with least 04150 expensive extension. 04151 There are two extreme cases: 04152 1. When (card(remaining_tables) < search_depth), the estimate finds the best 04153 complete continuation of the partial QEP. This continuation can be 04154 used directly as a result of the search. 04155 2. When (search_depth == 1) the 'best_extension_by_limited_search' 04156 consideres the extension of the current QEP with each of the remaining 04157 unoptimized tables. 04158 All other cases are in-between these two extremes. Thus the parameter 04159 'search_depth' controlls the exhaustiveness of the search. The higher the 04160 value, the longer the optimizaton time and possibly the better the 04161 resulting plan. The lower the value, the fewer alternative plans are 04162 estimated, but the more likely to get a bad QEP. 04163 04164 All intermediate and final results of the procedure are stored in 'join': 04165 join->positions modified for every partial QEP that is explored 04166 join->best_positions modified for the current best complete QEP 04167 join->best_read modified for the current best complete QEP 04168 join->best_ref might be partially reordered 04169 The final optimal plan is stored in 'join->best_positions', and its 04170 corresponding cost in 'join->best_read'. 04171 04172 NOTES 04173 The following pseudocode describes the algorithm of 'greedy_search': 04174 04175 procedure greedy_search 04176 input: remaining_tables 04177 output: pplan; 04178 { 04179 pplan = <>; 04180 do { 04181 (t, a) = best_extension(pplan, remaining_tables); 04182 pplan = concat(pplan, (t, a)); 04183 remaining_tables = remaining_tables - t; 04184 } while (remaining_tables != {}) 04185 return pplan; 04186 } 04187 04188 where 'best_extension' is a placeholder for a procedure that selects the 04189 most "promising" of all tables in 'remaining_tables'. 04190 Currently this estimate is performed by calling 04191 'best_extension_by_limited_search' to evaluate all extensions of the 04192 current QEP of size 'search_depth', thus the complexity of 'greedy_search' 04193 mainly depends on that of 'best_extension_by_limited_search'. 04194 04195 If 'best_extension()' == 'best_extension_by_limited_search()', then the 04196 worst-case complexity of this algorithm is <= 04197 O(N*N^search_depth/search_depth). When serch_depth >= N, then the 04198 complexity of greedy_search is O(N!). 04199 04200 In the future, 'greedy_search' might be extended to support other 04201 implementations of 'best_extension', e.g. some simpler quadratic procedure. 04202 04203 RETURN 04204 None 04205 */ 04206 04207 static void 04208 greedy_search(JOIN *join, 04209 table_map remaining_tables, 04210 uint search_depth, 04211 uint prune_level) 04212 { 04213 double record_count= 1.0; 04214 double read_time= 0.0; 04215 uint idx= join->const_tables; // index into 'join->best_ref' 04216 uint best_idx; 04217 uint size_remain; // cardinality of remaining_tables 04218 POSITION best_pos; 04219 JOIN_TAB *best_table; // the next plan node to be added to the curr QEP 04220 04221 DBUG_ENTER("greedy_search"); 04222 04223 /* number of tables that remain to be optimized */ 04224 size_remain= my_count_bits(remaining_tables); 04225 04226 do { 04227 /* Find the extension of the current QEP with the lowest cost */ 04228 join->best_read= DBL_MAX; 04229 best_extension_by_limited_search(join, remaining_tables, idx, record_count, 04230 read_time, search_depth, prune_level); 04231 04232 if (size_remain <= search_depth) 04233 { 04234 /* 04235 'join->best_positions' contains a complete optimal extension of the 04236 current partial QEP. 04237 */ 04238 DBUG_EXECUTE("opt", print_plan(join, join->tables, 04239 record_count, read_time, read_time, 04240 "optimal");); 04241 DBUG_VOID_RETURN; 04242 } 04243 04244 /* select the first table in the optimal extension as most promising */ 04245 best_pos= join->best_positions[idx]; 04246 best_table= best_pos.table; 04247 /* 04248 Each subsequent loop of 'best_extension_by_limited_search' uses 04249 'join->positions' for cost estimates, therefore we have to update its 04250 value. 04251 */ 04252 join->positions[idx]= best_pos; 04253 04254 /* find the position of 'best_table' in 'join->best_ref' */ 04255 best_idx= idx; 04256 JOIN_TAB *pos= join->best_ref[best_idx]; 04257 while (pos && best_table != pos) 04258 pos= join->best_ref[++best_idx]; 04259 DBUG_ASSERT((pos != NULL)); // should always find 'best_table' 04260 /* move 'best_table' at the first free position in the array of joins */ 04261 swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]); 04262 04263 /* compute the cost of the new plan extended with 'best_table' */ 04264 record_count*= join->positions[idx].records_read; 04265 read_time+= join->positions[idx].read_time; 04266 04267 remaining_tables&= ~(best_table->table->map); 04268 --size_remain; 04269 ++idx; 04270 04271 DBUG_EXECUTE("opt", print_plan(join, join->tables, 04272 record_count, read_time, read_time, 04273 "extended");); 04274 } while (TRUE); 04275 } 04276 04277 04278 /* 04279 Find a good, possibly optimal, query execution plan (QEP) by a possibly 04280 exhaustive search. 04281 04282 SYNOPSIS 04283 best_extension_by_limited_search() 04284 join pointer to the structure providing all context info for 04285 the query 04286 remaining_tables set of tables not included into the partial plan yet 04287 idx length of the partial QEP in 'join->positions'; 04288 since a depth-first search is used, also corresponds to 04289 the current depth of the search tree; 04290 also an index in the array 'join->best_ref'; 04291 record_count estimate for the number of records returned by the best 04292 partial plan 04293 read_time the cost of the best partial plan 04294 search_depth maximum depth of the recursion and thus size of the found 04295 optimal plan (0 < search_depth <= join->tables+1). 04296 prune_level pruning heuristics that should be applied during 04297 optimization 04298 (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS) 04299 04300 DESCRIPTION 04301 The procedure searches for the optimal ordering of the query tables in set 04302 'remaining_tables' of size N, and the corresponding optimal access paths to 04303 each table. The choice of a table order and an access path for each table 04304 constitutes a query execution plan (QEP) that fully specifies how to 04305 execute the query. 04306 04307 The maximal size of the found plan is controlled by the parameter 04308 'search_depth'. When search_depth == N, the resulting plan is complete and 04309 can be used directly as a QEP. If search_depth < N, the found plan consists 04310 of only some of the query tables. Such "partial" optimal plans are useful 04311 only as input to query optimization procedures, and cannot be used directly 04312 to execute a query. 04313 04314 The algorithm begins with an empty partial plan stored in 'join->positions' 04315 and a set of N tables - 'remaining_tables'. Each step of the algorithm 04316 evaluates the cost of the partial plan extended by all access plans for 04317 each of the relations in 'remaining_tables', expands the current partial 04318 plan with the access plan that results in lowest cost of the expanded 04319 partial plan, and removes the corresponding relation from 04320 'remaining_tables'. The algorithm continues until it either constructs a 04321 complete optimal plan, or constructs an optimal plartial plan with size = 04322 search_depth. 04323 04324 The final optimal plan is stored in 'join->best_positions'. The 04325 corresponding cost of the optimal plan is in 'join->best_read'. 04326 04327 NOTES 04328 The procedure uses a recursive depth-first search where the depth of the 04329 recursion (and thus the exhaustiveness of the search) is controlled by the 04330 parameter 'search_depth'. 04331 04332 The pseudocode below describes the algorithm of 04333 'best_extension_by_limited_search'. The worst-case complexity of this 04334 algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then 04335 the complexity of greedy_search is O(N!). 04336 04337 procedure best_extension_by_limited_search( 04338 pplan in, // in, partial plan of tables-joined-so-far 04339 pplan_cost, // in, cost of pplan 04340 remaining_tables, // in, set of tables not referenced in pplan 04341 best_plan_so_far, // in/out, best plan found so far 04342 best_plan_so_far_cost,// in/out, cost of best_plan_so_far 04343 search_depth) // in, maximum size of the plans being considered 04344 { 04345 for each table T from remaining_tables 04346 { 04347 // Calculate the cost of using table T as above 04348 cost = complex-series-of-calculations; 04349 04350 // Add the cost to the cost so far. 04351 pplan_cost+= cost; 04352 04353 if (pplan_cost >= best_plan_so_far_cost) 04354 // pplan_cost already too great, stop search 04355 continue; 04356 04357 pplan= expand pplan by best_access_method; 04358 remaining_tables= remaining_tables - table T; 04359 if (remaining_tables is not an empty set 04360 and 04361 search_depth > 1) 04362 { 04363 best_extension_by_limited_search(pplan, pplan_cost, 04364 remaining_tables, 04365 best_plan_so_far, 04366 best_plan_so_far_cost, 04367 search_depth - 1); 04368 } 04369 else 04370 { 04371 best_plan_so_far_cost= pplan_cost; 04372 best_plan_so_far= pplan; 04373 } 04374 } 04375 } 04376 04377 IMPLEMENTATION 04378 When 'best_extension_by_limited_search' is called for the first time, 04379 'join->best_read' must be set to the largest possible value (e.g. DBL_MAX). 04380 The actual implementation provides a way to optionally use pruning 04381 heuristic (controlled by the parameter 'prune_level') to reduce the search 04382 space by skipping some partial plans. 04383 The parameter 'search_depth' provides control over the recursion 04384 depth, and thus the size of the resulting optimal plan. 04385 04386 RETURN 04387 None 04388 */ 04389 04390 static void 04391 best_extension_by_limited_search(JOIN *join, 04392 table_map remaining_tables, 04393 uint idx, 04394 double record_count, 04395 double read_time, 04396 uint search_depth, 04397 uint prune_level) 04398 { 04399 THD *thd= join->thd; 04400 if (thd->killed) // Abort 04401 return; 04402 04403 DBUG_ENTER("best_extension_by_limited_search"); 04404 DBUG_EXECUTE("opt", print_plan(join, idx, read_time, record_count, idx, 04405 "SOFAR:");); 04406 04407 /* 04408 'join' is a partial plan with lower cost than the best plan so far, 04409 so continue expanding it further with the tables in 'remaining_tables'. 04410 */ 04411 JOIN_TAB *s; 04412 double best_record_count= DBL_MAX; 04413 double best_read_time= DBL_MAX; 04414 04415 DBUG_EXECUTE("opt", print_plan(join, idx, record_count, read_time, read_time, 04416 "part_plan");); 04417 04418 for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++) 04419 { 04420 table_map real_table_bit= s->table->map; 04421 if ((remaining_tables & real_table_bit) && 04422 !(remaining_tables & s->dependent) && 04423 (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s))) 04424 { 04425 double current_record_count, current_read_time; 04426 04427 /* Find the best access method from 's' to the current partial plan */ 04428 best_access_path(join, s, thd, remaining_tables, idx, 04429 record_count, read_time); 04430 /* Compute the cost of extending the plan with 's' */ 04431 current_record_count= record_count * join->positions[idx].records_read; 04432 current_read_time= read_time + join->positions[idx].read_time; 04433 04434 /* Expand only partial plans with lower cost than the best QEP so far */ 04435 if ((current_read_time + 04436 current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read) 04437 { 04438 DBUG_EXECUTE("opt", print_plan(join, idx+1, 04439 current_record_count, 04440 read_time, 04441 (current_read_time + 04442 current_record_count / 04443 (double) TIME_FOR_COMPARE), 04444 "prune_by_cost");); 04445 restore_prev_nj_state(s); 04446 continue; 04447 } 04448 04449 /* 04450 Prune some less promising partial plans. This heuristic may miss 04451 the optimal QEPs, thus it results in a non-exhaustive search. 04452 */ 04453 if (prune_level == 1) 04454 { 04455 if (best_record_count > current_record_count || 04456 best_read_time > current_read_time || 04457 idx == join->const_tables && // 's' is the first table in the QEP 04458 s->table == join->sort_by_table) 04459 { 04460 if (best_record_count >= current_record_count && 04461 best_read_time >= current_read_time && 04462 /* TODO: What is the reasoning behind this condition? */ 04463 (!(s->key_dependent & remaining_tables) || 04464 join->positions[idx].records_read < 2.0)) 04465 { 04466 best_record_count= current_record_count; 04467 best_read_time= current_read_time; 04468 } 04469 } 04470 else 04471 { 04472 DBUG_EXECUTE("opt", print_plan(join, idx+1, 04473 current_record_count, 04474 read_time, 04475 current_read_time, 04476 "pruned_by_heuristic");); 04477 restore_prev_nj_state(s); 04478 continue; 04479 } 04480 } 04481 04482 if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) ) 04483 { /* Recursively expand the current partial plan */ 04484 swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); 04485 best_extension_by_limited_search(join, 04486 remaining_tables & ~real_table_bit, 04487 idx + 1, 04488 current_record_count, 04489 current_read_time, 04490 search_depth - 1, 04491 prune_level); 04492 if (thd->killed) 04493 DBUG_VOID_RETURN; 04494 swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); 04495 } 04496 else 04497 { /* 04498 'join' is either the best partial QEP with 'search_depth' relations, 04499 or the best complete QEP so far, whichever is smaller. 04500 */ 04501 current_read_time+= current_record_count / (double) TIME_FOR_COMPARE; 04502 if (join->sort_by_table && 04503 join->sort_by_table != 04504 join->positions[join->const_tables].table->table) 04505 /* We have to make a temp table */ 04506 current_read_time+= current_record_count; 04507 if ((search_depth == 1) || (current_read_time < join->best_read)) 04508 { 04509 memcpy((gptr) join->best_positions, (gptr) join->positions, 04510 sizeof(POSITION) * (idx + 1)); 04511 join->best_read= current_read_time - 0.001; 04512 } 04513 DBUG_EXECUTE("opt", print_plan(join, idx+1, 04514 current_record_count, 04515 read_time, 04516 current_read_time, 04517 "full_plan");); 04518 } 04519 restore_prev_nj_state(s); 04520 } 04521 } 04522 DBUG_VOID_RETURN; 04523 } 04524 04525 04526 /* 04527 TODO: this function is here only temporarily until 'greedy_search' is 04528 tested and accepted. 04529 */ 04530 static void 04531 find_best(JOIN *join,table_map rest_tables,uint idx,double record_count, 04532 double read_time) 04533 { 04534 ha_rows rec; 04535 double tmp; 04536 THD *thd= join->thd; 04537 if (!rest_tables) 04538 { 04539 DBUG_PRINT("best",("read_time: %g record_count: %g",read_time, 04540 record_count)); 04541 04542 read_time+=record_count/(double) TIME_FOR_COMPARE; 04543 if (join->sort_by_table && 04544 join->sort_by_table != 04545 join->positions[join->const_tables].table->table) 04546 read_time+=record_count; // We have to make a temp table 04547 if (read_time < join->best_read) 04548 { 04549 memcpy((gptr) join->best_positions,(gptr) join->positions, 04550 sizeof(POSITION)*idx); 04551 join->best_read= read_time - 0.001; 04552 } 04553 return; 04554 } 04555 if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read) 04556 return; /* Found better before */ 04557 04558 JOIN_TAB *s; 04559 double best_record_count=DBL_MAX,best_read_time=DBL_MAX; 04560 for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++) 04561 { 04562 table_map real_table_bit=s->table->map; 04563 if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) && 04564 (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s))) 04565 { 04566 double records, best; 04567 best_access_path(join, s, thd, rest_tables, idx, record_count, 04568 read_time); 04569 records= join->positions[idx].records_read; 04570 best= join->positions[idx].read_time; 04571 /* 04572 Go to the next level only if there hasn't been a better key on 04573 this level! This will cut down the search for a lot simple cases! 04574 */ 04575 double current_record_count=record_count*records; 04576 double current_read_time=read_time+best; 04577 if (best_record_count > current_record_count || 04578 best_read_time > current_read_time || 04579 idx == join->const_tables && s->table == join->sort_by_table) 04580 { 04581 if (best_record_count >= current_record_count && 04582 best_read_time >= current_read_time && 04583 (!(s->key_dependent & rest_tables) || records < 2.0)) 04584 { 04585 best_record_count=current_record_count; 04586 best_read_time=current_read_time; 04587 } 04588 swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); 04589 find_best(join,rest_tables & ~real_table_bit,idx+1, 04590 current_record_count,current_read_time); 04591 if (thd->killed) 04592 return; 04593 swap_variables(JOIN_TAB*, join->best_ref[idx], *pos); 04594 } 04595 restore_prev_nj_state(s); 04596 if (join->select_options & SELECT_STRAIGHT_JOIN) 04597 break; // Don't test all combinations 04598 } 04599 } 04600 } 04601 04602 04603 /* 04604 Find how much space the prevous read not const tables takes in cache 04605 */ 04606 04607 static void calc_used_field_length(THD *thd, JOIN_TAB *join_tab) 04608 { 04609 uint null_fields,blobs,fields,rec_length; 04610 Field **f_ptr,*field; 04611 MY_BITMAP *read_set= join_tab->table->read_set;; 04612 04613 null_fields= blobs= fields= rec_length=0; 04614 for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++) 04615 { 04616 if (bitmap_is_set(read_set, field->field_index)) 04617 { 04618 uint flags=field->flags; 04619 fields++; 04620 rec_length+=field->pack_length(); 04621 if (flags & BLOB_FLAG) 04622 blobs++; 04623 if (!(flags & NOT_NULL_FLAG)) 04624 null_fields++; 04625 } 04626 } 04627 if (null_fields) 04628 rec_length+=(join_tab->table->s->null_fields+7)/8; 04629 if (join_tab->table->maybe_null) 04630 rec_length+=sizeof(my_bool); 04631 if (blobs) 04632 { 04633 uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length- 04634 (join_tab->table->s->reclength- rec_length)); 04635 rec_length+=(uint) max(4,blob_length); 04636 } 04637 join_tab->used_fields=fields; 04638 join_tab->used_fieldlength=rec_length; 04639 join_tab->used_blobs=blobs; 04640 } 04641 04642 04643 static uint 04644 cache_record_length(JOIN *join,uint idx) 04645 { 04646 uint length=0; 04647 JOIN_TAB **pos,**end; 04648 THD *thd=join->thd; 04649 04650 for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ; 04651 pos != end ; 04652 pos++) 04653 { 04654 JOIN_TAB *join_tab= *pos; 04655 if (!join_tab->used_fieldlength) /* Not calced yet */ 04656 calc_used_field_length(thd, join_tab); 04657 length+=join_tab->used_fieldlength; 04658 } 04659 return length; 04660 } 04661 04662 04663 static double 04664 prev_record_reads(JOIN *join,table_map found_ref) 04665 { 04666 double found=1.0; 04667 found_ref&= ~OUTER_REF_TABLE_BIT; 04668 for (POSITION *pos=join->positions ; found_ref ; pos++) 04669 { 04670 if (pos->table->table->map & found_ref) 04671 { 04672 found_ref&= ~pos->table->table->map; 04673 found*=pos->records_read; 04674 } 04675 } 04676 return found; 04677 } 04678 04679 04680 /***************************************************************************** 04681 Set up join struct according to best position. 04682 *****************************************************************************/ 04683 04684 static bool 04685 get_best_combination(JOIN *join) 04686 { 04687 uint i,tablenr; 04688 table_map used_tables; 04689 JOIN_TAB *join_tab,*j; 04690 KEYUSE *keyuse; 04691 uint table_count; 04692 THD *thd=join->thd; 04693 DBUG_ENTER("get_best_combination"); 04694 04695 table_count=join->tables; 04696 if (!(join->join_tab=join_tab= 04697 (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count))) 04698 DBUG_RETURN(TRUE); 04699 04700 join->full_join=0; 04701 04702 used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read 04703 for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++) 04704 { 04705 TABLE *form; 04706 *j= *join->best_positions[tablenr].table; 04707 form=join->table[tablenr]=j->table; 04708 used_tables|= form->map; 04709 form->reginfo.join_tab=j; 04710 if (!*j->on_expr_ref) 04711 form->reginfo.not_exists_optimize=0; // Only with LEFT JOIN 04712 DBUG_PRINT("info",("type: %d", j->type)); 04713 if (j->type == JT_CONST) 04714 continue; // Handled in make_join_stat.. 04715 04716 j->ref.key = -1; 04717 j->ref.key_parts=0; 04718 04719 if (j->type == JT_SYSTEM) 04720 continue; 04721 if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key)) 04722 { 04723 j->type=JT_ALL; 04724 if (tablenr != join->const_tables) 04725 join->full_join=1; 04726 } 04727 else if (create_ref_for_key(join, j, keyuse, used_tables)) 04728 DBUG_RETURN(TRUE); // Something went wrong 04729 } 04730 04731 for (i=0 ; i < table_count ; i++) 04732 join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i; 04733 update_depend_map(join); 04734 DBUG_RETURN(0); 04735 } 04736 04737 04738 static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse, 04739 table_map used_tables) 04740 { 04741 KEYUSE *keyuse=org_keyuse; 04742 bool ftkey=(keyuse->keypart == FT_KEYPART); 04743 THD *thd= join->thd; 04744 uint keyparts,length,key; 04745 TABLE *table; 04746 KEY *keyinfo; 04747 DBUG_ENTER("create_ref_for_key"); 04748 04749 /* Use best key from find_best */ 04750 table=j->table; 04751 key=keyuse->key; 04752 keyinfo=table->key_info+key; 04753 04754 if (ftkey) 04755 { 04756 Item_func_match *ifm=(Item_func_match *)keyuse->val; 04757 04758 length=0; 04759 keyparts=1; 04760 ifm->join_key=1; 04761 } 04762 else 04763 { 04764 keyparts=length=0; 04765 uint found_part_ref_or_null= 0; 04766 /* 04767 Calculate length for the used key 04768 Stop if there is a missing key part or when we find second key_part 04769 with KEY_OPTIMIZE_REF_OR_NULL 04770 */ 04771 do 04772 { 04773 if (!(~used_tables & keyuse->used_tables)) 04774 { 04775 if (keyparts == keyuse->keypart && 04776 !(found_part_ref_or_null & keyuse->optimize)) 04777 { 04778 keyparts++; 04779 length+= keyinfo->key_part[keyuse->keypart].store_length; 04780 found_part_ref_or_null|= keyuse->optimize; 04781 } 04782 } 04783 keyuse++; 04784 } while (keyuse->table == table && keyuse->key == key); 04785 } /* not ftkey */ 04786 04787 /* set up fieldref */ 04788 keyinfo=table->key_info+key; 04789 j->ref.key_parts=keyparts; 04790 j->ref.key_length=length; 04791 j->ref.key=(int) key; 04792 if (!(j->ref.key_buff= (byte*) thd->calloc(ALIGN_SIZE(length)*2)) || 04793 !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) * 04794 (keyparts+1)))) || 04795 !(j->ref.items= (Item**) thd->alloc(sizeof(Item*)*keyparts))) 04796 { 04797 DBUG_RETURN(TRUE); 04798 } 04799 j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length); 04800 j->ref.key_err=1; 04801 j->ref.null_rejecting= 0; 04802 keyuse=org_keyuse; 04803 04804 store_key **ref_key= j->ref.key_copy; 04805 byte *key_buff=j->ref.key_buff, *null_ref_key= 0; 04806 bool keyuse_uses_no_tables= TRUE; 04807 if (ftkey) 04808 { 04809 j->ref.items[0]=((Item_func*)(keyuse->val))->key_item(); 04810 if (keyuse->used_tables) 04811 DBUG_RETURN(TRUE); // not supported yet. SerG 04812 04813 j->type=JT_FT; 04814 } 04815 else 04816 { 04817 uint i; 04818 for (i=0 ; i < keyparts ; keyuse++,i++) 04819 { 04820 while (keyuse->keypart != i || 04821 ((~used_tables) & keyuse->used_tables)) 04822 keyuse++; /* Skip other parts */ 04823 04824 uint maybe_null= test(keyinfo->key_part[i].null_bit); 04825 j->ref.items[i]=keyuse->val; // Save for cond removal 04826 if (keyuse->null_rejecting) 04827 j->ref.null_rejecting |= 1 << i; 04828 keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables; 04829 if (!keyuse->used_tables && 04830 !(join->select_options & SELECT_DESCRIBE)) 04831 { // Compare against constant 04832 store_key_item tmp(thd, keyinfo->key_part[i].field, 04833 (char*)key_buff + maybe_null, 04834 maybe_null ? (char*) key_buff : 0, 04835 keyinfo->key_part[i].length, keyuse->val); 04836 if (thd->is_fatal_error) 04837 DBUG_RETURN(TRUE); 04838 tmp.copy(); 04839 } 04840 else 04841 *ref_key++= get_store_key(thd, 04842 keyuse,join->const_table_map, 04843 &keyinfo->key_part[i], 04844 (char*) key_buff,maybe_null); 04845 /* 04846 Remember if we are going to use REF_OR_NULL 04847 But only if field _really_ can be null i.e. we force JT_REF 04848 instead of JT_REF_OR_NULL in case if field can't be null 04849 */ 04850 if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null) 04851 null_ref_key= key_buff; 04852 key_buff+=keyinfo->key_part[i].store_length; 04853 } 04854 } /* not ftkey */ 04855 *ref_key=0; // end_marker 04856 if (j->type == JT_FT) 04857 DBUG_RETURN(0); 04858 if (j->type == JT_CONST) 04859 j->table->const_table= 1; 04860 else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY | 04861 HA_END_SPACE_KEY)) != HA_NOSAME) || 04862 keyparts != keyinfo->key_parts || null_ref_key) 04863 { 04864 /* Must read with repeat */ 04865 j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF; 04866 j->ref.null_ref_key= null_ref_key; 04867 } 04868 else if (keyuse_uses_no_tables) 04869 { 04870 /* 04871 This happen if we are using a constant expression in the ON part 04872 of an LEFT JOIN. 04873 SELECT * FROM a LEFT JOIN b ON b.key=30 04874 Here we should not mark the table as a 'const' as a field may 04875 have a 'normal' value or a NULL value. 04876 */ 04877 j->type=JT_CONST; 04878 } 04879 else 04880 j->type=JT_EQ_REF; 04881 DBUG_RETURN(0); 04882 } 04883 04884 04885 04886 static store_key * 04887 get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables, 04888 KEY_PART_INFO *key_part, char *key_buff, uint maybe_null) 04889 { 04890 if (!((~used_tables) & keyuse->used_tables)) // if const item 04891 { 04892 return new store_key_const_item(thd, 04893 key_part->field, 04894 key_buff + maybe_null, 04895 maybe_null ? key_buff : 0, 04896 key_part->length, 04897 keyuse->val); 04898 } 04899 else if (keyuse->val->type() == Item::FIELD_ITEM) 04900 return new store_key_field(thd, 04901 key_part->field, 04902 key_buff + maybe_null, 04903 maybe_null ? key_buff : 0, 04904 key_part->length, 04905 ((Item_field*) keyuse->val)->field, 04906 keyuse->val->full_name()); 04907 return new store_key_item(thd, 04908 key_part->field, 04909 key_buff + maybe_null, 04910 maybe_null ? key_buff : 0, 04911 key_part->length, 04912 keyuse->val); 04913 } 04914 04915 /* 04916 This function is only called for const items on fields which are keys 04917 returns 1 if there was some conversion made when the field was stored. 04918 */ 04919 04920 bool 04921 store_val_in_field(Field *field, Item *item, enum_check_fields check_flag) 04922 { 04923 bool error; 04924 TABLE *table= field->table; 04925 THD *thd= table->in_use; 04926 ha_rows cuted_fields=thd->cuted_fields; 04927 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, 04928 table->write_set); 04929 04930 /* 04931 we should restore old value of count_cuted_fields because 04932 store_val_in_field can be called from mysql_insert 04933 with select_insert, which make count_cuted_fields= 1 04934 */ 04935 enum_check_fields old_count_cuted_fields= thd->count_cuted_fields; 04936 thd->count_cuted_fields= check_flag; 04937 error= item->save_in_field(field, 1); 04938 thd->count_cuted_fields= old_count_cuted_fields; 04939 dbug_tmp_restore_column_map(table->write_set, old_map); 04940 return error || cuted_fields != thd->cuted_fields; 04941 } 04942 04943 04944 static bool 04945 make_simple_join(JOIN *join,TABLE *tmp_table) 04946 { 04947 TABLE **tableptr; 04948 JOIN_TAB *join_tab; 04949 DBUG_ENTER("make_simple_join"); 04950 04951 if (!(tableptr=(TABLE**) join->thd->alloc(sizeof(TABLE*))) || 04952 !(join_tab=(JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB)))) 04953 DBUG_RETURN(TRUE); 04954 join->join_tab=join_tab; 04955 join->table=tableptr; tableptr[0]=tmp_table; 04956 join->tables=1; 04957 join->const_tables=0; 04958 join->const_table_map=0; 04959 join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count= 04960 join->tmp_table_param.func_count=0; 04961 join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0; 04962 join->first_record=join->sort_and_group=0; 04963 join->send_records=(ha_rows) 0; 04964 join->group=0; 04965 join->row_limit=join->unit->select_limit_cnt; 04966 join->do_send_rows = (join->row_limit) ? 1 : 0; 04967 04968 join_tab->cache.buff=0; /* No caching */ 04969 join_tab->table=tmp_table; 04970 join_tab->select=0; 04971 join_tab->select_cond=0; 04972 join_tab->quick=0; 04973 join_tab->type= JT_ALL; /* Map through all records */ 04974 join_tab->keys.init(); 04975 join_tab->keys.set_all(); /* test everything in quick */ 04976 join_tab->info=0; 04977 join_tab->on_expr_ref=0; 04978 join_tab->last_inner= 0; 04979 join_tab->first_unmatched= 0; 04980 join_tab->ref.key = -1; 04981 join_tab->not_used_in_distinct=0; 04982 join_tab->read_first_record= join_init_read_record; 04983 join_tab->join=join; 04984 join_tab->ref.key_parts= 0; 04985 bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record)); 04986 tmp_table->status=0; 04987 tmp_table->null_row=0; 04988 DBUG_RETURN(FALSE); 04989 } 04990 04991 04992 inline void add_cond_and_fix(Item **e1, Item *e2) 04993 { 04994 if (*e1) 04995 { 04996 Item *res; 04997 if ((res= new Item_cond_and(*e1, e2))) 04998 { 04999 *e1= res; 05000 res->quick_fix_field(); 05001 } 05002 } 05003 else 05004 *e1= e2; 05005 } 05006 05007 05008 /* 05009 Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions we've 05010 inferred from ref/eq_ref access performed. 05011 05012 SYNOPSIS 05013 add_not_null_conds() 05014 join Join to process 05015 05016 NOTES 05017 This function is a part of "Early NULL-values filtering for ref access" 05018 optimization. 05019 05020 Example of this optimization: 05021 For query SELECT * FROM t1,t2 WHERE t2.key=t1.field 05022 and plan " any-access(t1), ref(t2.key=t1.field) " 05023 add "t1.field IS NOT NULL" to t1's table condition. 05024 Description of the optimization: 05025 05026 We look through equalities choosen to perform ref/eq_ref access, 05027 pick equalities that have form "tbl.part_of_key = othertbl.field" 05028 (where othertbl is a non-const table and othertbl.field may be NULL) 05029 and add them to conditions on correspoding tables (othertbl in this 05030 example). 05031 05032 Exception from that is the case when referred_tab->join != join. 05033 I.e. don't add NOT NULL constraints from any embedded subquery. 05034 Consider this query: 05035 SELECT A.f2 FROM t1 LEFT JOIN t2 A ON A.f2 = f1 05036 WHERE A.f3=(SELECT MIN(f3) FROM t2 C WHERE A.f4 = C.f4) OR A.f3 IS NULL; 05037 Here condition A.f3 IS NOT NULL is going to be added to the WHERE 05038 condition of the embedding query. 05039 Another example: 05040 SELECT * FROM t10, t11 WHERE (t10.a < 10 OR t10.a IS NULL) 05041 AND t11.b <=> t10.b AND (t11.a = (SELECT MAX(a) FROM t12 05042 WHERE t12.b = t10.a )); 05043 Here condition t10.a IS NOT NULL is going to be added. 05044 In both cases addition of NOT NULL condition will erroneously reject 05045 some rows of the result set. 05046 referred_tab->join != join constraint would disallow such additions. 05047 05048 This optimization doesn't affect the choices that ref, range, or join 05049 optimizer make. This was intentional because this was added after 4.1 05050 was GA. 05051 05052 Implementation overview 05053 1. update_ref_and_keys() accumulates info about null-rejecting 05054 predicates in in KEY_FIELD::null_rejecting 05055 1.1 add_key_part saves these to KEYUSE. 05056 2. create_ref_for_key copies them to TABLE_REF. 05057 3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of 05058 appropiate JOIN_TAB members. 05059 */ 05060 05061 static void add_not_null_conds(JOIN *join) 05062 { 05063 DBUG_ENTER("add_not_null_conds"); 05064 for (uint i=join->const_tables ; i < join->tables ; i++) 05065 { 05066 JOIN_TAB *tab=join->join_tab+i; 05067 if ((tab->type == JT_REF || tab->type == JT_REF_OR_NULL) && 05068 !tab->table->maybe_null) 05069 { 05070 for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++) 05071 { 05072 if (tab->ref.null_rejecting & (1 << keypart)) 05073 { 05074 Item *item= tab->ref.items[keypart]; 05075 Item *notnull; 05076 DBUG_ASSERT(item->type() == Item::FIELD_ITEM); 05077 Item_field *not_null_item= (Item_field*)item; 05078 JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab; 05079 /* 05080 For UPDATE queries such as: 05081 UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1); 05082 not_null_item is the t1.f1, but it's referred_tab is 0. 05083 */ 05084 if (!referred_tab || referred_tab->join != join) 05085 continue; 05086 if (!(notnull= new Item_func_isnotnull(not_null_item))) 05087 DBUG_VOID_RETURN; 05088 /* 05089 We need to do full fix_fields() call here in order to have correct 05090 notnull->const_item(). This is needed e.g. by test_quick_select 05091 when it is called from make_join_select after this function is 05092 called. 05093 */ 05094 if (notnull->fix_fields(join->thd, ¬null)) 05095 DBUG_VOID_RETURN; 05096 DBUG_EXECUTE("where",print_where(notnull, 05097 referred_tab->table->alias);); 05098 add_cond_and_fix(&referred_tab->select_cond, notnull); 05099 } 05100 } 05101 } 05102 } 05103 DBUG_VOID_RETURN; 05104 } 05105 05106 /* 05107 Build a predicate guarded by match variables for embedding outer joins 05108 05109 SYNOPSIS 05110 add_found_match_trig_cond() 05111 tab the first inner table for most nested outer join 05112 cond the predicate to be guarded (must be set) 05113 root_tab the first inner table to stop 05114 05115 DESCRIPTION 05116 The function recursively adds guards for predicate cond 05117 assending from tab to the first inner table next embedding 05118 nested outer join and so on until it reaches root_tab 05119 (root_tab can be 0). 05120 05121 RETURN VALUE 05122 pointer to the guarded predicate, if success 05123 0, otherwise 05124 */ 05125 05126 static COND* 05127 add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab) 05128 { 05129 COND *tmp; 05130 DBUG_ASSERT(cond != 0); 05131 if (tab == root_tab) 05132 return cond; 05133 if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab))) 05134 tmp= new Item_func_trig_cond(tmp, &tab->found); 05135 if (tmp) 05136 { 05137 tmp->quick_fix_field(); 05138 tmp->update_used_tables(); 05139 } 05140 return tmp; 05141 } 05142 05143 05144 /* 05145 Fill in outer join related info for the execution plan structure 05146 05147 SYNOPSIS 05148 make_outerjoin_info() 05149 join - reference to the info fully describing the query 05150 05151 DESCRIPTION 05152 For each outer join operation left after simplification of the 05153 original query the function set up the following pointers in the linear 05154 structure join->join_tab representing the selected execution plan. 05155 The first inner table t0 for the operation is set to refer to the last 05156 inner table tk through the field t0->last_inner. 05157 Any inner table ti for the operation are set to refer to the first 05158 inner table ti->first_inner. 05159 The first inner table t0 for the operation is set to refer to the 05160 first inner table of the embedding outer join operation, if there is any, 05161 through the field t0->first_upper. 05162 The on expression for the outer join operation is attached to the 05163 corresponding first inner table through the field t0->on_expr_ref. 05164 Here ti are structures of the JOIN_TAB type. 05165 05166 EXAMPLE 05167 For the query: 05168 SELECT * FROM t1 05169 LEFT JOIN 05170 (t2, t3 LEFT JOIN t4 ON t3.a=t4.a) 05171 ON (t1.a=t2.a AND t1.b=t3.b) 05172 WHERE t1.c > 5, 05173 given the execution plan with the table order t1,t2,t3,t4 05174 is selected, the following references will be set; 05175 t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2] 05176 t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2], 05177 on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 05178 *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref. 05179 05180 NOTES 05181 The function assumes that the simplification procedure has been 05182 already applied to the join query (see simplify_joins). 05183 This function can be called only after the execution plan 05184 has been chosen. 05185 */ 05186 05187 static void 05188 make_outerjoin_info(JOIN *join) 05189 { 05190 DBUG_ENTER("make_outerjoin_info"); 05191 for (uint i=join->const_tables ; i < join->tables ; i++) 05192 { 05193 JOIN_TAB *tab=join->join_tab+i; 05194 TABLE *table=tab->table; 05195 TABLE_LIST *tbl= table->pos_in_table_list; 05196 TABLE_LIST *embedding= tbl->embedding; 05197 05198 if (tbl->outer_join) 05199 { 05200 /* 05201 Table tab is the only one inner table for outer join. 05202 (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a 05203 is in the query above.) 05204 */ 05205 tab->last_inner= tab->first_inner= tab; 05206 tab->on_expr_ref= &tbl->on_expr; 05207 tab->cond_equal= tbl->cond_equal; 05208 if (embedding) 05209 tab->first_upper= embedding->nested_join->first_nested; 05210 } 05211 for ( ; embedding ; embedding= embedding->embedding) 05212 { 05213 NESTED_JOIN *nested_join= embedding->nested_join; 05214 if (!nested_join->counter) 05215 { 05216 /* 05217 Table tab is the first inner table for nested_join. 05218 Save reference to it in the nested join structure. 05219 */ 05220 nested_join->first_nested= tab; 05221 tab->on_expr_ref= &embedding->on_expr; 05222 tab->cond_equal= tbl->cond_equal; 05223 if (embedding->embedding) 05224 tab->first_upper= embedding->embedding->nested_join->first_nested; 05225 } 05226 if (!tab->first_inner) 05227 tab->first_inner= nested_join->first_nested; 05228 if (++nested_join->counter < nested_join->join_list.elements) 05229 break; 05230 /* Table tab is the last inner table for nested join. */ 05231 nested_join->first_nested->last_inner= tab; 05232 } 05233 } 05234 DBUG_VOID_RETURN; 05235 } 05236 05237 05238 static bool 05239 make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) 05240 { 05241 THD *thd= join->thd; 05242 DBUG_ENTER("make_join_select"); 05243 if (select) 05244 { 05245 add_not_null_conds(join); 05246 table_map used_tables; 05247 if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */ 05248 { /* there may be a select without a cond. */ 05249 if (join->tables > 1) 05250 cond->update_used_tables(); // Tablenr may have changed 05251 if (join->const_tables == join->tables && 05252 thd->lex->current_select->master_unit() == 05253 &thd->lex->unit) // not upper level SELECT 05254 join->const_table_map|=RAND_TABLE_BIT; 05255 { // Check const tables 05256 COND *const_cond= 05257 make_cond_for_table(cond, 05258 join->const_table_map, 05259 (table_map) 0); 05260 DBUG_EXECUTE("where",print_where(const_cond,"constants");); 05261 for (JOIN_TAB *tab= join->join_tab+join->const_tables; 05262 tab < join->join_tab+join->tables ; tab++) 05263 { 05264 if (*tab->on_expr_ref) 05265 { 05266 JOIN_TAB *cond_tab= tab->first_inner; 05267 COND *tmp= make_cond_for_table(*tab->on_expr_ref, 05268 join->const_table_map, 05269 ( table_map) 0); 05270 if (!tmp) 05271 continue; 05272 tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl); 05273 if (!tmp) 05274 DBUG_RETURN(1); 05275 tmp->quick_fix_field(); 05276 cond_tab->select_cond= !cond_tab->select_cond ? tmp : 05277 new Item_cond_and(cond_tab->select_cond, 05278 tmp); 05279 if (!cond_tab->select_cond) 05280 DBUG_RETURN(1); 05281 cond_tab->select_cond->quick_fix_field(); 05282 } 05283 } 05284 if (const_cond && !const_cond->val_int()) 05285 { 05286 DBUG_PRINT("info",("Found impossible WHERE condition")); 05287 DBUG_RETURN(1); // Impossible const condition 05288 } 05289 } 05290 } 05291 used_tables=((select->const_tables=join->const_table_map) | 05292 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT); 05293 for (uint i=join->const_tables ; i < join->tables ; i++) 05294 { 05295 JOIN_TAB *tab=join->join_tab+i; 05296 /* 05297 first_inner is the X in queries like: 05298 SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X 05299 */ 05300 JOIN_TAB *first_inner_tab= tab->first_inner; 05301 table_map current_map= tab->table->map; 05302 bool use_quick_range=0; 05303 COND *tmp; 05304 05305 /* 05306 Following force including random expression in last table condition. 05307 It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5 05308 */ 05309 if (i == join->tables-1) 05310 current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT; 05311 used_tables|=current_map; 05312 05313 if (tab->type == JT_REF && tab->quick && 05314 (uint) tab->ref.key == tab->quick->index && 05315 tab->ref.key_length < tab->quick->max_used_key_length) 05316 { 05317 /* Range uses longer key; Use this instead of ref on key */ 05318 tab->type=JT_ALL; 05319 use_quick_range=1; 05320 tab->use_quick=1; 05321 tab->ref.key= -1; 05322 tab->ref.key_parts=0; // Don't use ref key. 05323 join->best_positions[i].records_read= rows2double(tab->quick->records); 05324 } 05325 05326 tmp= NULL; 05327 if (cond) 05328 tmp= make_cond_for_table(cond,used_tables,current_map); 05329 if (cond && !tmp && tab->quick) 05330 { // Outer join 05331 if (tab->type != JT_ALL) 05332 { 05333 /* 05334 Don't use the quick method 05335 We come here in the case where we have 'key=constant' and 05336 the test is removed by make_cond_for_table() 05337 */ 05338 delete tab->quick; 05339 tab->quick= 0; 05340 } 05341 else 05342 { 05343 /* 05344 Hack to handle the case where we only refer to a table 05345 in the ON part of an OUTER JOIN. In this case we want the code 05346 below to check if we should use 'quick' instead. 05347 */ 05348 DBUG_PRINT("info", ("Item_int")); 05349 tmp= new Item_int((longlong) 1,1); // Always true 05350 } 05351 05352 } 05353 if (tmp || !cond) 05354 { 05355 DBUG_EXECUTE("where",print_where(tmp,tab->table->alias);); 05356 SQL_SELECT *sel= tab->select= ((SQL_SELECT*) 05357 thd->memdup((gptr) select, 05358 sizeof(*select))); 05359 if (!sel) 05360 DBUG_RETURN(1); // End of memory 05361 /* 05362 If tab is an inner table of an outer join operation, 05363 add a match guard to the pushed down predicate. 05364 The guard will turn the predicate on only after 05365 the first match for outer tables is encountered. 05366 */ 05367 if (cond) 05368 { 05369 /* 05370 Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without 05371 a cond, so neutralize the hack above. 05372 */ 05373 if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0))) 05374 DBUG_RETURN(1); 05375 tab->select_cond=sel->cond=tmp; 05376 /* Push condition to storage engine if this is enabled 05377 and the condition is not guarded */ 05378 tab->table->file->pushed_cond= NULL; 05379 if (thd->variables.engine_condition_pushdown) 05380 { 05381 COND *push_cond= 05382 make_cond_for_table(tmp, current_map, current_map); 05383 if (push_cond) 05384 { 05385 /* Push condition to handler */ 05386 if (!tab->table->file->cond_push(push_cond)) 05387 tab->table->file->pushed_cond= push_cond; 05388 } 05389 } 05390 } 05391 else 05392 tab->select_cond= sel->cond= NULL; 05393 05394 sel->head=tab->table; 05395 DBUG_EXECUTE("where",print_where(tmp,tab->table->alias);); 05396 if (tab->quick) 05397 { 05398 /* Use quick key read if it's a constant and it's not used 05399 with key reading */ 05400 if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF 05401 && tab->type != JT_FT && (tab->type != JT_REF || 05402 (uint) tab->ref.key == tab->quick->index)) 05403 { 05404 sel->quick=tab->quick; // Use value from get_quick_... 05405 sel->quick_keys.clear_all(); 05406 sel->needed_reg.clear_all(); 05407 } 05408 else 05409 { 05410 delete tab->quick; 05411 } 05412 tab->quick=0; 05413 } 05414 uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1; 05415 if (i == join->const_tables && ref_key) 05416 { 05417 if (!tab->const_keys.is_clear_all() && 05418 tab->table->reginfo.impossible_range) 05419 DBUG_RETURN(1); 05420 } 05421 else if (tab->type == JT_ALL && ! use_quick_range) 05422 { 05423 if (!tab->const_keys.is_clear_all() && 05424 tab->table->reginfo.impossible_range) 05425 DBUG_RETURN(1); // Impossible range 05426 /* 05427 We plan to scan all rows. 05428 Check again if we should use an index. 05429 We could have used an column from a previous table in 05430 the index if we are using limit and this is the first table 05431 */ 05432 05433 if (cond && 05434 (!tab->keys.is_subset(tab->const_keys) && i > 0) || 05435 (!tab->const_keys.is_clear_all() && i == join->const_tables && 05436 join->unit->select_limit_cnt < 05437 join->best_positions[i].records_read && 05438 !(join->select_options & OPTION_FOUND_ROWS))) 05439 { 05440 /* Join with outer join condition */ 05441 COND *orig_cond=sel->cond; 05442 sel->cond= and_conds(sel->cond, *tab->on_expr_ref); 05443 05444 /* 05445 We can't call sel->cond->fix_fields, 05446 as it will break tab->on_expr if it's AND condition 05447 (fix_fields currently removes extra AND/OR levels). 05448 Yet attributes of the just built condition are not needed. 05449 Thus we call sel->cond->quick_fix_field for safety. 05450 */ 05451 if (sel->cond && !sel->cond->fixed) 05452 sel->cond->quick_fix_field(); 05453 05454 if (sel->test_quick_select(thd, tab->keys, 05455 used_tables & ~ current_map, 05456 (join->select_options & 05457 OPTION_FOUND_ROWS ? 05458 HA_POS_ERROR : 05459 join->unit->select_limit_cnt), 0) < 0) 05460 { 05461 /* 05462 Before reporting "Impossible WHERE" for the whole query 05463 we have to check isn't it only "impossible ON" instead 05464 */ 05465 sel->cond=orig_cond; 05466 if (!*tab->on_expr_ref || 05467 sel->test_quick_select(thd, tab->keys, 05468 used_tables & ~ current_map, 05469 (join->select_options & 05470 OPTION_FOUND_ROWS ? 05471 HA_POS_ERROR : 05472 join->unit->select_limit_cnt),0) < 0) 05473 DBUG_RETURN(1); // Impossible WHERE 05474 } 05475 else 05476 sel->cond=orig_cond; 05477 05478 /* Fix for EXPLAIN */ 05479 if (sel->quick) 05480 join->best_positions[i].records_read= sel->quick->records; 05481 } 05482 else 05483 { 05484 sel->needed_reg=tab->needed_reg; 05485 sel->quick_keys.clear_all(); 05486 } 05487 if (!sel->quick_keys.is_subset(tab->checked_keys) || 05488 !sel->needed_reg.is_subset(tab->checked_keys)) 05489 { 05490 tab->keys=sel->quick_keys; 05491 tab->keys.merge(sel->needed_reg); 05492 tab->use_quick= (!sel->needed_reg.is_clear_all() && 05493 (select->quick_keys.is_clear_all() || 05494 (select->quick && 05495 (select->quick->records >= 100L)))) ? 05496 2 : 1; 05497 sel->read_tables= used_tables & ~current_map; 05498 } 05499 if (i != join->const_tables && tab->use_quick != 2) 05500 { /* Read with cache */ 05501 if (cond && 05502 (tmp=make_cond_for_table(cond, 05503 join->const_table_map | 05504 current_map, 05505 current_map))) 05506 { 05507 DBUG_EXECUTE("where",print_where(tmp,"cache");); 05508 tab->cache.select=(SQL_SELECT*) 05509 thd->memdup((gptr) sel, sizeof(SQL_SELECT)); 05510 tab->cache.select->cond=tmp; 05511 tab->cache.select->read_tables=join->const_table_map; 05512 } 05513 } 05514 } 05515 } 05516 05517 /* 05518 Push down all predicates from on expressions. 05519 Each of these predicated are guarded by a variable 05520 that turns if off just before null complemented row for 05521 outer joins is formed. Thus, the predicates from an 05522 'on expression' are guaranteed not to be checked for 05523 the null complemented row. 05524 */ 05525 JOIN_TAB *last_tab= tab; 05526 while (first_inner_tab && first_inner_tab->last_inner == last_tab) 05527 { 05528 /* 05529 Table tab is the last inner table of an outer join. 05530 An on expression is always attached to it. 05531 */ 05532 COND *on_expr= *first_inner_tab->on_expr_ref; 05533 05534 table_map used_tables= join->const_table_map | 05535 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT; 05536 for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++) 05537 { 05538 current_map= tab->table->map; 05539 used_tables|= current_map; 05540 COND *tmp= make_cond_for_table(on_expr, used_tables, current_map); 05541 if (tmp) 05542 { 05543 JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab; 05544 /* 05545 First add the guards for match variables of 05546 all embedding outer join operations. 05547 */ 05548 if (!(tmp= add_found_match_trig_cond(cond_tab->first_inner, 05549 tmp, first_inner_tab))) 05550 DBUG_RETURN(1); 05551 /* 05552 Now add the guard turning the predicate off for 05553 the null complemented row. 05554 */ 05555 DBUG_PRINT("info", ("Item_func_trig_cond")); 05556 tmp= new Item_func_trig_cond(tmp, 05557 &first_inner_tab->not_null_compl); 05558 DBUG_PRINT("info", ("Item_func_trig_cond 0x%lx", (ulong) tmp)); 05559 if (tmp) 05560 tmp->quick_fix_field(); 05561 /* Add the predicate to other pushed down predicates */ 05562 DBUG_PRINT("info", ("Item_cond_and")); 05563 cond_tab->select_cond= !cond_tab->select_cond ? tmp : 05564 new Item_cond_and(cond_tab->select_cond,tmp); 05565 DBUG_PRINT("info", ("Item_cond_and 0x%lx", 05566 (ulong)cond_tab->select_cond)); 05567 if (!cond_tab->select_cond) 05568 DBUG_RETURN(1); 05569 cond_tab->select_cond->quick_fix_field(); 05570 } 05571 } 05572 first_inner_tab= first_inner_tab->first_upper; 05573 } 05574 } 05575 } 05576 DBUG_RETURN(0); 05577 } 05578 05579 static void 05580 make_join_readinfo(JOIN *join, uint options) 05581 { 05582 uint i; 05583 bool statistics= test(!(join->select_options & SELECT_DESCRIBE)); 05584 bool ordered_set= 0; 05585 bool sorted= 1; 05586 DBUG_ENTER("make_join_readinfo"); 05587 05588 for (i=join->const_tables ; i < join->tables ; i++) 05589 { 05590 JOIN_TAB *tab=join->join_tab+i; 05591 TABLE *table=tab->table; 05592 tab->read_record.table= table; 05593 tab->read_record.file=table->file; 05594 tab->next_select=sub_select; /* normal select */ 05595 05596 /* 05597 Determine if the set is already ordered for ORDER BY, so it can 05598 disable join cache because it will change the ordering of the results. 05599 Code handles sort table that is at any location (not only first after 05600 the const tables) despite the fact that it's currently prohibited. 05601 */ 05602 if (!ordered_set && 05603 (table == join->sort_by_table && 05604 (!join->order || join->skip_sort_order || 05605 test_if_skip_sort_order(tab, join->order, join->select_limit, 05606 1)) 05607 ) || 05608 (join->sort_by_table == (TABLE *) 1 && i != join->const_tables)) 05609 ordered_set= 1; 05610 05611 tab->sorted= sorted; 05612 sorted= 0; // only first must be sorted 05613 switch (tab->type) { 05614 case JT_SYSTEM: // Only happens with left join 05615 table->status=STATUS_NO_RECORD; 05616 tab->read_first_record= join_read_system; 05617 tab->read_record.read_record= join_no_more_records; 05618 break; 05619 case JT_CONST: // Only happens with left join 05620 table->status=STATUS_NO_RECORD; 05621 tab->read_first_record= join_read_const; 05622 tab->read_record.read_record= join_no_more_records; 05623 if (table->used_keys.is_set(tab->ref.key) && 05624 !table->no_keyread) 05625 { 05626 table->key_read=1; 05627 table->file->extra(HA_EXTRA_KEYREAD); 05628 } 05629 break; 05630 case JT_EQ_REF: 05631 table->status=STATUS_NO_RECORD; 05632 if (tab->select) 05633 { 05634 delete tab->select->quick; 05635 tab->select->quick=0; 05636 } 05637 delete tab->quick; 05638 tab->quick=0; 05639 tab->read_first_record= join_read_key; 05640 tab->read_record.read_record= join_no_more_records; 05641 if (table->used_keys.is_set(tab->ref.key) && 05642 !table->no_keyread) 05643 { 05644 table->key_read=1; 05645 table->file->extra(HA_EXTRA_KEYREAD); 05646 } 05647 break; 05648 case JT_REF_OR_NULL: 05649 case JT_REF: 05650 table->status=STATUS_NO_RECORD; 05651 if (tab->select) 05652 { 05653 delete tab->select->quick; 05654 tab->select->quick=0; 05655 } 05656 delete tab->quick; 05657 tab->quick=0; 05658 if (table->used_keys.is_set(tab->ref.key) && 05659 !table->no_keyread) 05660 { 05661 table->key_read=1; 05662 table->file->extra(HA_EXTRA_KEYREAD); 05663 } 05664 if (tab->type == JT_REF) 05665 { 05666 tab->read_first_record= join_read_always_key; 05667 tab->read_record.read_record= join_read_next_same; 05668 } 05669 else 05670 { 05671 tab->read_first_record= join_read_always_key_or_null; 05672 tab->read_record.read_record= join_read_next_same_or_null; 05673 } 05674 break; 05675 case JT_FT: 05676 table->status=STATUS_NO_RECORD; 05677 tab->read_first_record= join_ft_read_first; 05678 tab->read_record.read_record= join_ft_read_next; 05679 break; 05680 case JT_ALL: 05681 /* 05682 If previous table use cache 05683 If the incoming data set is already sorted don't use cache. 05684 */ 05685 table->status=STATUS_NO_RECORD; 05686 if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) && 05687 tab->use_quick != 2 && !tab->first_inner && !ordered_set) 05688 { 05689 if ((options & SELECT_DESCRIBE) || 05690 !join_init_cache(join->thd,join->join_tab+join->const_tables, 05691 i-join->const_tables)) 05692 { 05693 tab[-1].next_select=sub_select_cache; /* Patch previous */ 05694 } 05695 } 05696 /* These init changes read_record */ 05697 if (tab->use_quick == 2) 05698 { 05699 join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED; 05700 tab->read_first_record= join_init_quick_read_record; 05701 if (statistics) 05702 statistic_increment(join->thd->status_var.select_range_check_count, 05703 &LOCK_status); 05704 } 05705 else 05706 { 05707 tab->read_first_record= join_init_read_record; 05708 if (i == join->const_tables) 05709 { 05710 if (tab->select && tab->select->quick) 05711 { 05712 if (statistics) 05713 statistic_increment(join->thd->status_var.select_range_count, 05714 &LOCK_status); 05715 } 05716 else 05717 { 05718 join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED; 05719 if (statistics) 05720 statistic_increment(join->thd->status_var.select_scan_count, 05721 &LOCK_status); 05722 } 05723 } 05724 else 05725 { 05726 if (tab->select && tab->select->quick) 05727 { 05728 if (statistics) 05729 statistic_increment(join->thd->status_var.select_full_range_join_count, 05730 &LOCK_status); 05731 } 05732 else 05733 { 05734 join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED; 05735 if (statistics) 05736 statistic_increment(join->thd->status_var.select_full_join_count, 05737 &LOCK_status); 05738 } 05739 } 05740 if (!table->no_keyread) 05741 { 05742 if (tab->select && tab->select->quick && 05743 tab->select->quick->index != MAX_KEY && //not index_merge 05744 table->used_keys.is_set(tab->select->quick->index)) 05745 { 05746 table->key_read=1; 05747 table->file->extra(HA_EXTRA_KEYREAD); 05748 } 05749 else if (!table->used_keys.is_clear_all() && 05750 !(tab->select && tab->select->quick)) 05751 { // Only read index tree 05752 tab->index=find_shortest_key(table, & table->used_keys); 05753 tab->read_first_record= join_read_first; 05754 tab->type=JT_NEXT; // Read with index_first / index_next 05755 } 05756 } 05757 } 05758 break; 05759 default: 05760 DBUG_PRINT("error",("Table type %d found",tab->type)); /* purecov: deadcode */ 05761 break; /* purecov: deadcode */ 05762 case JT_UNKNOWN: 05763 case JT_MAYBE_REF: 05764 abort(); /* purecov: deadcode */ 05765 } 05766 } 05767 join->join_tab[join->tables-1].next_select=0; /* Set by do_select */ 05768 DBUG_VOID_RETURN; 05769 } 05770 05771 05772 /* 05773 Give error if we some tables are done with a full join 05774 05775 SYNOPSIS 05776 error_if_full_join() 05777 join Join condition 05778 05779 USAGE 05780 This is used by multi_table_update and multi_table_delete when running 05781 in safe mode 05782 05783 RETURN VALUES 05784 0 ok 05785 1 Error (full join used) 05786 */ 05787 05788 bool error_if_full_join(JOIN *join) 05789 { 05790 for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables; 05791 tab < end; 05792 tab++) 05793 { 05794 if (tab->type == JT_ALL && (!tab->select || !tab->select->quick)) 05795 { 05796 my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE, 05797 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0)); 05798 return(1); 05799 } 05800 } 05801 return(0); 05802 } 05803 05804 05805 /* 05806 cleanup JOIN_TAB 05807 05808 SYNOPSIS 05809 JOIN_TAB::cleanup() 05810 */ 05811 05812 void JOIN_TAB::cleanup() 05813 { 05814 delete select; 05815 select= 0; 05816 delete quick; 05817 quick= 0; 05818 x_free(cache.buff); 05819 cache.buff= 0; 05820 if (table) 05821 { 05822 if (table->key_read) 05823 { 05824 table->key_read= 0; 05825 table->file->extra(HA_EXTRA_NO_KEYREAD); 05826 } 05827 table->file->ha_index_or_rnd_end(); 05828 /* 05829 We need to reset this for next select 05830 (Tested in part_of_refkey) 05831 */ 05832 table->reginfo.join_tab= 0; 05833 } 05834 end_read_record(&read_record); 05835 } 05836 05837 05838 /* 05839 Partially cleanup JOIN after it has executed: close index or rnd read 05840 (table cursors), free quick selects. 05841 05842 DESCRIPTION 05843 This function is called in the end of execution of a JOIN, before the used 05844 tables are unlocked and closed. 05845 05846 For a join that is resolved using a temporary table, the first sweep is 05847 performed against actual tables and an intermediate result is inserted 05848 into the temprorary table. 05849 The last sweep is performed against the temporary table. Therefore, 05850 the base tables and associated buffers used to fill the temporary table 05851 are no longer needed, and this function is called to free them. 05852 05853 For a join that is performed without a temporary table, this function 05854 is called after all rows are sent, but before EOF packet is sent. 05855 05856 For a simple SELECT with no subqueries this function performs a full 05857 cleanup of the JOIN and calls mysql_unlock_read_tables to free used base 05858 tables. 05859 05860 If a JOIN is executed for a subquery or if it has a subquery, we can't 05861 do the full cleanup and need to do a partial cleanup only. 05862 o If a JOIN is not the top level join, we must not unlock the tables 05863 because the outer select may not have been evaluated yet, and we 05864 can't unlock only selected tables of a query. 05865 05866 o Additionally, if this JOIN corresponds to a correlated subquery, we 05867 should not free quick selects and join buffers because they will be 05868 needed for the next execution of the correlated subquery. 05869 05870 o However, if this is a JOIN for a [sub]select, which is not 05871 a correlated subquery itself, but has subqueries, we can free it 05872 fully and also free JOINs of all its subqueries. The exception 05873 is a subquery in SELECT list, e.g: 05874 SELECT a, (select max(b) from t1) group by c 05875 This subquery will not be evaluated at first sweep and its value will 05876 not be inserted into the temporary table. Instead, it's evaluated 05877 when selecting from the temporary table. Therefore, it can't be freed 05878 here even though it's not correlated. 05879 */ 05880 05881 void JOIN::join_free() 05882 { 05883 SELECT_LEX_UNIT *unit; 05884 SELECT_LEX *sl; 05885 /* 05886 Optimization: if not EXPLAIN and we are done with the JOIN, 05887 free all tables. 05888 */ 05889 bool full= (!select_lex->uncacheable && !thd->lex->describe); 05890 bool can_unlock= full; 05891 DBUG_ENTER("JOIN::join_free"); 05892 05893 cleanup(full); 05894 05895 for (unit= select_lex->first_inner_unit(); unit; unit= unit->next_unit()) 05896 for (sl= unit->first_select(); sl; sl= sl->next_select()) 05897 { 05898 Item_subselect *subselect= sl->master_unit()->item; 05899 bool full_local= full && (!subselect || subselect->is_evaluated()); 05900 /* 05901 If this join is evaluated, we can fully clean it up and clean up all 05902 its underlying joins even if they are correlated -- they will not be 05903 used any more anyway. 05904 If this join is not yet evaluated, we still must clean it up to 05905 close its table cursors -- it may never get evaluated, as in case of 05906 ... HAVING FALSE OR a IN (SELECT ...)) 05907 but all table cursors must be closed before the unlock. 05908 */ 05909 sl->cleanup_all_joins(full_local); 05910 /* Can't unlock if at least one JOIN is still needed */ 05911 can_unlock= can_unlock && full_local; 05912 } 05913 05914 /* 05915 We are not using tables anymore 05916 Unlock all tables. We may be in an INSERT .... SELECT statement. 05917 */ 05918 if (can_unlock && lock && thd->lock && 05919 !(select_options & SELECT_NO_UNLOCK) && 05920 !select_lex->subquery_in_having && 05921 (select_lex == (thd->lex->unit.fake_select_lex ? 05922 thd->lex->unit.fake_select_lex : &thd->lex->select_lex))) 05923 { 05924 /* 05925 TODO: unlock tables even if the join isn't top level select in the 05926 tree. 05927 */ 05928 mysql_unlock_read_tables(thd, lock); // Don't free join->lock 05929 lock= 0; 05930 } 05931 05932 DBUG_VOID_RETURN; 05933 } 05934 05935 05936 /* 05937 Free resources of given join 05938 05939 SYNOPSIS 05940 JOIN::cleanup() 05941 fill - true if we should free all resources, call with full==1 should be 05942 last, before it this function can be called with full==0 05943 05944 NOTE: with subquery this function definitely will be called several times, 05945 but even for simple query it can be called several times. 05946 */ 05947 05948 void JOIN::cleanup(bool full) 05949 { 05950 DBUG_ENTER("JOIN::cleanup"); 05951 05952 if (table) 05953 { 05954 JOIN_TAB *tab,*end; 05955 /* 05956 Only a sorted table may be cached. This sorted table is always the 05957 first non const table in join->table 05958 */ 05959 if (tables > const_tables) // Test for not-const tables 05960 { 05961 free_io_cache(table[const_tables]); 05962 filesort_free_buffers(table[const_tables]); 05963 } 05964 05965 if (full) 05966 { 05967 for (tab= join_tab, end= tab+tables; tab != end; tab++) 05968 tab->cleanup(); 05969 table= 0; 05970 tables= 0; 05971 } 05972 else 05973 { 05974 for (tab= join_tab, end= tab+tables; tab != end; tab++) 05975 { 05976 if (tab->table) 05977 tab->table->file->ha_index_or_rnd_end(); 05978 } 05979 } 05980 } 05981 /* 05982 We are not using tables anymore 05983 Unlock all tables. We may be in an INSERT .... SELECT statement. 05984 */ 05985 if (full) 05986 { 05987 if (tmp_join) 05988 tmp_table_param.copy_field= 0; 05989 group_fields.delete_elements(); 05990 /* 05991 We can't call delete_elements() on copy_funcs as this will cause 05992 problems in free_elements() as some of the elements are then deleted. 05993 */ 05994 tmp_table_param.copy_funcs.empty(); 05995 /* 05996 If we have tmp_join and 'this' JOIN is not tmp_join and 05997 tmp_table_param.copy_field's of them are equal then we have to remove 05998 pointer to tmp_table_param.copy_field from tmp_join, because it qill 05999 be removed in tmp_table_param.cleanup(). 06000 */ 06001 if (tmp_join && 06002 tmp_join != this && 06003 tmp_join->tmp_table_param.copy_field == 06004 tmp_table_param.copy_field) 06005 { 06006 tmp_join->tmp_table_param.copy_field= 06007 tmp_join->tmp_table_param.save_copy_field= 0; 06008 } 06009 tmp_table_param.cleanup(); 06010 } 06011 DBUG_VOID_RETURN; 06012 } 06013 06014 06015 /***************************************************************************** 06016 Remove the following expressions from ORDER BY and GROUP BY: 06017 Constant expressions 06018 Expression that only uses tables that are of type EQ_REF and the reference 06019 is in the ORDER list or if all refereed tables are of the above type. 06020 06021 In the following, the X field can be removed: 06022 SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X 06023 SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X 06024 06025 These can't be optimized: 06026 SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a 06027 SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c 06028 SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a 06029 *****************************************************************************/ 06030 06031 static bool 06032 eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab) 06033 { 06034 if (tab->cached_eq_ref_table) // If cached 06035 return tab->eq_ref_table; 06036 tab->cached_eq_ref_table=1; 06037 if (tab->type == JT_CONST) // We can skip const tables 06038 return (tab->eq_ref_table=1); /* purecov: inspected */ 06039 if (tab->type != JT_EQ_REF || tab->table->maybe_null) 06040 return (tab->eq_ref_table=0); // We must use this 06041 Item **ref_item=tab->ref.items; 06042 Item **end=ref_item+tab->ref.key_parts; 06043 uint found=0; 06044 table_map map=tab->table->map; 06045 06046 for (; ref_item != end ; ref_item++) 06047 { 06048 if (! (*ref_item)->const_item()) 06049 { // Not a const ref 06050 ORDER *order; 06051 for (order=start_order ; order ; order=order->next) 06052 { 06053 if ((*ref_item)->eq(order->item[0],0)) 06054 break; 06055 } 06056 if (order) 06057 { 06058 found++; 06059 DBUG_ASSERT(!(order->used & map)); 06060 order->used|=map; 06061 continue; // Used in ORDER BY 06062 } 06063 if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables())) 06064 return (tab->eq_ref_table=0); 06065 } 06066 } 06067 /* Check that there was no reference to table before sort order */ 06068 for (; found && start_order ; start_order=start_order->next) 06069 { 06070 if (start_order->used & map) 06071 { 06072 found--; 06073 continue; 06074 } 06075 if (start_order->depend_map & map) 06076 return (tab->eq_ref_table=0); 06077 } 06078 return tab->eq_ref_table=1; 06079 } 06080 06081 06082 static bool 06083 only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables) 06084 { 06085 if (specialflag & SPECIAL_SAFE_MODE) 06086 return 0; // skip this optimize /* purecov: inspected */ 06087 for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1) 06088 { 06089 if (tables & 1 && !eq_ref_table(join, order, *tab)) 06090 return 0; 06091 } 06092 return 1; 06093 } 06094 06095 06096 /* Update the dependency map for the tables */ 06097 06098 static void update_depend_map(JOIN *join) 06099 { 06100 JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables; 06101 06102 for (; join_tab != end ; join_tab++) 06103 { 06104 TABLE_REF *ref= &join_tab->ref; 06105 table_map depend_map=0; 06106 Item **item=ref->items; 06107 uint i; 06108 for (i=0 ; i < ref->key_parts ; i++,item++) 06109 depend_map|=(*item)->used_tables(); 06110 ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT; 06111 depend_map&= ~OUTER_REF_TABLE_BIT; 06112 for (JOIN_TAB **tab=join->map2table; 06113 depend_map ; 06114 tab++,depend_map>>=1 ) 06115 { 06116 if (depend_map & 1) 06117 ref->depend_map|=(*tab)->ref.depend_map; 06118 } 06119 } 06120 } 06121 06122 06123 /* Update the dependency map for the sort order */ 06124 06125 static void update_depend_map(JOIN *join, ORDER *order) 06126 { 06127 for (; order ; order=order->next) 06128 { 06129 table_map depend_map; 06130 order->item[0]->update_used_tables(); 06131 order->depend_map=depend_map=order->item[0]->used_tables(); 06132 // Not item_sum(), RAND() and no reference to table outside of sub select 06133 if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))) 06134 { 06135 for (JOIN_TAB **tab=join->map2table; 06136 depend_map ; 06137 tab++, depend_map>>=1) 06138 { 06139 if (depend_map & 1) 06140 order->depend_map|=(*tab)->ref.depend_map; 06141 } 06142 } 06143 } 06144 } 06145 06146 06147 /* 06148 Remove all constants and check if ORDER only contains simple expressions 06149 06150 SYNOPSIS 06151 remove_const() 06152 join Join handler 06153 first_order List of SORT or GROUP order 06154 cond WHERE statement 06155 change_list Set to 1 if we should remove things from list 06156 If this is not set, then only simple_order is 06157 calculated 06158 simple_order Set to 1 if we are only using simple expressions 06159 06160 RETURN 06161 Returns new sort order 06162 06163 simple_order is set to 1 if sort_order only uses fields from head table 06164 and the head table is not a LEFT JOIN table 06165 06166 */ 06167 06168 static ORDER * 06169 remove_const(JOIN *join,ORDER *first_order, COND *cond, 06170 bool change_list, bool *simple_order) 06171 { 06172 if (join->tables == join->const_tables) 06173 return change_list ? 0 : first_order; // No need to sort 06174 06175 ORDER *order,**prev_ptr; 06176 table_map first_table= join->join_tab[join->const_tables].table->map; 06177 table_map not_const_tables= ~join->const_table_map; 06178 table_map ref; 06179 DBUG_ENTER("remove_const"); 06180 06181 prev_ptr= &first_order; 06182 *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1; 06183 06184 /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */ 06185 06186 update_depend_map(join, first_order); 06187 for (order=first_order; order ; order=order->next) 06188 { 06189 table_map order_tables=order->item[0]->used_tables(); 06190 if (order->item[0]->with_sum_func) 06191 *simple_order=0; // Must do a temp table to sort 06192 else if (!(order_tables & not_const_tables)) 06193 { 06194 DBUG_PRINT("info",("removing: %s", order->item[0]->full_name())); 06195 continue; // skip const item 06196 } 06197 else 06198 { 06199 if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)) 06200 *simple_order=0; 06201 else 06202 { 06203 Item *comp_item=0; 06204 if (cond && const_expression_in_where(cond,order->item[0], &comp_item)) 06205 { 06206 DBUG_PRINT("info",("removing: %s", order->item[0]->full_name())); 06207 continue; 06208 } 06209 if ((ref=order_tables & (not_const_tables ^ first_table))) 06210 { 06211 if (!(order_tables & first_table) && 06212 only_eq_ref_tables(join,first_order, ref)) 06213 { 06214 DBUG_PRINT("info",("removing: %s", order->item[0]->full_name())); 06215 continue; 06216 } 06217 *simple_order=0; // Must do a temp table to sort 06218 } 06219 } 06220 } 06221 if (change_list) 06222 *prev_ptr= order; // use this entry 06223 prev_ptr= &order->next; 06224 } 06225 if (change_list) 06226 *prev_ptr=0; 06227 if (prev_ptr == &first_order) // Nothing to sort/group 06228 *simple_order=1; 06229 DBUG_PRINT("exit",("simple_order: %d",(int) *simple_order)); 06230 DBUG_RETURN(first_order); 06231 } 06232 06233 06234 static int 06235 return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables, 06236 List<Item> &fields, bool send_row, uint select_options, 06237 const char *info, Item *having) 06238 { 06239 DBUG_ENTER("return_zero_rows"); 06240 06241 if (select_options & SELECT_DESCRIBE) 06242 { 06243 select_describe(join, FALSE, FALSE, FALSE, info); 06244 DBUG_RETURN(0); 06245 } 06246 06247 join->join_free(); 06248 06249 if (send_row) 06250 { 06251 for (TABLE_LIST *table= tables; table; table= table->next_leaf) 06252 mark_as_null_row(table->table); // All fields are NULL 06253 if (having && having->val_int() == 0) 06254 send_row=0; 06255 } 06256 if (!(result->send_fields(fields, 06257 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))) 06258 { 06259 if (send_row) 06260 { 06261 List_iterator_fast<Item> it(fields); 06262 Item *item; 06263 while ((item= it++)) 06264 item->no_rows_in_result(); 06265 result->send_data(fields); 06266 } 06267 result->send_eof(); // Should be safe 06268 } 06269 /* Update results for FOUND_ROWS */ 06270 join->thd->limit_found_rows= join->thd->examined_row_count= 0; 06271 DBUG_RETURN(0); 06272 } 06273 06274 /* 06275 used only in JOIN::clear 06276 */ 06277 static void clear_tables(JOIN *join) 06278 { 06279 /* 06280 must clear only the non-const tables, as const tables 06281 are not re-calculated. 06282 */ 06283 for (uint i=join->const_tables ; i < join->tables ; i++) 06284 mark_as_null_row(join->table[i]); // All fields are NULL 06285 } 06286 06287 /***************************************************************************** 06288 Make som simple condition optimization: 06289 If there is a test 'field = const' change all refs to 'field' to 'const' 06290 Remove all dummy tests 'item = item', 'const op const'. 06291 Remove all 'item is NULL', when item can never be null! 06292 item->marker should be 0 for all items on entry 06293 Return in cond_value FALSE if condition is impossible (1 = 2) 06294 *****************************************************************************/ 06295 06296 class COND_CMP :public ilink { 06297 public: 06298 static void *operator new(size_t size) 06299 { 06300 return (void*) sql_alloc((uint) size); 06301 } 06302 static void operator delete(void *ptr __attribute__((unused)), 06303 size_t size __attribute__((unused))) 06304 { TRASH(ptr, size); } 06305 06306 Item *and_level; 06307 Item_func *cmp_func; 06308 COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {} 06309 }; 06310 06311 #ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION 06312 template class I_List<COND_CMP>; 06313 template class I_List_iterator<COND_CMP>; 06314 template class List<Item_func_match>; 06315 template class List_iterator<Item_func_match>; 06316 #endif 06317 06318 06319 /* 06320 Find the multiple equality predicate containing a field 06321 06322 SYNOPSIS 06323 find_item_equal() 06324 cond_equal multiple equalities to search in 06325 field field to look for 06326 inherited_fl :out set up to TRUE if multiple equality is found 06327 on upper levels (not on current level of cond_equal) 06328 06329 DESCRIPTION 06330 The function retrieves the multiple equalities accessed through 06331 the con_equal structure from current level and up looking for 06332 an equality containing field. It stops retrieval as soon as the equality 06333 is found and set up inherited_fl to TRUE if it's found on upper levels. 06334 06335 RETURN 06336 Item_equal for the found multiple equality predicate if a success; 06337 NULL - otherwise. 06338 */ 06339 06340 Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, 06341 bool *inherited_fl) 06342 { 06343 Item_equal *item= 0; 06344 bool in_upper_level= FALSE; 06345 while (cond_equal) 06346 { 06347 List_iterator_fast<Item_equal> li(cond_equal->current_level); 06348 while ((item= li++)) 06349 { 06350 if (item->contains(field)) 06351 goto finish; 06352 } 06353 in_upper_level= TRUE; 06354 cond_equal= cond_equal->upper_levels; 06355 } 06356 in_upper_level= FALSE; 06357 finish: 06358 *inherited_fl= in_upper_level; 06359 return item; 06360 } 06361 06362 06363 /* 06364 Check whether an item is a simple equality predicate and if so 06365 create/find a multiple equality for this predicate 06366 06367 SYNOPSIS 06368 check_equality() 06369 item item to check 06370 cond_equal multiple equalities that must hold together with the predicate 06371 06372 DESCRIPTION 06373 This function first checks whether an item is a simple equality i.e. 06374 the one that equates a field with another field or a constant 06375 (item=constant_item or item=field_item). 06376 If this is the case the function looks a for a multiple equality 06377 in the lists referenced directly or indirectly by cond_equal inferring 06378 the given simple equality. If it doesn't find any, it builds a multiple 06379 equality that covers the predicate, i.e. the predicate can be inferred 06380 from it. 06381 The built multiple equality could be obtained in such a way: 06382 create a binary multiple equality equivalent to the predicate, then 06383 merge it, if possible, with one of old multiple equalities. 06384 This guarantees that the set of multiple equalities covering equality 06385 predicates will 06386 be minimal. 06387 06388 EXAMPLE 06389 For the where condition 06390 WHERE a=b AND b=c AND 06391 (b=2 OR f=e) 06392 the check_equality will be called for the following equality 06393 predicates a=b, b=c, b=2 and f=e. 06394 For a=b it will be called with *cond_equal=(0,[]) and will transform 06395 *cond_equal into (0,[Item_equal(a,b)]). 06396 For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)]) 06397 and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]). 06398 For b=2 it will be called with *cond_equal=(ptr(CE),[]) 06399 and will transform *cond_equal into (ptr(CE),[Item_equal(2,a,b,c)]). 06400 For f=e it will be called with *cond_equal=(ptr(CE), []) 06401 and will transform *cond_equal into (ptr(CE),[Item_equal(f,e)]). 06402 06403 NOTES 06404 Now only fields that have the same type defintions (verified by 06405 the Field::eq_def method) are placed to the same multiple equalities. 06406 Because of this some equality predicates are not eliminated and 06407 can be used in the constant propagation procedure. 06408 We could weeken the equlity test as soon as at least one of the 06409 equal fields is to be equal to a constant. It would require a 06410 more complicated implementation: we would have to store, in 06411 general case, its own constant for each fields from the multiple 06412 equality. But at the same time it would allow us to get rid 06413 of constant propagation completely: it would be done by the call 06414 to build_equal_items_for_cond. 06415 06416 IMPLEMENTATION 06417 The implementation does not follow exactly the above rules to 06418 build a new multiple equality for the equality predicate. 06419 If it processes the equality of the form field1=field2, it 06420 looks for multiple equalities me1 containig field1 and me2 containing 06421 field2. If only one of them is found the fuction expands it with 06422 the lacking field. If multiple equalities for both fields are 06423 found they are merged. If both searches fail a new multiple equality 06424 containing just field1 and field2 is added to the existing 06425 multiple equalities. 06426 If the function processes the predicate of the form field1=const, 06427 it looks for a multiple equality containing field1. If found, the 06428 function checks the constant of the multiple equality. If the value 06429 is unknown, it is setup to const. Otherwise the value is compared with 06430 const and the evaluation of the equality predicate is performed. 06431 When expanding/merging equality predicates from the upper levels 06432 the function first copies them for the current level. It looks 06433 acceptable, as this happens rarely. The implementation without 06434 copying would be much more complicated. 06435 06436 RETURN 06437 TRUE - if the predicate is a simple equality predicate 06438 FALSE - otherwise 06439 */ 06440 06441 static bool check_equality(Item *item, COND_EQUAL *cond_equal) 06442 { 06443 if (item->type() == Item::FUNC_ITEM && 06444 ((Item_func*) item)->functype() == Item_func::EQ_FUNC) 06445 { 06446 Item *left_item= ((Item_func*) item)->arguments()[0]; 06447 Item *right_item= ((Item_func*) item)->arguments()[1]; 06448 06449 if (left_item->type() == Item::REF_ITEM && 06450 ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF) 06451 { 06452 if (((Item_ref*)left_item)->depended_from) 06453 return FALSE; 06454 left_item= left_item->real_item(); 06455 } 06456 if (right_item->type() == Item::REF_ITEM && 06457 ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF) 06458 { 06459 if (((Item_ref*)right_item)->depended_from) 06460 return FALSE; 06461 right_item= right_item->real_item(); 06462 } 06463 if (left_item->type() == Item::FIELD_ITEM && 06464 right_item->type() == Item::FIELD_ITEM && 06465 !((Item_field*)left_item)->depended_from && 06466 !((Item_field*)right_item)->depended_from) 06467 { 06468 /* The predicate the form field1=field2 is processed */ 06469 06470 Field *left_field= ((Item_field*) left_item)->field; 06471 Field *right_field= ((Item_field*) right_item)->field; 06472 06473 if (!left_field->eq_def(right_field)) 06474 return FALSE; 06475 06476 if (left_field->eq(right_field)) /* f = f */ 06477 return TRUE; 06478 06479 /* Search for multiple equalities containing field1 and/or field2 */ 06480 bool left_copyfl, right_copyfl; 06481 Item_equal *left_item_equal= 06482 find_item_equal(cond_equal, left_field, &left_copyfl); 06483 Item_equal *right_item_equal= 06484 find_item_equal(cond_equal, right_field, &right_copyfl); 06485 06486 if (left_item_equal && left_item_equal == right_item_equal) 06487 { 06488 /* 06489 The equality predicate is inference of one of the existing 06490 multiple equalities, i.e the condition is already covered 06491 by upper level equalities 06492 */ 06493 return TRUE; 06494 } 06495 06496 /* Copy the found multiple equalities at the current level if needed */ 06497 if (left_copyfl) 06498 { 06499 /* left_item_equal of an upper level contains left_item */ 06500 left_item_equal= new Item_equal(left_item_equal); 06501 cond_equal->current_level.push_back(left_item_equal); 06502 } 06503 if (right_copyfl) 06504 { 06505 /* right_item_equal of an upper level contains right_item */ 06506 right_item_equal= new Item_equal(right_item_equal); 06507 cond_equal->current_level.push_back(right_item_equal); 06508 } 06509 06510 if (left_item_equal) 06511 { 06512 /* left item was found in the current or one of the upper levels */ 06513 if (! right_item_equal) 06514 left_item_equal->add((Item_field *) right_item); 06515 else 06516 { 06517 /* Merge two multiple equalities forming a new one */ 06518 left_item_equal->merge(right_item_equal); 06519 /* Remove the merged multiple equality from the list */ 06520 List_iterator<Item_equal> li(cond_equal->current_level); 06521 while ((li++) != right_item_equal); 06522 li.remove(); 06523 } 06524 } 06525 else 06526 { 06527 /* left item was not found neither the current nor in upper levels */ 06528 if (right_item_equal) 06529 right_item_equal->add((Item_field *) left_item); 06530 else 06531 { 06532 /* None of the fields was found in multiple equalities */ 06533 Item_equal *item= new Item_equal((Item_field *) left_item, 06534 (Item_field *) right_item); 06535 cond_equal->current_level.push_back(item); 06536 } 06537 } 06538 return TRUE; 06539 } 06540 06541 { 06542 /* The predicate of the form field=const/const=field is processed */ 06543 Item *const_item= 0; 06544 Item_field *field_item= 0; 06545 if (left_item->type() == Item::FIELD_ITEM && 06546 !((Item_field*)left_item)->depended_from && 06547 right_item->const_item()) 06548 { 06549 field_item= (Item_field*) left_item; 06550 const_item= right_item; 06551 } 06552 else if (right_item->type() == Item::FIELD_ITEM && 06553 !((Item_field*)right_item)->depended_from && 06554 left_item->const_item()) 06555 { 06556 field_item= (Item_field*) right_item; 06557 const_item= left_item; 06558 } 06559 if (const_item && 06560 field_item->result_type() == const_item->result_type()) 06561 { 06562 bool copyfl; 06563 06564 if (field_item->result_type() == STRING_RESULT) 06565 { 06566 CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset(); 06567 if ((cs != ((Item_cond *) item)->compare_collation()) || 06568 !cs->coll->propagate(cs, 0, 0)) 06569 return FALSE; 06570 } 06571 06572 Item_equal *item_equal = find_item_equal(cond_equal, 06573 field_item->field, ©fl); 06574 if (copyfl) 06575 { 06576 item_equal= new Item_equal(item_equal); 06577 cond_equal->current_level.push_back(item_equal); 06578 } 06579 if (item_equal) 06580 { 06581 /* 06582 The flag cond_false will be set to 1 after this, if item_equal 06583 already contains a constant and its value is not equal to 06584 the value of const_item. 06585 */ 06586 item_equal->add(const_item); 06587 } 06588 else 06589 { 06590 item_equal= new Item_equal(const_item, field_item); 06591 cond_equal->current_level.push_back(item_equal); 06592 } 06593 return TRUE; 06594 } 06595 } 06596 } 06597 return FALSE; 06598 } 06599 06600 /* 06601 Replace all equality predicates in a condition by multiple equality items 06602 06603 SYNOPSIS 06604 build_equal_items_for_cond() 06605 cond condition(expression) where to make replacement 06606 inherited path to all inherited multiple equality items 06607 06608 DESCRIPTION 06609 At each 'and' level the function detects items for equality predicates 06610 and replaced them by a set of multiple equality items of class Item_equal, 06611 taking into account inherited equalities from upper levels. 06612 If an equality predicate is used not in a conjunction it's just 06613 replaced by a multiple equality predicate. 06614 For each 'and' level the function set a pointer to the inherited 06615 multiple equalities in the cond_equal field of the associated 06616 object of the type Item_cond_and. 06617 The function also traverses the cond tree and and for each field reference 06618 sets a pointer to the multiple equality item containing the field, if there 06619 is any. If this multiple equality equates fields to a constant the 06620 function replace the field reference by the constant. 06621 The function also determines the maximum number of members in 06622 equality lists of each Item_cond_and object assigning it to 06623 cond_equal->max_members of this object and updating accordingly 06624 the upper levels COND_EQUAL structures. 06625 06626 NOTES 06627 Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of 06628 f1=f2, .., fn-1=fn. It substitutes any inference from these 06629 equality predicates that is equivalent to the conjunction. 06630 Thus, =(a1,a2,a3) can substitute for ((a1=a3) AND (a2=a3) AND (a2=a1)) as 06631 it is equivalent to ((a1=a2) AND (a2=a3)). 06632 The function always makes a substitution of all equality predicates occured 06633 in a conjuction for a minimal set of multiple equality predicates. 06634 This set can be considered as a canonical representation of the 06635 sub-conjunction of the equality predicates. 06636 E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 06637 (=(t1.a,t2.b,t3.c) AND t2.b>5), not by 06638 (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5); 06639 while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by 06640 (=(t1.a,t2.b) AND =(t3.c=t4.d) AND t2.b>5), 06641 but if additionally =(t4.d,t2.b) is inherited, it 06642 will be replaced by (=(t1.a,t2.b,t3.c,t4.d) AND t2.b>5) 06643 06644 IMPLEMENTATION 06645 The function performs the substitution in a recursive descent by 06646 the condtion tree, passing to the next AND level a chain of multiple 06647 equality predicates which have been built at the upper levels. 06648 The Item_equal items built at the level are attached to other 06649 non-equality conjucts as a sublist. The pointer to the inherited 06650 multiple equalities is saved in the and condition object (Item_cond_and). 06651 This chain allows us for any field reference occurence easyly to find a 06652 multiple equality that must be held for this occurence. 06653 For each AND level we do the following: 06654 - scan it for all equality predicate (=) items 06655 - join them into disjoint Item_equal() groups 06656 - process the included OR conditions recursively to do the same for 06657 lower AND levels. 06658 We need to do things in this order as lower AND levels need to know about 06659 all possible Item_equal objects in upper levels. 06660 06661 RETURN 06662 pointer to the transformed condition 06663 */ 06664 06665 static COND *build_equal_items_for_cond(COND *cond, 06666 COND_EQUAL *inherited) 06667 { 06668 Item_equal *item_equal; 06669 uint members; 06670 COND_EQUAL cond_equal; 06671 cond_equal.upper_levels= inherited; 06672 06673 if (cond->type() == Item::COND_ITEM) 06674 { 06675 bool and_level= ((Item_cond*) cond)->functype() == 06676 Item_func::COND_AND_FUNC; 06677 List<Item> *args= ((Item_cond*) cond)->argument_list(); 06678 06679 List_iterator<Item> li(*args); 06680 Item *item; 06681 06682 if (and_level) 06683 { 06684 /* 06685 Retrieve all conjucts of this level detecting the equality 06686 that are subject to substitution by multiple equality items and 06687 removing each such predicate from the conjunction after having 06688 found/created a multiple equality whose inference the predicate is. 06689 */ 06690 while ((item= li++)) 06691 { 06692 /* 06693 PS/SP note: we can safely remove a node from AND-OR 06694 structure here because it's restored before each 06695 re-execution of any prepared statement/stored procedure. 06696 */ 06697 if (check_equality(item, &cond_equal)) 06698 li.remove(); 06699 } 06700 06701 List_iterator_fast<Item_equal> it(cond_equal.current_level); 06702 while ((item_equal= it++)) 06703 { 06704 item_equal->fix_length_and_dec(); 06705 item_equal->update_used_tables(); 06706 members= item_equal->members(); 06707 if (cond_equal.max_members < members) 06708 cond_equal.max_members= members; 06709 } 06710 members= cond_equal.max_members; 06711 if (inherited && inherited->max_members < members) 06712 { 06713 do 06714 { 06715 inherited->max_members= members; 06716 inherited= inherited->upper_levels; 06717 } 06718 while (inherited); 06719 } 06720 06721 ((Item_cond_and*)cond)->cond_equal= cond_equal; 06722 inherited= &(((Item_cond_and*)cond)->cond_equal); 06723 } 06724 /* 06725 Make replacement of equality predicates for lower levels 06726 of the condition expression. 06727 */ 06728 li.rewind(); 06729 while ((item= li++)) 06730 { 06731 Item *new_item; 06732 if ((new_item = build_equal_items_for_cond(item, inherited))!= item) 06733 { 06734 /* This replacement happens only for standalone equalities */ 06735 /* 06736 This is ok with PS/SP as the replacement is done for 06737 arguments of an AND/OR item, which are restored for each 06738 execution of PS/SP. 06739 */ 06740 li.replace(new_item); 06741 } 06742 } 06743 if (and_level) 06744 args->concat((List<Item> *)&cond_equal.current_level); 06745 } 06746 else if (cond->type() == Item::FUNC_ITEM) 06747 { 06748 /* 06749 If an equality predicate forms the whole and level, 06750 we call it standalone equality and it's processed here. 06751 E.g. in the following where condition 06752 WHERE a=5 AND (b=5 or a=c) 06753 (b=5) and (a=c) are standalone equalities. 06754 In general we can't leave alone standalone eqalities: 06755 for WHERE a=b AND c=d AND (b=c OR d=5) 06756 b=c is replaced by =(a,b,c,d). 06757 */ 06758 if (check_equality(cond, &cond_equal) && 06759 (item_equal= cond_equal.current_level.pop())) 06760 { 06761 item_equal->fix_length_and_dec(); 06762 item_equal->update_used_tables(); 06763 return item_equal; 06764 } 06765 /* 06766 For each field reference in cond, not from equal item predicates, 06767 set a pointer to the multiple equality it belongs to (if there is any) 06768 */ 06769 cond= cond->transform(&Item::equal_fields_propagator, 06770 (byte *) inherited); 06771 cond->update_used_tables(); 06772 } 06773 return cond; 06774 } 06775 06776 06777 /* 06778 Build multiple equalities for a condition and all on expressions that 06779 inherit these multiple equalities 06780 06781 SYNOPSIS 06782 build_equal_items() 06783 thd Thread handler 06784 cond condition to build the multiple equalities for 06785 inherited path to all inherited multiple equality items 06786 join_list list of join tables to which the condition refers to 06787 cond_equal_ref :out pointer to the structure to place built equalities in 06788 06789 DESCRIPTION 06790 The function first applies the build_equal_items_for_cond function 06791 to build all multiple equalities for condition cond utilizing equalities 06792 referred through the parameter inherited. The extended set of 06793 equalities is returned in the structure referred by the cond_equal_ref 06794 parameter. After this the function calls itself recursively for 06795 all on expressions whose direct references can be found in join_list 06796 and who inherit directly the multiple equalities just having built. 06797 06798 NOTES 06799 The on expression used in an outer join operation inherits all equalities 06800 from the on expression of the embedding join, if there is any, or 06801 otherwise - from the where condition. 06802 This fact is not obvious, but presumably can be proved. 06803 Consider the following query: 06804 SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t2.a=t4.a 06805 WHERE t1.a=t2.a; 06806 If the on expression in the query inherits =(t1.a,t2.a), then we 06807 can build the multiple equality =(t1.a,t2.a,t3.a,t4.a) that infers 06808 the equality t3.a=t4.a. Although the on expression 06809 t1.a=t3.a AND t2.a=t4.a AND t3.a=t4.a is not equivalent to the one 06810 in the query the latter can be replaced by the former: the new query 06811 will return the same result set as the original one. 06812 06813 Interesting that multiple equality =(t1.a,t2.a,t3.a,t4.a) allows us 06814 to use t1.a=t3.a AND t3.a=t4.a under the on condition: 06815 SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a 06816 WHERE t1.a=t2.a 06817 This query equivalent to: 06818 SELECT * FROM (t1 LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a),t2 06819 WHERE t1.a=t2.a 06820 Similarly the original query can be rewritten to the query: 06821 SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a 06822 WHERE t1.a=t2.a 06823 that is equivalent to: 06824 SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1 06825 WHERE t1.a=t2.a 06826 Thus, applying equalities from the where condition we basically 06827 can get more freedom in performing join operations. 06828 Althogh we don't use this property now, it probably makes sense to use 06829 it in the future. 06830 06831 RETURN 06832 pointer to the transformed condition containing multiple equalities 06833 */ 06834 06835 static COND *build_equal_items(THD *thd, COND *cond, 06836 COND_EQUAL *inherited, 06837 List<TABLE_LIST> *join_list, 06838 COND_EQUAL **cond_equal_ref) 06839 { 06840 COND_EQUAL *cond_equal= 0; 06841 06842 if (cond) 06843 { 06844 cond= build_equal_items_for_cond(cond, inherited); 06845 cond->update_used_tables(); 06846 if (cond->type() == Item::COND_ITEM && 06847 ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC) 06848 cond_equal= &((Item_cond_and*) cond)->cond_equal; 06849 else if (cond->type() == Item::FUNC_ITEM && 06850 ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC) 06851 { 06852 cond_equal= new COND_EQUAL; 06853 cond_equal->current_level.push_back((Item_equal *) cond); 06854 } 06855 } 06856 if (cond_equal) 06857 { 06858 cond_equal->upper_levels= inherited; 06859 inherited= cond_equal; 06860 } 06861 *cond_equal_ref= cond_equal; 06862 06863 if (join_list) 06864 { 06865 TABLE_LIST *table; 06866 List_iterator<TABLE_LIST> li(*join_list); 06867 06868 while ((table= li++)) 06869 { 06870 if (table->on_expr) 06871 { 06872 List<TABLE_LIST> *join_list= table->nested_join ? 06873 &table->nested_join->join_list : NULL; 06874 /* 06875 We can modify table->on_expr because its old value will 06876 be restored before re-execution of PS/SP. 06877 */ 06878 table->on_expr= build_equal_items(thd, table->on_expr, inherited, 06879 join_list, &table->cond_equal); 06880 } 06881 } 06882 } 06883 06884 return cond; 06885 } 06886 06887 06888 /* 06889 Compare field items by table order in the execution plan 06890 06891 SYNOPSIS 06892 compare_fields_by_table_order() 06893 field1 first field item to compare 06894 field2 second field item to compare 06895 table_join_idx index to tables determining table order 06896 06897 DESCRIPTION 06898 field1 considered as better than field2 if the table containing 06899 field1 is accessed earlier than the table containing field2. 06900 The function finds out what of two fields is better according 06901 this criteria. 06902 06903 RETURN 06904 1, if field1 is better than field2 06905 -1, if field2 is better than field1 06906 0, otherwise 06907 */ 06908 06909 static int compare_fields_by_table_order(Item_field *field1, 06910 Item_field *field2, 06911 void *table_join_idx) 06912 { 06913 int cmp= 0; 06914 bool outer_ref= 0; 06915 if (field2->used_tables() & OUTER_REF_TABLE_BIT) 06916 { 06917 outer_ref= 1; 06918 cmp= -1; 06919 } 06920 if (field2->used_tables() & OUTER_REF_TABLE_BIT) 06921 { 06922 outer_ref= 1; 06923 cmp++; 06924 } 06925 if (outer_ref) 06926 return cmp; 06927 JOIN_TAB **idx= (JOIN_TAB **) table_join_idx; 06928 cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr]; 06929 return cmp < 0 ? -1 : (cmp ? 1 : 0); 06930 } 06931 06932 06933 /* 06934 Generate minimal set of simple equalities equivalent to a multiple equality 06935 06936 SYNOPSIS 06937 eliminate_item_equal() 06938 cond condition to add the generated equality to 06939 upper_levels structure to access multiple equality of upper levels 06940 item_equal multiple equality to generate simple equality from 06941 06942 DESCRIPTION 06943 The function retrieves the fields of the multiple equality item 06944 item_equal and for each field f: 06945 - if item_equal contains const it generates the equality f=const_item; 06946 - otherwise, if f is not the first field, generates the equality 06947 f=item_equal->get_first(). 06948 All generated equality are added to the cond conjunction. 06949 06950 NOTES 06951 Before generating an equality function checks that it has not 06952 been generated for multiple equalities of the upper levels. 06953 E.g. for the following where condition 06954 WHERE a=5 AND ((a=b AND b=c) OR c>4) 06955 the upper level AND condition will contain =(5,a), 06956 while the lower level AND condition will contain =(5,a,b,c). 06957 When splitting =(5,a,b,c) into a separate equality predicates 06958 we should omit 5=a, as we have it already in the upper level. 06959 The following where condition gives us a more complicated case: 06960 WHERE t1.a=t2.b AND t3.c=t4.d AND (t2.b=t3.c OR t4.e>5 ...) AND ... 06961 Given the tables are accessed in the order t1->t2->t3->t4 for 06962 the selected query execution plan the lower level multiple 06963 equality =(t1.a,t2.b,t3.c,t4.d) formally should be converted to 06964 t1.a=t2.b AND t1.a=t3.c AND t1.a=t4.d. But t1.a=t2.a will be 06965 generated for the upper level. Also t3.c=t4.d will be generated there. 06966 So only t1.a=t3.c should be left in the lower level. 06967 If cond is equal to 0, then not more then one equality is generated 06968 and a pointer to it is returned as the result of the function. 06969 06970 RETURN 06971 The condition with generated simple equalities or 06972 a pointer to the simple generated equality, if success. 06973 0, otherwise. 06974 */ 06975 06976 static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, 06977 Item_equal *item_equal) 06978 { 06979 List<Item> eq_list; 06980 Item_func_eq *eq_item= 0; 06981 if (((Item *) item_equal)->const_item() && !item_equal->val_int()) 06982 return new Item_int((longlong) 0,1); 06983 Item *item_const= item_equal->get_const(); 06984 Item_equal_iterator it(*item_equal); 06985 Item *head; 06986 if (item_const) 06987 head= item_const; 06988 else 06989 { 06990 head= item_equal->get_first(); 06991 it++; 06992 } 06993 Item_field *item_field; 06994 while ((item_field= it++)) 06995 { 06996 Item_equal *upper= item_field->find_item_equal(upper_levels); 06997 Item_field *item= item_field; 06998 if (upper) 06999 { 07000 if (item_const && upper->get_const()) 07001 item= 0; 07002 else 07003 { 07004 Item_equal_iterator li(*item_equal); 07005 while ((item= li++) != item_field) 07006 { 07007 if (item->find_item_equal(upper_levels) == upper) 07008 break; 07009 } 07010 } 07011 } 07012 if (item == item_field) 07013 { 07014 if (eq_item) 07015 eq_list.push_back(eq_item); 07016 eq_item= new Item_func_eq(item_field, head); 07017 if (!eq_item) 07018 return 0; 07019 eq_item->set_cmp_func(); 07020 eq_item->quick_fix_field(); 07021 } 07022 } 07023 07024 if (!cond && !eq_list.head()) 07025 { 07026 if (!eq_item) 07027 return new Item_int((longlong) 1,1); 07028 return eq_item; 07029 } 07030 07031 if (eq_item) 07032 eq_list.push_back(eq_item); 07033 if (!cond) 07034 cond= new Item_cond_and(eq_list); 07035 else 07036 { 07037 DBUG_ASSERT(cond->type() == Item::COND_ITEM); 07038 ((Item_cond *) cond)->add_at_head(&eq_list); 07039 } 07040 07041 cond->quick_fix_field(); 07042 cond->update_used_tables(); 07043 07044 return cond; 07045 } 07046 07047 07048 /* 07049 Substitute every field reference in a condition by the best equal field 07050 and eliminate all multiplle equality predicates 07051 07052 SYNOPSIS 07053 substitute_for_best_equal_field() 07054 cond condition to process 07055 cond_equal multiple equalities to take into consideration 07056 table_join_idx index to tables determining field preference 07057 07058 DESCRIPTION 07059 The function retrieves the cond condition and for each encountered 07060 multiple equality predicate it sorts the field references in it 07061 according to the order of tables specified by the table_join_idx 07062 parameter. Then it eliminates the multiple equality predicate it 07063 replacing it by the conjunction of simple equality predicates 07064 equating every field from the multiple equality to the first 07065 field in it, or to the constant, if there is any. 07066 After this the function retrieves all other conjuncted 07067 predicates substitute every field reference by the field reference 07068 to the first equal field or equal constant if there are any. 07069 07070 NOTES 07071 At the first glance full sort of fields in multiple equality 07072 seems to be an overkill. Yet it's not the case due to possible 07073 new fields in multiple equality item of lower levels. We want 07074 the order in them to comply with the order of upper levels. 07075 07076 RETURN 07077 The transformed condition 07078 */ 07079 07080 static COND* substitute_for_best_equal_field(COND *cond, 07081 COND_EQUAL *cond_equal, 07082 void *table_join_idx) 07083 { 07084 Item_equal *item_equal; 07085 07086 if (cond->type() == Item::COND_ITEM) 07087 { 07088 List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 07089 07090 bool and_level= ((Item_cond*) cond)->functype() == 07091 Item_func::COND_AND_FUNC; 07092 if (and_level) 07093 { 07094 cond_equal= &((Item_cond_and *) cond)->cond_equal; 07095 cond_list->disjoin((List<Item> *) &cond_equal->current_level); 07096 07097 List_iterator_fast<Item_equal> it(cond_equal->current_level); 07098 while ((item_equal= it++)) 07099 { 07100 item_equal->sort(&compare_fields_by_table_order, table_join_idx); 07101 } 07102 } 07103 07104 List_iterator<Item> li(*cond_list); 07105 Item *item; 07106 while ((item= li++)) 07107 { 07108 Item *new_item =substitute_for_best_equal_field(item, cond_equal, 07109 table_join_idx); 07110 /* 07111 This works OK with PS/SP re-execution as changes are made to 07112 the arguments of AND/OR items only 07113 */ 07114 if (new_item != item) 07115 li.replace(new_item); 07116 } 07117 07118 if (and_level) 07119 { 07120 List_iterator_fast<Item_equal> it(cond_equal->current_level); 07121 while ((item_equal= it++)) 07122 { 07123 cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal); 07124 // This occurs when eliminate_item_equal() founds that cond is 07125 // always false and substitutes it with Item_int 0. 07126 // Due to this, value of item_equal will be 0, so just return it. 07127 if (cond->type() != Item::COND_ITEM) 07128 break; 07129 } 07130 } 07131 } 07132 else if (cond->type() == Item::FUNC_ITEM && 07133 ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC) 07134 { 07135 item_equal= (Item_equal *) cond; 07136 item_equal->sort(&compare_fields_by_table_order, table_join_idx); 07137 if (cond_equal && cond_equal->current_level.head() == item_equal) 07138 cond_equal= 0; 07139 return eliminate_item_equal(0, cond_equal, item_equal); 07140 } 07141 else 07142 cond->transform(&Item::replace_equal_field, 0); 07143 return cond; 07144 } 07145 07146 07147 /* 07148 Check appearance of new constant items in multiple equalities 07149 of a condition after reading a constant table 07150 07151 SYNOPSIS 07152 update_const_equal_items() 07153 cond condition whose multiple equalities are to be checked 07154 table constant table that has been read 07155 07156 DESCRIPTION 07157 The function retrieves the cond condition and for each encountered 07158 multiple equality checks whether new constants have appeared after 07159 reading the constant (single row) table tab. If so it adjusts 07160 the multiple equality appropriately. 07161 */ 07162 07163 static void update_const_equal_items(COND *cond, JOIN_TAB *tab) 07164 { 07165 if (!(cond->used_tables() & tab->table->map)) 07166 return; 07167 07168 if (cond->type() == Item::COND_ITEM) 07169 { 07170 List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 07171 List_iterator_fast<Item> li(*cond_list); 07172 Item *item; 07173 while ((item= li++)) 07174 update_const_equal_items(item, tab); 07175 } 07176 else if (cond->type() == Item::FUNC_ITEM && 07177 ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC) 07178 { 07179 Item_equal *item_equal= (Item_equal *) cond; 07180 item_equal->update_const(); 07181 } 07182 } 07183 07184 07185 /* 07186 change field = field to field = const for each found field = const in the 07187 and_level 07188 */ 07189 07190 static void 07191 change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list, 07192 Item *and_father, Item *cond, 07193 Item *field, Item *value) 07194 { 07195 if (cond->type() == Item::COND_ITEM) 07196 { 07197 bool and_level= ((Item_cond*) cond)->functype() == 07198 Item_func::COND_AND_FUNC; 07199 List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); 07200 Item *item; 07201 while ((item=li++)) 07202 change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item, 07203 field, value); 07204 return; 07205 } 07206 if (cond->eq_cmp_result() == Item::COND_OK) 07207 return; // Not a boolean function 07208 07209 Item_bool_func2 *func= (Item_bool_func2*) cond; 07210 Item **args= func->arguments(); 07211 Item *left_item= args[0]; 07212 Item *right_item= args[1]; 07213 Item_func::Functype functype= func->functype(); 07214 07215 if (right_item->eq(field,0) && left_item != value && 07216 (left_item->result_type() != STRING_RESULT || 07217 value->result_type() != STRING_RESULT || 07218 left_item->collation.collation == value->collation.collation)) 07219 { 07220 Item *tmp=value->new_item(); 07221 if (tmp) 07222 { 07223 thd->change_item_tree(args + 1, tmp); 07224 func->update_used_tables(); 07225 if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) 07226 && and_father != cond && !left_item->const_item()) 07227 { 07228 cond->marker=1; 07229 COND_CMP *tmp2; 07230 if ((tmp2=new COND_CMP(and_father,func))) 07231 save_list->push_back(tmp2); 07232 } 07233 func->set_cmp_func(); 07234 } 07235 } 07236 else if (left_item->eq(field,0) && right_item != value && 07237 (right_item->result_type() != STRING_RESULT || 07238 value->result_type() != STRING_RESULT || 07239 right_item->collation.collation == value->collation.collation)) 07240 { 07241 Item *tmp=value->new_item(); 07242 if (tmp) 07243 { 07244 thd->change_item_tree(args, tmp); 07245 value= tmp; 07246 func->update_used_tables(); 07247 if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) 07248 && and_father != cond && !right_item->const_item()) 07249 { 07250 args[0]= args[1]; // For easy check 07251 thd->change_item_tree(args + 1, value); 07252 cond->marker=1; 07253 COND_CMP *tmp2; 07254 if ((tmp2=new COND_CMP(and_father,func))) 07255 save_list->push_back(tmp2); 07256 } 07257 func->set_cmp_func(); 07258 } 07259 } 07260 } 07261 07262 /* 07263 Remove additional condition inserted by IN/ALL/ANY transformation 07264 07265 SYNOPSIS 07266 remove_additional_cond() 07267 conds - condition for processing 07268 07269 RETURN VALUES 07270 new conditions 07271 */ 07272 07273 static Item *remove_additional_cond(Item* conds) 07274 { 07275 if (conds->name == in_additional_cond) 07276 return 0; 07277 if (conds->type() == Item::COND_ITEM) 07278 { 07279 Item_cond *cnd= (Item_cond*) conds; 07280 List_iterator<Item> li(*(cnd->argument_list())); 07281 Item *item; 07282 while ((item= li++)) 07283 { 07284 if (item->name == in_additional_cond) 07285 { 07286 li.remove(); 07287 if (cnd->argument_list()->elements == 1) 07288 return cnd->argument_list()->head(); 07289 return conds; 07290 } 07291 } 07292 } 07293 return conds; 07294 } 07295 07296 static void 07297 propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list, 07298 COND *and_father, COND *cond) 07299 { 07300 if (cond->type() == Item::COND_ITEM) 07301 { 07302 bool and_level= ((Item_cond*) cond)->functype() == 07303 Item_func::COND_AND_FUNC; 07304 List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); 07305 Item *item; 07306 I_List<COND_CMP> save; 07307 while ((item=li++)) 07308 { 07309 propagate_cond_constants(thd, &save,and_level ? cond : item, item); 07310 } 07311 if (and_level) 07312 { // Handle other found items 07313 I_List_iterator<COND_CMP> cond_itr(save); 07314 COND_CMP *cond_cmp; 07315 while ((cond_cmp=cond_itr++)) 07316 { 07317 Item **args= cond_cmp->cmp_func->arguments(); 07318 if (!args[0]->const_item()) 07319 change_cond_ref_to_const(thd, &save,cond_cmp->and_level, 07320 cond_cmp->and_level, args[0], args[1]); 07321 } 07322 } 07323 } 07324 else if (and_father != cond && !cond->marker) // In a AND group 07325 { 07326 if (cond->type() == Item::FUNC_ITEM && 07327 (((Item_func*) cond)->functype() == Item_func::EQ_FUNC || 07328 ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)) 07329 { 07330 Item_func_eq *func=(Item_func_eq*) cond; 07331 Item **args= func->arguments(); 07332 bool left_const= args[0]->const_item(); 07333 bool right_const= args[1]->const_item(); 07334 if (!(left_const && right_const) && 07335 args[0]->result_type() == args[1]->result_type()) 07336 { 07337 if (right_const) 07338 { 07339 resolve_const_item(thd, &args[1], args[0]); 07340 func->update_used_tables(); 07341 change_cond_ref_to_const(thd, save_list, and_father, and_father, 07342 args[0], args[1]); 07343 } 07344 else if (left_const) 07345 { 07346 resolve_const_item(thd, &args[0], args[1]); 07347 func->update_used_tables(); 07348 change_cond_ref_to_const(thd, save_list, and_father, and_father, 07349 args[1], args[0]); 07350 } 07351 } 07352 } 07353 } 07354 } 07355 07356 07357 /* 07358 Simplify joins replacing outer joins by inner joins whenever it's possible 07359 07360 SYNOPSIS 07361 simplify_joins() 07362 join reference to the query info 07363 join_list list representation of the join to be converted 07364 conds conditions to add on expressions for converted joins 07365 top true <=> conds is the where condition 07366 07367 DESCRIPTION 07368 The function, during a retrieval of join_list, eliminates those 07369 outer joins that can be converted into inner join, possibly nested. 07370 It also moves the on expressions for the converted outer joins 07371 and from inner joins to conds. 07372 The function also calculates some attributes for nested joins: 07373 - used_tables 07374 - not_null_tables 07375 - dep_tables. 07376 - on_expr_dep_tables 07377 The first two attributes are used to test whether an outer join can 07378 be substituted for an inner join. The third attribute represents the 07379 relation 'to be dependent on' for tables. If table t2 is dependent 07380 on table t1, then in any evaluated execution plan table access to 07381 table t2 must precede access to table t2. This relation is used also 07382 to check whether the query contains invalid cross-references. 07383 The forth attribute is an auxiliary one and is used to calculate 07384 dep_tables. 07385 As the attribute dep_tables qualifies possibles orders of tables in the 07386 execution plan, the dependencies required by the straight join 07387 modifiers are reflected in this attribute as well. 07388 The function also removes all braces that can be removed from the join 07389 expression without changing its meaning. 07390 07391 NOTES 07392 An outer join can be replaced by an inner join if the where condition 07393 or the on expression for an embedding nested join contains a conjunctive 07394 predicate rejecting null values for some attribute of the inner tables. 07395 07396 E.g. in the query: 07397 SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5 07398 the predicate t2.b < 5 rejects nulls. 07399 The query is converted first to: 07400 SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5 07401 then to the equivalent form: 07402 SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a. 07403 07404 Similarly the following query: 07405 SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b 07406 WHERE t2.c < 5 07407 is converted to: 07408 SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 07409 07410 One conversion might trigger another: 07411 SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a 07412 LEFT JOIN t3 ON t3.b=t2.b 07413 WHERE t3 IS NOT NULL => 07414 SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3 07415 WHERE t3 IS NOT NULL AND t3.b=t2.b => 07416 SELECT * FROM t1, t2, t3 07417 WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a 07418 07419 The function removes all unnecessary braces from the expression 07420 produced by the conversions. 07421 E.g. SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b 07422 finally is converted to: 07423 SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b 07424 07425 It also will remove braces from the following queries: 07426 SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b 07427 SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b. 07428 07429 The benefit of this simplification procedure is that it might return 07430 a query for which the optimizer can evaluate execution plan with more 07431 join orders. With a left join operation the optimizer does not 07432 consider any plan where one of the inner tables is before some of outer 07433 tables. 07434 07435 IMPLEMENTATION. 07436 The function is implemented by a recursive procedure. On the recursive 07437 ascent all attributes are calculated, all outer joins that can be 07438 converted are replaced and then all unnecessary braces are removed. 07439 As join list contains join tables in the reverse order sequential 07440 elimination of outer joins does not require extra recursive calls. 07441 07442 EXAMPLES 07443 Here is an example of a join query with invalid cross references: 07444 SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 07445 07446 RETURN VALUE 07447 The new condition, if success 07448 0, otherwise 07449 */ 07450 07451 static COND * 07452 simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top) 07453 { 07454 TABLE_LIST *table; 07455 NESTED_JOIN *nested_join; 07456 TABLE_LIST *prev_table= 0; 07457 List_iterator<TABLE_LIST> li(*join_list); 07458 DBUG_ENTER("simplify_joins"); 07459 07460 /* 07461 Try to simplify join operations from join_list. 07462 The most outer join operation is checked for conversion first. 07463 */ 07464 while ((table= li++)) 07465 { 07466 table_map used_tables; 07467 table_map not_null_tables= (table_map) 0; 07468 07469 if ((nested_join= table->nested_join)) 07470 { 07471 /* 07472 If the element of join_list is a nested join apply 07473 the procedure to its nested join list first. 07474 */ 07475 if (table->on_expr) 07476 { 07477 Item *expr= table->on_expr; 07478 /* 07479 If an on expression E is attached to the table, 07480 check all null rejected predicates in this expression. 07481 If such a predicate over an attribute belonging to 07482 an inner table of an embedded outer join is found, 07483 the outer join is converted to an inner join and 07484 the corresponding on expression is added to E. 07485 */ 07486 expr= simplify_joins(join, &nested_join->join_list, 07487 expr, FALSE); 07488 table->on_expr= expr; 07489 if (!table->prep_on_expr) 07490 table->prep_on_expr= expr->copy_andor_structure(join->thd); 07491 } 07492 nested_join->used_tables= (table_map) 0; 07493 nested_join->not_null_tables=(table_map) 0; 07494 conds= simplify_joins(join, &nested_join->join_list, conds, top); 07495 used_tables= nested_join->used_tables; 07496 not_null_tables= nested_join->not_null_tables; 07497 } 07498 else 07499 { 07500 if (!(table->prep_on_expr)) 07501 table->prep_on_expr= table->on_expr; 07502 used_tables= table->table->map; 07503 if (conds) 07504 not_null_tables= conds->not_null_tables(); 07505 } 07506 07507 if (table->embedding) 07508 { 07509 table->embedding->nested_join->used_tables|= used_tables; 07510 table->embedding->nested_join->not_null_tables|= not_null_tables; 07511 } 07512 07513 if (!table->outer_join || (used_tables & not_null_tables)) 07514 { 07515 /* 07516 For some of the inner tables there are conjunctive predicates 07517 that reject nulls => the outer join can be replaced by an inner join. 07518 */ 07519 table->outer_join= 0; 07520 if (table->on_expr) 07521 { 07522 /* Add on expression to the where condition. */ 07523 if (conds) 07524 { 07525 conds= and_conds(conds, table->on_expr); 07526 conds->top_level_item(); 07527 /* conds is always a new item as both cond and on_expr existed */ 07528 DBUG_ASSERT(!conds->fixed); 07529 conds->fix_fields(join->thd, &conds); 07530 } 07531 else 07532 conds= table->on_expr; 07533 table->prep_on_expr= table->on_expr= 0; 07534 } 07535 } 07536 07537 if (!top) 07538 continue; 07539 07540 /* 07541 Only inner tables of non-convertible outer joins 07542 remain with on_expr. 07543 */ 07544 if (table->on_expr) 07545 { 07546 table->dep_tables|= table->on_expr->used_tables(); 07547 if (table->embedding) 07548 { 07549 table->dep_tables&= ~table->embedding->nested_join->used_tables; 07550 /* 07551 Embedding table depends on tables used 07552 in embedded on expressions. 07553 */ 07554 table->embedding->on_expr_dep_tables|= table->on_expr->used_tables(); 07555 } 07556 else 07557 table->dep_tables&= ~table->table->map; 07558 } 07559 07560 if (prev_table) 07561 { 07562 /* The order of tables is reverse: prev_table follows table */ 07563 if (prev_table->straight) 07564 prev_table->dep_tables|= used_tables; 07565 if (prev_table->on_expr) 07566 { 07567 prev_table->dep_tables|= table->on_expr_dep_tables; 07568 table_map prev_used_tables= prev_table->nested_join ? 07569 prev_table->nested_join->used_tables : 07570 prev_table->table->map; 07571 /* 07572 If on expression contains only references to inner tables 07573 we still make the inner tables dependent on the outer tables. 07574 It would be enough to set dependency only on one outer table 07575 for them. Yet this is really a rare case. 07576 */ 07577 if (!(prev_table->on_expr->used_tables() & ~prev_used_tables)) 07578 prev_table->dep_tables|= used_tables; 07579 } 07580 } 07581 prev_table= table; 07582 } 07583 07584 /* Flatten nested joins that can be flattened. */ 07585 li.rewind(); 07586 while ((table= li++)) 07587 { 07588 nested_join= table->nested_join; 07589 if (nested_join && !table->on_expr) 07590 { 07591 TABLE_LIST *tbl; 07592 List_iterator<TABLE_LIST> it(nested_join->join_list); 07593 while ((tbl= it++)) 07594 { 07595 tbl->embedding= table->embedding; 07596 tbl->join_list= table->join_list; 07597 } 07598 li.replace(nested_join->join_list); 07599 } 07600 } 07601 DBUG_RETURN(conds); 07602 } 07603 07604 07605 /* 07606 Assign each nested join structure a bit in nested_join_map 07607 07608 SYNOPSIS 07609 build_bitmap_for_nested_joins() 07610 join Join being processed 07611 join_list List of tables 07612 first_unused Number of first unused bit in nested_join_map before the 07613 call 07614 07615 DESCRIPTION 07616 Assign each nested join structure (except "confluent" ones - those that 07617 embed only one element) a bit in nested_join_map. 07618 07619 NOTE 07620 This function is called after simplify_joins(), when there are no 07621 redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so 07622 we will not run out of bits in nested_join_map. 07623 07624 RETURN 07625 First unused bit in nested_join_map after the call. 07626 */ 07627 07628 static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 07629 uint first_unused) 07630 { 07631 List_iterator<TABLE_LIST> li(*join_list); 07632 TABLE_LIST *table; 07633 DBUG_ENTER("build_bitmap_for_nested_joins"); 07634 while ((table= li++)) 07635 { 07636 NESTED_JOIN *nested_join; 07637 if ((nested_join= table->nested_join)) 07638 { 07639 /* 07640 It is guaranteed by simplify_joins() function that a nested join 07641 that has only one child represents a single table VIEW (and the child 07642 is an underlying table). We don't assign bits to such nested join 07643 structures because 07644 1. it is redundant (a "sequence" of one table cannot be interleaved 07645 with anything) 07646 2. we could run out bits in nested_join_map otherwise. 07647 */ 07648 if (nested_join->join_list.elements != 1) 07649 { 07650 nested_join->nj_map= 1 << first_unused++; 07651 first_unused= build_bitmap_for_nested_joins(&nested_join->join_list, 07652 first_unused); 07653 } 07654 } 07655 } 07656 DBUG_RETURN(first_unused); 07657 } 07658 07659 07660 /* 07661 Set NESTED_JOIN::counter=0 in all nested joins in passed list 07662 07663 SYNOPSIS 07664 reset_nj_counters() 07665 join_list List of nested joins to process. It may also contain base 07666 tables which will be ignored. 07667 07668 DESCRIPTION 07669 Recursively set NESTED_JOIN::counter=0 for all nested joins contained in 07670 the passed join_list. 07671 */ 07672 07673 static void reset_nj_counters(List<TABLE_LIST> *join_list) 07674 { 07675 List_iterator<TABLE_LIST> li(*join_list); 07676 TABLE_LIST *table; 07677 DBUG_ENTER("reset_nj_counters"); 07678 while ((table= li++)) 07679 { 07680 NESTED_JOIN *nested_join; 07681 if ((nested_join= table->nested_join)) 07682 { 07683 nested_join->counter= 0; 07684 reset_nj_counters(&nested_join->join_list); 07685 } 07686 } 07687 DBUG_VOID_RETURN; 07688 } 07689 07690 07691 /* 07692 Check interleaving with an inner tables of an outer join for extension table 07693 07694 SYNOPSIS 07695 check_interleaving_with_nj() 07696 join Join being processed 07697 last_tab Last table in current partial join order (this function is 07698 not called for empty partial join orders) 07699 next_tab Table we're going to extend the current partial join with 07700 07701 DESCRIPTION 07702 Check if table next_tab can be added to current partial join order, and 07703 if yes, record that it has been added. 07704 07705 The function assumes that both current partial join order and its 07706 extension with next_tab are valid wrt table dependencies. 07707 07708 IMPLEMENTATION 07709 LIMITATIONS ON JOIN ORDER 07710 The nested [outer] joins executioner algorithm imposes these limitations 07711 on join order: 07712 1. "Outer tables first" - any "outer" table must be before any 07713 corresponding "inner" table. 07714 2. "No interleaving" - tables inside a nested join must form a continuous 07715 sequence in join order (i.e. the sequence must not be interrupted by 07716 tables that are outside of this nested join). 07717 07718 #1 is checked elsewhere, this function checks #2 provided that #1 has 07719 been already checked. 07720 07721 WHY NEED NON-INTERLEAVING 07722 Consider an example: 07723 07724 select * from t0 join t1 left join (t2 join t3) on cond1 07725 07726 The join order "t1 t2 t0 t3" is invalid: 07727 07728 table t0 is outside of the nested join, so WHERE condition for t0 is 07729 attached directly to t0 (without triggers, and it may be used to access 07730 t0). Applying WHERE(t0) to (t2,t0,t3) record is invalid as we may miss 07731 combinations of (t1, t2, t3) that satisfy condition cond1, and produce a 07732 null-complemented (t1, t2.NULLs, t3.NULLs) row, which should not have 07733 been produced. 07734 07735 If table t0 is not between t2 and t3, the problem doesn't exist: 07736 * If t0 is located after (t2,t3), WHERE(t0) is applied after nested join 07737 processing has finished. 07738 * If t0 is located before (t2,t3), predicates like WHERE_cond(t0, t2) are 07739 wrapped into condition triggers, which takes care of correct nested 07740 join processing. 07741 07742 HOW IT IS IMPLEMENTED 07743 The limitations on join order can be rephrased as follows: for valid 07744 join order one must be able to: 07745 1. write down the used tables in the join order on one line. 07746 2. for each nested join, put one '(' and one ')' on the said line 07747 3. write "LEFT JOIN" and "ON (...)" where appropriate 07748 4. get a query equivalent to the query we're trying to execute. 07749 07750 Calls to check_interleaving_with_nj() are equivalent to writing the 07751 above described line from left to right. 07752 A single check_interleaving_with_nj(A,B) call is equivalent to writing 07753 table B and appropriate brackets on condition that table A and 07754 appropriate brackets is the last what was written. Graphically the 07755 transition is as follows: 07756 07757 +---- current position 07758 | 07759 ... last_tab ))) | ( next_tab ) )..) | ... 07760 X Y Z | 07761 +- need to move to this 07762 position. 07763 07764 Notes about the position: 07765 The caller guarantees that there is no more then one X-bracket by 07766 checking "!(remaining_tables & s->dependent)" before calling this 07767 function. X-bracket may have a pair in Y-bracket. 07768 07769 When "writing" we store/update this auxilary info about the current 07770 position: 07771 1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested 07772 joins) we've opened but didn't close. 07773 2. {each NESTED_JOIN structure not simplified away}->counter - number 07774 of this nested join's children that have already been added to to 07775 the partial join order. 07776 07777 RETURN 07778 FALSE Join order extended, nested joins info about current join order 07779 (see NOTE section) updated. 07780 TRUE Requested join order extension not allowed. 07781 */ 07782 07783 static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab) 07784 { 07785 TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding; 07786 JOIN *join= last_tab->join; 07787 07788 if (join->cur_embedding_map & ~next_tab->embedding_map) 07789 { 07790 /* 07791 next_tab is outside of the "pair of brackets" we're currently in. 07792 Cannot add it. 07793 */ 07794 return TRUE; 07795 } 07796 07797 /* 07798 Do update counters for "pairs of brackets" that we've left (marked as 07799 X,Y,Z in the above picture) 07800 */ 07801 for (;next_emb; next_emb= next_emb->embedding) 07802 { 07803 next_emb->nested_join->counter++; 07804 if (next_emb->nested_join->counter == 1) 07805 { 07806 /* 07807 next_emb is the first table inside a nested join we've "entered". In 07808 the picture above, we're looking at the 'X' bracket. Don't exit yet as 07809 X bracket might have Y pair bracket. 07810 */ 07811 join->cur_embedding_map |= next_emb->nested_join->nj_map; 07812 } 07813 07814 if (next_emb->nested_join->join_list.elements != 07815 next_emb->nested_join->counter) 07816 break; 07817 07818 /* 07819 We're currently at Y or Z-bracket as depicted in the above picture. 07820 Mark that we've left it and continue walking up the brackets hierarchy. 07821 */ 07822 join->cur_embedding_map &= ~next_emb->nested_join->nj_map; 07823 } 07824 return FALSE; 07825 } 07826 07827 07828 /* 07829 Nested joins perspective: Remove the last table from the join order 07830 07831 SYNOPSIS 07832 restore_prev_nj_state() 07833 last join table to remove, it is assumed to be the last in current 07834 partial join order. 07835 07836 DESCRIPTION 07837 Remove the last table from the partial join order and update the nested 07838 joins counters and join->cur_embedding_map. It is ok to call this 07839 function for the first table in join order (for which 07840 check_interleaving_with_nj has not been called) 07841 */ 07842 07843 static void restore_prev_nj_state(JOIN_TAB *last) 07844 { 07845 TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding; 07846 JOIN *join= last->join; 07847 while (last_emb && !(--last_emb->nested_join->counter)) 07848 { 07849 join->cur_embedding_map &= last_emb->nested_join->nj_map; 07850 last_emb= last_emb->embedding; 07851 } 07852 } 07853 07854 07855 static COND * 07856 optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list, 07857 Item::cond_result *cond_value) 07858 { 07859 THD *thd= join->thd; 07860 SELECT_LEX *select= thd->lex->current_select; 07861 DBUG_ENTER("optimize_cond"); 07862 07863 if (!conds) 07864 *cond_value= Item::COND_TRUE; 07865 else 07866 { 07867 /* 07868 Build all multiple equality predicates and eliminate equality 07869 predicates that can be inferred from these multiple equalities. 07870 For each reference of a field included into a multiple equality 07871 that occurs in a function set a pointer to the multiple equality 07872 predicate. Substitute a constant instead of this field if the 07873 multiple equality contains a constant. 07874 */ 07875 DBUG_EXECUTE("where", print_where(conds, "original");); 07876 conds= build_equal_items(join->thd, conds, NULL, join_list, 07877 &join->cond_equal); 07878 DBUG_EXECUTE("where",print_where(conds,"after equal_items");); 07879 07880 /* change field = field to field = const for each found field = const */ 07881 propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds); 07882 /* 07883 Remove all instances of item == item 07884 Remove all and-levels where CONST item != CONST item 07885 */ 07886 DBUG_EXECUTE("where",print_where(conds,"after const change");); 07887 conds= remove_eq_conds(thd, conds, cond_value) ; 07888 DBUG_EXECUTE("info",print_where(conds,"after remove");); 07889 } 07890 DBUG_RETURN(conds); 07891 } 07892 07893 07894 /* 07895 Remove const and eq items. Return new item, or NULL if no condition 07896 cond_value is set to according: 07897 COND_OK query is possible (field = constant) 07898 COND_TRUE always true ( 1 = 1 ) 07899 COND_FALSE always false ( 1 = 2 ) 07900 */ 07901 07902 COND * 07903 remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value) 07904 { 07905 if (cond->type() == Item::COND_ITEM) 07906 { 07907 bool and_level= ((Item_cond*) cond)->functype() 07908 == Item_func::COND_AND_FUNC; 07909 List_iterator<Item> li(*((Item_cond*) cond)->argument_list()); 07910 Item::cond_result tmp_cond_value; 07911 bool should_fix_fields=0; 07912 07913 *cond_value=Item::COND_UNDEF; 07914 Item *item; 07915 while ((item=li++)) 07916 { 07917 Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value); 07918 if (!new_item) 07919 li.remove(); 07920 else if (item != new_item) 07921 { 07922 VOID(li.replace(new_item)); 07923 should_fix_fields=1; 07924 } 07925 if (*cond_value == Item::COND_UNDEF) 07926 *cond_value=tmp_cond_value; 07927 switch (tmp_cond_value) { 07928 case Item::COND_OK: // Not TRUE or FALSE 07929 if (and_level || *cond_value == Item::COND_FALSE) 07930 *cond_value=tmp_cond_value; 07931 break; 07932 case Item::COND_FALSE: 07933 if (and_level) 07934 { 07935 *cond_value=tmp_cond_value; 07936 return (COND*) 0; // Always false 07937 } 07938 break; 07939 case Item::COND_TRUE: 07940 if (!and_level) 07941 { 07942 *cond_value= tmp_cond_value; 07943 return (COND*) 0; // Always true 07944 } 07945 break; 07946 case Item::COND_UNDEF: // Impossible 07947 break; /* purecov: deadcode */ 07948 } 07949 } 07950 if (should_fix_fields) 07951 cond->update_used_tables(); 07952 07953 if (!((Item_cond*) cond)->argument_list()->elements || 07954 *cond_value != Item::COND_OK) 07955 return (COND*) 0; 07956 if (((Item_cond*) cond)->argument_list()->elements == 1) 07957 { // Remove list 07958 item= ((Item_cond*) cond)->argument_list()->head(); 07959 ((Item_cond*) cond)->argument_list()->empty(); 07960 return item; 07961 } 07962 } 07963 else if (cond->type() == Item::FUNC_ITEM && 07964 ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC) 07965 { 07966 /* 07967 Handles this special case for some ODBC applications: 07968 The are requesting the row that was just updated with a auto_increment 07969 value with this construct: 07970 07971 SELECT * from table_name where auto_increment_column IS NULL 07972 This will be changed to: 07973 SELECT * from table_name where auto_increment_column = LAST_INSERT_ID 07974 */ 07975 07976 Item_func_isnull *func=(Item_func_isnull*) cond; 07977 Item **args= func->arguments(); 07978 if (args[0]->type() == Item::FIELD_ITEM) 07979 { 07980 Field *field=((Item_field*) args[0])->field; 07981 if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null && 07982 (thd->options & OPTION_AUTO_IS_NULL) && 07983 (thd->first_successful_insert_id_in_prev_stmt > 0 && 07984 thd->substitute_null_with_insert_id)) 07985 { 07986 #ifdef HAVE_QUERY_CACHE 07987 query_cache_abort(&thd->net); 07988 #endif 07989 COND *new_cond; 07990 if ((new_cond= new Item_func_eq(args[0], 07991 new Item_int("last_insert_id()", 07992 thd->read_first_successful_insert_id_in_prev_stmt(), 07993 21)))) 07994 { 07995 cond=new_cond; 07996 /* 07997 Item_func_eq can't be fixed after creation so we do not check 07998 cond->fixed, also it do not need tables so we use 0 as second 07999 argument. 08000 */ 08001 cond->fix_fields(thd, &cond); 08002 } 08003 /* 08004 IS NULL should be mapped to LAST_INSERT_ID only for first row, so 08005 clear for next row 08006 */ 08007 thd->substitute_null_with_insert_id= FALSE; 08008 } 08009 /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */ 08010 else if (((field->type() == FIELD_TYPE_DATE) || 08011 (field->type() == FIELD_TYPE_DATETIME)) && 08012 (field->flags & NOT_NULL_FLAG) && 08013 !field->table->maybe_null) 08014 { 08015 COND *new_cond; 08016 if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2)))) 08017 { 08018 cond=new_cond; 08019 /* 08020 Item_func_eq can't be fixed after creation so we do not check 08021 cond->fixed, also it do not need tables so we use 0 as second 08022 argument. 08023 */ 08024 cond->fix_fields(thd, &cond); 08025 } 08026 } 08027 } 08028 if (cond->const_item()) 08029 { 08030 *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; 08031 return (COND*) 0; 08032 } 08033 } 08034 else if (cond->const_item()) 08035 { 08036 *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE; 08037 return (COND*) 0; 08038 } 08039 else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK) 08040 { // boolan compare function 08041 Item *left_item= ((Item_func*) cond)->arguments()[0]; 08042 Item *right_item= ((Item_func*) cond)->arguments()[1]; 08043 if (left_item->eq(right_item,1)) 08044 { 08045 if (!left_item->maybe_null || 08046 ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC) 08047 return (COND*) 0; // Compare of identical items 08048 } 08049 } 08050 *cond_value=Item::COND_OK; 08051 return cond; // Point at next and level 08052 } 08053 08054 /* 08055 Return 1 if the item is a const value in all the WHERE clause 08056 */ 08057 08058 static bool 08059 const_expression_in_where(COND *cond, Item *comp_item, Item **const_item) 08060 { 08061 if (cond->type() == Item::COND_ITEM) 08062 { 08063 bool and_level= (((Item_cond*) cond)->functype() 08064 == Item_func::COND_AND_FUNC); 08065 List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list()); 08066 Item *item; 08067 while ((item=li++)) 08068 { 08069 bool res=const_expression_in_where(item, comp_item, const_item); 08070 if (res) // Is a const value 08071 { 08072 if (and_level) 08073 return 1; 08074 } 08075 else if (!and_level) 08076 return 0; 08077 } 08078 return and_level ? 0 : 1; 08079 } 08080 else if (cond->eq_cmp_result() != Item::COND_OK) 08081 { // boolan compare function 08082 Item_func* func= (Item_func*) cond; 08083 if (func->functype() != Item_func::EQUAL_FUNC && 08084 func->functype() != Item_func::EQ_FUNC) 08085 return 0; 08086 Item *left_item= ((Item_func*) cond)->arguments()[0]; 08087 Item *right_item= ((Item_func*) cond)->arguments()[1]; 08088 if (left_item->eq(comp_item,1)) 08089 { 08090 if (right_item->const_item()) 08091 { 08092 if (*const_item) 08093 return right_item->eq(*const_item, 1); 08094 *const_item=right_item; 08095 return 1; 08096 } 08097 } 08098 else if (right_item->eq(comp_item,1)) 08099 { 08100 if (left_item->const_item()) 08101 { 08102 if (*const_item) 08103 return left_item->eq(*const_item, 1); 08104 *const_item=left_item; 08105 return 1; 08106 } 08107 } 08108 } 08109 return 0; 08110 } 08111 08112 /**************************************************************************** 08113 Create internal temporary table 08114 ****************************************************************************/ 08115 08116 /* 08117 Create field for temporary table from given field 08118 08119 SYNOPSIS 08120 create_tmp_field_from_field() 08121 thd Thread handler 08122 org_field field from which new field will be created 08123 name New field name 08124 table Temporary table 08125 item !=NULL if item->result_field should point to new field. 08126 This is relevant for how fill_record() is going to work: 08127 If item != NULL then fill_record() will update 08128 the record in the original table. 08129 If item == NULL then fill_record() will update 08130 the temporary table 08131 convert_blob_length If >0 create a varstring(convert_blob_length) field 08132 instead of blob. 08133 08134 RETURN 08135 0 on error 08136 new_created field 08137 */ 08138 08139 Field *create_tmp_field_from_field(THD *thd, Field *org_field, 08140 const char *name, TABLE *table, 08141 Item_field *item, uint convert_blob_length) 08142 { 08143 Field *new_field; 08144 08145 /* 08146 Make sure that the blob fits into a Field_varstring which has 08147 2-byte lenght. 08148 */ 08149 if (convert_blob_length && convert_blob_length < UINT_MAX16 && 08150 (org_field->flags & BLOB_FLAG)) 08151 new_field= new Field_varstring(convert_blob_length, 08152 org_field->maybe_null(), 08153 org_field->field_name, table->s, 08154 org_field->charset()); 08155 else 08156 new_field= org_field->new_field(thd->mem_root, table, 08157 table == org_field->table); 08158 if (new_field) 08159 { 08160 new_field->init(table); 08161 new_field->orig_table= org_field->orig_table; 08162 if (item) 08163 item->result_field= new_field; 08164 else 08165 new_field->field_name= name; 08166 new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG); 08167 if (org_field->maybe_null() || (item && item->maybe_null)) 08168 new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join 08169 if (org_field->type() == MYSQL_TYPE_VAR_STRING || 08170 org_field->type() == MYSQL_TYPE_VARCHAR) 08171 table->s->db_create_options|= HA_OPTION_PACK_RECORD; 08172 } 08173 return new_field; 08174 } 08175 08176 /* 08177 Create field for temporary table using type of given item 08178 08179 SYNOPSIS 08180 create_tmp_field_from_item() 08181 thd Thread handler 08182 item Item to create a field for 08183 table Temporary table 08184 copy_func If set and item is a function, store copy of item 08185 in this array 08186 modify_item 1 if item->result_field should point to new item. 08187 This is relevent for how fill_record() is going to 08188 work: 08189 If modify_item is 1 then fill_record() will update 08190 the record in the original table. 08191 If modify_item is 0 then fill_record() will update 08192 the temporary table 08193 convert_blob_length If >0 create a varstring(convert_blob_length) field 08194 instead of blob. 08195 08196 RETURN 08197 0 on error 08198 new_created field 08199 */ 08200 08201 static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table, 08202 Item ***copy_func, bool modify_item, 08203 uint convert_blob_length) 08204 { 08205 bool maybe_null= item->maybe_null; 08206 Field *new_field; 08207 LINT_INIT(new_field); 08208 08209 switch (item->result_type()) { 08210 case REAL_RESULT: 08211 new_field= new Field_double(i

