1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20
mysql_select and join optimization
23
@defgroup Query_Optimizer Query Optimizer
27
#ifdef USE_PRAGMA_IMPLEMENTATION
28
#pragma implementation // gcc: Class implementation
31
#include "mysql_priv.h"
32
#include "sql_select.h"
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
39
"MAYBE_REF","ALL","range","index",
40
"ref_or_null","unique_subquery","index_subquery",
44
struct st_sargable_param;
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
48
DYNAMIC_ARRAY *keyuse);
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
51
uint tables, COND *conds,
52
COND_EQUAL *cond_equal,
53
table_map table_map, SELECT_LEX *select_lex,
54
st_sargable_param **sargables);
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
58
table_map used_tables);
59
static bool choose_plan(JOIN *join,table_map join_tables);
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
62
table_map remaining_tables, uint idx,
63
double record_count, double read_time);
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
66
uint depth, uint prune_level);
67
static bool best_extension_by_limited_search(JOIN *join,
68
table_map remaining_tables,
69
uint idx, double record_count,
70
double read_time, uint depth,
72
static uint determine_search_depth(JOIN* join);
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
76
TODO: 'find_best' is here only temporarily until 'greedy_search' is
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
80
double record_count,double read_time);
81
static uint cache_record_length(JOIN *join,uint index);
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
83
static bool get_best_combination(JOIN *join);
84
static store_key *get_store_key(THD *thd,
85
KEYUSE *keyuse, table_map used_tables,
86
KEY_PART_INFO *key_part, uchar *key_buff,
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
89
static void make_outerjoin_info(JOIN *join);
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
91
static bool make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after);
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
93
static void update_depend_map(JOIN *join);
94
static void update_depend_map(JOIN *join, ORDER *order);
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
96
bool change_list, bool *simple_order);
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
98
List<Item> &fields, bool send_row,
99
ulonglong select_options, const char *info,
101
static COND *build_equal_items(THD *thd, COND *cond,
102
COND_EQUAL *inherited,
103
List<TABLE_LIST> *join_list,
104
COND_EQUAL **cond_equal_ref);
105
static COND* substitute_for_best_equal_field(COND *cond,
106
COND_EQUAL *cond_equal,
107
void *table_join_idx);
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
109
COND *conds, bool top, bool in_sj);
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
111
static void restore_prev_nj_state(JOIN_TAB *last);
112
static void reset_nj_counters(List<TABLE_LIST> *join_list);
113
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
118
static void restore_prev_sj_state(const table_map remaining_tables,
119
const JOIN_TAB *tab);
121
static COND *optimize_cond(JOIN *join, COND *conds,
122
List<TABLE_LIST> *join_list,
123
Item::cond_result *cond_value);
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
125
static bool open_tmp_table(TABLE *table);
126
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
127
MI_COLUMNDEF *start_recinfo,
128
MI_COLUMNDEF **recinfo,
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
132
static enum_nested_loop_state
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
135
static enum_nested_loop_state
136
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
137
static enum_nested_loop_state
138
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
139
static enum_nested_loop_state
140
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
141
static enum_nested_loop_state
142
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
143
static enum_nested_loop_state
144
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
145
static enum_nested_loop_state
146
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
148
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
149
static int join_read_system(JOIN_TAB *tab);
150
static int join_read_const(JOIN_TAB *tab);
151
static int join_read_key(JOIN_TAB *tab);
152
static int join_read_always_key(JOIN_TAB *tab);
153
static int join_read_last_key(JOIN_TAB *tab);
154
static int join_no_more_records(READ_RECORD *info);
155
static int join_read_next(READ_RECORD *info);
156
static int join_read_next_different(READ_RECORD *info);
157
static int join_init_quick_read_record(JOIN_TAB *tab);
158
static int test_if_quick_select(JOIN_TAB *tab);
159
static int join_init_read_record(JOIN_TAB *tab);
160
static int join_read_first(JOIN_TAB *tab);
161
static int join_read_next_same(READ_RECORD *info);
162
static int join_read_next_same_diff(READ_RECORD *info);
163
static int join_read_last(JOIN_TAB *tab);
164
static int join_read_prev_same(READ_RECORD *info);
165
static int join_read_prev(READ_RECORD *info);
166
int join_read_always_key_or_null(JOIN_TAB *tab);
167
int join_read_next_same_or_null(READ_RECORD *info);
168
static COND *make_cond_for_table(COND *cond,table_map table,
169
table_map used_table,
170
bool exclude_expensive_cond);
171
static Item* part_of_refkey(TABLE *form,Field *field);
172
uint find_shortest_key(TABLE *table, const key_map *usable_keys);
173
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
174
ha_rows select_limit, bool no_changes,
176
static bool list_contains_unique_index(TABLE *table,
177
bool (*find_func) (Field *, void *), void *data);
178
static bool find_field_in_item_list (Field *field, void *data);
179
static bool find_field_in_order_list (Field *field, void *data);
180
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
181
ha_rows filesort_limit, ha_rows select_limit,
183
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
185
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
186
ulong offset,Item *having);
187
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
188
uint field_count, Field **first_field,
190
ulong key_length,Item *having);
191
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
192
static ulong used_blob_length(CACHE_FIELD **ptr);
193
static bool store_record_in_cache(JOIN_CACHE *cache);
194
static void reset_cache_read(JOIN_CACHE *cache);
195
static void reset_cache_write(JOIN_CACHE *cache);
196
static void read_cached_record(JOIN_TAB *tab);
197
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
198
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
199
ORDER *order, List<Item> &fields,
200
List<Item> &all_fields,
201
bool *all_order_by_fields_used);
202
static bool test_if_subpart(ORDER *a,ORDER *b);
203
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
204
static void calc_group_buffer(JOIN *join,ORDER *group);
205
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
206
static bool alloc_group_fields(JOIN *join,ORDER *group);
207
// Create list for using with tempory table
208
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
209
List<Item> &new_list1,
210
List<Item> &new_list2,
211
uint elements, List<Item> &items);
212
// Create list for using with tempory table
213
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
214
List<Item> &new_list1,
215
List<Item> &new_list2,
216
uint elements, List<Item> &items);
217
static void init_tmptable_sum_functions(Item_sum **func);
218
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
219
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
220
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
221
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
222
static bool init_sum_functions(Item_sum **func, Item_sum **end);
223
static bool update_sum_func(Item_sum **func);
224
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
225
bool distinct, const char *message=NullS);
226
static Item *remove_additional_cond(Item* conds);
227
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
228
static bool test_if_ref(Item_field *left_item,Item *right_item);
229
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
230
Item *new_cond, bool fix_fields);
233
This is used to mark equalities that were made from i-th IN-equality.
234
We limit semi-join InsideOut optimization to handling max 64 inequalities,
235
The following variable occupies 64 addresses.
237
const char *subq_sj_cond_name=
238
"0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
240
static bool bitmap_covers(const table_map x, const table_map y)
242
return !test(y & ~x);
246
This handles SELECT with and without UNION.
249
bool handle_select(THD *thd, LEX *lex, select_result *result,
250
ulong setup_tables_done_option)
253
register SELECT_LEX *select_lex = &lex->select_lex;
254
DBUG_ENTER("handle_select");
255
MYSQL_SELECT_START();
257
if (select_lex->master_unit()->is_union() ||
258
select_lex->master_unit()->fake_select_lex)
259
res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
262
SELECT_LEX_UNIT *unit= &lex->unit;
263
unit->set_limit(unit->global_parameters);
266
'options' of mysql_select will be set in JOIN, as far as JOIN for
267
every PS/SP execution new, we will not need reset this flag if
268
setup_tables_done_option changed for next rexecution
270
res= mysql_select(thd, &select_lex->ref_pointer_array,
271
(TABLE_LIST*) select_lex->table_list.first,
272
select_lex->with_wild, select_lex->item_list,
274
select_lex->order_list.elements +
275
select_lex->group_list.elements,
276
(ORDER*) select_lex->order_list.first,
277
(ORDER*) select_lex->group_list.first,
279
(ORDER*) lex->proc_list.first,
280
select_lex->options | thd->options |
281
setup_tables_done_option,
282
result, unit, select_lex);
284
DBUG_PRINT("info",("res: %d report_error: %d", res,
286
res|= thd->is_error();
296
Fix fields referenced from inner selects.
301
all_fields List of all fields used in select
302
select Current select
303
ref_pointer_array Array of references to Items used in current select
306
The function serves 3 purposes - adds fields referenced from inner
307
selects to the current select list, resolves which class to use
308
to access referenced item (Item_ref of Item_direct_ref) and fixes
309
references (Item_ref objects) to these fields.
311
If a field isn't already in the select list and the ref_pointer_array
312
is provided then it is added to the all_fields list and the pointer to
313
it is saved in the ref_pointer_array.
315
The class to access the outer field is determined by the following rules:
316
1. If the outer field isn't used under an aggregate function
317
then the Item_ref class should be used.
318
2. If the outer field is used under an aggregate function and this
319
function is aggregated in the select where the outer field was
320
resolved or in some more inner select then the Item_direct_ref
321
class should be used.
322
The resolution is done here and not at the fix_fields() stage as
323
it can be done only after sum functions are fixed and pulled up to
324
selects where they are have to be aggregated.
325
When the class is chosen it substitutes the original field in the
326
Item_outer_ref object.
328
After this we proceed with fixing references (Item_outer_ref objects) to
329
this field from inner subqueries.
332
TRUE an error occured
337
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
338
Item **ref_pointer_array)
342
bool direct_ref= FALSE;
344
List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
345
while ((ref= ref_it++))
347
Item *item= ref->outer_ref;
348
Item **item_ref= ref->ref;
351
TODO: this field item already might be present in the select list.
352
In this case instead of adding new field item we could use an
353
existing one. The change will lead to less operations for copying fields,
354
smaller temporary tables and less data passed through filesort.
356
if (ref_pointer_array && !ref->found_in_select_list)
358
int el= all_fields.elements;
359
ref_pointer_array[el]= item;
360
/* Add the field item to the select list of the current select. */
361
all_fields.push_front(item);
363
If it's needed reset each Item_ref item that refers this field with
364
a new reference taken from ref_pointer_array.
366
item_ref= ref_pointer_array + el;
369
if (ref->in_sum_func)
372
if (ref->in_sum_func->nest_level > select->nest_level)
376
for (sum_func= ref->in_sum_func; sum_func &&
377
sum_func->aggr_level >= select->nest_level;
378
sum_func= sum_func->in_sum_func)
380
if (sum_func->aggr_level == select->nest_level)
388
new_ref= direct_ref ?
389
new Item_direct_ref(ref->context, item_ref, ref->table_name,
390
ref->field_name, ref->alias_name_used) :
391
new Item_ref(ref->context, item_ref, ref->table_name,
392
ref->field_name, ref->alias_name_used);
395
ref->outer_ref= new_ref;
396
ref->ref= &ref->outer_ref;
398
if (!ref->fixed && ref->fix_fields(thd, 0))
400
thd->used_tables|= item->used_tables();
405
#define MAGIC_IN_WHERE_TOP_LEVEL 10
407
Function to setup clauses without sum functions.
409
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
413
List<Item> &all_fields,
416
ORDER *group, bool *hidden_group_fields)
419
nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
420
DBUG_ENTER("setup_without_group");
422
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
423
res= setup_conds(thd, tables, leaves, conds);
425
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
426
res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
428
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
429
res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
430
group, hidden_group_fields);
431
thd->lex->allow_sum_func= save_allow_sum_func;
435
/*****************************************************************************
436
Check fields, find best join, do the select and output fields.
437
mysql_select assumes that all tables are already opened
438
*****************************************************************************/
441
Prepare of whole select (including sub queries in future).
444
Add check of calculation of GROUP functions and fields:
445
SELECT COUNT(*)+table.col1 from table1;
453
JOIN::prepare(Item ***rref_pointer_array,
454
TABLE_LIST *tables_init,
455
uint wild_num, COND *conds_init, uint og_num,
456
ORDER *order_init, ORDER *group_init,
458
ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
459
SELECT_LEX_UNIT *unit_arg)
461
DBUG_ENTER("JOIN::prepare");
463
// to prevent double initialization on EXPLAIN
469
group_list= group_init;
471
proc_param= proc_param_init;
472
tables_list= tables_init;
473
select_lex= select_lex_arg;
474
select_lex->join= this;
475
join_list= &select_lex->top_join_list;
476
union_part= unit_arg->is_union();
478
thd->lex->current_select->is_item_list_lookup= 1;
480
If we have already executed SELECT, then it have not sense to prevent
481
its table from update (see unique_table())
483
if (thd->derived_tables_processing)
484
select_lex->exclude_from_table_unique_test= TRUE;
486
/* Check that all tables, fields, conds and order are ok */
488
if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
489
setup_tables_and_check_access(thd, &select_lex->context, join_list,
490
tables_list, &select_lex->leaf_tables,
494
TABLE_LIST *table_ptr;
495
for (table_ptr= select_lex->leaf_tables;
497
table_ptr= table_ptr->next_leaf)
500
if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
501
select_lex->setup_ref_array(thd, og_num) ||
502
setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
504
setup_without_group(thd, (*rref_pointer_array), tables_list,
505
select_lex->leaf_tables, fields_list,
506
all_fields, &conds, order, group_list,
507
&hidden_group_fields))
508
DBUG_RETURN(-1); /* purecov: inspected */
510
ref_pointer_array= *rref_pointer_array;
514
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
515
thd->where="having clause";
516
thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
517
select_lex->having_fix_field= 1;
518
bool having_fix_rc= (!having->fixed &&
519
(having->fix_fields(thd, &having) ||
520
having->check_cols(1)));
521
select_lex->having_fix_field= 0;
522
if (having_fix_rc || thd->is_error())
523
DBUG_RETURN(-1); /* purecov: inspected */
524
thd->lex->allow_sum_func= save_allow_sum_func;
527
if (!thd->lex->view_prepare_mode)
529
Item_subselect *subselect;
530
Item_in_subselect *in_subs= NULL;
532
Are we in a subquery predicate?
533
TODO: the block below will be executed for every PS execution without need.
535
if ((subselect= select_lex->master_unit()->item))
537
bool do_semijoin= !test(thd->variables.optimizer_switch &
538
OPTIMIZER_SWITCH_NO_SEMIJOIN);
539
if (subselect->substype() == Item_subselect::IN_SUBS)
540
in_subs= (Item_in_subselect*)subselect;
542
DBUG_PRINT("info", ("Checking if subq can be converted to semi-join"));
544
Check if we're in subquery that is a candidate for flattening into a
545
semi-join (which is done done in flatten_subqueries()). The
547
1. Subquery predicate is an IN/=ANY subq predicate
548
2. Subquery is a single SELECT (not a UNION)
549
3. Subquery does not have GROUP BY or ORDER BY
550
4. Subquery does not use aggregate functions or HAVING
551
5. Subquery predicate is at the AND-top-level of ON/WHERE clause
552
6. No execution method was already chosen (by a prepared statement).
554
(*). We are not in a subquery of a single table UPDATE/DELETE that
555
doesn't have a JOIN (TODO: We should handle this at some
556
point by switching to multi-table UPDATE/DELETE)
558
(**). We're not in a confluent table-less subquery, like
562
!select_lex->master_unit()->first_select()->next_select() && // 2
563
!select_lex->group_list.elements && !order && // 3
564
!having && !select_lex->with_sum_func && // 4
565
thd->thd_marker && // 5
566
select_lex->outer_select()->join && // (*)
567
select_lex->master_unit()->first_select()->leaf_tables && // (**)
569
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
571
DBUG_PRINT("info", ("Subquery is semi-join conversion candidate"));
573
if (!in_subs->left_expr->fixed &&
574
in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
579
Check that the right part of the subselect contains no more than one
580
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
582
if (subselect->substype() == Item_subselect::IN_SUBS &&
583
(select_lex->item_list.elements !=
584
((Item_in_subselect*)subselect)->left_expr->cols()))
586
my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
591
/* Register the subquery for further processing */
592
select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
593
in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
597
DBUG_PRINT("info", ("Subquery can't be converted to semi-join"));
598
bool do_materialize= !test(thd->variables.optimizer_switch &
599
OPTIMIZER_SWITCH_NO_MATERIALIZATION);
601
Check if the subquery predicate can be executed via materialization.
602
The required conditions are:
603
1. Subquery predicate is an IN/=ANY subq predicate
604
2. Subquery is a single SELECT (not a UNION)
605
3. Subquery is not a table-less query. In this case there is no
606
point in materializing.
607
4. Subquery predicate is a top-level predicate
608
(this implies it is not negated)
609
TODO: this is a limitation that should be lifeted once we
610
implement correct NULL semantics (WL#3830)
611
5. Subquery is non-correlated
613
This is an overly restrictive condition. It can be extended to:
614
(Subquery is non-correlated ||
615
Subquery is correlated to any query outer to IN predicate ||
616
(Subquery is correlated to the immediate outer query &&
617
Subquery !contains {GROUP BY, ORDER BY [LIMIT],
618
aggregate functions) && subquery predicate is not under "NOT IN"))
619
6. No execution method was already chosen (by a prepared statement).
621
(*) The subquery must be part of a SELECT statement. The current
622
condition also excludes multi-table update statements.
624
We have to determine whether we will perform subquery materialization
625
before calling the IN=>EXISTS transformation, so that we know whether to
626
perform the whole transformation or only that part of it which wraps
627
Item_in_subselect in an Item_in_optimizer.
629
if (do_materialize &&
631
!select_lex->master_unit()->first_select()->next_select() && // 2
632
select_lex->master_unit()->first_select()->leaf_tables && // 3
633
thd->lex->sql_command == SQLCOM_SELECT) // *
635
if (in_subs->is_top_level_item() && // 4
636
!in_subs->is_correlated && // 5
637
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
638
in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
641
Item_subselect::trans_res trans_res;
642
if ((trans_res= subselect->select_transformer(this)) !=
643
Item_subselect::RES_OK)
645
select_lex->fix_prepare_information(thd, &conds, &having);
646
DBUG_RETURN((trans_res == Item_subselect::RES_ERROR));
652
select_lex->fix_prepare_information(thd, &conds, &having);
657
for (ord= order; ord; ord= ord->next)
659
Item *item= *ord->item;
660
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
661
item->split_sum_func(thd, ref_pointer_array, all_fields);
665
if (having && having->with_sum_func)
666
having->split_sum_func2(thd, ref_pointer_array, all_fields,
668
if (select_lex->inner_sum_func_list)
670
Item_sum *end=select_lex->inner_sum_func_list;
671
Item_sum *item_sum= end;
674
item_sum= item_sum->next;
675
item_sum->split_sum_func2(thd, ref_pointer_array,
676
all_fields, item_sum->ref_by, FALSE);
677
} while (item_sum != end);
680
if (select_lex->inner_refs_list.elements &&
681
fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
687
Because HEAP tables can't index BIT fields we need to use an
688
additional hidden field for grouping because later it will be
689
converted to a LONG field. Original field will remain of the
690
BIT type and will be returned to a client.
692
for (ORDER *ord= group_list; ord; ord= ord->next)
694
if ((*ord->item)->type() == Item::FIELD_ITEM &&
695
(*ord->item)->field_type() == MYSQL_TYPE_BIT)
697
Item_field *field= new Item_field(thd, *(Item_field**)ord->item);
698
int el= all_fields.elements;
699
ref_pointer_array[el]= field;
700
all_fields.push_front(field);
701
ord->item= ref_pointer_array + el;
707
Check if there are references to un-aggregated columns when computing
708
aggregate functions with implicit grouping (there is no GROUP BY).
710
MODE_ONLY_FULL_GROUP_BY is enabled here by default
712
if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
714
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
715
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
719
/* Caclulate the number of groups */
721
for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
726
goto err; /* purecov: inspected */
728
if (result && result->prepare(fields_list, unit_arg))
729
goto err; /* purecov: inspected */
731
/* Init join struct */
732
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
733
ref_pointer_array_size= all_fields.elements*sizeof(Item*);
734
this->group= group_list != 0;
737
#ifdef RESTRICTED_GROUP
738
if (sum_func_count && !group_list && (func_count || field_count))
740
my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
744
if (select_lex->olap == ROLLUP_TYPE && rollup_init())
746
if (alloc_func_list())
749
DBUG_RETURN(0); // All OK
752
DBUG_RETURN(-1); /* purecov: inspected */
757
Remove the predicates pushed down into the subquery
760
JOIN::remove_subq_pushed_predicates()
761
where IN Must be NULL
762
OUT The remaining WHERE condition, or NULL
765
Given that this join will be executed using (unique|index)_subquery,
766
without "checking NULL", remove the predicates that were pushed down
769
If the subquery compares scalar values, we can remove the condition that
770
was wrapped into trig_cond (it will be checked when needed by the subquery
773
If the subquery compares row values, we need to keep the wrapped
774
equalities in the WHERE clause: when the left (outer) tuple has both NULL
775
and non-NULL values, we'll do a full table scan and will rely on the
776
equalities corresponding to non-NULL parts of left tuple to filter out
777
non-matching records.
779
TODO: We can remove the equalities that will be guaranteed to be true by the
780
fact that subquery engine will be using index lookup. This must be done only
781
for cases where there are no conversion errors of significance, e.g. 257
782
that is searched in a byte. But this requires homogenization of the return
783
codes of all Field*::store() methods.
786
void JOIN::remove_subq_pushed_predicates(Item **where)
788
if (conds->type() == Item::FUNC_ITEM &&
789
((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
790
((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
791
((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
792
test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
793
((Item_func *)conds)->arguments()[0]))
802
Index lookup-based subquery: save some flags for EXPLAIN output
805
save_index_subquery_explain_info()
806
join_tab Subquery's join tab (there is only one as index lookup is
807
only used for subqueries that are single-table SELECTs)
808
where Subquery's WHERE clause
811
For index lookup-based subquery (i.e. one executed with
812
subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
813
check its EXPLAIN output row should contain
814
"Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
815
"Using Where" (TAB_INFO_USING_WHERE)
816
"Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
817
and set appropriate flags in join_tab->packed_info.
820
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
822
join_tab->packed_info= TAB_INFO_HAVE_VALUE;
823
if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
824
join_tab->packed_info |= TAB_INFO_USING_INDEX;
826
join_tab->packed_info |= TAB_INFO_USING_WHERE;
827
for (uint i = 0; i < join_tab->ref.key_parts; i++)
829
if (join_tab->ref.cond_guards[i])
831
join_tab->packed_info |= TAB_INFO_FULL_SCAN_ON_NULL;
841
Check if the table's rowid is included in the temptable
844
sj_table_is_included()
846
join_tab The table to be checked
849
SemiJoinDuplicateElimination: check the table's rowid should be included
850
in the temptable. This is so if
852
1. The table is not embedded within some semi-join nest
853
2. The has been pulled out of a semi-join nest, or
855
3. The table is functionally dependent on some previous table
857
[4. This is also true for constant tables that can't be
858
NULL-complemented but this function is not called for such tables]
861
TRUE - Include table's rowid
865
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
867
if (join_tab->emb_sj_nest)
870
/* Check if this table is functionally dependent on the tables that
871
are within the same outer join nest
873
TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
874
if (join_tab->type == JT_EQ_REF)
876
Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
878
while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
880
JOIN_TAB *ref_tab= join->join_tab + idx;
881
if (embedding == ref_tab->table->pos_in_table_list->embedding)
884
/* Ok, functionally dependent */
887
/* Not functionally dependent => need to include*/
892
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
893
SJ_TMP_TABLE *sjtbl);
897
Setup the strategies to eliminate semi-join duplicates.
900
setup_semijoin_dups_elimination()
902
options Join options (needed to see if join buffering will be
904
no_jbuf_after Another bit of information re where join buffering will
908
Setup the strategies to eliminate semi-join duplicates. ATM there are 3
911
1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
913
2. FirstMatch (pick only the 1st matching row combination of inner tables)
914
3. InsideOut (scanning the sj-inner table in a way that groups duplicates
915
together and picking the 1st one)
917
The join order has "duplicate-generating ranges", and every range is
918
served by one strategy or a combination of FirstMatch with with some
921
"Duplicate-generating range" is defined as a range within the join order
922
that contains all of the inner tables of a semi-join. All ranges must be
923
disjoint, if tables of several semi-joins are interleaved, then the ranges
924
are joined together, which is equivalent to converting
925
SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
927
SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
930
Applicability conditions are as follows:
932
DuplicateWeedout strategy
933
~~~~~~~~~~~~~~~~~~~~~~~~~
935
(ot|nt)* [ it ((it|ot|nt)* (it|ot))] (nt)*
936
+------+ +=========================+ +---+
939
(1) - Prefix of OuterTables (those that participate in
940
IN-equality and/or are correlated with subquery) and outer
941
Noncorrelated Tables.
942
(2) - The handled range. The range starts with the first sj-inner
943
table, and covers all sj-inner and outer tables
944
Within the range, Inner, Outer, outer Noncorrelated tables
945
may follow in any order.
946
(3) - The suffix of outer Noncorrelated tables.
951
(ot|nt)* [ it ((it|nt)* it) ] (nt)*
952
+------+ +==================+ +---+
955
(1) - Prefix of outer and non-correlated tables
956
(2) - The handled range, which may contain only inner and
957
non-correlated tables.
958
(3) - The suffix of outer Noncorrelated tables.
963
(ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ] (ot|nt)*
964
+--------+ +===========+ +=============+ +------+
967
(1) - Prefix that may contain any outer tables. The prefix must contain
968
all the non-trivially correlated outer tables. (non-trivially means
969
that the correlation is not just through the IN-equality).
971
(2) - Inner table for which the InsideOut scan is performed.
973
(3) - The remainder of the duplicate-generating range. It is served by
974
application of FirstMatch strategy, with the exception that
975
outer IN-correlated tables are considered to be non-correlated.
977
(4) - THe suffix of outer and outer non-correlated tables.
979
If several strategies are applicable, their relative priorities are:
984
This function walks over the join order and sets up the strategies by
985
setting appropriate members in join_tab structures.
989
TRUE Out of memory error
993
int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, uint no_jbuf_after)
995
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
998
0 - invalid (EOF marker),
1000
2 - Temptable (maybe confluent),
1001
3 - Temptable with join buffering
1004
uint start_idx; /* Left range bound */
1005
uint end_idx; /* Right range bound */
1007
For Temptable strategy: Bitmap of all outer and correlated tables from
1008
all involved join nests.
1010
table_map outer_tables;
1011
} dups_ranges [MAX_TABLES];
1013
TABLE_LIST *emb_insideout_nest= NULL;
1014
table_map emb_sj_map= 0; /* A bitmap of sj-nests (that is, their sj-inner
1015
tables) whose ranges we're in */
1016
table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
1017
table_map range_start_map= 0; /* table_map at current range start */
1018
bool dealing_with_jbuf= FALSE; /* TRUE <=> table within cur range uses join buf */
1022
DBUG_ENTER("setup_semijoin_dups_elimination");
1024
First pass: locate the duplicate-generating ranges and pick the strategies.
1026
for (i=join->const_tables ; i < join->tables ; i++)
1028
JOIN_TAB *tab=join->join_tab+i;
1029
TABLE *table=tab->table;
1030
cur_map |= table->map;
1032
if (tab->emb_sj_nest) // Encountered an sj-inner table
1036
dups_ranges[cur_range].start_idx= i;
1037
range_start_map= cur_map & ~table->map;
1039
Remember if this is a possible start of range that is covered by
1040
the InsideOut strategy (the reason that it is not covered could
1041
be that it overlaps with anther semi-join's range. we don't
1042
support InsideOut for joined ranges)
1044
if (join->best_positions[i].use_insideout_scan)
1045
emb_insideout_nest= tab->emb_sj_nest;
1048
emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1049
emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1051
if (tab->emb_sj_nest != emb_insideout_nest)
1054
Two different semi-joins interleave. This cannot be handled by
1057
emb_insideout_nest= NULL;
1061
if (emb_sj_map) /* We're in duplicate-generating range */
1063
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1064
tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1065
i <= no_jbuf_after && !dealing_with_jbuf)
1068
This table uses join buffering, which makes use of FirstMatch or
1069
InsideOut strategies impossible for the current and (we assume)
1070
preceding duplicate-producing ranges.
1071
That is, for the join order:
1073
x x [ x x] x [x x x] x [x x X* x] x
1075
+-----+ +-----+ | join buffering use
1078
we'll have to remove r1 and r2 and use duplicate-elimination
1079
strategy that spans all the tables, starting from the very 1st
1082
dealing_with_jbuf= TRUE;
1083
emb_insideout_nest= FALSE;
1086
Absorb all preceding duplicate-eliminating ranges. Their strategies
1089
for (int prev_range= 0; prev_range < cur_range; prev_range++)
1091
dups_ranges[cur_range].outer_tables |=
1092
dups_ranges[prev_range].outer_tables;
1094
dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1095
dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1100
Check if we are at the end of duplicate-producing range. We are if
1102
1. It's an InsideOut range (which presumes all correlated tables are
1103
in the prefix), and all inner tables are in the join order prefix,
1105
2. It's a DuplicateElimination range (possibly covering several
1106
SJ-nests), and all inner, outer, and correlated tables of all
1107
sj-nests are in the join order prefix.
1109
bool end_of_range= FALSE;
1110
if (emb_insideout_nest &&
1111
bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1113
/* Save that this range is handled with InsideOut: */
1114
dups_ranges[cur_range].strategy= 1;
1117
else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1120
This is a complete range to be handled with either DuplicateWeedout
1123
dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1125
This will hold tables from within the range that need to be put
1126
into the join buffer before we can use the FirstMatch on its tail.
1128
dups_ranges[cur_range].outer_tables= emb_outer_tables &
1135
dups_ranges[cur_range].end_idx= i+1;
1136
emb_sj_map= emb_outer_tables= 0;
1137
emb_insideout_nest= NULL;
1138
dealing_with_jbuf= FALSE;
1139
dups_ranges[++cur_range].strategy= 0;
1144
THD *thd= join->thd;
1145
SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1147
Second pass: setup the chosen strategies
1149
for (int j= 0; j < cur_range; j++)
1151
JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1153
if (dups_ranges[j].strategy == 1) // InsideOut strategy
1155
tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1158
else // DuplicateWeedout strategy
1160
SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1161
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1162
uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1163
uint jt_null_bits= 0; // # null bits in tuple bytes
1164
SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1165
uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1166
JOIN_TAB *last_outer_tab= tab - 1;
1168
Walk through the range and remember
1169
- tables that need their rowids to be put into temptable
1170
- the last outer table
1172
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1174
if (sj_table_is_included(join, tab))
1176
last_tab->join_tab= tab;
1177
last_tab->rowid_offset= jt_rowid_offset;
1178
jt_rowid_offset += tab->table->file->ref_length;
1179
if (tab->table->maybe_null)
1181
last_tab->null_byte= jt_null_bits / 8;
1182
last_tab->null_bit= jt_null_bits++;
1185
tab->table->prepare_for_position();
1186
tab->rowid_keep_flags= rowid_keep_flags;
1188
cur_map |= tab->table->map;
1189
if (!tab->emb_sj_nest && bitmap_covers(cur_map,
1190
dups_ranges[j].outer_tables))
1191
last_outer_tab= tab;
1194
if (jt_rowid_offset) /* Temptable has at least one rowid */
1196
SJ_TMP_TABLE *sjtbl;
1197
uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1198
if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1199
!(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1201
memcpy(sjtbl->tabs, sjtabs, tabs_size);
1202
sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1203
sjtbl->rowid_len= jt_rowid_offset;
1204
sjtbl->null_bits= jt_null_bits;
1205
sjtbl->null_bytes= (jt_null_bits + 7)/8;
1207
*next_sjtbl_ptr= sjtbl;
1208
next_sjtbl_ptr= &(sjtbl->next);
1212
create_duplicate_weedout_tmp_table(thd,
1217
join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1218
join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1220
tab= last_outer_tab + 1;
1221
jump_to= last_outer_tab;
1224
/* Create the FirstMatch tail */
1225
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1227
if (tab->emb_sj_nest)
1228
tab->do_firstmatch= jump_to;
1237
static void cleanup_sj_tmp_tables(JOIN *join)
1239
for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
1240
sj_tbl= sj_tbl->next)
1242
if (sj_tbl->tmp_table)
1244
free_tmp_table(join->thd, sj_tbl->tmp_table);
1247
join->sj_tmp_tables= NULL;
1250
uint make_join_orderinfo(JOIN *join);
1253
global select optimisation.
1256
error code saved in field 'error'
1267
DBUG_ENTER("JOIN::optimize");
1268
// to prevent double initialization on EXPLAIN
1273
thd_proc_info(thd, "optimizing");
1274
row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1275
unit->select_limit_cnt);
1276
/* select_limit is used to decide if we are likely to scan the whole table */
1277
select_limit= unit->select_limit_cnt;
1278
if (having || (select_options & OPTION_FOUND_ROWS))
1279
select_limit= HA_POS_ERROR;
1280
do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1281
// Ignore errors of execution if option IGNORE present
1282
if (thd->lex->ignore)
1283
thd->lex->current_select->no_error= 1;
1285
#ifdef HAVE_REF_TO_FIELDS // Not done yet
1286
/* Add HAVING to WHERE if possible */
1287
if (having && !group_list && !sum_func_count)
1294
else if ((conds=new Item_cond_and(conds,having)))
1297
Item_cond_and can't be fixed after creation, so we do not check
1300
conds->fix_fields(thd, &conds);
1301
conds->change_ref_to_fields(thd, tables_list);
1302
conds->top_level_item();
1307
SELECT_LEX *sel= thd->lex->current_select;
1308
if (sel->first_cond_optimization)
1311
The following code will allocate the new items in a permanent
1312
MEMROOT for prepared statements and stored procedures.
1314
sel->first_cond_optimization= 0;
1316
/* Convert all outer joins to inner joins if possible */
1317
conds= simplify_joins(this, join_list, conds, TRUE, FALSE);
1318
build_bitmap_for_nested_joins(join_list, 0);
1320
sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;
1323
conds= optimize_cond(this, conds, join_list, &cond_value);
1324
if (thd->is_error())
1327
DBUG_PRINT("error",("Error from optimize_cond"));
1332
having= optimize_cond(this, having, join_list, &having_value);
1333
if (thd->is_error())
1336
DBUG_PRINT("error",("Error from optimize_cond"));
1339
if (select_lex->where)
1340
select_lex->cond_value= cond_value;
1341
if (select_lex->having)
1342
select_lex->having_value= having_value;
1344
if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
1345
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1346
{ /* Impossible cond */
1347
DBUG_PRINT("info", (having_value == Item::COND_FALSE ?
1348
"Impossible HAVING" : "Impossible WHERE"));
1349
zero_result_cause= having_value == Item::COND_FALSE ?
1350
"Impossible HAVING" : "Impossible WHERE";
1356
/* Optimize count(*), min() and max() */
1357
if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1361
opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1362
to the WHERE conditions,
1363
or 1 if all items were resolved,
1364
or 0, or an error number HA_ERR_...
1366
if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1368
if (res == HA_ERR_KEY_NOT_FOUND)
1370
DBUG_PRINT("info",("No matching min/max row"));
1371
zero_result_cause= "No matching min/max row";
1378
DBUG_PRINT("error",("Error from opt_sum_query"));
1383
DBUG_PRINT("info",("No matching min/max row"));
1384
zero_result_cause= "No matching min/max row";
1388
DBUG_PRINT("info",("Select tables optimized away"));
1389
zero_result_cause= "Select tables optimized away";
1390
tables_list= 0; // All tables resolved
1392
Extract all table-independent conditions and replace the WHERE
1393
clause with them. All other conditions were computed by opt_sum_query
1394
and the MIN/MAX/COUNT function(s) have been replaced by constants,
1395
so there is no need to compute the whole WHERE clause again.
1396
Notice that make_cond_for_table() will always succeed to remove all
1397
computed conditions, because opt_sum_query() is applicable only to
1399
Preserve conditions for EXPLAIN.
1401
if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1403
COND *table_independent_conds=
1404
make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1405
DBUG_EXECUTE("where",
1406
print_where(table_independent_conds,
1407
"where after opt_sum_query()",
1409
conds= table_independent_conds;
1415
DBUG_PRINT("info",("No tables"));
1419
error= -1; // Error is sent to client
1420
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1422
/* Calculate how to do the join */
1423
thd_proc_info(thd, "statistics");
1424
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1425
thd->is_fatal_error)
1427
DBUG_PRINT("error",("Error: make_join_statistics() failed"));
1431
/* Remove distinct if only const tables */
1432
select_distinct= select_distinct && (const_tables != tables);
1433
thd_proc_info(thd, "preparing");
1434
if (result->initialize_tables(this))
1436
DBUG_PRINT("error",("Error: initialize_tables() failed"));
1437
DBUG_RETURN(1); // error == -1
1439
if (const_table_map != found_const_table_map &&
1440
!(select_options & SELECT_DESCRIBE) &&
1442
!(conds->used_tables() & RAND_TABLE_BIT) ||
1443
select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1445
zero_result_cause= "no matching row in const table";
1446
DBUG_PRINT("error",("Error: %s", zero_result_cause));
1450
if (!(thd->options & OPTION_BIG_SELECTS) &&
1451
best_read > (double) thd->variables.max_join_size &&
1452
!(select_options & SELECT_DESCRIBE))
1453
{ /* purecov: inspected */
1454
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1458
if (const_tables && !thd->locked_tables &&
1459
!(select_options & SELECT_NO_UNLOCK))
1460
mysql_unlock_some_tables(thd, table, const_tables);
1461
if (!conds && outer_join)
1463
/* Handle the case where we have an OUTER JOIN without a WHERE */
1464
conds=new Item_int((longlong) 1,1); // Always true
1466
select= make_select(*table, const_table_map,
1467
const_table_map, conds, 1, &error);
1469
{ /* purecov: inspected */
1470
error= -1; /* purecov: inspected */
1471
DBUG_PRINT("error",("Error: make_select() failed"));
1475
reset_nj_counters(join_list);
1476
make_outerjoin_info(this);
1479
Among the equal fields belonging to the same multiple equality
1480
choose the one that is to be retrieved first and substitute
1481
all references to these in where condition for a reference for
1486
conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1487
conds->update_used_tables();
1488
DBUG_EXECUTE("where",
1490
"after substitute_best_equal",
1495
Permorm the the optimization on fields evaluation mentioned above
1496
for all on expressions.
1498
for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1500
if (*tab->on_expr_ref)
1502
*tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1505
(*tab->on_expr_ref)->update_used_tables();
1509
if (conds &&!outer_join && const_table_map != found_const_table_map &&
1510
(select_options & SELECT_DESCRIBE) &&
1511
select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1513
conds=new Item_int((longlong) 0,1); // Always false
1515
if (make_join_select(this, select, conds))
1518
"Impossible WHERE noticed after reading const tables";
1519
DBUG_RETURN(0); // error == 0
1522
error= -1; /* if goto err */
1524
/* Optimize distinct away if possible */
1526
ORDER *org_order= order;
1527
order=remove_const(this, order,conds,1, &simple_order);
1528
if (thd->is_error())
1531
DBUG_PRINT("error",("Error from remove_const"));
1536
If we are using ORDER BY NULL or ORDER BY const_expression,
1537
return result in any order (even if we are using a GROUP BY)
1539
if (!order && org_order)
1543
Check if we can optimize away GROUP BY/DISTINCT.
1544
We can do that if there are no aggregate functions, the
1545
fields in DISTINCT clause (if present) and/or columns in GROUP BY
1546
(if present) contain direct references to all key parts of
1547
an unique index (in whatever order) and if the key parts of the
1548
unique index cannot contain NULLs.
1549
Note that the unique keys for DISTINCT and GROUP BY should not
1550
be the same (as long as they are unique).
1552
The FROM clause must contain a single non-constant table.
1554
if (tables - const_tables == 1 && (group_list || select_distinct) &&
1555
!tmp_table_param.sum_func_count &&
1556
(!join_tab[const_tables].select ||
1557
!join_tab[const_tables].select->quick ||
1558
join_tab[const_tables].select->quick->get_type() !=
1559
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1562
list_contains_unique_index(join_tab[const_tables].table,
1563
find_field_in_order_list,
1564
(void *) group_list))
1567
We have found that grouping can be removed since groups correspond to
1568
only one row anyway, but we still have to guarantee correct result
1569
order. The line below effectively rewrites the query from GROUP BY
1570
<fields> to ORDER BY <fields>. There are two exceptions:
1571
- if skip_sort_order is set (see above), then we can simply skip
1573
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1574
with the GROUP BY ones, i.e. either one is a prefix of another.
1575
We only check if the ORDER BY is a prefix of GROUP BY. In this case
1576
test_if_subpart() copies the ASC/DESC attributes from the original
1578
If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1581
if (!order || test_if_subpart(group_list, order))
1582
order= skip_sort_order ? 0 : group_list;
1584
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1585
rewritten to IGNORE INDEX FOR ORDER BY(fields).
1587
join_tab->table->keys_in_use_for_order_by=
1588
join_tab->table->keys_in_use_for_group_by;
1592
if (select_distinct &&
1593
list_contains_unique_index(join_tab[const_tables].table,
1594
find_field_in_item_list,
1595
(void *) &fields_list))
1600
if (group_list || tmp_table_param.sum_func_count)
1602
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1605
else if (select_distinct && tables - const_tables == 1)
1608
We are only using one table. In this case we change DISTINCT to a
1610
- The GROUP BY can be done through indexes (no sort) and the ORDER
1611
BY only uses selected fields.
1612
(In this case we can later optimize away GROUP BY and ORDER BY)
1613
- We are scanning the whole table without LIMIT
1615
- We are using CALC_FOUND_ROWS
1616
- We are using an ORDER BY that can't be optimized away.
1618
We don't want to use this optimization when we are using LIMIT
1619
because in this case we can just create a temporary table that
1620
holds LIMIT rows and stop when this table is full.
1622
JOIN_TAB *tab= &join_tab[const_tables];
1623
bool all_order_fields_used;
1625
skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
1626
&tab->table->keys_in_use_for_order_by);
1627
if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1628
order, fields_list, all_fields,
1629
&all_order_fields_used)))
1631
bool skip_group= (skip_sort_order &&
1632
test_if_skip_sort_order(tab, group_list, select_limit, 1,
1633
&tab->table->keys_in_use_for_group_by) != 0);
1634
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1635
if ((skip_group && all_order_fields_used) ||
1636
select_limit == HA_POS_ERROR ||
1637
(order && !skip_sort_order))
1639
/* Change DISTINCT to GROUP BY */
1642
if (all_order_fields_used)
1644
if (order && skip_sort_order)
1647
Force MySQL to read the table in sorted order to get result in
1650
tmp_table_param.quick_group=0;
1654
group=1; // For end_write_group
1659
else if (thd->is_fatal_error) // End of memory
1664
ORDER *old_group_list;
1665
group_list= remove_const(this, (old_group_list= group_list), conds,
1666
rollup.state == ROLLUP::STATE_NONE,
1668
if (thd->is_error())
1671
DBUG_PRINT("error",("Error from remove_const"));
1674
if (old_group_list && !group_list)
1677
if (!group_list && group)
1679
order=0; // The output has only one row
1681
select_distinct= 0; // No need in distinct for 1 row
1682
group_optimized_away= 1;
1685
calc_group_buffer(this, group_list);
1686
send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1688
if (test_if_subpart(group_list, order) ||
1689
(!group_list && tmp_table_param.sum_func_count))
1692
// Can't use sort on head table if using row cache
1702
Check if we need to create a temporary table.
1703
This has to be done if all tables are not already read (const tables)
1704
and one of the following conditions holds:
1705
- We are using DISTINCT (simple distinct's are already optimized away)
1706
- We are using an ORDER BY or GROUP BY on fields not in the first table
1707
- We are using different ORDER BY and GROUP BY orders
1708
- The user wants us to buffer the result.
1710
need_tmp= (const_tables != tables &&
1711
((select_distinct || !simple_order || !simple_group) ||
1712
(group_list && order) ||
1713
test(select_options & OPTION_BUFFER_RESULT)));
1715
uint no_jbuf_after= make_join_orderinfo(this);
1716
ulonglong select_opts_for_readinfo=
1717
(select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1719
sj_tmp_tables= NULL;
1720
if (!select_lex->sj_nests.is_empty())
1721
setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1724
// No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1725
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1728
/* Create all structures needed for materialized subquery execution. */
1729
if (setup_subquery_materialization())
1733
is this simple IN subquery?
1735
if (!group_list && !order &&
1736
unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1737
tables == 1 && conds &&
1743
if (join_tab[0].type == JT_EQ_REF &&
1744
join_tab[0].ref.items[0]->name == in_left_expr_name)
1746
remove_subq_pushed_predicates(&where);
1747
save_index_subquery_explain_info(join_tab, where);
1748
join_tab[0].type= JT_UNIQUE_SUBQUERY;
1750
DBUG_RETURN(unit->item->
1752
subselect_uniquesubquery_engine(thd,
1757
else if (join_tab[0].type == JT_REF &&
1758
join_tab[0].ref.items[0]->name == in_left_expr_name)
1760
remove_subq_pushed_predicates(&where);
1761
save_index_subquery_explain_info(join_tab, where);
1762
join_tab[0].type= JT_INDEX_SUBQUERY;
1764
DBUG_RETURN(unit->item->
1766
subselect_indexsubquery_engine(thd,
1773
} else if (join_tab[0].type == JT_REF_OR_NULL &&
1774
join_tab[0].ref.items[0]->name == in_left_expr_name &&
1775
having->name == in_having_cond)
1777
join_tab[0].type= JT_INDEX_SUBQUERY;
1779
conds= remove_additional_cond(conds);
1780
save_index_subquery_explain_info(join_tab, conds);
1781
DBUG_RETURN(unit->item->
1782
change_engine(new subselect_indexsubquery_engine(thd,
1792
Need to tell handlers that to play it safe, it should fetch all
1793
columns of the primary key of the tables: this is because MySQL may
1794
build row pointers for the rows, and for all columns of the primary key
1795
the read set has not necessarily been set by the server code.
1797
if (need_tmp || select_distinct || group_list || order)
1799
for (uint i = const_tables; i < tables; i++)
1800
join_tab[i].table->prepare_for_position();
1803
DBUG_EXECUTE("info",TEST_join(this););
1805
if (const_tables != tables)
1808
Because filesort always does a full table scan or a quick range scan
1809
we must add the removed reference to the select for the table.
1810
We only need to do this when we have a simple_order or simple_group
1811
as in other cases the join is done before the sort.
1813
if ((order || group_list) &&
1814
(join_tab[const_tables].type != JT_ALL) &&
1815
(join_tab[const_tables].type != JT_REF_OR_NULL) &&
1816
((order && simple_order) || (group_list && simple_group)))
1818
if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1823
if (!(select_options & SELECT_BIG_RESULT) &&
1826
!test_if_skip_sort_order(&join_tab[const_tables], group_list,
1827
unit->select_limit_cnt, 0,
1828
&join_tab[const_tables].table->
1829
keys_in_use_for_group_by))) ||
1831
tmp_table_param.quick_group)
1833
need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort
1838
Force using of tmp table if sorting by a SP or UDF function due to
1839
their expensive and probably non-deterministic nature.
1841
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1843
Item *item= *tmp_order->item;
1844
if (item->is_expensive())
1846
/* Force tmp table without sort */
1847
need_tmp=1; simple_order=simple_group=0;
1855
if (select_options & SELECT_DESCRIBE)
1863
The loose index scan access method guarantees that all grouping or
1864
duplicate row elimination (for distinct) is already performed
1865
during data retrieval, and that all MIN/MAX functions are already
1866
computed for each group. Thus all MIN/MAX functions should be
1867
treated as regular functions, and there is no need to perform
1868
grouping in the main execution loop.
1869
Notice that currently loose index scan is applicable only for
1870
single table queries, thus it is sufficient to test only the first
1871
join_tab element of the plan for its access method.
1873
if (join_tab->is_using_loose_index_scan())
1874
tmp_table_param.precomputed_group_by= TRUE;
1876
/* Create a tmp table if distinct or if the sort is too complicated */
1879
DBUG_PRINT("info",("Creating tmp table"));
1880
thd_proc_info(thd, "Creating tmp table");
1882
init_items_ref_array();
1884
tmp_table_param.hidden_field_count= (all_fields.elements -
1885
fields_list.elements);
1886
ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1889
Pushing LIMIT to the temporary table creation is not applicable
1890
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1891
there are aggregate functions, because in all these cases we need
1894
ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1896
!thd->lex->current_select->with_sum_func) ?
1897
select_limit : HA_POS_ERROR;
1899
if (!(exec_tmp_table1=
1900
create_tmp_table(thd, &tmp_table_param, all_fields,
1902
group_list ? 0 : select_distinct,
1903
group_list && simple_group,
1912
We don't have to store rows in temp table that doesn't match HAVING if:
1913
- we are sorting the table and writing complete group rows to the
1915
- We are using DISTINCT without resolving the distinct as a GROUP BY
1918
If having is not handled here, it will be checked before the row
1919
is sent to the client.
1922
(sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1925
/* if group or order on first table, sort first */
1926
if (group_list && simple_group)
1928
DBUG_PRINT("info",("Sorting for group"));
1929
thd_proc_info(thd, "Sorting for group");
1930
if (create_sort_index(thd, this, group_list,
1931
HA_POS_ERROR, HA_POS_ERROR, FALSE) ||
1932
alloc_group_fields(this, group_list) ||
1933
make_sum_func_list(all_fields, fields_list, 1) ||
1934
setup_sum_funcs(thd, sum_funcs))
1942
if (make_sum_func_list(all_fields, fields_list, 0) ||
1943
setup_sum_funcs(thd, sum_funcs))
1948
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1950
DBUG_PRINT("info",("Sorting for order"));
1951
thd_proc_info(thd, "Sorting for order");
1952
if (create_sort_index(thd, this, order,
1953
HA_POS_ERROR, HA_POS_ERROR, TRUE))
1962
Optimize distinct when used on some of the tables
1963
SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1964
In this case we can stop scanning t2 when we have found one t1.a
1967
if (exec_tmp_table1->distinct)
1969
table_map used_tables= thd->used_tables;
1970
JOIN_TAB *last_join_tab= join_tab+tables-1;
1973
if (used_tables & last_join_tab->table->map)
1975
last_join_tab->not_used_in_distinct=1;
1976
} while (last_join_tab-- != join_tab);
1977
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1978
if (order && skip_sort_order)
1980
/* Should always succeed */
1981
if (test_if_skip_sort_order(&join_tab[const_tables],
1982
order, unit->select_limit_cnt, 0,
1983
&join_tab[const_tables].table->
1984
keys_in_use_for_order_by))
1990
If this join belongs to an uncacheable subquery save
1993
if (select_lex->uncacheable && !is_top_level_join() &&
1994
init_save_join_tab())
1995
DBUG_RETURN(-1); /* purecov: inspected */
2004
Restore values in temporary join.
2006
void JOIN::restore_tmp()
2008
memcpy(tmp_join, this, (size_t) sizeof(JOIN));
2015
DBUG_ENTER("JOIN::reinit");
2017
unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
2018
select_lex->offset_limit->val_uint() :
2023
if (exec_tmp_table1)
2025
exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
2026
exec_tmp_table1->file->ha_delete_all_rows();
2027
free_io_cache(exec_tmp_table1);
2028
filesort_free_buffers(exec_tmp_table1,0);
2030
if (exec_tmp_table2)
2032
exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
2033
exec_tmp_table2->file->ha_delete_all_rows();
2034
free_io_cache(exec_tmp_table2);
2035
filesort_free_buffers(exec_tmp_table2,0);
2038
set_items_ref_array(items0);
2041
memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
2046
/* Reset of sum functions */
2049
Item_sum *func, **func_ptr= sum_funcs;
2050
while ((func= *(func_ptr++)))
2058
@brief Save the original join layout
2060
@details Saves the original join layout so it can be reused in
2061
re-execution and for EXPLAIN.
2063
@return Operation status
2065
@retval 1 error occurred.
2069
JOIN::init_save_join_tab()
2071
if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
2072
return 1; /* purecov: inspected */
2073
error= 0; // Ensure that tmp_join.error= 0
2080
JOIN::save_join_tab()
2082
if (!join_tab_save && select_lex->master_unit()->uncacheable)
2084
if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2085
sizeof(JOIN_TAB) * tables)))
2096
Note, that create_sort_index calls test_if_skip_sort_order and may
2097
finally replace sorting with index scan if there is a LIMIT clause in
2098
the query. It's never shown in EXPLAIN!
2101
When can we have here thd->net.report_error not zero?
2106
List<Item> *columns_list= &fields_list;
2108
DBUG_ENTER("JOIN::exec");
2110
thd_proc_info(thd, "executing");
2112
(void) result->prepare2(); // Currently, this cannot fail.
2114
if (!tables_list && (tables || !select_lex->with_sum_func))
2115
{ // Only test of functions
2116
if (select_options & SELECT_DESCRIBE)
2117
select_describe(this, FALSE, FALSE, FALSE,
2118
(zero_result_cause?zero_result_cause:"No tables used"));
2121
result->send_fields(*columns_list,
2122
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2124
We have to test for 'conds' here as the WHERE may not be constant
2125
even if we don't have any tables for prepared statements or if
2126
conds uses something like 'rand()'.
2128
if (cond_value != Item::COND_FALSE &&
2129
(!conds || conds->val_int()) &&
2130
(!having || having->val_int()))
2132
if (do_send_rows && result->send_data(fields_list))
2136
error= (int) result->send_eof();
2137
send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2138
thd->sent_row_count);
2143
error=(int) result->send_eof();
2147
/* Single select (without union) always returns 0 or 1 row */
2148
thd->limit_found_rows= send_records;
2149
thd->examined_row_count= 0;
2153
Don't reset the found rows count if there're no tables as
2154
FOUND_ROWS() may be called. Never reset the examined row count here.
2155
It must be accumulated from all join iterations of all join parts.
2158
thd->limit_found_rows= 0;
2160
if (zero_result_cause)
2162
(void) return_zero_rows(this, result, select_lex->leaf_tables,
2164
send_row_on_empty_set(),
2171
if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2172
get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2175
if (select_options & SELECT_DESCRIBE)
2178
Check if we managed to optimize ORDER BY away and don't use temporary
2179
table to resolve ORDER BY: in that case, we only may need to do
2180
filesort for GROUP BY.
2182
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2185
Reset 'order' to 'group_list' and reinit variables describing
2189
simple_order= simple_group;
2193
(order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2194
(const_tables == tables ||
2195
((simple_order || skip_sort_order) &&
2196
test_if_skip_sort_order(&join_tab[const_tables], order,
2198
&join_tab[const_tables].table->
2199
keys_in_use_for_query))))
2202
select_describe(this, need_tmp,
2203
order != 0 && !skip_sort_order,
2205
!tables ? "No tables used" : NullS);
2209
JOIN *curr_join= this;
2210
List<Item> *curr_all_fields= &all_fields;
2211
List<Item> *curr_fields_list= &fields_list;
2212
TABLE *curr_tmp_table= 0;
2214
Initialize examined rows here because the values from all join parts
2215
must be accumulated in examined_row_count. Hence every join
2216
iteration must count from zero.
2218
curr_join->examined_rows= 0;
2220
/* Create a tmp table if distinct or if the sort is too complicated */
2226
We are in a non cacheable sub query. Get the saved join structure
2228
(curr_join may have been modified during last exection and we need
2231
curr_join= tmp_join;
2233
curr_tmp_table= exec_tmp_table1;
2235
/* Copy data to the temporary table */
2236
thd_proc_info(thd, "Copying to tmp table");
2237
DBUG_PRINT("info", ("%s", thd->proc_info));
2238
if (!curr_join->sort_and_group &&
2239
curr_join->const_tables != curr_join->tables)
2240
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2241
if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2246
curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2248
if (curr_join->having)
2249
curr_join->having= curr_join->tmp_having= 0; // Allready done
2251
/* Change sum_fields reference to calculated fields in tmp_table */
2252
curr_join->all_fields= *curr_all_fields;
2255
items1= items0 + all_fields.elements;
2256
if (sort_and_group || curr_tmp_table->group)
2258
if (change_to_use_tmp_fields(thd, items1,
2259
tmp_fields_list1, tmp_all_fields1,
2260
fields_list.elements, all_fields))
2265
if (change_refs_to_tmp_fields(thd, items1,
2266
tmp_fields_list1, tmp_all_fields1,
2267
fields_list.elements, all_fields))
2270
curr_join->tmp_all_fields1= tmp_all_fields1;
2271
curr_join->tmp_fields_list1= tmp_fields_list1;
2272
curr_join->items1= items1;
2274
curr_all_fields= &tmp_all_fields1;
2275
curr_fields_list= &tmp_fields_list1;
2276
curr_join->set_items_ref_array(items1);
2278
if (sort_and_group || curr_tmp_table->group)
2280
curr_join->tmp_table_param.field_count+=
2281
curr_join->tmp_table_param.sum_func_count+
2282
curr_join->tmp_table_param.func_count;
2283
curr_join->tmp_table_param.sum_func_count=
2284
curr_join->tmp_table_param.func_count= 0;
2288
curr_join->tmp_table_param.field_count+=
2289
curr_join->tmp_table_param.func_count;
2290
curr_join->tmp_table_param.func_count= 0;
2293
if (curr_tmp_table->group)
2294
{ // Already grouped
2295
if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2296
curr_join->order= curr_join->group_list; /* order by group */
2297
curr_join->group_list= 0;
2301
If we have different sort & group then we must sort the data by group
2302
and copy it to another tmp table
2303
This code is also used if we are using distinct something
2304
we haven't been able to store in the temporary table yet
2305
like SEC_TO_TIME(SUM(...)).
2308
if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2309
{ /* Must copy to another table */
2310
DBUG_PRINT("info",("Creating group table"));
2312
/* Free first data from old join */
2313
curr_join->join_free();
2314
if (make_simple_join(curr_join, curr_tmp_table))
2316
calc_group_buffer(curr_join, group_list);
2317
count_field_types(select_lex, &curr_join->tmp_table_param,
2318
curr_join->tmp_all_fields1,
2319
curr_join->select_distinct && !curr_join->group_list);
2320
curr_join->tmp_table_param.hidden_field_count=
2321
(curr_join->tmp_all_fields1.elements-
2322
curr_join->tmp_fields_list1.elements);
2325
if (exec_tmp_table2)
2326
curr_tmp_table= exec_tmp_table2;
2329
/* group data to new table */
2332
If the access method is loose index scan then all MIN/MAX
2333
functions are precomputed, and should be treated as regular
2334
functions. See extended comment in JOIN::exec.
2336
if (curr_join->join_tab->is_using_loose_index_scan())
2337
curr_join->tmp_table_param.precomputed_group_by= TRUE;
2339
if (!(curr_tmp_table=
2340
exec_tmp_table2= create_tmp_table(thd,
2341
&curr_join->tmp_table_param,
2344
curr_join->select_distinct &&
2345
!curr_join->group_list,
2346
1, curr_join->select_options,
2350
curr_join->exec_tmp_table2= exec_tmp_table2;
2352
if (curr_join->group_list)
2354
thd_proc_info(thd, "Creating sort index");
2355
if (curr_join->join_tab == join_tab && save_join_tab())
2359
if (create_sort_index(thd, curr_join, curr_join->group_list,
2360
HA_POS_ERROR, HA_POS_ERROR, FALSE) ||
2361
make_group_fields(this, curr_join))
2365
sortorder= curr_join->sortorder;
2368
thd_proc_info(thd, "Copying to group table");
2369
DBUG_PRINT("info", ("%s", thd->proc_info));
2371
if (curr_join != this)
2375
curr_join->sum_funcs= sum_funcs2;
2376
curr_join->sum_funcs_end= sum_funcs_end2;
2380
curr_join->alloc_func_list();
2381
sum_funcs2= curr_join->sum_funcs;
2382
sum_funcs_end2= curr_join->sum_funcs_end;
2385
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2388
curr_join->group_list= 0;
2389
if (!curr_join->sort_and_group &&
2390
curr_join->const_tables != curr_join->tables)
2391
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2392
if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2393
(tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2398
end_read_record(&curr_join->join_tab->read_record);
2399
curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2400
curr_join->join_tab[0].table= 0; // Table is freed
2402
// No sum funcs anymore
2405
items2= items1 + all_fields.elements;
2406
if (change_to_use_tmp_fields(thd, items2,
2407
tmp_fields_list2, tmp_all_fields2,
2408
fields_list.elements, tmp_all_fields1))
2410
curr_join->tmp_fields_list2= tmp_fields_list2;
2411
curr_join->tmp_all_fields2= tmp_all_fields2;
2413
curr_fields_list= &curr_join->tmp_fields_list2;
2414
curr_all_fields= &curr_join->tmp_all_fields2;
2415
curr_join->set_items_ref_array(items2);
2416
curr_join->tmp_table_param.field_count+=
2417
curr_join->tmp_table_param.sum_func_count;
2418
curr_join->tmp_table_param.sum_func_count= 0;
2420
if (curr_tmp_table->distinct)
2421
curr_join->select_distinct=0; /* Each row is unique */
2423
curr_join->join_free(); /* Free quick selects */
2424
if (curr_join->select_distinct && ! curr_join->group_list)
2426
thd_proc_info(thd, "Removing duplicates");
2427
if (curr_join->tmp_having)
2428
curr_join->tmp_having->update_used_tables();
2429
if (remove_duplicates(curr_join, curr_tmp_table,
2430
*curr_fields_list, curr_join->tmp_having))
2432
curr_join->tmp_having=0;
2433
curr_join->select_distinct=0;
2435
curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2436
if (make_simple_join(curr_join, curr_tmp_table))
2438
calc_group_buffer(curr_join, curr_join->group_list);
2439
count_field_types(select_lex, &curr_join->tmp_table_param,
2440
*curr_all_fields, 0);
2444
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2446
if (make_group_fields(this, curr_join))
2453
init_items_ref_array();
2454
items3= ref_pointer_array + (all_fields.elements*4);
2455
setup_copy_fields(thd, &curr_join->tmp_table_param,
2456
items3, tmp_fields_list3, tmp_all_fields3,
2457
curr_fields_list->elements, *curr_all_fields);
2458
tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2459
tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2460
tmp_table_param.save_copy_field_end=
2461
curr_join->tmp_table_param.copy_field_end;
2462
curr_join->tmp_all_fields3= tmp_all_fields3;
2463
curr_join->tmp_fields_list3= tmp_fields_list3;
2467
curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2468
curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2469
curr_join->tmp_table_param.copy_field_end=
2470
tmp_table_param.save_copy_field_end;
2472
curr_fields_list= &tmp_fields_list3;
2473
curr_all_fields= &tmp_all_fields3;
2474
curr_join->set_items_ref_array(items3);
2476
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2478
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2479
thd->is_fatal_error)
2482
if (curr_join->group_list || curr_join->order)
2484
DBUG_PRINT("info",("Sorting for send_fields"));
2485
thd_proc_info(thd, "Sorting result");
2486
/* If we have already done the group, add HAVING to sorted table */
2487
if (curr_join->tmp_having && ! curr_join->group_list &&
2488
! curr_join->sort_and_group)
2490
// Some tables may have been const
2491
curr_join->tmp_having->update_used_tables();
2492
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2493
table_map used_tables= (curr_join->const_table_map |
2494
curr_table->table->map);
2496
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2499
if (sort_table_cond)
2501
if (!curr_table->select)
2502
if (!(curr_table->select= new SQL_SELECT))
2504
if (!curr_table->select->cond)
2505
curr_table->select->cond= sort_table_cond;
2506
else // This should never happen
2508
if (!(curr_table->select->cond=
2509
new Item_cond_and(curr_table->select->cond,
2513
Item_cond_and do not need fix_fields for execution, its parameters
2514
are fixed or do not need fix_fields, too
2516
curr_table->select->cond->quick_fix_field();
2518
curr_table->select_cond= curr_table->select->cond;
2519
curr_table->select_cond->top_level_item();
2520
DBUG_EXECUTE("where",print_where(curr_table->select->cond,
2521
"select and having",
2523
curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2526
DBUG_EXECUTE("where",print_where(curr_join->tmp_having,
2527
"having after sort",
2533
curr_join->select_limit= HA_POS_ERROR;
2537
We can abort sorting after thd->select_limit rows if we there is no
2538
WHERE clause for any tables after the sorted one.
2540
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2541
JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2542
for (; curr_table < end_table ; curr_table++)
2545
table->keyuse is set in the case there was an original WHERE clause
2546
on the table that was optimized away.
2548
if (curr_table->select_cond ||
2549
(curr_table->keyuse && !curr_table->first_inner))
2551
/* We have to sort all rows */
2552
curr_join->select_limit= HA_POS_ERROR;
2557
if (curr_join->join_tab == join_tab && save_join_tab())
2562
Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2563
chose FILESORT to be faster than INDEX SCAN or there is no
2564
suitable index present.
2565
Note, that create_sort_index calls test_if_skip_sort_order and may
2566
finally replace sorting with index scan if there is a LIMIT clause in
2567
the query. XXX: it's never shown in EXPLAIN!
2568
OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2570
if (create_sort_index(thd, curr_join,
2571
curr_join->group_list ?
2572
curr_join->group_list : curr_join->order,
2573
curr_join->select_limit,
2574
(select_options & OPTION_FOUND_ROWS ?
2575
HA_POS_ERROR : unit->select_limit_cnt),
2576
curr_join->group_list ? TRUE : FALSE))
2578
sortorder= curr_join->sortorder;
2579
if (curr_join->const_tables != curr_join->tables &&
2580
!curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2583
If no IO cache exists for the first table then we are using an
2584
INDEX SCAN and no filesort. Thus we should not remove the sorted
2585
attribute on the INDEX SCAN.
2591
/* XXX: When can we have here thd->is_error() not zero? */
2592
if (thd->is_error())
2594
error= thd->is_error();
2597
curr_join->having= curr_join->tmp_having;
2598
curr_join->fields= curr_fields_list;
2601
thd_proc_info(thd, "Sending data");
2602
DBUG_PRINT("info", ("%s", thd->proc_info));
2603
result->send_fields(*curr_fields_list,
2604
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2605
error= do_select(curr_join, curr_fields_list, NULL);
2606
thd->limit_found_rows= curr_join->send_records;
2609
/* Accumulate the counts from all join iterations of all join parts. */
2610
thd->examined_row_count+= curr_join->examined_rows;
2611
DBUG_PRINT("counts", ("thd->examined_row_count: %lu",
2612
(ulong) thd->examined_row_count));
2615
With EXPLAIN EXTENDED we have to restore original ref_array
2616
for a derived table which is always materialized.
2617
Otherwise we would not be able to print the query correctly.
2620
(thd->lex->describe & DESCRIBE_EXTENDED) &&
2621
select_lex->linkage == DERIVED_TABLE_TYPE)
2622
set_items_ref_array(items0);
2632
Return error that hold JOIN.
2638
DBUG_ENTER("JOIN::destroy");
2639
select_lex->join= 0;
2643
if (join_tab != tmp_join->join_tab)
2645
JOIN_TAB *tab, *end;
2646
for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2649
tmp_join->tmp_join= 0;
2650
tmp_table_param.copy_field=0;
2651
DBUG_RETURN(tmp_join->destroy());
2656
if (exec_tmp_table1)
2657
free_tmp_table(thd, exec_tmp_table1);
2658
if (exec_tmp_table2)
2659
free_tmp_table(thd, exec_tmp_table2);
2661
delete_dynamic(&keyuse);
2668
An entry point to single-unit select (a select without UNION).
2670
@param thd thread handler
2671
@param rref_pointer_array a reference to ref_pointer_array of
2672
the top-level select_lex for this query
2673
@param tables list of all tables used in this query.
2674
The tables have been pre-opened.
2675
@param wild_num number of wildcards used in the top level
2676
select of this query.
2677
For example statement
2678
SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2680
@param fields list of items in SELECT list of the top-level
2682
e.g. SELECT a, b, c FROM t1 will have Item_field
2683
for a, b and c in this list.
2684
@param conds top level item of an expression representing
2685
WHERE clause of the top level select
2686
@param og_num total number of ORDER BY and GROUP BY clauses
2688
@param order linked list of ORDER BY agruments
2689
@param group linked list of GROUP BY arguments
2690
@param having top level item of HAVING expression
2691
@param proc_param list of PROCEDUREs
2692
@param select_options select options (BIG_RESULT, etc)
2693
@param result an instance of result set handling class.
2694
This object is responsible for send result
2695
set rows to the client or inserting them
2697
@param select_lex the only SELECT_LEX of this query
2698
@param unit top-level UNIT of this query
2699
UNIT is an artificial object created by the
2700
parser for every SELECT clause.
2702
SELECT * FROM t1 WHERE a1 IN (SELECT * FROM t2)
2712
mysql_select(THD *thd, Item ***rref_pointer_array,
2713
TABLE_LIST *tables, uint wild_num, List<Item> &fields,
2714
COND *conds, uint og_num, ORDER *order, ORDER *group,
2715
Item *having, ORDER *proc_param, ulonglong select_options,
2716
select_result *result, SELECT_LEX_UNIT *unit,
2717
SELECT_LEX *select_lex)
2721
DBUG_ENTER("mysql_select");
2723
select_lex->context.resolve_in_select_list= TRUE;
2725
if (select_lex->join != 0)
2727
join= select_lex->join;
2729
is it single SELECT in derived table, called in derived table
2732
if (select_lex->linkage != DERIVED_TABLE_TYPE ||
2733
(select_options & SELECT_DESCRIBE))
2735
if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
2737
//here is EXPLAIN of subselect or derived table
2738
if (join->change_result(result))
2745
if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2746
conds, og_num, order, group, having, proc_param,
2754
join->select_options= select_options;
2758
if (!(join= new JOIN(thd, fields, select_options, result)))
2760
thd_proc_info(thd, "init");
2761
thd->used_tables=0; // Updated by setup_fields
2762
if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2763
conds, og_num, order, group, having, proc_param,
2764
select_lex, unit)) == true)
2770
/* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2771
if (join->flatten_subqueries())
2776
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2778
if ((err= join->optimize()))
2783
if (thd->lex->describe & DESCRIBE_EXTENDED)
2785
join->conds_history= join->conds;
2786
join->having_history= (join->having?join->having:join->tmp_having);
2789
if (thd->is_error())
2794
if (thd->lex->describe & DESCRIBE_EXTENDED)
2796
select_lex->where= join->conds_history;
2797
select_lex->having= join->having_history;
2803
thd_proc_info(thd, "end");
2804
err|= select_lex->cleanup();
2805
DBUG_RETURN(err || thd->is_error());
2807
DBUG_RETURN(join->error);
2811
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
2812
Item_in_subselect* const *el2)
2814
return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
2815
( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2819
inline Item * and_items(Item* cond, Item *item)
2821
return (cond? (new Item_cond_and(cond, item)) : item);
2825
static TABLE_LIST *alloc_join_nest(THD *thd)
2828
if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2829
sizeof(NESTED_JOIN))))
2831
tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl +
2832
ALIGN_SIZE(sizeof(TABLE_LIST)));
2837
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2839
List_iterator<TABLE_LIST> it(*tlist);
2841
while ((table= it++))
2844
table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
2845
if (table->nested_join)
2846
fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
2852
Convert a subquery predicate into a TABLE_LIST semi-join nest
2855
convert_subq_to_sj()
2856
parent_join Parent join, the one that has subq_pred in its WHERE/ON
2858
subq_pred Subquery predicate to be converted
2861
Convert a subquery predicate into a TABLE_LIST semi-join nest. All the
2862
prerequisites are already checked, so the conversion is always successfull.
2864
Prepared Statements: the transformation is permanent:
2865
- Changes in TABLE_LIST structures are naturally permanent
2866
- Item tree changes are performed on statement MEM_ROOT:
2867
= we activate statement MEM_ROOT
2868
= this function is called before the first fix_prepare_information
2871
This is intended because the criteria for subquery-to-sj conversion remain
2872
constant for the lifetime of the Prepared Statement.
2876
TRUE Out of memory error
2879
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2881
SELECT_LEX *parent_lex= parent_join->select_lex;
2882
TABLE_LIST *emb_tbl_nest= NULL;
2883
List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2884
THD *thd= parent_join->thd;
2885
DBUG_ENTER("convert_subq_to_sj");
2888
1. Find out where to put the predicate into.
2889
Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2891
if ((void*)subq_pred->expr_join_nest != (void*)1)
2893
if (subq_pred->expr_join_nest->nested_join)
2898
... [LEFT] JOIN ( ... ) ON (subquery AND whatever) ...
2900
The sj-nest will be inserted into the brackets nest.
2902
emb_tbl_nest= subq_pred->expr_join_nest;
2903
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2905
else if (!subq_pred->expr_join_nest->outer_join)
2910
... INNER JOIN tblX ON (subquery AND whatever) ...
2912
The sj-nest will be tblX's "sibling", i.e. another child of its
2913
parent. This is ok because tblX is joined as an inner join.
2915
emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2917
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2919
else if (!subq_pred->expr_join_nest->nested_join)
2921
TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;
2922
TABLE_LIST *wrap_nest;
2926
... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2928
we'll need to convert it into:
2930
... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2932
|<----- wrap_nest ---->|
2934
Q: other subqueries may be pointing to this element. What to do?
2935
A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2936
But we'll need to fix other pointers.
2937
A2: Another way: have TABLE_LIST::next_ptr so the following
2938
subqueries know the table has been nested.
2939
A3: changes in the TABLE_LIST::outer_join will make everything work
2942
if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2946
wrap_nest->embedding= outer_tbl->embedding;
2947
wrap_nest->join_list= outer_tbl->join_list;
2948
wrap_nest->alias= (char*) "(sj-wrap)";
2950
wrap_nest->nested_join->join_list.empty();
2951
wrap_nest->nested_join->join_list.push_back(outer_tbl);
2953
outer_tbl->embedding= wrap_nest;
2954
outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2957
wrap_nest will take place of outer_tbl, so move the outer join flag
2960
wrap_nest->outer_join= outer_tbl->outer_join;
2961
outer_tbl->outer_join= 0;
2963
wrap_nest->on_expr= outer_tbl->on_expr;
2964
outer_tbl->on_expr= NULL;
2966
List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2970
if (tbl == outer_tbl)
2972
li.replace(wrap_nest);
2977
Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
2978
semi-join nest into it
2980
emb_join_list= &wrap_nest->nested_join->join_list;
2981
emb_tbl_nest= wrap_nest;
2985
TABLE_LIST *sj_nest;
2986
NESTED_JOIN *nested_join;
2987
if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2991
nested_join= sj_nest->nested_join;
2993
sj_nest->join_list= emb_join_list;
2994
sj_nest->embedding= emb_tbl_nest;
2995
sj_nest->alias= (char*) "(sj-nest)";
2996
/* Nests do not participate in those 'chains', so: */
2997
/* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2998
emb_join_list->push_back(sj_nest);
3001
nested_join->used_tables and nested_join->not_null_tables are
3002
initialized in simplify_joins().
3006
2. Walk through subquery's top list and set 'embedding' to point to the
3009
st_select_lex *subq_lex= subq_pred->unit->first_select();
3010
nested_join->join_list.empty();
3011
List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
3012
TABLE_LIST *tl, *last_leaf;
3015
tl->embedding= sj_nest;
3016
tl->join_list= &nested_join->join_list;
3017
nested_join->join_list.push_back(tl);
3021
Reconnect the next_leaf chain.
3022
TODO: Do we have to put subquery's tables at the end of the chain?
3023
Inserting them at the beginning would be a bit faster.
3024
NOTE: We actually insert them at the front! That's because the order is
3025
reversed in this list.
3027
for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
3028
tl->next_leaf= subq_lex->leaf_tables;
3032
Same as above for next_local chain
3033
(a theory: a next_local chain always starts with ::leaf_tables
3034
because view's tables are inserted after the view)
3036
for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
3037
tl->next_local= subq_lex->leaf_tables;
3039
/* A theory: no need to re-connect the next_global chain */
3041
/* 3. Remove the original subquery predicate from the WHERE/ON */
3043
// The subqueries were replaced for Item_int(1) earlier
3044
subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
3045
/*TODO: also reset the 'with_subselect' there. */
3047
/* n. Adjust the parent_join->tables counter */
3048
uint table_no= parent_join->tables;
3049
/* n. Walk through child's tables and adjust table->map */
3050
for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
3052
tl->table->tablenr= table_no;
3053
tl->table->map= ((table_map)1) << table_no;
3054
SELECT_LEX *old_sl= tl->select_lex;
3055
tl->select_lex= parent_join->select_lex;
3056
for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
3057
emb->select_lex= parent_join->select_lex;
3059
parent_join->tables += subq_lex->join->tables;
3062
Put the subquery's WHERE into semi-join's sj_on_expr
3063
Add the subquery-induced equalities too.
3065
SELECT_LEX *save_lex= thd->lex->current_select;
3066
thd->lex->current_select=subq_lex;
3067
if (!subq_pred->left_expr->fixed &&
3068
subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
3070
thd->lex->current_select=save_lex;
3072
sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
3073
sj_nest->nested_join->sj_depends_on= subq_pred->used_tables() |
3074
subq_pred->left_expr->used_tables();
3075
sj_nest->sj_on_expr= subq_lex->where;
3078
Create the IN-equalities and inject them into semi-join's ON expression.
3079
Additionally, for InsideOut strategy
3080
- Record the number of IN-equalities.
3081
- Create list of pointers to (oe1, ..., ieN). We'll need the list to
3082
see which of the expressions are bound and which are not (for those
3083
we'll produce a distinct stream of (ie_i1,...ie_ik).
3085
(TODO: can we just create a list of pointers and hope the expressions
3086
will not substitute themselves on fix_fields()? or we need to wrap
3087
them into Item_direct_view_refs and store pointers to those. The
3088
pointers to Item_direct_view_refs are guaranteed to be stable as
3089
Item_direct_view_refs doesn't substitute itself with anything in
3090
Item_direct_view_ref::fix_fields.
3092
sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
3093
sj_nest->nested_join->sj_outer_expr_list.empty();
3095
if (subq_pred->left_expr->cols() == 1)
3097
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3099
Item *item_eq= new Item_func_eq(subq_pred->left_expr,
3100
subq_lex->ref_pointer_array[0]);
3101
item_eq->name= (char*)subq_sj_cond_name;
3102
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3106
for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3108
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3111
new Item_func_eq(subq_pred->left_expr->element_index(i),
3112
subq_lex->ref_pointer_array[i]);
3113
item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3114
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3117
/* Fix the created equality and AND */
3118
sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3121
Walk through sj nest's WHERE and ON expressions and call
3122
item->fix_table_changes() for all items.
3124
sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3125
fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3128
/* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3129
subq_lex->master_unit()->exclude_level();
3131
DBUG_EXECUTE("where",
3132
print_where(sj_nest->sj_on_expr,"SJ-EXPR", QT_ORDINARY););
3134
/* Inject sj_on_expr into the parent's WHERE or ON */
3137
emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
3138
sj_nest->sj_on_expr);
3139
emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3143
/* Inject into the WHERE */
3144
parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3145
parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3146
parent_join->select_lex->where= parent_join->conds;
3154
Convert candidate subquery predicates to semi-joins
3157
JOIN::flatten_subqueries()
3160
Convert candidate subquery predicates to semi-joins.
3167
bool JOIN::flatten_subqueries()
3169
Item_in_subselect **in_subq;
3170
Item_in_subselect **in_subq_end;
3171
DBUG_ENTER("JOIN::flatten_subqueries");
3173
if (sj_subselects.elements() == 0)
3176
/* 1. Fix children subqueries */
3177
for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
3178
in_subq != in_subq_end; in_subq++)
3180
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3181
child_join->outer_tables = child_join->tables;
3182
if (child_join->flatten_subqueries())
3184
(*in_subq)->sj_convert_priority=
3185
(*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3188
//dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3190
2. Pick which subqueries to convert:
3191
sort the subquery array
3192
- prefer correlated subqueries over uncorrelated;
3193
- prefer subqueries that have greater number of outer tables;
3195
sj_subselects.sort(subq_sj_candidate_cmp);
3196
// #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3197
/* Replace all subqueries to be flattened with Item_int(1) */
3198
for (in_subq= sj_subselects.front();
3199
in_subq != in_subq_end &&
3200
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3203
if (replace_where_subcondition(this, *in_subq, new Item_int(1), FALSE))
3207
for (in_subq= sj_subselects.front();
3208
in_subq != in_subq_end &&
3209
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3212
if (convert_subq_to_sj(this, *in_subq))
3216
/* 3. Finalize those we didn't convert */
3217
for (; in_subq!= in_subq_end; in_subq++)
3219
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3220
Item_subselect::trans_res res;
3221
(*in_subq)->changed= 0;
3222
(*in_subq)->fixed= 0;
3223
res= (*in_subq)->select_transformer(child_join);
3224
if (res == Item_subselect::RES_ERROR)
3227
(*in_subq)->changed= 1;
3228
(*in_subq)->fixed= 1;
3230
Item *substitute= (*in_subq)->substitution;
3231
bool do_fix_fields= !(*in_subq)->substitution->fixed;
3232
if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3235
//if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3236
// DBUG_RETURN(TRUE);
3238
sj_subselects.clear();
3244
Setup for execution all subqueries of a query, for which the optimizer
3245
chose hash semi-join.
3247
@details Iterate over all subqueries of the query, and if they are under an
3248
IN predicate, and the optimizer chose to compute it via hash semi-join:
3249
- try to initialize all data structures needed for the materialized execution
3250
of the IN predicate,
3251
- if this fails, then perform the IN=>EXISTS transformation which was
3252
previously blocked during JOIN::prepare.
3254
This method is part of the "code generation" query processing phase.
3256
This phase must be called after substitute_for_best_equal_field() because
3257
that function may replace items with other items from a multiple equality,
3258
and we need to reference the correct items in the index access method of the
3261
@return Operation status
3262
@retval FALSE success.
3263
@retval TRUE error occurred.
3266
bool JOIN::setup_subquery_materialization()
3268
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3269
un= un->next_unit())
3271
for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3273
Item_subselect *subquery_predicate= sl->master_unit()->item;
3274
if (subquery_predicate &&
3275
subquery_predicate->substype() == Item_subselect::IN_SUBS)
3277
Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3278
if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3279
in_subs->setup_engine())
3289
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3292
find_eq_ref_candidate()
3293
table Table to be checked
3294
sj_inner_tables Bitmap of inner tables. eq_ref(inner_table) doesn't
3298
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3301
Check again if it is feasible to factor common parts with constant table
3305
TRUE - There exists an eq_ref(outer-tables) candidate
3309
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3311
KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3316
while (1) /* For each key */
3319
KEY *keyinfo= table->key_info + key;
3320
key_part_map bound_parts= 0;
3321
if ((keyinfo->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME)
3323
do /* For all equalities on all key parts */
3325
/* Check if this is "t.keypart = expr(outer_tables) */
3326
if (!(keyuse->used_tables & sj_inner_tables) &&
3327
!(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3329
bound_parts |= 1 << keyuse->keypart;
3332
} while (keyuse->key == key && keyuse->table == table);
3334
if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3336
if (keyuse->table != table)
3344
if (keyuse->table != table)
3347
while (keyuse->key == key);
3356
Pull tables out of semi-join nests, if possible
3359
pull_out_semijoin_tables()
3360
join The join where to do the semi-join flattening
3363
Try to pull tables out of semi-join nests.
3366
When this function is called, the join may have several semi-join nests
3367
(possibly within different semi-join nests), but it is guaranteed that
3368
one semi-join nest does not contain another.
3371
A table can be pulled out of the semi-join nest if
3372
- It is a constant table
3376
* Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3378
* Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3379
* Semi-join nests TABLE_LIST::sj_inner_tables
3381
This operation is (and should be) performed at each PS execution since
3382
tables may become/cease to be constant across PS reexecutions.
3386
1 - Out of memory error
3389
int pull_out_semijoin_tables(JOIN *join)
3391
TABLE_LIST *sj_nest;
3392
DBUG_ENTER("pull_out_semijoin_tables");
3393
List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3395
/* Try pulling out of the each of the semi-joins */
3396
while ((sj_nest= sj_list_it++))
3398
/* Action #1: Mark the constant tables to be pulled out */
3399
table_map pulled_tables= 0;
3401
List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3403
while ((tbl= child_li++))
3407
tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3408
if (tbl->table->map & join->const_table_map)
3410
pulled_tables |= tbl->table->map;
3411
DBUG_PRINT("info", ("Table %s pulled out (reason: constant)",
3412
tbl->table->alias));
3418
Action #2: Find which tables we can pull out based on
3419
update_ref_and_keys() data. Note that pulling one table out can allow
3420
us to pull out some other tables too.
3422
bool pulled_a_table;
3425
pulled_a_table= FALSE;
3427
while ((tbl= child_li++))
3429
if (tbl->table && !(pulled_tables & tbl->table->map))
3431
if (find_eq_ref_candidate(tbl->table,
3432
sj_nest->nested_join->used_tables &
3435
pulled_a_table= TRUE;
3436
pulled_tables |= tbl->table->map;
3437
DBUG_PRINT("info", ("Table %s pulled out (reason: func dep)",
3438
tbl->table->alias));
3442
} while (pulled_a_table);
3445
if ((sj_nest)->nested_join->used_tables == pulled_tables)
3447
(sj_nest)->sj_inner_tables= 0;
3448
DBUG_PRINT("info", ("All semi-join nest tables were pulled out"));
3449
while ((tbl= child_li++))
3452
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3457
/* Record the bitmap of inner tables, mark the inner tables */
3458
table_map inner_tables=(sj_nest)->nested_join->used_tables &
3460
(sj_nest)->sj_inner_tables= inner_tables;
3461
while ((tbl= child_li++))
3465
if (inner_tables & tbl->table->map)
3466
tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3468
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3476
/*****************************************************************************
3477
Create JOIN_TABS, make a guess about the table types,
3478
Approximate how many records will be used in each table
3479
*****************************************************************************/
3482
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3484
const key_map *keys,ha_rows limit)
3487
DBUG_ENTER("get_quick_record_count");
3488
if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
3489
DBUG_RETURN(0); // Fatal error flag is set
3493
table->reginfo.impossible_range=0;
3494
if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
3495
limit, 0, FALSE)) == 1)
3496
DBUG_RETURN(select->quick->records);
3499
table->reginfo.impossible_range=1;
3502
DBUG_PRINT("warning",("Couldn't use record count on const keypart"));
3504
DBUG_RETURN(HA_POS_ERROR); /* This shouldn't happend */
3508
This structure is used to collect info on potentially sargable
3509
predicates in order to check whether they become sargable after
3510
reading const tables.
3511
We form a bitmap of indexes that can be used for sargable predicates.
3512
Only such indexes are involved in range analysis.
3514
typedef struct st_sargable_param
3516
Field *field; /* field against which to check sargability */
3517
Item **arg_value; /* values of potential keys for lookups */
3518
uint num_values; /* number of values in the above array */
3522
Calculate the best possible join and initialize the join structure.
3531
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3532
DYNAMIC_ARRAY *keyuse_array)
3536
uint i,table_count,const_count,key;
3537
table_map found_const_table_map, all_table_map, found_ref, refs;
3538
key_map const_ref, eq_part;
3539
TABLE **table_vector;
3540
JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3541
KEYUSE *keyuse,*start_keyuse;
3542
table_map outer_join=0;
3543
SARGABLE_PARAM *sargables= 0;
3544
JOIN_TAB *stat_vector[MAX_TABLES+1];
3545
DBUG_ENTER("make_join_statistics");
3547
table_count=join->tables;
3548
stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3549
stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3550
table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3551
if (!stat || !stat_ref || !table_vector)
3552
DBUG_RETURN(1); // Eom /* purecov: inspected */
3554
join->best_ref=stat_vector;
3556
stat_end=stat+table_count;
3557
found_const_table_map= all_table_map=0;
3562
s++, tables= tables->next_leaf, i++)
3564
TABLE_LIST *embedding= tables->embedding;
3567
s->const_keys.init();
3568
s->checked_keys.init();
3569
s->needed_reg.init();
3570
table_vector[i]=s->table=table=tables->table;
3571
table->pos_in_table_list= tables;
3572
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3575
table->file->print_error(error, MYF(0));
3578
table->quick_keys.clear_all();
3579
table->reginfo.join_tab=s;
3580
table->reginfo.not_exists_optimize=0;
3581
bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
3582
all_table_map|= table->map;
3584
s->info=0; // For describe
3586
s->dependent= tables->dep_tables;
3587
s->key_dependent= 0;
3588
if (tables->schema_table)
3589
table->file->stats.records= 2;
3590
table->quick_condition_rows= table->file->stats.records;
3592
s->on_expr_ref= &tables->on_expr;
3593
if (*s->on_expr_ref)
3595
/* s is the only inner table of an outer join */
3596
if (!table->file->stats.records && !embedding)
3598
s->dependent= 0; // Ignore LEFT JOIN depend.
3599
set_position(join,const_count++,s,(KEYUSE*) 0);
3602
outer_join|= table->map;
3603
s->embedding_map= 0;
3604
for (;embedding; embedding= embedding->embedding)
3605
s->embedding_map|= embedding->nested_join->nj_map;
3608
if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3610
/* s belongs to a nested join, maybe to several embedded joins */
3611
s->embedding_map= 0;
3614
NESTED_JOIN *nested_join= embedding->nested_join;
3615
s->embedding_map|=nested_join->nj_map;
3616
s->dependent|= embedding->dep_tables;
3617
embedding= embedding->embedding;
3618
outer_join|= nested_join->used_tables;
3623
if ((table->s->system || table->file->stats.records <= 1) &&
3625
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3626
!table->fulltext_searched && !join->no_const_tables)
3628
set_position(join,const_count++,s,(KEYUSE*) 0);
3632
join->outer_join=outer_join;
3634
if (join->outer_join)
3637
Build transitive closure for relation 'to be dependent on'.
3638
This will speed up the plan search for many cases with outer joins,
3639
as well as allow us to catch illegal cross references/
3640
Warshall's algorithm is used to build the transitive closure.
3641
As we use bitmaps to represent the relation the complexity
3642
of the algorithm is O((number of tables)^2).
3644
for (i= 0, s= stat ; i < table_count ; i++, s++)
3646
for (uint j= 0 ; j < table_count ; j++)
3648
table= stat[j].table;
3649
if (s->dependent & table->map)
3650
s->dependent |= table->reginfo.join_tab->dependent;
3653
s->table->maybe_null= 1;
3655
/* Catch illegal cross references for outer joins */
3656
for (i= 0, s= stat ; i < table_count ; i++, s++)
3658
if (s->dependent & s->table->map)
3660
join->tables=0; // Don't use join->table
3661
my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3664
s->key_dependent= s->dependent;
3668
if (conds || outer_join)
3669
if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3670
conds, join->cond_equal,
3671
~outer_join, join->select_lex, &sargables))
3674
/* Read tables with 0 or 1 rows (system tables) */
3675
join->const_table_map= 0;
3677
for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3684
join->const_table_map|=s->table->map;
3685
if ((tmp=join_read_const_table(s, p_pos)))
3688
DBUG_RETURN(1); // Fatal error
3691
found_const_table_map|= s->table->map;
3694
/* loop until no more const tables are found */
3698
more_const_tables_found:
3703
We only have to loop from stat_vector + const_count as
3704
set_position() will move all const_tables first in stat_vector
3707
for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3712
If equi-join condition by a key is null rejecting and after a
3713
substitution of a const table the key value happens to be null
3714
then we can state that there are no matches for this equi-join.
3716
if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3719
When performing an outer join operation if there are no matching rows
3720
for the single row of the outer table all the inner tables are to be
3721
null complemented and thus considered as constant tables.
3722
Here we apply this consideration to the case of outer join operations
3723
with a single inner table only because the case with nested tables
3724
would require a more thorough analysis.
3725
TODO. Apply single row substitution to null complemented inner tables
3726
for nested outer join operations.
3728
while (keyuse->table == table)
3730
if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3731
keyuse->val->is_null() && keyuse->null_rejecting)
3734
mark_as_null_row(table);
3735
found_const_table_map|= table->map;
3736
join->const_table_map|= table->map;
3737
set_position(join,const_count++,s,(KEYUSE*) 0);
3738
goto more_const_tables_found;
3744
if (s->dependent) // If dependent on some table
3746
// All dep. must be constants
3747
if (s->dependent & ~(found_const_table_map))
3749
if (table->file->stats.records <= 1L &&
3750
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3751
!table->pos_in_table_list->embedding)
3755
join->const_table_map|=table->map;
3756
set_position(join,const_count++,s,(KEYUSE*) 0);
3757
if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3760
DBUG_RETURN(1); // Fatal error
3763
found_const_table_map|= table->map;
3767
/* check if table can be read by key or table only uses const refs */
3768
if ((keyuse=s->keyuse))
3771
while (keyuse->table == table)
3773
start_keyuse=keyuse;
3775
s->keys.set_bit(key); // QQ: remove this ?
3778
const_ref.clear_all();
3779
eq_part.clear_all();
3782
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3784
if (!((~found_const_table_map) & keyuse->used_tables))
3785
const_ref.set_bit(keyuse->keypart);
3787
refs|=keyuse->used_tables;
3788
eq_part.set_bit(keyuse->keypart);
3791
} while (keyuse->table == table && keyuse->key == key);
3793
if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3794
!table->fulltext_searched &&
3795
!table->pos_in_table_list->embedding)
3797
if ((table->key_info[key].flags & (HA_NOSAME | HA_END_SPACE_KEY))
3800
if (const_ref == eq_part)
3801
{ // Found everything for ref.
3805
join->const_table_map|=table->map;
3806
set_position(join,const_count++,s,start_keyuse);
3807
if (create_ref_for_key(join, s, start_keyuse,
3808
found_const_table_map))
3810
if ((tmp=join_read_const_table(s,
3811
join->positions+const_count-1)))
3814
DBUG_RETURN(1); // Fatal error
3817
found_const_table_map|= table->map;
3821
found_ref|= refs; // Table is const if all refs are const
3823
else if (const_ref == eq_part)
3824
s->const_keys.set_bit(key);
3829
} while (join->const_table_map & found_ref && ref_changed);
3832
Update info on indexes that can be used for search lookups as
3833
reading const tables may has added new sargable predicates.
3835
if (const_count && sargables)
3837
for( ; sargables->field ; sargables++)
3839
Field *field= sargables->field;
3840
JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3841
key_map possible_keys= field->key_start;
3842
possible_keys.intersect(field->table->keys_in_use_for_query);
3844
for (uint j=0; j < sargables->num_values; j++)
3845
is_const&= sargables->arg_value[j]->const_item();
3847
join_tab[0].const_keys.merge(possible_keys);
3851
if (pull_out_semijoin_tables(join))
3854
/* Calc how many (possible) matched records in each table */
3856
for (s=stat ; s < stat_end ; s++)
3858
if (s->type == JT_SYSTEM || s->type == JT_CONST)
3860
/* Only one matching row */
3861
s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3864
/* Approximate found rows and time to read them */
3865
s->found_records=s->records=s->table->file->stats.records;
3866
s->read_time=(ha_rows) s->table->file->scan_time();
3869
Set a max range of how many seeks we can expect when using keys
3870
This is can't be to high as otherwise we are likely to use
3873
s->worst_seeks= min((double) s->found_records / 10,
3874
(double) s->read_time*3);
3875
if (s->worst_seeks < 2.0) // Fix for small tables
3879
Add to stat->const_keys those indexes for which all group fields or
3880
all select distinct fields participate in one index.
3882
add_group_and_distinct_keys(join, s);
3884
if (!s->const_keys.is_clear_all() &&
3885
!s->table->pos_in_table_list->embedding)
3889
select= make_select(s->table, found_const_table_map,
3890
found_const_table_map,
3891
*s->on_expr_ref ? *s->on_expr_ref : conds,
3895
records= get_quick_record_count(join->thd, select, s->table,
3896
&s->const_keys, join->row_limit);
3897
s->quick=select->quick;
3898
s->needed_reg=select->needed_reg;
3900
if (records == 0 && s->table->reginfo.impossible_range)
3903
Impossible WHERE or ON expression
3904
In case of ON, we mark that the we match one empty NULL row.
3905
In case of WHERE, don't set found_const_table_map to get the
3906
caller to abort with a zero row result.
3908
join->const_table_map|= s->table->map;
3909
set_position(join,const_count++,s,(KEYUSE*) 0);
3911
if (*s->on_expr_ref)
3913
/* Generate empty row */
3914
s->info= "Impossible ON condition";
3915
found_const_table_map|= s->table->map;
3917
mark_as_null_row(s->table); // All fields are NULL
3920
if (records != HA_POS_ERROR)
3922
s->found_records=records;
3923
s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3929
join->join_tab=stat;
3930
join->map2table=stat_ref;
3931
join->table= join->all_tables=table_vector;
3932
join->const_tables=const_count;
3933
join->found_const_table_map=found_const_table_map;
3935
/* Find an optimal join order of the non-constant tables. */
3936
if (join->const_tables != join->tables)
3938
optimize_keyuse(join, keyuse_array);
3939
if (choose_plan(join, all_table_map & ~join->const_table_map))
3944
memcpy((uchar*) join->best_positions,(uchar*) join->positions,
3945
sizeof(POSITION)*join->const_tables);
3946
join->best_read=1.0;
3948
/* Generate an execution plan from the found optimal join order. */
3949
DBUG_RETURN(join->thd->killed || get_best_combination(join));
3953
/*****************************************************************************
3954
Check with keys are used and with tables references with tables
3956
keys Bitmap of all used keys
3957
const_keys Bitmap of all keys with may be used with quick_select
3958
keyuse Pointer to possible keys
3959
*****************************************************************************/
3961
/// Used when finding key fields
3962
typedef struct key_field_t {
3964
Item *val; ///< May be empty if diff constant
3966
uint optimize; // KEY_OPTIMIZE_*
3969
If true, the condition this struct represents will not be satisfied
3972
bool null_rejecting;
3973
bool *cond_guard; /* See KEYUSE::cond_guard */
3974
uint sj_pred_no; /* See KEYUSE::sj_pred_no */
3978
Merge new key definitions to old ones, remove those not used in both.
3980
This is called for OR between different levels.
3982
To be able to do 'ref_or_null' we merge a comparison of a column
3983
and 'column IS NULL' to one test. This is useful for sub select queries
3984
that are internally transformed to something like:.
3987
SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
3990
KEY_FIELD::null_rejecting is processed as follows: @n
3991
result has null_rejecting=true if it is set for both ORed references.
3993
- (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true
3994
- (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3997
The result of this is that we're missing some 'ref' accesses.
3998
OptimizerTeam: Fix this
4002
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
4005
if (start == new_fields)
4006
return start; // Impossible or
4007
if (new_fields == end)
4008
return start; // No new fields, skip all
4010
KEY_FIELD *first_free=new_fields;
4012
/* Mark all found fields in old array */
4013
for (; new_fields != end ; new_fields++)
4015
for (KEY_FIELD *old=start ; old != first_free ; old++)
4017
if (old->field == new_fields->field)
4020
NOTE: below const_item() call really works as "!used_tables()", i.e.
4021
it can return FALSE where it is feasible to make it return TRUE.
4023
The cause is as follows: Some of the tables are already known to be
4024
const tables (the detection code is in make_join_statistics(),
4025
above the update_ref_and_keys() call), but we didn't propagate
4026
information about this: TABLE::const_table is not set to TRUE, and
4027
Item::update_used_tables() hasn't been called for each item.
4028
The result of this is that we're missing some 'ref' accesses.
4029
TODO: OptimizerTeam: Fix this
4031
if (!new_fields->val->const_item())
4034
If the value matches, we can use the key reference.
4035
If not, we keep it until we have examined all new values
4037
if (old->val->eq(new_fields->val, old->field->binary()))
4039
old->level= and_level;
4040
old->optimize= ((old->optimize & new_fields->optimize &
4041
KEY_OPTIMIZE_EXISTS) |
4042
((old->optimize | new_fields->optimize) &
4043
KEY_OPTIMIZE_REF_OR_NULL));
4044
old->null_rejecting= (old->null_rejecting &&
4045
new_fields->null_rejecting);
4048
else if (old->eq_func && new_fields->eq_func &&
4049
old->val->eq_by_collation(new_fields->val,
4050
old->field->binary(),
4051
old->field->charset()))
4054
old->level= and_level;
4055
old->optimize= ((old->optimize & new_fields->optimize &
4056
KEY_OPTIMIZE_EXISTS) |
4057
((old->optimize | new_fields->optimize) &
4058
KEY_OPTIMIZE_REF_OR_NULL));
4059
old->null_rejecting= (old->null_rejecting &&
4060
new_fields->null_rejecting);
4062
else if (old->eq_func && new_fields->eq_func &&
4063
((old->val->const_item() && old->val->is_null()) ||
4064
new_fields->val->is_null()))
4066
/* field = expression OR field IS NULL */
4067
old->level= and_level;
4068
old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
4070
Remember the NOT NULL value unless the value does not depend
4073
if (!old->val->used_tables() && old->val->is_null())
4074
old->val= new_fields->val;
4075
/* The referred expression can be NULL: */
4076
old->null_rejecting= 0;
4081
We are comparing two different const. In this case we can't
4082
use a key-lookup on this so it's better to remove the value
4083
and let the range optimzier handle it
4085
if (old == --first_free) // If last item
4087
*old= *first_free; // Remove old value
4088
old--; // Retry this value
4093
/* Remove all not used items */
4094
for (KEY_FIELD *old=start ; old != first_free ;)
4096
if (old->level != and_level)
4097
{ // Not used in all levels
4098
if (old == --first_free)
4100
*old= *first_free; // Remove old value
4110
Add a possible key to array of possible keys if it's usable as a key
4112
@param key_fields Pointer to add key, if usable
4113
@param and_level And level, to be stored in KEY_FIELD
4114
@param cond Condition predicate
4115
@param field Field used in comparision
4116
@param eq_func True if we used =, <=> or IS NULL
4117
@param value Value used for comparison with field
4118
@param usable_tables Tables which can be used for key optimization
4119
@param sargables IN/OUT Array of found sargable candidates
4122
If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4123
table, we store this to be able to do not exists optimization later.
4126
*key_fields is incremented if we stored a key in the array
4130
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4131
Field *field, bool eq_func, Item **value, uint num_values,
4132
table_map usable_tables, SARGABLE_PARAM **sargables)
4134
uint exists_optimize= 0;
4135
if (!(field->flags & PART_KEY_FLAG))
4137
// Don't remove column IS NULL on a LEFT JOIN table
4138
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4139
!field->table->maybe_null || field->null_ptr)
4140
return; // Not a key. Skip it
4141
exists_optimize= KEY_OPTIMIZE_EXISTS;
4142
DBUG_ASSERT(num_values == 1);
4146
table_map used_tables=0;
4148
for (uint i=0; i<num_values; i++)
4150
used_tables|=(value[i])->used_tables();
4151
if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4156
if (!(usable_tables & field->table->map))
4158
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4159
!field->table->maybe_null || field->null_ptr)
4160
return; // Can't use left join optimize
4161
exists_optimize= KEY_OPTIMIZE_EXISTS;
4165
JOIN_TAB *stat=field->table->reginfo.join_tab;
4166
key_map possible_keys=field->key_start;
4167
possible_keys.intersect(field->table->keys_in_use_for_query);
4168
stat[0].keys.merge(possible_keys); // Add possible keys
4171
Save the following cases:
4173
Field LIKE constant where constant doesn't start with a wildcard
4174
Field = field2 where field2 is in a different table
4181
stat[0].key_dependent|=used_tables;
4184
for (uint i=0; i<num_values; i++)
4186
if (!(is_const&= value[i]->const_item()))
4190
stat[0].const_keys.merge(possible_keys);
4194
Save info to be able check whether this predicate can be
4195
considered as sargable for range analisis after reading const tables.
4196
We do not save info about equalities as update_const_equal_items
4197
will take care of updating info on keys from sargable equalities.
4200
(*sargables)->field= field;
4201
(*sargables)->arg_value= value;
4202
(*sargables)->num_values= num_values;
4205
We can't always use indexes when comparing a string index to a
4206
number. cmp_type() is checked to allow compare of dates to numbers.
4207
eq_func is NEVER true when num_values > 1
4212
Additional optimization: if we're processing
4213
"t.key BETWEEN c1 AND c1" then proceed as if we were processing
4215
TODO: This is a very limited fix. A more generic fix is possible.
4216
There are 2 options:
4217
A) Make equality propagation code be able to handle BETWEEN
4218
(including cases like t1.key BETWEEN t2.key AND t3.key)
4219
B) Make range optimizer to infer additional "t.key = c" equalities
4220
and use them in equality propagation process (see details in
4223
if ((cond->functype() != Item_func::BETWEEN) ||
4224
((Item_func_between*) cond)->negated ||
4225
!value[0]->eq(value[1], field->binary()))
4230
if (field->result_type() == STRING_RESULT)
4232
if ((*value)->result_type() != STRING_RESULT)
4234
if (field->cmp_type() != (*value)->result_type())
4240
We can't use indexes if the effective collation
4241
of the operation differ from the field collation.
4243
if (field->cmp_type() == STRING_RESULT &&
4244
((Field_str*)field)->charset() != cond->compare_collation())
4251
For the moment eq_func is always true. This slot is reserved for future
4252
extensions where we want to remembers other things than just eq comparisons
4254
DBUG_ASSERT(eq_func);
4255
/* Store possible eq field */
4256
(*key_fields)->field= field;
4257
(*key_fields)->eq_func= eq_func;
4258
(*key_fields)->val= *value;
4259
(*key_fields)->level= and_level;
4260
(*key_fields)->optimize= exists_optimize;
4262
If the condition has form "tbl.keypart = othertbl.field" and
4263
othertbl.field can be NULL, there will be no matches if othertbl.field
4265
We use null_rejecting in add_not_null_conds() to add
4266
'othertbl.field IS NOT NULL' to tab->select_cond.
4268
(*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4269
cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4270
((*value)->type() == Item::FIELD_ITEM) &&
4271
((Item_field*)*value)->field->maybe_null());
4272
(*key_fields)->cond_guard= NULL;
4273
(*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
4274
cond->name < subq_sj_cond_name + 64)?
4275
cond->name - subq_sj_cond_name: UINT_MAX;
4280
Add possible keys to array of possible keys originated from a simple
4283
@param key_fields Pointer to add key, if usable
4284
@param and_level And level, to be stored in KEY_FIELD
4285
@param cond Condition predicate
4286
@param field Field used in comparision
4287
@param eq_func True if we used =, <=> or IS NULL
4288
@param value Value used for comparison with field
4289
Is NULL for BETWEEN and IN
4290
@param usable_tables Tables which can be used for key optimization
4291
@param sargables IN/OUT Array of found sargable candidates
4294
If field items f1 and f2 belong to the same multiple equality and
4295
a key is added for f1, the the same key is added for f2.
4298
*key_fields is incremented if we stored a key in the array
4302
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4303
Item_func *cond, Item_field *field_item,
4304
bool eq_func, Item **val,
4305
uint num_values, table_map usable_tables,
4306
SARGABLE_PARAM **sargables)
4308
Field *field= field_item->field;
4309
add_key_field(key_fields, and_level, cond, field,
4310
eq_func, val, num_values, usable_tables, sargables);
4311
Item_equal *item_equal= field_item->item_equal;
4315
Add to the set of possible key values every substitution of
4316
the field for an equal field included into item_equal
4318
Item_equal_iterator it(*item_equal);
4320
while ((item= it++))
4322
if (!field->eq(item->field))
4324
add_key_field(key_fields, and_level, cond, item->field,
4325
eq_func, val, num_values, usable_tables,
4333
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4334
COND *cond, table_map usable_tables,
4335
SARGABLE_PARAM **sargables)
4337
if (cond->type() == Item_func::COND_ITEM)
4339
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4340
KEY_FIELD *org_key_fields= *key_fields;
4342
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4346
add_key_fields(join, key_fields, and_level, item, usable_tables,
4348
for (; org_key_fields != *key_fields ; org_key_fields++)
4349
org_key_fields->level= *and_level;
4354
add_key_fields(join, key_fields, and_level, li++, usable_tables,
4359
KEY_FIELD *start_key_fields= *key_fields;
4361
add_key_fields(join, key_fields, and_level, item, usable_tables,
4363
*key_fields=merge_key_fields(org_key_fields,start_key_fields,
4364
*key_fields,++(*and_level));
4371
Subquery optimization: Conditions that are pushed down into subqueries
4372
are wrapped into Item_func_trig_cond. We process the wrapped condition
4373
but need to set cond_guard for KEYUSE elements generated from it.
4376
if (cond->type() == Item::FUNC_ITEM &&
4377
((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4379
Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4380
if (!join->group_list && !join->order &&
4382
join->unit->item->substype() == Item_subselect::IN_SUBS &&
4383
!join->unit->is_union())
4385
KEY_FIELD *save= *key_fields;
4386
add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4388
// Indicate that this ref access candidate is for subquery lookup:
4389
for (; save != *key_fields; save++)
4390
save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4396
/* If item is of type 'field op field/constant' add it to key_fields */
4397
if (cond->type() != Item::FUNC_ITEM)
4399
Item_func *cond_func= (Item_func*) cond;
4400
switch (cond_func->select_optimize()) {
4401
case Item_func::OPTIMIZE_NONE:
4403
case Item_func::OPTIMIZE_KEY:
4407
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4408
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4410
values= cond_func->arguments()+1;
4411
if (cond_func->functype() == Item_func::NE_FUNC &&
4412
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4413
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4415
DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC ||
4416
cond_func->argument_count() != 2);
4417
add_key_equal_fields(key_fields, *and_level, cond_func,
4418
(Item_field*) (cond_func->key_item()->real_item()),
4420
cond_func->argument_count()-1,
4421
usable_tables, sargables);
4423
if (cond_func->functype() == Item_func::BETWEEN)
4425
values= cond_func->arguments();
4426
for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4428
Item_field *field_item;
4429
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4431
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4433
field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4434
add_key_equal_fields(key_fields, *and_level, cond_func,
4435
field_item, 0, values, 1, usable_tables,
4442
case Item_func::OPTIMIZE_OP:
4444
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4445
cond_func->functype() == Item_func::EQUAL_FUNC);
4447
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4448
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4450
add_key_equal_fields(key_fields, *and_level, cond_func,
4451
(Item_field*) (cond_func->arguments()[0])->real_item(),
4453
cond_func->arguments()+1, 1, usable_tables,
4456
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4457
cond_func->functype() != Item_func::LIKE_FUNC &&
4458
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4460
add_key_equal_fields(key_fields, *and_level, cond_func,
4461
(Item_field*) (cond_func->arguments()[1])->real_item(),
4463
cond_func->arguments(),1,usable_tables,
4468
case Item_func::OPTIMIZE_NULL:
4469
/* column_name IS [NOT] NULL */
4470
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4471
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4473
Item *tmp=new Item_null;
4474
if (unlikely(!tmp)) // Should never be true
4476
add_key_equal_fields(key_fields, *and_level, cond_func,
4477
(Item_field*) (cond_func->arguments()[0])->real_item(),
4478
cond_func->functype() == Item_func::ISNULL_FUNC,
4479
&tmp, 1, usable_tables, sargables);
4482
case Item_func::OPTIMIZE_EQUAL:
4483
Item_equal *item_equal= (Item_equal *) cond;
4484
Item *const_item= item_equal->get_const();
4485
Item_equal_iterator it(*item_equal);
4490
For each field field1 from item_equal consider the equality
4491
field1=const_item as a condition allowing an index access of the table
4492
with field1 by the keys value of field1.
4494
while ((item= it++))
4496
add_key_field(key_fields, *and_level, cond_func, item->field,
4497
TRUE, &const_item, 1, usable_tables, sargables);
4503
Consider all pairs of different fields included into item_equal.
4504
For each of them (field1, field1) consider the equality
4505
field1=field2 as a condition allowing an index access of the table
4506
with field1 by the keys value of field2.
4508
Item_equal_iterator fi(*item_equal);
4509
while ((item= fi++))
4511
Field *field= item->field;
4512
while ((item= it++))
4514
if (!field->eq(item->field))
4516
add_key_field(key_fields, *and_level, cond_func, field,
4517
TRUE, (Item **) &item, 1, usable_tables,
4529
Add all keys with uses 'field' for some keypart.
4531
If field->and_level != and_level then only mark key_part as const_part.
4535
max_part_bit(key_part_map bits)
4538
for (found=0; bits & 1 ; found++,bits>>=1) ;
4543
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4545
Field *field=key_field->field;
4546
TABLE *form= field->table;
4549
if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4551
for (uint key=0 ; key < form->s->keys ; key++)
4553
if (!(form->keys_in_use_for_query.is_set(key)))
4556
uint key_parts= (uint) form->key_info[key].key_parts;
4557
for (uint part=0 ; part < key_parts ; part++)
4559
if (field->eq(form->key_info[key].key_part[part].field))
4561
keyuse.table= field->table;
4562
keyuse.val = key_field->val;
4564
keyuse.keypart=part;
4565
keyuse.keypart_map= (key_part_map) 1 << part;
4566
keyuse.used_tables=key_field->val->used_tables();
4567
keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4568
keyuse.null_rejecting= key_field->null_rejecting;
4569
keyuse.cond_guard= key_field->cond_guard;
4570
keyuse.sj_pred_no= key_field->sj_pred_no;
4571
VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4579
sort_keyuse(KEYUSE *a,KEYUSE *b)
4582
if (a->table->tablenr != b->table->tablenr)
4583
return (int) (a->table->tablenr - b->table->tablenr);
4584
if (a->key != b->key)
4585
return (int) (a->key - b->key);
4586
if (a->keypart != b->keypart)
4587
return (int) (a->keypart - b->keypart);
4588
// Place const values before other ones
4589
if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4590
test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
4592
/* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
4593
return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4594
(b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4599
Add to KEY_FIELD array all 'ref' access candidates within nested join.
4601
This function populates KEY_FIELD array with entries generated from the
4602
ON condition of the given nested join, and does the same for nested joins
4603
contained within this nested join.
4605
@param[in] nested_join_table Nested join pseudo-table to process
4606
@param[in,out] end End of the key field array
4607
@param[in,out] and_level And-level
4608
@param[in,out] sargables Array of found sargable candidates
4612
We can add accesses to the tables that are direct children of this nested
4613
join (1), and are not inner tables w.r.t their neighbours (2).
4615
Example for #1 (outer brackets pair denotes nested join this function is
4618
... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4622
... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4624
In examples 1-2 for condition cond, we can add 'ref' access candidates to
4628
... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4630
Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4633
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4634
KEY_FIELD **end, uint *and_level,
4635
SARGABLE_PARAM **sargables)
4637
List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4638
List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4639
bool have_another = FALSE;
4640
table_map tables= 0;
4642
DBUG_ASSERT(nested_join_table->nested_join);
4644
while ((table= li++) || (have_another && (li=li2, have_another=FALSE,
4647
if (table->nested_join)
4649
if (!table->on_expr)
4651
/* It's a semi-join nest. Walk into it as if it wasn't a nest */
4654
li= List_iterator<TABLE_LIST>(table->nested_join->join_list);
4657
add_key_fields_for_nj(join, table, end, and_level, sargables);
4660
if (!table->on_expr)
4661
tables |= table->table->map;
4663
if (nested_join_table->on_expr)
4664
add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
4670
Update keyuse array with all possible keys we can use to fetch rows.
4673
@param[out] keyuse Put here ordered array of KEYUSE structures
4674
@param join_tab Array in tablenr_order
4675
@param tables Number of tables in join
4676
@param cond WHERE condition (note that the function analyzes
4677
join_tab[i]->on_expr too)
4678
@param normal_tables Tables not inner w.r.t some outer join (ones
4679
for which we can make ref access based the WHERE
4681
@param select_lex current SELECT
4682
@param[out] sargables Array of found sargable candidates
4691
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4692
uint tables, COND *cond, COND_EQUAL *cond_equal,
4693
table_map normal_tables, SELECT_LEX *select_lex,
4694
SARGABLE_PARAM **sargables)
4696
uint and_level,i,found_eq_constant;
4697
KEY_FIELD *key_fields, *end, *field;
4699
uint m= max(select_lex->max_equal_elems,1);
4702
We use the same piece of memory to store both KEY_FIELD
4703
and SARGABLE_PARAM structure.
4704
KEY_FIELD values are placed at the beginning this memory
4705
while SARGABLE_PARAM values are put at the end.
4706
All predicates that are used to fill arrays of KEY_FIELD
4707
and SARGABLE_PARAM structures have at most 2 arguments
4708
except BETWEEN predicates that have 3 arguments and
4710
This any predicate if it's not BETWEEN/IN can be used
4711
directly to fill at most 2 array elements, either of KEY_FIELD
4712
or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
4713
can be filled as this predicate is considered as
4714
saragable with respect to each of its argument.
4715
An IN predicate can require at most 1 element as currently
4716
it is considered as sargable only for its first argument.
4717
Multiple equality can add elements that are filled after
4718
substitution of field arguments by equal fields. There
4719
can be not more than select_lex->max_equal_elems such
4722
sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4723
(((thd->lex->current_select->cond_count+1)*2 +
4724
thd->lex->current_select->between_count)*m+1);
4725
if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
4726
return TRUE; /* purecov: inspected */
4728
field= end= key_fields;
4729
*sargables= (SARGABLE_PARAM *) key_fields +
4730
(sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
4731
/* set a barrier for the array of SARGABLE_PARAM */
4732
(*sargables)[0].field= 0;
4734
if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
4738
add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
4740
for (; field != end ; field++)
4742
add_key_part(keyuse,field);
4743
/* Mark that we can optimize LEFT JOIN */
4744
if (field->val->type() == Item::NULL_ITEM &&
4745
!field->field->real_maybe_null())
4746
field->field->table->reginfo.not_exists_optimize=1;
4749
for (i=0 ; i < tables ; i++)
4752
Block the creation of keys for inner tables of outer joins.
4753
Here only the outer joins that can not be converted to
4754
inner joins are left and all nests that can be eliminated
4756
In the future when we introduce conditional accesses
4757
for inner tables in outer joins these keys will be taken
4758
into account as well.
4760
if (*join_tab[i].on_expr_ref)
4761
add_key_fields(join_tab->join, &end, &and_level,
4762
*join_tab[i].on_expr_ref,
4763
join_tab[i].table->map, sargables);
4766
/* Process ON conditions for the nested joins */
4768
List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
4770
while ((table= li++))
4772
if (table->nested_join)
4773
add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
4778
/* fill keyuse with found key parts */
4779
for ( ; field != end ; field++)
4780
add_key_part(keyuse,field);
4783
Sort the array of possible keys and remove the following key parts:
4784
- ref if there is a keypart which is a ref and a const.
4785
(e.g. if there is a key(a,b) and the clause is a=3 and b=7 and b=t2.d,
4786
then we skip the key part corresponding to b=t2.d)
4787
- keyparts without previous keyparts
4788
(e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
4789
used in the query, we drop the partial key parts from consideration).
4790
Special treatment for ft-keys.
4792
if (keyuse->elements)
4794
KEYUSE key_end,*prev,*save_pos,*use;
4796
my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
4797
(qsort_cmp) sort_keyuse);
4799
bzero((char*) &key_end,sizeof(key_end)); /* Add for easy testing */
4800
VOID(insert_dynamic(keyuse,(uchar*) &key_end));
4802
use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
4804
found_eq_constant=0;
4805
for (i=0 ; i < keyuse->elements-1 ; i++,use++)
4807
if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
4808
use->table->const_key_parts[use->key]|= use->keypart_map;
4810
if (use->key == prev->key && use->table == prev->table)
4812
if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
4813
continue; /* remove */
4815
else if (use->keypart != 0) // First found must be 0
4820
/* Valgrind complains about overlapped memcpy when save_pos==use. */
4821
if (save_pos != use)
4825
found_eq_constant= !use->used_tables;
4826
/* Save ptr to first use */
4827
if (!use->table->reginfo.join_tab->keyuse)
4828
use->table->reginfo.join_tab->keyuse=save_pos;
4829
use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
4832
i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4833
VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4836
DBUG_EXECUTE("opt", print_keyuse_array(keyuse););
4841
Update some values in keyuse for faster choose_plan() loop.
4844
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
4846
KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
4848
for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
4852
If we find a ref, assume this table matches a proportional
4854
For example 100 records matching a table with 5000 records
4855
gives 5000/100 = 50 records per key
4856
Constant tables are ignored.
4857
To avoid bad matches, we don't make ref_table_rows less than 100.
4859
keyuse->ref_table_rows= ~(ha_rows) 0; // If no ref
4860
if (keyuse->used_tables &
4861
(map= (keyuse->used_tables & ~join->const_table_map &
4862
~OUTER_REF_TABLE_BIT)))
4865
for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4866
if (map == 1) // Only one table
4868
TABLE *tmp_table=join->all_tables[tablenr];
4869
keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
4873
Outer reference (external field) is constant for single executing
4876
if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
4877
keyuse->ref_table_rows= 1;
4883
Discover the indexes that can be used for GROUP BY or DISTINCT queries.
4885
If the query has a GROUP BY clause, find all indexes that contain all
4886
GROUP BY fields, and add those indexes to join->const_keys.
4888
If the query has a DISTINCT clause, find all indexes that contain all
4889
SELECT fields, and add those indexes to join->const_keys.
4890
This allows later on such queries to be processed by a
4891
QUICK_GROUP_MIN_MAX_SELECT.
4901
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
4903
List<Item_field> indexed_fields;
4904
List_iterator<Item_field> indexed_fields_it(indexed_fields);
4906
Item_field *cur_item;
4907
key_map possible_keys(0);
4909
if (join->group_list)
4910
{ /* Collect all query fields referenced in the GROUP clause. */
4911
for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4912
(*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4913
(uchar*) &indexed_fields);
4915
else if (join->select_distinct)
4916
{ /* Collect all query fields referenced in the SELECT clause. */
4917
List<Item> &select_items= join->fields_list;
4918
List_iterator<Item> select_items_it(select_items);
4920
while ((item= select_items_it++))
4921
item->walk(&Item::collect_item_field_processor, 0,
4922
(uchar*) &indexed_fields);
4927
if (indexed_fields.elements == 0)
4930
/* Intersect the keys of all group fields. */
4931
cur_item= indexed_fields_it++;
4932
possible_keys.merge(cur_item->field->part_of_key);
4933
while ((cur_item= indexed_fields_it++))
4935
possible_keys.intersect(cur_item->field->part_of_key);
4938
if (!possible_keys.is_clear_all())
4939
join_tab->const_keys.merge(possible_keys);
4943
/*****************************************************************************
4944
Go through all combinations of not marked tables and find the one
4945
which uses least records
4946
*****************************************************************************/
4948
/** Save const tables first as used tables. */
4951
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4953
join->positions[idx].table= table;
4954
join->positions[idx].key=key;
4955
join->positions[idx].records_read=1.0; /* This is a const table */
4956
join->positions[idx].ref_depend_map= 0;
4958
/* Move the const table as down as possible in best_ref */
4959
JOIN_TAB **pos=join->best_ref+idx+1;
4960
JOIN_TAB *next=join->best_ref[idx];
4961
for (;next != table ; pos++)
4963
JOIN_TAB *tmp=pos[0];
4967
join->best_ref[idx]=table;
4972
Given a semi-join nest, find out which of the IN-equalities are bound
4975
get_bound_sj_equalities()
4976
sj_nest Semi-join nest
4977
remaining_tables Tables that are not yet bound
4980
Given a semi-join nest, find out which of the IN-equalities have their
4981
left part expression bound (i.e. the said expression doesn't refer to
4982
any of remaining_tables and can be evaluated).
4985
Bitmap of bound IN-equalities.
4988
ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest,
4989
table_map remaining_tables)
4991
List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4995
while ((item= li++))
4998
Q: should this take into account equality propagation and how?
4999
A: If e->outer_side is an Item_field, walk over the equality
5000
class and see if there is an element that is bound?
5001
(this is an optional feature)
5003
if (!(item->used_tables() & remaining_tables))
5013
Find the best access path for an extension of a partial execution
5014
plan and add this path to the plan.
5016
The function finds the best access path to table 's' from the passed
5017
partial plan where an access path is the general term for any means to
5018
access the data in 's'. An access path may use either an index or a scan,
5019
whichever is cheaper. The input partial plan is passed via the array
5020
'join->positions' of length 'idx'. The chosen access method for 's' and its
5021
cost are stored in 'join->positions[idx]'.
5023
@param join pointer to the structure providing all context info
5025
@param s the table to be joined by the function
5026
@param thd thread for the connection that submitted the query
5027
@param remaining_tables set of tables not included into the partial plan yet
5028
@param idx the length of the partial plan
5029
@param record_count estimate for the number of records returned by the
5031
@param read_time the cost of the partial plan
5038
best_access_path(JOIN *join,
5041
table_map remaining_tables,
5043
double record_count,
5046
KEYUSE *best_key= 0;
5047
uint best_max_key_part= 0;
5048
my_bool found_constraint= 0;
5049
double best= DBL_MAX;
5050
double best_time= DBL_MAX;
5051
double records= DBL_MAX;
5052
table_map best_ref_depends_map= 0;
5055
uint best_is_sj_inside_out= 0;
5056
DBUG_ENTER("best_access_path");
5059
{ /* Use key if possible */
5060
TABLE *table= s->table;
5061
KEYUSE *keyuse,*start_key=0;
5062
double best_records= DBL_MAX;
5063
uint max_key_part=0;
5064
ulonglong bound_sj_equalities= 0;
5065
bool try_sj_inside_out= FALSE;
5067
Discover the bound equalites. We need to do this, if
5068
1. The next table is an SJ-inner table, and
5069
2. It is the first table from that semijoin, and
5070
3. We're not within a semi-join range (i.e. all semi-joins either have
5071
all or none of their tables in join_table_map), except
5072
s->emb_sj_nest (which we've just entered).
5073
3. All correlation references from this sj-nest are bound
5075
if (s->emb_sj_nest && // (1)
5076
s->emb_sj_nest->sj_in_exprs < 64 &&
5077
((remaining_tables & s->emb_sj_nest->sj_inner_tables) == // (2)
5078
s->emb_sj_nest->sj_inner_tables) && // (2)
5079
join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables && // (3)
5080
!(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
5082
/* This table is an InsideOut scan candidate */
5083
bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
5085
try_sj_inside_out= TRUE;
5088
/* Test how we can use keys */
5089
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key
5090
for (keyuse=s->keyuse ; keyuse->table == table ;)
5092
key_part_map found_part= 0;
5093
table_map found_ref= 0;
5094
uint key= keyuse->key;
5095
KEY *keyinfo= table->key_info+key;
5096
/* Bitmap of keyparts where the ref access is over 'keypart=const': */
5097
key_part_map const_part= 0;
5098
/* The or-null keypart in ref-or-null access: */
5099
key_part_map ref_or_null_part= 0;
5101
/* Calculate how many key segments of the current key we can use */
5103
ulonglong handled_sj_equalities=0;
5104
key_part_map sj_insideout_map= 0;
5106
do /* For each keypart */
5108
uint keypart= keyuse->keypart;
5109
table_map best_part_found_ref= 0;
5110
double best_prev_record_reads= DBL_MAX;
5112
do /* For each way to access the keypart */
5116
if 1. expression doesn't refer to forward tables
5117
2. we won't get two ref-or-null's
5119
if (!(remaining_tables & keyuse->used_tables) &&
5120
!(ref_or_null_part && (keyuse->optimize &
5121
KEY_OPTIMIZE_REF_OR_NULL)))
5123
found_part|= keyuse->keypart_map;
5124
if (!(keyuse->used_tables & ~join->const_table_map))
5125
const_part|= keyuse->keypart_map;
5127
double tmp2= prev_record_reads(join, idx, (found_ref |
5128
keyuse->used_tables));
5129
if (tmp2 < best_prev_record_reads)
5131
best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5132
best_prev_record_reads= tmp2;
5134
if (rec > keyuse->ref_table_rows)
5135
rec= keyuse->ref_table_rows;
5137
If there is one 'key_column IS NULL' expression, we can
5138
use this ref_or_null optimisation of this field
5140
if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5141
ref_or_null_part |= keyuse->keypart_map;
5144
if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5146
if (!(remaining_tables & keyuse->used_tables))
5147
bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5150
handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5151
sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5156
} while (keyuse->table == table && keyuse->key == key &&
5157
keyuse->keypart == keypart);
5158
found_ref|= best_part_found_ref;
5159
} while (keyuse->table == table && keyuse->key == key);
5162
Assume that that each key matches a proportional part of table.
5164
if (!found_part && !handled_sj_equalities)
5165
continue; // Nothing usable found
5167
if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5168
rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables
5170
bool sj_inside_out_scan= FALSE;
5172
found_constraint= 1;
5174
Check if InsideOut scan is applicable:
5175
1. All IN-equalities are either "bound" or "handled"
5176
2. Index keyparts are
5179
if (try_sj_inside_out &&
5180
table->covering_keys.is_set(key) &&
5181
(handled_sj_equalities | bound_sj_equalities) == // (1)
5182
PREV_BITS(ulonglong, s->emb_sj_nest->sj_in_exprs)) // (1)
5184
uint n_fixed_parts= max_part_bit(found_part);
5185
if (n_fixed_parts != keyinfo->key_parts &&
5186
(PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5187
PREV_BITS(uint, keyinfo->key_parts))
5190
Not all parts are fixed. Produce bitmap of remaining bits and
5191
check if all of them are covered.
5193
sj_inside_out_scan= TRUE;
5194
DBUG_PRINT("info", ("Using sj InsideOut scan"));
5198
It's a confluent ref scan.
5200
That is, all found KEYUSE elements refer to IN-equalities,
5201
and there is really no ref access because there is no
5202
t.keypart0 = {bound expression}
5204
Calculate the cost of complete loose index scan.
5206
records= (double)s->table->file->stats.records;
5208
/* The cost is entire index scan cost (divided by 2) */
5209
best_time= s->table->file->index_only_read_time(key, records);
5211
/* Now figure how many different keys we will get */
5213
if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5214
records= records / rpc;
5221
Check if we found full key
5223
if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5226
max_key_part= (uint) ~0;
5227
if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5229
tmp = prev_record_reads(join, idx, found_ref);
5235
{ /* We found a const key */
5237
ReuseRangeEstimateForRef-1:
5238
We get here if we've found a ref(const) (c_i are constants):
5239
"(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond]
5241
If range optimizer was able to construct a "range"
5242
access on this index, then its condition "quick_cond" was
5243
eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5244
from the range optimizer.
5246
Proof of (*): By properties of range and ref optimizers
5247
quick_cond will be equal or tighther than ref_const_cond.
5248
ref_const_cond already covers "smallest" possible interval -
5249
a singlepoint interval over all keyparts. Therefore,
5250
quick_cond is equivalent to ref_const_cond (if it was an
5251
empty interval we wouldn't have got here).
5253
if (table->quick_keys.is_set(key))
5254
records= (double) table->quick_rows[key];
5257
/* quick_range couldn't use key! */
5258
records= (double) s->records/rec;
5263
if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5264
{ /* Prefer longer keys */
5266
((double) s->records / (double) rec *
5268
((double) (table->s->max_key_length-keyinfo->key_length) /
5269
(double) table->s->max_key_length)));
5271
records=2.0; /* Can't be as good as a unique */
5274
ReuseRangeEstimateForRef-2: We get here if we could not reuse
5275
E(#rows) from range optimizer. Make another try:
5277
If range optimizer produced E(#rows) for a prefix of the ref
5278
access we're considering, and that E(#rows) is lower then our
5279
current estimate, make an adjustment. The criteria of when we
5280
can make an adjustment is a special case of the criteria used
5281
in ReuseRangeEstimateForRef-3.
5283
if (table->quick_keys.is_set(key) &&
5284
const_part & (1 << table->quick_key_parts[key]) &&
5285
table->quick_n_ranges[key] == 1 &&
5286
records > (double) table->quick_rows[key])
5288
records= (double) table->quick_rows[key];
5291
/* Limit the number of matched rows */
5293
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5294
if (table->covering_keys.is_set(key))
5296
/* we can use only index tree */
5297
tmp= record_count * table->file->index_only_read_time(key, tmp);
5300
tmp= record_count*min(tmp,s->worst_seeks);
5306
Use as much key-parts as possible and a uniq key is better
5307
than a not unique key
5308
Set tmp to (previous record count) * (records / combination)
5310
if ((found_part & 1) &&
5311
(!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5312
found_part == PREV_BITS(uint,keyinfo->key_parts)))
5314
max_key_part= max_part_bit(found_part);
5316
ReuseRangeEstimateForRef-3:
5317
We're now considering a ref[or_null] access via
5318
(t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
5319
(same-as-above but with one cond replaced
5320
with "t.keypart_i IS NULL")] (**)
5322
Try re-using E(#rows) from "range" optimizer:
5323
We can do so if "range" optimizer used the same intervals as
5324
in (**). The intervals used by range optimizer may be not
5325
available at this point (as "range" access might have choosen to
5326
create quick select over another index), so we can't compare
5327
them to (**). We'll make indirect judgements instead.
5328
The sufficient conditions for re-use are:
5329
(C1) All e_i in (**) are constants, i.e. found_ref==FALSE. (if
5330
this is not satisfied we have no way to know which ranges
5331
will be actually scanned by 'ref' until we execute the
5333
(C2) max #key parts in 'range' access == K == max_key_part (this
5334
is apparently a necessary requirement)
5336
We also have a property that "range optimizer produces equal or
5337
tighter set of scan intervals than ref(const) optimizer". Each
5338
of the intervals in (**) are "tightest possible" intervals when
5339
one limits itself to using keyparts 1..K (which we do in #2).
5340
From here it follows that range access used either one, or
5341
both of the (I1) and (I2) intervals:
5343
(t.keypart1=c1 AND ... AND t.keypartK=eK) (I1)
5344
(same-as-above but with one cond replaced
5345
with "t.keypart_i IS NULL") (I2)
5347
The remaining part is to exclude the situation where range
5348
optimizer used one interval while we're considering
5349
ref-or-null and looking for estimate for two intervals. This
5350
is done by last limitation:
5352
(C3) "range optimizer used (have ref_or_null?2:1) intervals"
5354
if (table->quick_keys.is_set(key) && !found_ref && //(C1)
5355
table->quick_key_parts[key] == max_key_part && //(C2)
5356
table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5358
tmp= records= (double) table->quick_rows[key];
5362
/* Check if we have statistic about the distribution */
5363
if ((records= keyinfo->rec_per_key[max_key_part-1]))
5366
Fix for the case where the index statistics is too
5368
(1) We're considering ref(const) and there is quick select
5370
(2) and that quick select uses more keyparts (i.e. it will
5371
scan equal/smaller interval then this ref(const))
5372
(3) and E(#rows) for quick select is higher then our
5375
We'll use E(#rows) from quick select.
5377
Q: Why do we choose to use 'ref'? Won't quick select be
5378
cheaper in some cases ?
5379
TODO: figure this out and adjust the plan choice if needed.
5381
if (!found_ref && table->quick_keys.is_set(key) && // (1)
5382
table->quick_key_parts[key] > max_key_part && // (2)
5383
records < (double)table->quick_rows[key]) // (3)
5384
records= (double)table->quick_rows[key];
5391
Assume that the first key part matches 1% of the file
5392
and that the whole key matches 10 (duplicates) or 1
5394
Assume also that more key matches proportionally more
5396
This gives the formula:
5397
records = (x * (b-a) + a*c-b)/(c-1)
5399
b = records matched by whole key
5400
a = records matched by first key part (1% of all records?)
5401
c = number of key parts in key
5402
x = used key parts (1 <= x <= c)
5405
if (!(rec_per_key=(double)
5406
keyinfo->rec_per_key[keyinfo->key_parts-1]))
5407
rec_per_key=(double) s->records/rec+1;
5411
else if (rec_per_key/(double) s->records >= 0.01)
5415
double a=s->records*0.01;
5416
if (keyinfo->key_parts > 1)
5417
tmp= (max_key_part * (rec_per_key - a) +
5418
a*keyinfo->key_parts - rec_per_key)/
5419
(keyinfo->key_parts-1);
5422
set_if_bigger(tmp,1.0);
5424
records = (ulong) tmp;
5427
if (ref_or_null_part)
5429
/* We need to do two key searches to find key */
5435
ReuseRangeEstimateForRef-4: We get here if we could not reuse
5436
E(#rows) from range optimizer. Make another try:
5438
If range optimizer produced E(#rows) for a prefix of the ref
5439
access we're considering, and that E(#rows) is lower then our
5440
current estimate, make the adjustment.
5442
The decision whether we can re-use the estimate from the range
5443
optimizer is the same as in ReuseRangeEstimateForRef-3,
5444
applied to first table->quick_key_parts[key] key parts.
5446
if (table->quick_keys.is_set(key) &&
5447
table->quick_key_parts[key] <= max_key_part &&
5448
const_part & (1 << table->quick_key_parts[key]) &&
5449
table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5451
records > (double) table->quick_rows[key])
5453
tmp= records= (double) table->quick_rows[key];
5457
/* Limit the number of matched rows */
5458
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5459
if (table->covering_keys.is_set(key))
5461
/* we can use only index tree */
5462
tmp= record_count * table->file->index_only_read_time(key, tmp);
5465
tmp= record_count * min(tmp,s->worst_seeks);
5468
tmp= best_time; // Do nothing
5471
if (sj_inside_out_scan && !start_key)
5479
if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5481
best_time= tmp + records/(double) TIME_FOR_COMPARE;
5483
best_records= records;
5484
best_key= start_key;
5485
best_max_key_part= max_key_part;
5486
best_ref_depends_map= found_ref;
5487
best_is_sj_inside_out= sj_inside_out_scan;
5490
records= best_records;
5494
Don't test table scan if it can't be better.
5495
Prefer key lookup if we would use the same key for scanning.
5497
Don't do a table scan on InnoDB tables, if we can read the used
5498
parts of the row from any of the used index.
5499
This is because table scans uses index and we would not win
5500
anything by using a table scan.
5502
A word for word translation of the below if-statement in sergefp's
5503
understanding: we check if we should use table scan if:
5504
(1) The found 'ref' access produces more records than a table scan
5505
(or index scan, or quick select), or 'ref' is more expensive than
5507
(2) This doesn't hold: the best way to perform table scan is to to perform
5508
'range' access using index IDX, and the best way to perform 'ref'
5509
access is to use the same index IDX, with the same or more key parts.
5510
(note: it is not clear how this rule is/should be extended to
5511
index_merge quick selects)
5512
(3) See above note about InnoDB.
5513
(4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5514
path, but there is no quick select)
5515
If the condition in the above brackets holds, then the only possible
5516
"table scan" access method is ALL/index (there is no quick select).
5517
Since we have a 'ref' access path, and FORCE INDEX instructs us to
5518
choose it over ALL/index, there is no need to consider a full table
5521
if ((records >= s->found_records || best > s->read_time) && // (1)
5522
!(s->quick && best_key && s->quick->index == best_key->key && // (2)
5523
best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5524
!((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3)
5525
! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5526
!(s->table->force_index && best_key && !s->quick)) // (4)
5527
{ // Check full join
5528
ha_rows rnd_records= s->found_records;
5530
If there is a filtering condition on the table (i.e. ref analyzer found
5531
at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5532
preceding this table in the join order we're now considering), then
5533
assume that 25% of the rows will be filtered out by this condition.
5535
This heuristic is supposed to force tables used in exprZ to be before
5536
this table in join order.
5538
if (found_constraint)
5539
rnd_records-= rnd_records/4;
5542
If applicable, get a more accurate estimate. Don't use the two
5545
if (s->table->quick_condition_rows != s->found_records)
5546
rnd_records= s->table->quick_condition_rows;
5549
Range optimizer never proposes a RANGE if it isn't better
5550
than FULL: so if RANGE is present, it's always preferred to FULL.
5551
Here we estimate its cost.
5557
- read record range through 'quick'
5558
- skip rows which does not satisfy WHERE constraints
5560
We take into account possible use of join cache for ALL/index
5561
access (see first else-branch below), but we don't take it into
5562
account here for range/index_merge access. Find out why this is so.
5565
(s->quick->read_time +
5566
(s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5570
/* Estimate cost of reading table. */
5571
tmp= s->table->file->scan_time();
5572
if (s->table->map & join->outer_join) // Can't use join cache
5575
For each record we have to:
5576
- read the whole table record
5577
- skip rows which does not satisfy join condition
5581
(s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5585
/* We read the table as many times as join buffer becomes full. */
5586
tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5588
(double) thd->variables.join_buff_size));
5590
We don't make full cartesian product between rows in the scanned
5591
table and existing records because we skip all rows from the
5592
scanned table, which does not satisfy join condition when
5593
we read the table (see flush_cached_records for details). Here we
5594
take into account cost to read and skip these records.
5596
tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5601
We estimate the cost of evaluating WHERE clause for found records
5602
as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5603
tmp give us total cost of using TABLE SCAN
5605
if (best == DBL_MAX ||
5606
(tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5607
best + record_count/(double) TIME_FOR_COMPARE*records))
5610
If the table has a range (s->quick is set) make_join_select()
5611
will ensure that this will be used
5614
records= rows2double(rnd_records);
5616
/* range/index_merge/ALL/index access method are "independent", so: */
5617
best_ref_depends_map= 0;
5618
best_is_sj_inside_out= FALSE;
5622
/* Update the cost information for the current partial plan */
5623
join->positions[idx].records_read= records;
5624
join->positions[idx].read_time= best;
5625
join->positions[idx].key= best_key;
5626
join->positions[idx].table= s;
5627
join->positions[idx].ref_depend_map= best_ref_depends_map;
5628
join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5631
idx == join->const_tables &&
5632
s->table == join->sort_by_table &&
5633
join->unit->select_limit_cnt >= records)
5634
join->sort_by_table= (TABLE*) 1; // Must use temporary table
5641
Selects and invokes a search strategy for an optimal query plan.
5643
The function checks user-configurable parameters that control the search
5644
strategy for an optimal plan, selects the search method and then invokes
5645
it. Each specific optimization procedure stores the final optimal plan in
5646
the array 'join->best_positions', and the cost of the plan in
5649
@param join pointer to the structure providing all context info for
5651
@param join_tables set of the tables in the query
5654
'MAX_TABLES+2' denotes the old implementation of find_best before
5655
the greedy version. Will be removed when greedy_search is approved.
5664
choose_plan(JOIN *join, table_map join_tables)
5666
uint search_depth= join->thd->variables.optimizer_search_depth;
5667
uint prune_level= join->thd->variables.optimizer_prune_level;
5668
bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5669
DBUG_ENTER("choose_plan");
5671
join->cur_embedding_map= 0;
5672
reset_nj_counters(join->join_list);
5674
if (SELECT_STRAIGHT_JOIN option is set)
5675
reorder tables so dependent tables come after tables they depend
5676
on, otherwise keep tables in the order they were specified in the query
5678
Apply heuristic: pre-sort all access plans with respect to the number of
5681
my_qsort(join->best_ref + join->const_tables,
5682
join->tables - join->const_tables, sizeof(JOIN_TAB*),
5683
straight_join ? join_tab_cmp_straight : join_tab_cmp);
5684
join->cur_emb_sj_nests= 0;
5687
optimize_straight_join(join, join_tables);
5691
if (search_depth == MAX_TABLES+2)
5693
TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5694
the greedy version. Will be removed when greedy_search is approved.
5696
join->best_read= DBL_MAX;
5697
if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5702
if (search_depth == 0)
5703
/* Automatically determine a reasonable value for 'search_depth' */
5704
search_depth= determine_search_depth(join);
5705
if (greedy_search(join, join_tables, search_depth, prune_level))
5711
Store the cost of this query into a user variable
5712
Don't update last_query_cost for statements that are not "flat joins" :
5713
i.e. they have subqueries, unions or call stored procedures.
5714
TODO: calculate a correct cost for a query with subqueries and UNIONs.
5716
if (join->thd->lex->is_single_level_stmt())
5717
join->thd->status_var.last_query_cost= join->best_read;
5723
Compare two JOIN_TAB objects based on the number of accessed records.
5725
@param ptr1 pointer to first JOIN_TAB object
5726
@param ptr2 pointer to second JOIN_TAB object
5729
The order relation implemented by join_tab_cmp() is not transitive,
5730
i.e. it is possible to choose such a, b and c that (a < b) && (b < c)
5731
but (c < a). This implies that result of a sort using the relation
5732
implemented by join_tab_cmp() depends on the order in which
5733
elements are compared, i.e. the result is implementation-specific.
5735
a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
5736
b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
5737
c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
5740
1 if first is bigger
5742
-1 if second is bigger
5748
join_tab_cmp(const void* ptr1, const void* ptr2)
5750
JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5751
JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
5753
if (jt1->dependent & jt2->table->map)
5755
if (jt2->dependent & jt1->table->map)
5757
if (jt1->found_records > jt2->found_records)
5759
if (jt1->found_records < jt2->found_records)
5761
return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5766
Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
5770
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
5772
JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5773
JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
5775
if (jt1->dependent & jt2->table->map)
5777
if (jt2->dependent & jt1->table->map)
5779
return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5783
Heuristic procedure to automatically guess a reasonable degree of
5784
exhaustiveness for the greedy search procedure.
5786
The procedure estimates the optimization time and selects a search depth
5787
big enough to result in a near-optimal QEP, that doesn't take too long to
5788
find. If the number of tables in the query exceeds some constant, then
5789
search_depth is set to this constant.
5791
@param join pointer to the structure providing all context info for
5795
This is an extremely simplistic implementation that serves as a stub for a
5796
more advanced analysis of the join. Ideally the search depth should be
5797
determined by learning from previous query optimizations, because it will
5798
depend on the CPU power (and other factors).
5801
this value should be determined dynamically, based on statistics:
5802
uint max_tables_for_exhaustive_opt= 7;
5805
this value could be determined by some mapping of the form:
5806
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5809
A positive integer that specifies the search depth (and thus the
5810
exhaustiveness) of the depth-first search algorithm used by
5815
determine_search_depth(JOIN *join)
5817
uint table_count= join->tables - join->const_tables;
5819
/* TODO: this value should be determined dynamically, based on statistics: */
5820
uint max_tables_for_exhaustive_opt= 7;
5822
if (table_count <= max_tables_for_exhaustive_opt)
5823
search_depth= table_count+1; // use exhaustive for small number of tables
5826
TODO: this value could be determined by some mapping of the form:
5827
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5829
search_depth= max_tables_for_exhaustive_opt; // use greedy search
5831
return search_depth;
5836
Select the best ways to access the tables in a query without reordering them.
5838
Find the best access paths for each query table and compute their costs
5839
according to their order in the array 'join->best_ref' (thus without
5840
reordering the join tables). The function calls sequentially
5841
'best_access_path' for each table in the query to select the best table
5842
access method. The final optimal plan is stored in the array
5843
'join->best_positions', and the corresponding cost in 'join->best_read'.
5845
@param join pointer to the structure providing all context info for
5847
@param join_tables set of the tables in the query
5850
This function can be applied to:
5851
- queries with STRAIGHT_JOIN
5852
- internally to compute the cost of an arbitrary QEP
5854
Thus 'optimize_straight_join' can be used at any stage of the query
5855
optimization process to finalize a QEP as it is.
5859
optimize_straight_join(JOIN *join, table_map join_tables)
5862
uint idx= join->const_tables;
5863
double record_count= 1.0;
5864
double read_time= 0.0;
5866
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5868
/* Find the best access method from 's' to the current partial plan */
5869
advance_sj_state(join_tables, s);
5870
best_access_path(join, s, join->thd, join_tables, idx,
5871
record_count, read_time);
5872
/* compute the cost of the new plan extended with 's' */
5873
record_count*= join->positions[idx].records_read;
5874
read_time+= join->positions[idx].read_time;
5875
join_tables&= ~(s->table->map);
5879
read_time+= record_count / (double) TIME_FOR_COMPARE;
5880
if (join->sort_by_table &&
5881
join->sort_by_table != join->positions[join->const_tables].table->table)
5882
read_time+= record_count; // We have to make a temp table
5883
memcpy((uchar*) join->best_positions, (uchar*) join->positions,
5884
sizeof(POSITION)*idx);
5885
join->best_read= read_time;
5890
Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5892
The search procedure uses a hybrid greedy/exhaustive search with controlled
5893
exhaustiveness. The search is performed in N = card(remaining_tables)
5894
steps. Each step evaluates how promising is each of the unoptimized tables,
5895
selects the most promising table, and extends the current partial QEP with
5896
that table. Currenly the most 'promising' table is the one with least
5897
expensive extension.\
5899
There are two extreme cases:
5900
-# When (card(remaining_tables) < search_depth), the estimate finds the
5901
best complete continuation of the partial QEP. This continuation can be
5902
used directly as a result of the search.
5903
-# When (search_depth == 1) the 'best_extension_by_limited_search'
5904
consideres the extension of the current QEP with each of the remaining
5907
All other cases are in-between these two extremes. Thus the parameter
5908
'search_depth' controlls the exhaustiveness of the search. The higher the
5909
value, the longer the optimizaton time and possibly the better the
5910
resulting plan. The lower the value, the fewer alternative plans are
5911
estimated, but the more likely to get a bad QEP.
5913
All intermediate and final results of the procedure are stored in 'join':
5914
- join->positions : modified for every partial QEP that is explored
5915
- join->best_positions: modified for the current best complete QEP
5916
- join->best_read : modified for the current best complete QEP
5917
- join->best_ref : might be partially reordered
5919
The final optimal plan is stored in 'join->best_positions', and its
5920
corresponding cost in 'join->best_read'.
5923
The following pseudocode describes the algorithm of 'greedy_search':
5926
procedure greedy_search
5927
input: remaining_tables
5932
(t, a) = best_extension(pplan, remaining_tables);
5933
pplan = concat(pplan, (t, a));
5934
remaining_tables = remaining_tables - t;
5935
} while (remaining_tables != {})
5940
where 'best_extension' is a placeholder for a procedure that selects the
5941
most "promising" of all tables in 'remaining_tables'.
5942
Currently this estimate is performed by calling
5943
'best_extension_by_limited_search' to evaluate all extensions of the
5944
current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5945
mainly depends on that of 'best_extension_by_limited_search'.
5948
If 'best_extension()' == 'best_extension_by_limited_search()', then the
5949
worst-case complexity of this algorithm is <=
5950
O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5951
complexity of greedy_search is O(N!).
5954
In the future, 'greedy_search' might be extended to support other
5955
implementations of 'best_extension', e.g. some simpler quadratic procedure.
5957
@param join pointer to the structure providing all context info
5959
@param remaining_tables set of tables not included into the partial plan yet
5960
@param search_depth controlls the exhaustiveness of the search
5961
@param prune_level the pruning heuristics that should be applied during
5971
greedy_search(JOIN *join,
5972
table_map remaining_tables,
5976
double record_count= 1.0;
5977
double read_time= 0.0;
5978
uint idx= join->const_tables; // index into 'join->best_ref'
5980
uint size_remain; // cardinality of remaining_tables
5982
JOIN_TAB *best_table; // the next plan node to be added to the curr QEP
5984
DBUG_ENTER("greedy_search");
5986
/* number of tables that remain to be optimized */
5987
size_remain= my_count_bits(remaining_tables);
5990
/* Find the extension of the current QEP with the lowest cost */
5991
join->best_read= DBL_MAX;
5992
if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5993
read_time, search_depth, prune_level))
5996
if (size_remain <= search_depth)
5999
'join->best_positions' contains a complete optimal extension of the
6000
current partial QEP.
6002
DBUG_EXECUTE("opt", print_plan(join, join->tables,
6003
record_count, read_time, read_time,
6008
/* select the first table in the optimal extension as most promising */
6009
best_pos= join->best_positions[idx];
6010
best_table= best_pos.table;
6012
Each subsequent loop of 'best_extension_by_limited_search' uses
6013
'join->positions' for cost estimates, therefore we have to update its
6016
join->positions[idx]= best_pos;
6018
/* find the position of 'best_table' in 'join->best_ref' */
6020
JOIN_TAB *pos= join->best_ref[best_idx];
6021
while (pos && best_table != pos)
6022
pos= join->best_ref[++best_idx];
6023
DBUG_ASSERT((pos != NULL)); // should always find 'best_table'
6024
/* move 'best_table' at the first free position in the array of joins */
6025
swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
6027
/* compute the cost of the new plan extended with 'best_table' */
6028
record_count*= join->positions[idx].records_read;
6029
read_time+= join->positions[idx].read_time;
6031
remaining_tables&= ~(best_table->table->map);
6035
DBUG_EXECUTE("opt", print_plan(join, join->tables,
6036
record_count, read_time, read_time,
6043
Find a good, possibly optimal, query execution plan (QEP) by a possibly
6046
The procedure searches for the optimal ordering of the query tables in set
6047
'remaining_tables' of size N, and the corresponding optimal access paths to
6048
each table. The choice of a table order and an access path for each table
6049
constitutes a query execution plan (QEP) that fully specifies how to
6052
The maximal size of the found plan is controlled by the parameter
6053
'search_depth'. When search_depth == N, the resulting plan is complete and
6054
can be used directly as a QEP. If search_depth < N, the found plan consists
6055
of only some of the query tables. Such "partial" optimal plans are useful
6056
only as input to query optimization procedures, and cannot be used directly
6059
The algorithm begins with an empty partial plan stored in 'join->positions'
6060
and a set of N tables - 'remaining_tables'. Each step of the algorithm
6061
evaluates the cost of the partial plan extended by all access plans for
6062
each of the relations in 'remaining_tables', expands the current partial
6063
plan with the access plan that results in lowest cost of the expanded
6064
partial plan, and removes the corresponding relation from
6065
'remaining_tables'. The algorithm continues until it either constructs a
6066
complete optimal plan, or constructs an optimal plartial plan with size =
6069
The final optimal plan is stored in 'join->best_positions'. The
6070
corresponding cost of the optimal plan is in 'join->best_read'.
6073
The procedure uses a recursive depth-first search where the depth of the
6074
recursion (and thus the exhaustiveness of the search) is controlled by the
6075
parameter 'search_depth'.
6078
The pseudocode below describes the algorithm of
6079
'best_extension_by_limited_search'. The worst-case complexity of this
6080
algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
6081
the complexity of greedy_search is O(N!).
6084
procedure best_extension_by_limited_search(
6085
pplan in, // in, partial plan of tables-joined-so-far
6086
pplan_cost, // in, cost of pplan
6087
remaining_tables, // in, set of tables not referenced in pplan
6088
best_plan_so_far, // in/out, best plan found so far
6089
best_plan_so_far_cost,// in/out, cost of best_plan_so_far
6090
search_depth) // in, maximum size of the plans being considered
6092
for each table T from remaining_tables
6094
// Calculate the cost of using table T as above
6095
cost = complex-series-of-calculations;
6097
// Add the cost to the cost so far.
6100
if (pplan_cost >= best_plan_so_far_cost)
6101
// pplan_cost already too great, stop search
6104
pplan= expand pplan by best_access_method;
6105
remaining_tables= remaining_tables - table T;
6106
if (remaining_tables is not an empty set
6110
best_extension_by_limited_search(pplan, pplan_cost,
6113
best_plan_so_far_cost,
6118
best_plan_so_far_cost= pplan_cost;
6119
best_plan_so_far= pplan;
6126
When 'best_extension_by_limited_search' is called for the first time,
6127
'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6128
The actual implementation provides a way to optionally use pruning
6129
heuristic (controlled by the parameter 'prune_level') to reduce the search
6130
space by skipping some partial plans.
6133
The parameter 'search_depth' provides control over the recursion
6134
depth, and thus the size of the resulting optimal plan.
6136
@param join pointer to the structure providing all context info
6138
@param remaining_tables set of tables not included into the partial plan yet
6139
@param idx length of the partial QEP in 'join->positions';
6140
since a depth-first search is used, also corresponds
6141
to the current depth of the search tree;
6142
also an index in the array 'join->best_ref';
6143
@param record_count estimate for the number of records returned by the
6145
@param read_time the cost of the best partial plan
6146
@param search_depth maximum depth of the recursion and thus size of the
6148
(0 < search_depth <= join->tables+1).
6149
@param prune_level pruning heuristics that should be applied during
6151
(values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6160
best_extension_by_limited_search(JOIN *join,
6161
table_map remaining_tables,
6163
double record_count,
6168
DBUG_ENTER("best_extension_by_limited_search");
6170
THD *thd= join->thd;
6171
if (thd->killed) // Abort
6174
DBUG_EXECUTE("opt", print_plan(join, idx, read_time, record_count, idx,
6178
'join' is a partial plan with lower cost than the best plan so far,
6179
so continue expanding it further with the tables in 'remaining_tables'.
6182
double best_record_count= DBL_MAX;
6183
double best_read_time= DBL_MAX;
6185
DBUG_EXECUTE("opt", print_plan(join, idx, record_count, read_time, read_time,
6188
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6190
table_map real_table_bit= s->table->map;
6191
if ((remaining_tables & real_table_bit) &&
6192
!(remaining_tables & s->dependent) &&
6193
(!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6195
double current_record_count, current_read_time;
6196
advance_sj_state(remaining_tables, s);
6199
psergey-insideout-todo:
6200
when best_access_path() detects it could do an InsideOut scan or
6201
some other scan, have it return an insideout scan and a flag that
6202
requests to "fork" this loop iteration. (Q: how does that behave
6203
when the depth is insufficient??)
6205
/* Find the best access method from 's' to the current partial plan */
6206
best_access_path(join, s, thd, remaining_tables, idx,
6207
record_count, read_time);
6208
/* Compute the cost of extending the plan with 's' */
6209
current_record_count= record_count * join->positions[idx].records_read;
6210
current_read_time= read_time + join->positions[idx].read_time;
6212
/* Expand only partial plans with lower cost than the best QEP so far */
6213
if ((current_read_time +
6214
current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6216
DBUG_EXECUTE("opt", print_plan(join, idx+1,
6217
current_record_count,
6219
(current_read_time +
6220
current_record_count /
6221
(double) TIME_FOR_COMPARE),
6223
restore_prev_nj_state(s);
6224
restore_prev_sj_state(remaining_tables, s);
6229
Prune some less promising partial plans. This heuristic may miss
6230
the optimal QEPs, thus it results in a non-exhaustive search.
6232
if (prune_level == 1)
6234
if (best_record_count > current_record_count ||
6235
best_read_time > current_read_time ||
6236
(idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6238
if (best_record_count >= current_record_count &&
6239
best_read_time >= current_read_time &&
6240
/* TODO: What is the reasoning behind this condition? */
6241
(!(s->key_dependent & remaining_tables) ||
6242
join->positions[idx].records_read < 2.0))
6244
best_record_count= current_record_count;
6245
best_read_time= current_read_time;
6250
DBUG_EXECUTE("opt", print_plan(join, idx+1,
6251
current_record_count,
6254
"pruned_by_heuristic"););
6255
restore_prev_nj_state(s);
6256
restore_prev_sj_state(remaining_tables, s);
6261
if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6262
{ /* Recursively expand the current partial plan */
6263
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6264
if (best_extension_by_limited_search(join,
6265
remaining_tables & ~real_table_bit,
6267
current_record_count,
6272
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6276
'join' is either the best partial QEP with 'search_depth' relations,
6277
or the best complete QEP so far, whichever is smaller.
6279
current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6280
if (join->sort_by_table &&
6281
join->sort_by_table !=
6282
join->positions[join->const_tables].table->table)
6283
/* We have to make a temp table */
6284
current_read_time+= current_record_count;
6285
if ((search_depth == 1) || (current_read_time < join->best_read))
6287
memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6288
sizeof(POSITION) * (idx + 1));
6289
join->best_read= current_read_time - 0.001;
6291
DBUG_EXECUTE("opt", print_plan(join, idx+1,
6292
current_record_count,
6297
restore_prev_nj_state(s);
6298
restore_prev_sj_state(remaining_tables, s);
6307
- TODO: this function is here only temporarily until 'greedy_search' is
6308
tested and accepted.
6315
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6318
DBUG_ENTER("find_best");
6319
THD *thd= join->thd;
6324
DBUG_PRINT("best",("read_time: %g record_count: %g",read_time,
6327
read_time+=record_count/(double) TIME_FOR_COMPARE;
6328
if (join->sort_by_table &&
6329
join->sort_by_table !=
6330
join->positions[join->const_tables].table->table)
6331
read_time+=record_count; // We have to make a temp table
6332
if (read_time < join->best_read)
6334
memcpy((uchar*) join->best_positions,(uchar*) join->positions,
6335
sizeof(POSITION)*idx);
6336
join->best_read= read_time - 0.001;
6340
if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6341
DBUG_RETURN(FALSE); /* Found better before */
6344
double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6345
for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6347
table_map real_table_bit=s->table->map;
6348
if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6349
(!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6351
double records, best;
6352
advance_sj_state(rest_tables, s);
6353
best_access_path(join, s, thd, rest_tables, idx, record_count,
6355
records= join->positions[idx].records_read;
6356
best= join->positions[idx].read_time;
6358
Go to the next level only if there hasn't been a better key on
6359
this level! This will cut down the search for a lot simple cases!
6361
double current_record_count=record_count*records;
6362
double current_read_time=read_time+best;
6363
if (best_record_count > current_record_count ||
6364
best_read_time > current_read_time ||
6365
(idx == join->const_tables && s->table == join->sort_by_table))
6367
if (best_record_count >= current_record_count &&
6368
best_read_time >= current_read_time &&
6369
(!(s->key_dependent & rest_tables) || records < 2.0))
6371
best_record_count=current_record_count;
6372
best_read_time=current_read_time;
6374
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6375
if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6376
current_record_count,current_read_time))
6378
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6380
restore_prev_nj_state(s);
6381
restore_prev_sj_state(rest_tables, s);
6382
if (join->select_options & SELECT_STRAIGHT_JOIN)
6383
break; // Don't test all combinations
6391
Find how much space the prevous read not const tables takes in cache.
6394
static void calc_used_field_length(THD *thd, JOIN_TAB *join_tab)
6396
uint null_fields,blobs,fields,rec_length;
6397
Field **f_ptr,*field;
6398
MY_BITMAP *read_set= join_tab->table->read_set;;
6400
null_fields= blobs= fields= rec_length=0;
6401
for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
6403
if (bitmap_is_set(read_set, field->field_index))
6405
uint flags=field->flags;
6407
rec_length+=field->pack_length();
6408
if (flags & BLOB_FLAG)
6410
if (!(flags & NOT_NULL_FLAG))
6415
rec_length+=(join_tab->table->s->null_fields+7)/8;
6416
if (join_tab->table->maybe_null)
6417
rec_length+=sizeof(my_bool);
6420
uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6421
(join_tab->table->s->reclength- rec_length));
6422
rec_length+=(uint) max(4,blob_length);
6424
join_tab->used_fields=fields;
6425
join_tab->used_fieldlength=rec_length;
6426
join_tab->used_blobs=blobs;
6431
cache_record_length(JOIN *join,uint idx)
6434
JOIN_TAB **pos,**end;
6437
for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6441
JOIN_TAB *join_tab= *pos;
6442
if (!join_tab->used_fieldlength) /* Not calced yet */
6443
calc_used_field_length(thd, join_tab);
6444
length+=join_tab->used_fieldlength;
6451
Get the number of different row combinations for subset of partial join
6455
join The join structure
6456
idx Number of tables in the partial join order (i.e. the
6457
partial join order is in join->positions[0..idx-1])
6458
found_ref Bitmap of tables for which we need to find # of distinct
6462
Given a partial join order (in join->positions[0..idx-1]) and a subset of
6463
tables within that join order (specified in found_ref), find out how many
6464
distinct row combinations of subset tables will be in the result of the
6467
This is used as follows: Suppose we have a table accessed with a ref-based
6468
method. The ref access depends on current rows of tables in found_ref.
6469
We want to count # of different ref accesses. We assume two ref accesses
6470
will be different if at least one of access parameters is different.
6471
Example: consider a query
6473
SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6476
t1, ref access on t1.key=c1
6477
t2, ref access on t2.key=c2
6478
t3, ref access on t3.key=t1.field
6480
For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6481
For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6482
For t3: n_ref_scans = records_read(t1)*records_read(t2)
6483
n_distinct_ref_scans = #records_read(t1)
6485
The reason for having this function (at least the latest version of it)
6486
is that we need to account for buffering in join execution.
6488
An edge-case example: if we have a non-first table in join accessed via
6489
ref(const) or ref(param) where there is a small number of different
6490
values of param, then the access will likely hit the disk cache and will
6491
not require any disk seeks.
6493
The proper solution would be to assume an LRU disk cache of some size,
6494
calculate probability of cache hits, etc. For now we just count
6495
identical ref accesses as one.
6498
Expected number of row combinations
6502
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6505
POSITION *pos_end= join->positions - 1;
6506
for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6508
if (pos->table->table->map & found_ref)
6510
found_ref|= pos->ref_depend_map;
6512
For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
6513
with no matching row we will get position[t2].records_read==0.
6514
Actually the size of output is one null-complemented row, therefore
6515
we will use value of 1 whenever we get records_read==0.
6518
- the above case can't occur if inner part of outer join has more
6519
than one table: table with no matches will not be marked as const.
6521
- Ideally we should add 1 to records_read for every possible null-
6522
complemented row. We're not doing it because: 1. it will require
6523
non-trivial code and add overhead. 2. The value of records_read
6524
is an inprecise estimate and adding 1 (or, in the worst case,
6525
#max_nested_outer_joins=64-1) will not make it any more precise.
6527
if (pos->records_read > DBL_EPSILON)
6528
found*= pos->records_read;
6536
Set up join struct according to best position.
6540
get_best_combination(JOIN *join)
6543
table_map used_tables;
6544
JOIN_TAB *join_tab,*j;
6548
DBUG_ENTER("get_best_combination");
6550
table_count=join->tables;
6551
if (!(join->join_tab=join_tab=
6552
(JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6557
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read
6558
for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
6561
*j= *join->best_positions[tablenr].table;
6562
form=join->table[tablenr]=j->table;
6563
used_tables|= form->map;
6564
form->reginfo.join_tab=j;
6565
if (!*j->on_expr_ref)
6566
form->reginfo.not_exists_optimize=0; // Only with LEFT JOIN
6567
DBUG_PRINT("info",("type: %d", j->type));
6568
if (j->type == JT_CONST)
6569
continue; // Handled in make_join_stat..
6574
if (j->type == JT_SYSTEM)
6576
if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
6579
if (tablenr != join->const_tables)
6582
else if (create_ref_for_key(join, j, keyuse, used_tables))
6583
DBUG_RETURN(TRUE); // Something went wrong
6586
for (i=0 ; i < table_count ; i++)
6587
join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6588
update_depend_map(join);
6593
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6594
table_map used_tables)
6596
KEYUSE *keyuse=org_keyuse;
6597
THD *thd= join->thd;
6598
uint keyparts,length,key;
6601
DBUG_ENTER("create_ref_for_key");
6603
/* Use best key from find_best */
6606
keyinfo=table->key_info+key;
6610
uint found_part_ref_or_null= 0;
6612
Calculate length for the used key
6613
Stop if there is a missing key part or when we find second key_part
6614
with KEY_OPTIMIZE_REF_OR_NULL
6618
if (!(~used_tables & keyuse->used_tables))
6620
if (keyparts == keyuse->keypart &&
6621
!(found_part_ref_or_null & keyuse->optimize))
6624
length+= keyinfo->key_part[keyuse->keypart].store_length;
6625
found_part_ref_or_null|= keyuse->optimize;
6629
} while (keyuse->table == table && keyuse->key == key);
6632
/* set up fieldref */
6633
keyinfo=table->key_info+key;
6634
j->ref.key_parts=keyparts;
6635
j->ref.key_length=length;
6636
j->ref.key=(int) key;
6637
if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6638
!(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6640
!(j->ref.items= (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6641
!(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
6645
j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
6647
j->ref.null_rejecting= 0;
6648
j->ref.disable_cache= FALSE;
6651
store_key **ref_key= j->ref.key_copy;
6652
uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
6653
bool keyuse_uses_no_tables= TRUE;
6656
for (i=0 ; i < keyparts ; keyuse++,i++)
6658
while (keyuse->keypart != i ||
6659
((~used_tables) & keyuse->used_tables))
6660
keyuse++; /* Skip other parts */
6662
uint maybe_null= test(keyinfo->key_part[i].null_bit);
6663
j->ref.items[i]=keyuse->val; // Save for cond removal
6664
j->ref.cond_guards[i]= keyuse->cond_guard;
6665
if (keyuse->null_rejecting)
6666
j->ref.null_rejecting |= 1 << i;
6667
keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
6668
if (!keyuse->used_tables &&
6669
!(join->select_options & SELECT_DESCRIBE))
6670
{ // Compare against constant
6671
store_key_item tmp(thd, keyinfo->key_part[i].field,
6672
key_buff + maybe_null,
6673
maybe_null ? key_buff : 0,
6674
keyinfo->key_part[i].length, keyuse->val);
6675
if (thd->is_fatal_error)
6680
*ref_key++= get_store_key(thd,
6681
keyuse,join->const_table_map,
6682
&keyinfo->key_part[i],
6683
key_buff, maybe_null);
6685
Remember if we are going to use REF_OR_NULL
6686
But only if field _really_ can be null i.e. we force JT_REF
6687
instead of JT_REF_OR_NULL in case if field can't be null
6689
if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
6690
null_ref_key= key_buff;
6691
key_buff+=keyinfo->key_part[i].store_length;
6694
*ref_key=0; // end_marker
6695
if (j->type == JT_CONST)
6696
j->table->const_table= 1;
6697
else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY |
6698
HA_END_SPACE_KEY)) != HA_NOSAME) ||
6699
keyparts != keyinfo->key_parts || null_ref_key)
6701
/* Must read with repeat */
6702
j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
6703
j->ref.null_ref_key= null_ref_key;
6705
else if (keyuse_uses_no_tables)
6708
This happen if we are using a constant expression in the ON part
6710
SELECT * FROM a LEFT JOIN b ON b.key=30
6711
Here we should not mark the table as a 'const' as a field may
6712
have a 'normal' value or a NULL value.
6724
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6725
KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6727
if (!((~used_tables) & keyuse->used_tables)) // if const item
6729
return new store_key_const_item(thd,
6731
key_buff + maybe_null,
6732
maybe_null ? key_buff : 0,
6736
else if (keyuse->val->type() == Item::FIELD_ITEM ||
6737
(keyuse->val->type() == Item::REF_ITEM &&
6738
((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
6739
(*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
6740
Item_ref::DIRECT_REF &&
6741
keyuse->val->real_item()->type() == Item::FIELD_ITEM))
6742
return new store_key_field(thd,
6744
key_buff + maybe_null,
6745
maybe_null ? key_buff : 0,
6747
((Item_field*) keyuse->val->real_item())->field,
6748
keyuse->val->full_name());
6749
return new store_key_item(thd,
6751
key_buff + maybe_null,
6752
maybe_null ? key_buff : 0,
6758
This function is only called for const items on fields which are keys.
6761
returns 1 if there was some conversion made when the field was stored.
6765
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6768
TABLE *table= field->table;
6769
THD *thd= table->in_use;
6770
ha_rows cuted_fields=thd->cuted_fields;
6771
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
6775
we should restore old value of count_cuted_fields because
6776
store_val_in_field can be called from mysql_insert
6777
with select_insert, which make count_cuted_fields= 1
6779
enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
6780
thd->count_cuted_fields= check_flag;
6781
error= item->save_in_field(field, 1);
6782
thd->count_cuted_fields= old_count_cuted_fields;
6783
dbug_tmp_restore_column_map(table->write_set, old_map);
6784
return error || cuted_fields != thd->cuted_fields;
6789
make_simple_join(JOIN *join,TABLE *tmp_table)
6793
DBUG_ENTER("make_simple_join");
6796
Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6797
to this function through JOIN::exec (may happen for sub-queries).
6799
if (!join->table_reexec)
6801
if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
6802
DBUG_RETURN(TRUE); /* purecov: inspected */
6804
join->tmp_join->table_reexec= join->table_reexec;
6806
if (!join->join_tab_reexec)
6808
if (!(join->join_tab_reexec=
6809
(JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
6810
DBUG_RETURN(TRUE); /* purecov: inspected */
6812
join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6814
tableptr= join->table_reexec;
6815
join_tab= join->join_tab_reexec;
6817
join->join_tab=join_tab;
6818
join->table=tableptr; tableptr[0]=tmp_table;
6820
join->const_tables=0;
6821
join->const_table_map=0;
6822
join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
6823
join->tmp_table_param.func_count=0;
6824
join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
6825
join->first_record=join->sort_and_group=0;
6826
join->send_records=(ha_rows) 0;
6828
join->row_limit=join->unit->select_limit_cnt;
6829
join->do_send_rows = (join->row_limit) ? 1 : 0;
6831
join_tab->cache.buff=0; /* No caching */
6832
join_tab->table=tmp_table;
6834
join_tab->select_cond=0;
6836
join_tab->type= JT_ALL; /* Map through all records */
6837
join_tab->keys.init();
6838
join_tab->keys.set_all(); /* test everything in quick */
6840
join_tab->on_expr_ref=0;
6841
join_tab->last_inner= 0;
6842
join_tab->first_unmatched= 0;
6843
join_tab->ref.key = -1;
6844
join_tab->not_used_in_distinct=0;
6845
join_tab->read_first_record= join_init_read_record;
6846
join_tab->join=join;
6847
join_tab->ref.key_parts= 0;
6848
join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6849
join_tab->do_firstmatch= NULL;
6850
bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
6851
tmp_table->status=0;
6852
tmp_table->null_row=0;
6857
inline void add_cond_and_fix(Item **e1, Item *e2)
6862
if ((res= new Item_cond_and(*e1, e2)))
6865
res->quick_fix_field();
6874
Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
6875
we've inferred from ref/eq_ref access performed.
6877
This function is a part of "Early NULL-values filtering for ref access"
6880
Example of this optimization:
6881
For query SELECT * FROM t1,t2 WHERE t2.key=t1.field @n
6882
and plan " any-access(t1), ref(t2.key=t1.field) " @n
6883
add "t1.field IS NOT NULL" to t1's table condition. @n
6885
Description of the optimization:
6887
We look through equalities choosen to perform ref/eq_ref access,
6888
pick equalities that have form "tbl.part_of_key = othertbl.field"
6889
(where othertbl is a non-const table and othertbl.field may be NULL)
6890
and add them to conditions on correspoding tables (othertbl in this
6893
Exception from that is the case when referred_tab->join != join.
6894
I.e. don't add NOT NULL constraints from any embedded subquery.
6895
Consider this query:
6897
SELECT A.f2 FROM t1 LEFT JOIN t2 A ON A.f2 = f1
6898
WHERE A.f3=(SELECT MIN(f3) FROM t2 C WHERE A.f4 = C.f4) OR A.f3 IS NULL;
6900
Here condition A.f3 IS NOT NULL is going to be added to the WHERE
6901
condition of the embedding query.
6903
SELECT * FROM t10, t11 WHERE (t10.a < 10 OR t10.a IS NULL)
6904
AND t11.b <=> t10.b AND (t11.a = (SELECT MAX(a) FROM t12
6905
WHERE t12.b = t10.a ));
6906
Here condition t10.a IS NOT NULL is going to be added.
6907
In both cases addition of NOT NULL condition will erroneously reject
6908
some rows of the result set.
6909
referred_tab->join != join constraint would disallow such additions.
6911
This optimization doesn't affect the choices that ref, range, or join
6912
optimizer make. This was intentional because this was added after 4.1
6915
Implementation overview
6916
1. update_ref_and_keys() accumulates info about null-rejecting
6917
predicates in in KEY_FIELD::null_rejecting
6918
1.1 add_key_part saves these to KEYUSE.
6919
2. create_ref_for_key copies them to TABLE_REF.
6920
3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
6921
appropiate JOIN_TAB members.
6924
static void add_not_null_conds(JOIN *join)
6926
DBUG_ENTER("add_not_null_conds");
6927
for (uint i=join->const_tables ; i < join->tables ; i++)
6929
JOIN_TAB *tab=join->join_tab+i;
6930
if ((tab->type == JT_REF || tab->type == JT_EQ_REF ||
6931
tab->type == JT_REF_OR_NULL) &&
6932
!tab->table->maybe_null)
6934
for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
6936
if (tab->ref.null_rejecting & (1 << keypart))
6938
Item *item= tab->ref.items[keypart];
6940
DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
6941
Item_field *not_null_item= (Item_field*)item;
6942
JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
6944
For UPDATE queries such as:
6945
UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
6946
not_null_item is the t1.f1, but it's referred_tab is 0.
6948
if (!referred_tab || referred_tab->join != join)
6950
if (!(notnull= new Item_func_isnotnull(not_null_item)))
6953
We need to do full fix_fields() call here in order to have correct
6954
notnull->const_item(). This is needed e.g. by test_quick_select
6955
when it is called from make_join_select after this function is
6958
if (notnull->fix_fields(join->thd, ¬null))
6960
DBUG_EXECUTE("where",print_where(notnull,
6961
referred_tab->table->alias,
6963
add_cond_and_fix(&referred_tab->select_cond, notnull);
6972
Build a predicate guarded by match variables for embedding outer joins.
6973
The function recursively adds guards for predicate cond
6974
assending from tab to the first inner table next embedding
6975
nested outer join and so on until it reaches root_tab
6976
(root_tab can be 0).
6978
@param tab the first inner table for most nested outer join
6979
@param cond the predicate to be guarded (must be set)
6980
@param root_tab the first inner table to stop
6983
- pointer to the guarded predicate, if success
6988
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
6991
DBUG_ASSERT(cond != 0);
6992
if (tab == root_tab)
6994
if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
6995
tmp= new Item_func_trig_cond(tmp, &tab->found);
6998
tmp->quick_fix_field();
6999
tmp->update_used_tables();
7006
Fill in outer join related info for the execution plan structure.
7008
For each outer join operation left after simplification of the
7009
original query the function set up the following pointers in the linear
7010
structure join->join_tab representing the selected execution plan.
7011
The first inner table t0 for the operation is set to refer to the last
7012
inner table tk through the field t0->last_inner.
7013
Any inner table ti for the operation are set to refer to the first
7014
inner table ti->first_inner.
7015
The first inner table t0 for the operation is set to refer to the
7016
first inner table of the embedding outer join operation, if there is any,
7017
through the field t0->first_upper.
7018
The on expression for the outer join operation is attached to the
7019
corresponding first inner table through the field t0->on_expr_ref.
7020
Here ti are structures of the JOIN_TAB type.
7022
EXAMPLE. For the query:
7026
(t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
7027
ON (t1.a=t2.a AND t1.b=t3.b)
7031
given the execution plan with the table order t1,t2,t3,t4
7032
is selected, the following references will be set;
7033
t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
7034
t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
7035
on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
7036
*t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
7038
@param join reference to the info fully describing the query
7041
The function assumes that the simplification procedure has been
7042
already applied to the join query (see simplify_joins).
7043
This function can be called only after the execution plan
7048
make_outerjoin_info(JOIN *join)
7050
DBUG_ENTER("make_outerjoin_info");
7051
for (uint i=join->const_tables ; i < join->tables ; i++)
7053
JOIN_TAB *tab=join->join_tab+i;
7054
TABLE *table=tab->table;
7055
TABLE_LIST *tbl= table->pos_in_table_list;
7056
TABLE_LIST *embedding= tbl->embedding;
7058
if (tbl->outer_join)
7061
Table tab is the only one inner table for outer join.
7062
(Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
7063
is in the query above.)
7065
tab->last_inner= tab->first_inner= tab;
7066
tab->on_expr_ref= &tbl->on_expr;
7067
tab->cond_equal= tbl->cond_equal;
7069
tab->first_upper= embedding->nested_join->first_nested;
7071
for ( ; embedding ; embedding= embedding->embedding)
7073
/* Ignore sj-nests: */
7074
if (!embedding->on_expr)
7076
NESTED_JOIN *nested_join= embedding->nested_join;
7077
if (!nested_join->counter_)
7080
Table tab is the first inner table for nested_join.
7081
Save reference to it in the nested join structure.
7083
nested_join->first_nested= tab;
7084
tab->on_expr_ref= &embedding->on_expr;
7085
tab->cond_equal= tbl->cond_equal;
7086
if (embedding->embedding)
7087
tab->first_upper= embedding->embedding->nested_join->first_nested;
7089
if (!tab->first_inner)
7090
tab->first_inner= nested_join->first_nested;
7091
if (++nested_join->counter_ < nested_join->join_list.elements)
7093
/* Table tab is the last inner table for nested join. */
7094
nested_join->first_nested->last_inner= tab;
7102
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
7104
THD *thd= join->thd;
7105
DBUG_ENTER("make_join_select");
7108
add_not_null_conds(join);
7109
table_map used_tables;
7110
if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */
7111
{ /* there may be a select without a cond. */
7112
if (join->tables > 1)
7113
cond->update_used_tables(); // Tablenr may have changed
7114
if (join->const_tables == join->tables &&
7115
thd->lex->current_select->master_unit() ==
7116
&thd->lex->unit) // not upper level SELECT
7117
join->const_table_map|=RAND_TABLE_BIT;
7118
{ // Check const tables
7120
make_cond_for_table(cond,
7121
join->const_table_map,
7123
DBUG_EXECUTE("where",print_where(const_cond,"constants", QT_ORDINARY););
7124
for (JOIN_TAB *tab= join->join_tab+join->const_tables;
7125
tab < join->join_tab+join->tables ; tab++)
7127
if (*tab->on_expr_ref)
7129
JOIN_TAB *cond_tab= tab->first_inner;
7130
COND *tmp= make_cond_for_table(*tab->on_expr_ref,
7131
join->const_table_map,
7135
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7138
tmp->quick_fix_field();
7139
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7140
new Item_cond_and(cond_tab->select_cond,
7142
if (!cond_tab->select_cond)
7144
cond_tab->select_cond->quick_fix_field();
7147
if (const_cond && !const_cond->val_int())
7149
DBUG_PRINT("info",("Found impossible WHERE condition"));
7150
DBUG_RETURN(1); // Impossible const condition
7154
used_tables=((select->const_tables=join->const_table_map) |
7155
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7156
for (uint i=join->const_tables ; i < join->tables ; i++)
7158
JOIN_TAB *tab=join->join_tab+i;
7160
first_inner is the X in queries like:
7161
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7163
JOIN_TAB *first_inner_tab= tab->first_inner;
7164
table_map current_map= tab->table->map;
7165
bool use_quick_range=0;
7169
Following force including random expression in last table condition.
7170
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7172
if (i == join->tables-1)
7173
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7174
used_tables|=current_map;
7176
if (tab->type == JT_REF && tab->quick &&
7177
(uint) tab->ref.key == tab->quick->index &&
7178
tab->ref.key_length < tab->quick->max_used_key_length)
7180
/* Range uses longer key; Use this instead of ref on key */
7185
tab->ref.key_parts=0; // Don't use ref key.
7186
join->best_positions[i].records_read= rows2double(tab->quick->records);
7188
We will use join cache here : prevent sorting of the first
7189
table only and sort at the end.
7191
if (i != join->const_tables && join->tables > join->const_tables + 1)
7197
tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7198
if (cond && !tmp && tab->quick)
7200
if (tab->type != JT_ALL)
7203
Don't use the quick method
7204
We come here in the case where we have 'key=constant' and
7205
the test is removed by make_cond_for_table()
7213
Hack to handle the case where we only refer to a table
7214
in the ON part of an OUTER JOIN. In this case we want the code
7215
below to check if we should use 'quick' instead.
7217
DBUG_PRINT("info", ("Item_int"));
7218
tmp= new Item_int((longlong) 1,1); // Always true
7222
if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7223
tab->type == JT_EQ_REF)
7225
DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7226
SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7227
thd->memdup((uchar*) select,
7230
DBUG_RETURN(1); // End of memory
7232
If tab is an inner table of an outer join operation,
7233
add a match guard to the pushed down predicate.
7234
The guard will turn the predicate on only after
7235
the first match for outer tables is encountered.
7240
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7241
a cond, so neutralize the hack above.
7243
if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7245
tab->select_cond=sel->cond=tmp;
7246
/* Push condition to storage engine if this is enabled
7247
and the condition is not guarded */
7248
tab->table->file->pushed_cond= NULL;
7249
if (thd->variables.engine_condition_pushdown)
7252
make_cond_for_table(tmp, current_map, current_map, 0);
7255
/* Push condition to handler */
7256
if (!tab->table->file->cond_push(push_cond))
7257
tab->table->file->pushed_cond= push_cond;
7262
tab->select_cond= sel->cond= NULL;
7264
sel->head=tab->table;
7265
DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7268
/* Use quick key read if it's a constant and it's not used
7270
if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7271
&& (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7273
sel->quick=tab->quick; // Use value from get_quick_...
7274
sel->quick_keys.clear_all();
7275
sel->needed_reg.clear_all();
7283
uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7284
if (i == join->const_tables && ref_key)
7286
if (!tab->const_keys.is_clear_all() &&
7287
tab->table->reginfo.impossible_range)
7290
else if (tab->type == JT_ALL && ! use_quick_range)
7292
if (!tab->const_keys.is_clear_all() &&
7293
tab->table->reginfo.impossible_range)
7294
DBUG_RETURN(1); // Impossible range
7296
We plan to scan all rows.
7297
Check again if we should use an index.
7298
We could have used an column from a previous table in
7299
the index if we are using limit and this is the first table
7302
if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7303
(!tab->const_keys.is_clear_all() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
7305
/* Join with outer join condition */
7306
COND *orig_cond=sel->cond;
7307
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7310
We can't call sel->cond->fix_fields,
7311
as it will break tab->on_expr if it's AND condition
7312
(fix_fields currently removes extra AND/OR levels).
7313
Yet attributes of the just built condition are not needed.
7314
Thus we call sel->cond->quick_fix_field for safety.
7316
if (sel->cond && !sel->cond->fixed)
7317
sel->cond->quick_fix_field();
7319
if (sel->test_quick_select(thd, tab->keys,
7320
used_tables & ~ current_map,
7321
(join->select_options &
7324
join->unit->select_limit_cnt), 0,
7328
Before reporting "Impossible WHERE" for the whole query
7329
we have to check isn't it only "impossible ON" instead
7331
sel->cond=orig_cond;
7332
if (!*tab->on_expr_ref ||
7333
sel->test_quick_select(thd, tab->keys,
7334
used_tables & ~ current_map,
7335
(join->select_options &
7338
join->unit->select_limit_cnt),0,
7340
DBUG_RETURN(1); // Impossible WHERE
7343
sel->cond=orig_cond;
7345
/* Fix for EXPLAIN */
7347
join->best_positions[i].records_read= (double)sel->quick->records;
7351
sel->needed_reg=tab->needed_reg;
7352
sel->quick_keys.clear_all();
7354
if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7355
!sel->needed_reg.is_subset(tab->checked_keys))
7357
tab->keys=sel->quick_keys;
7358
tab->keys.merge(sel->needed_reg);
7359
tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7360
(select->quick_keys.is_clear_all() ||
7362
(select->quick->records >= 100L)))) ?
7364
sel->read_tables= used_tables & ~current_map;
7366
if (i != join->const_tables && tab->use_quick != 2)
7367
{ /* Read with cache */
7369
(tmp=make_cond_for_table(cond,
7370
join->const_table_map |
7374
DBUG_EXECUTE("where",print_where(tmp,"cache", QT_ORDINARY););
7375
tab->cache.select=(SQL_SELECT*)
7376
thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7377
tab->cache.select->cond=tmp;
7378
tab->cache.select->read_tables=join->const_table_map;
7385
Push down conditions from all on expressions.
7386
Each of these conditions are guarded by a variable
7387
that turns if off just before null complemented row for
7388
outer joins is formed. Thus, the condition from an
7389
'on expression' are guaranteed not to be checked for
7390
the null complemented row.
7393
/* First push down constant conditions from on expressions */
7394
for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7395
join_tab < join->join_tab+join->tables ; join_tab++)
7397
if (*join_tab->on_expr_ref)
7399
JOIN_TAB *cond_tab= join_tab->first_inner;
7400
COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7401
join->const_table_map,
7405
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7408
tmp->quick_fix_field();
7409
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7410
new Item_cond_and(cond_tab->select_cond,tmp);
7411
if (!cond_tab->select_cond)
7413
cond_tab->select_cond->quick_fix_field();
7417
/* Push down non-constant conditions from on expressions */
7418
JOIN_TAB *last_tab= tab;
7419
while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7422
Table tab is the last inner table of an outer join.
7423
An on expression is always attached to it.
7425
COND *on_expr= *first_inner_tab->on_expr_ref;
7427
table_map used_tables2= (join->const_table_map |
7428
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7429
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7431
current_map= tab->table->map;
7432
used_tables2|= current_map;
7433
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7437
JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7439
First add the guards for match variables of
7440
all embedding outer join operations.
7442
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7447
Now add the guard turning the predicate off for
7448
the null complemented row.
7450
DBUG_PRINT("info", ("Item_func_trig_cond"));
7451
tmp_cond= new Item_func_trig_cond(tmp_cond,
7454
DBUG_PRINT("info", ("Item_func_trig_cond 0x%lx",
7457
tmp_cond->quick_fix_field();
7458
/* Add the predicate to other pushed down predicates */
7459
DBUG_PRINT("info", ("Item_cond_and"));
7460
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7461
new Item_cond_and(cond_tab->select_cond,
7463
DBUG_PRINT("info", ("Item_cond_and 0x%lx",
7464
(ulong)cond_tab->select_cond));
7465
if (!cond_tab->select_cond)
7467
cond_tab->select_cond->quick_fix_field();
7470
first_inner_tab= first_inner_tab->first_upper;
7479
Check if given expression uses only table fields covered by the given index
7482
uses_index_fields_only()
7483
item Expression to check
7484
tbl The table having the index
7485
keyno The index number
7486
other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
7489
Check if given expression only uses fields covered by index #keyno in the
7490
table tbl. The expression can use any fields in any other tables.
7492
The expression is guaranteed not to be AND or OR - those constructs are
7493
handled outside of this function.
7500
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno,
7503
if (item->const_item())
7507
Don't push down the triggered conditions. Nested outer joins execution
7508
code may need to evaluate a condition several times (both triggered and
7509
untriggered), and there is no way to put thi
7510
TODO: Consider cloning the triggered condition and using the copies for:
7511
1. push the first copy down, to have most restrictive index condition
7513
2. Put the second copy into tab->select_cond.
7515
if (item->type() == Item::FUNC_ITEM &&
7516
((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7519
if (!(item->used_tables() & tbl->map))
7520
return other_tbls_ok;
7522
Item::Type item_type= item->type();
7523
switch (item_type) {
7524
case Item::FUNC_ITEM:
7526
/* This is a function, apply condition recursively to arguments */
7527
Item_func *item_func= (Item_func*)item;
7529
Item **item_end= (item_func->arguments()) + item_func->argument_count();
7530
for (child= item_func->arguments(); child != item_end; child++)
7532
if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
7537
case Item::COND_ITEM:
7539
/* This is a function, apply condition recursively to arguments */
7540
List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7544
if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7549
case Item::FIELD_ITEM:
7551
Item_field *item_field= (Item_field*)item;
7552
if (item_field->field->table != tbl)
7554
return item_field->field->part_of_key.is_set(keyno);
7556
case Item::REF_ITEM:
7557
return uses_index_fields_only(item->real_item(), tbl, keyno,
7560
return FALSE; /* Play it safe, don't push unknown non-const items */
7565
#define ICP_COND_USES_INDEX_ONLY 10
7568
Get a part of the condition that can be checked using only index fields
7571
make_cond_for_index()
7572
cond The source condition
7573
table The table that is partially available
7574
keyno The index in the above table. Only fields covered by the index
7576
other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
7579
Get a part of the condition that can be checked when for the given table
7580
we have values only of fields covered by some index. The condition may
7581
refer to other tables, it is assumed that we have values of all of their
7585
make_cond_for_index(
7586
"cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7589
"cond(t1.field) AND cond(t2.key2)"
7592
Index condition, or NULL if no condition could be inferred.
7595
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7600
if (cond->type() == Item::COND_ITEM)
7603
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7605
Item_cond_and *new_cond=new Item_cond_and;
7608
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7612
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7614
new_cond->argument_list()->push_back(fix);
7615
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7617
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7618
cond->marker= ICP_COND_USES_INDEX_ONLY;
7619
switch (new_cond->argument_list()->elements) {
7623
return new_cond->argument_list()->head();
7625
new_cond->quick_fix_field();
7631
Item_cond_or *new_cond=new Item_cond_or;
7634
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7638
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7641
new_cond->argument_list()->push_back(fix);
7642
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7644
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7645
cond->marker= ICP_COND_USES_INDEX_ONLY;
7646
new_cond->quick_fix_field();
7647
new_cond->top_level_item();
7652
if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
7654
cond->marker= ICP_COND_USES_INDEX_ONLY;
7659
Item *make_cond_remainder(Item *cond, bool exclude_index)
7661
if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7662
return 0; /* Already checked */
7664
if (cond->type() == Item::COND_ITEM)
7666
table_map tbl_map= 0;
7667
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7669
/* Create new top level AND item */
7670
Item_cond_and *new_cond=new Item_cond_and;
7673
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7677
Item *fix= make_cond_remainder(item, exclude_index);
7680
new_cond->argument_list()->push_back(fix);
7681
tbl_map |= fix->used_tables();
7684
switch (new_cond->argument_list()->elements) {
7688
return new_cond->argument_list()->head();
7690
new_cond->quick_fix_field();
7691
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7697
Item_cond_or *new_cond=new Item_cond_or;
7700
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7704
Item *fix= make_cond_remainder(item, FALSE);
7707
new_cond->argument_list()->push_back(fix);
7708
tbl_map |= fix->used_tables();
7710
new_cond->quick_fix_field();
7711
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7712
new_cond->top_level_item();
7721
Try to extract and push the index condition
7725
tab A join tab that has tab->table->file and its condition
7727
keyno Index for which extract and push the condition
7728
other_tbls_ok TRUE <=> Fields of other non-const tables are allowed
7731
Try to extract and push the index condition down to table handler
7734
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7736
DBUG_ENTER("push_index_cond");
7738
if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7739
tab->join->thd->variables.engine_condition_pushdown)
7741
DBUG_EXECUTE("where",
7742
print_where(tab->select_cond, "full cond", QT_ORDINARY););
7744
idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7747
DBUG_EXECUTE("where",
7748
print_where(idx_cond, "idx cond", QT_ORDINARY););
7752
tab->pre_idx_push_select_cond= tab->select_cond;
7753
Item *idx_remainder_cond=
7754
tab->table->file->idx_cond_push(keyno, idx_cond);
7757
Disable eq_ref's "lookup cache" if we've pushed down an index
7759
TODO: This check happens to work on current ICP implementations, but
7760
there may exist a compliant implementation that will not work
7761
correctly with it. Sort this out when we stabilize the condition
7764
if (idx_remainder_cond != idx_cond)
7765
tab->ref.disable_cache= TRUE;
7767
Item *row_cond= make_cond_remainder(tab->select_cond, TRUE);
7769
DBUG_EXECUTE("where",
7770
print_where(row_cond, "remainder cond", QT_ORDINARY););
7774
if (!idx_remainder_cond)
7775
tab->select_cond= row_cond;
7778
tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7779
tab->select_cond->quick_fix_field();
7780
((Item_cond_and*)tab->select_cond)->used_tables_cache=
7781
row_cond->used_tables() | idx_remainder_cond->used_tables();
7785
tab->select_cond= idx_remainder_cond;
7788
DBUG_EXECUTE("where",
7789
print_where(tab->select->cond,
7793
tab->select->cond= tab->select_cond;
7803
Determine if the set is already ordered for ORDER BY, so it can
7804
disable join cache because it will change the ordering of the results.
7805
Code handles sort table that is at any location (not only first after
7806
the const tables) despite the fact that it's currently prohibited.
7807
We must disable join cache if the first non-const table alone is
7808
ordered. If there is a temp table the ordering is done as a last
7809
operation and doesn't prevent join cache usage.
7811
uint make_join_orderinfo(JOIN *join)
7815
return join->tables;
7817
for (i=join->const_tables ; i < join->tables ; i++)
7819
JOIN_TAB *tab=join->join_tab+i;
7820
TABLE *table=tab->table;
7821
if ((table == join->sort_by_table &&
7822
(!join->order || join->skip_sort_order)) ||
7823
(join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7833
Plan refinement stage: do various set ups for the executioner
7836
make_join_readinfo()
7837
join Join being processed
7838
options Join's options (checking for SELECT_DESCRIBE,
7839
SELECT_NO_JOIN_CACHE)
7840
no_jbuf_after Don't use join buffering after table with this number.
7843
Plan refinement stage: do various set ups for the executioner
7844
- set up use of join buffering
7845
- push index conditions
7846
- increment counters
7851
TRUE - Out of memory
7855
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
7858
bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7860
DBUG_ENTER("make_join_readinfo");
7862
for (i=join->const_tables ; i < join->tables ; i++)
7864
JOIN_TAB *tab=join->join_tab+i;
7865
TABLE *table=tab->table;
7866
bool using_join_cache;
7867
tab->read_record.table= table;
7868
tab->read_record.file=table->file;
7869
tab->next_select=sub_select; /* normal select */
7871
TODO: don't always instruct first table's ref/range access method to
7872
produce sorted output.
7874
tab->sorted= sorted;
7875
sorted= 0; // only first must be sorted
7876
if (tab->insideout_match_tab)
7878
if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7883
switch (tab->type) {
7884
case JT_SYSTEM: // Only happens with left join
7885
table->status=STATUS_NO_RECORD;
7886
tab->read_first_record= join_read_system;
7887
tab->read_record.read_record= join_no_more_records;
7889
case JT_CONST: // Only happens with left join
7890
table->status=STATUS_NO_RECORD;
7891
tab->read_first_record= join_read_const;
7892
tab->read_record.read_record= join_no_more_records;
7893
if (table->covering_keys.is_set(tab->ref.key) &&
7897
table->file->extra(HA_EXTRA_KEYREAD);
7901
table->status=STATUS_NO_RECORD;
7904
delete tab->select->quick;
7905
tab->select->quick=0;
7909
tab->read_first_record= join_read_key;
7910
tab->read_record.read_record= join_no_more_records;
7911
if (table->covering_keys.is_set(tab->ref.key) &&
7915
table->file->extra(HA_EXTRA_KEYREAD);
7918
push_index_cond(tab, tab->ref.key, TRUE);
7920
case JT_REF_OR_NULL:
7922
table->status=STATUS_NO_RECORD;
7925
delete tab->select->quick;
7926
tab->select->quick=0;
7930
if (table->covering_keys.is_set(tab->ref.key) &&
7934
table->file->extra(HA_EXTRA_KEYREAD);
7937
push_index_cond(tab, tab->ref.key, TRUE);
7938
if (tab->type == JT_REF)
7940
tab->read_first_record= join_read_always_key;
7941
tab->read_record.read_record= tab->insideout_match_tab?
7942
join_read_next_same_diff : join_read_next_same;
7946
tab->read_first_record= join_read_always_key_or_null;
7947
tab->read_record.read_record= join_read_next_same_or_null;
7952
If previous table use cache
7953
If the incoming data set is already sorted don't use cache.
7955
table->status=STATUS_NO_RECORD;
7956
using_join_cache= FALSE;
7957
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7958
tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7959
!tab->insideout_match_tab)
7961
if ((options & SELECT_DESCRIBE) ||
7962
!join_init_cache(join->thd,join->join_tab+join->const_tables,
7963
i-join->const_tables))
7965
using_join_cache= TRUE;
7966
tab[-1].next_select=sub_select_cache; /* Patch previous */
7969
/* These init changes read_record */
7970
if (tab->use_quick == 2)
7972
join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7973
tab->read_first_record= join_init_quick_read_record;
7975
status_var_increment(join->thd->status_var.select_range_check_count);
7979
tab->read_first_record= join_init_read_record;
7980
if (i == join->const_tables)
7982
if (tab->select && tab->select->quick)
7985
status_var_increment(join->thd->status_var.select_range_count);
7989
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7991
status_var_increment(join->thd->status_var.select_scan_count);
7996
if (tab->select && tab->select->quick)
7999
status_var_increment(join->thd->status_var.select_full_range_join_count);
8003
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
8005
status_var_increment(join->thd->status_var.select_full_join_count);
8008
if (!table->no_keyread)
8010
if (tab->select && tab->select->quick &&
8011
tab->select->quick->index != MAX_KEY && //not index_merge
8012
table->covering_keys.is_set(tab->select->quick->index))
8015
table->file->extra(HA_EXTRA_KEYREAD);
8017
else if (!table->covering_keys.is_clear_all() &&
8018
!(tab->select && tab->select->quick))
8019
{ // Only read index tree
8020
if (!tab->insideout_match_tab)
8023
See bug #26447: "Using the clustered index for a table scan
8024
is always faster than using a secondary index".
8026
if (table->s->primary_key != MAX_KEY &&
8027
table->file->primary_key_is_clustered())
8028
tab->index= table->s->primary_key;
8030
tab->index=find_shortest_key(table, & table->covering_keys);
8032
tab->read_first_record= join_read_first;
8033
tab->type=JT_NEXT; // Read with index_first / index_next
8036
if (tab->select && tab->select->quick &&
8037
tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
8038
push_index_cond(tab, tab->select->quick->index, !using_join_cache);
8042
DBUG_PRINT("error",("Table type %d found",tab->type)); /* purecov: deadcode */
8043
break; /* purecov: deadcode */
8046
abort(); /* purecov: deadcode */
8049
join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
8055
Give error if we some tables are done with a full join.
8057
This is used by multi_table_update and multi_table_delete when running
8060
@param join Join condition
8065
1 Error (full join used)
8068
bool error_if_full_join(JOIN *join)
8070
for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
8074
if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
8076
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
8077
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
8089
void JOIN_TAB::cleanup()
8100
if (table->key_read)
8103
table->file->extra(HA_EXTRA_NO_KEYREAD);
8105
table->file->ha_index_or_rnd_end();
8107
We need to reset this for next select
8108
(Tested in part_of_refkey)
8110
table->reginfo.join_tab= 0;
8112
end_read_record(&read_record);
8117
Partially cleanup JOIN after it has executed: close index or rnd read
8118
(table cursors), free quick selects.
8120
This function is called in the end of execution of a JOIN, before the used
8121
tables are unlocked and closed.
8123
For a join that is resolved using a temporary table, the first sweep is
8124
performed against actual tables and an intermediate result is inserted
8125
into the temprorary table.
8126
The last sweep is performed against the temporary table. Therefore,
8127
the base tables and associated buffers used to fill the temporary table
8128
are no longer needed, and this function is called to free them.
8130
For a join that is performed without a temporary table, this function
8131
is called after all rows are sent, but before EOF packet is sent.
8133
For a simple SELECT with no subqueries this function performs a full
8134
cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
8137
If a JOIN is executed for a subquery or if it has a subquery, we can't
8138
do the full cleanup and need to do a partial cleanup only.
8139
- If a JOIN is not the top level join, we must not unlock the tables
8140
because the outer select may not have been evaluated yet, and we
8141
can't unlock only selected tables of a query.
8142
- Additionally, if this JOIN corresponds to a correlated subquery, we
8143
should not free quick selects and join buffers because they will be
8144
needed for the next execution of the correlated subquery.
8145
- However, if this is a JOIN for a [sub]select, which is not
8146
a correlated subquery itself, but has subqueries, we can free it
8147
fully and also free JOINs of all its subqueries. The exception
8148
is a subquery in SELECT list, e.g: @n
8149
SELECT a, (select max(b) from t1) group by c @n
8150
This subquery will not be evaluated at first sweep and its value will
8151
not be inserted into the temporary table. Instead, it's evaluated
8152
when selecting from the temporary table. Therefore, it can't be freed
8153
here even though it's not correlated.
8156
Unlock tables even if the join isn't top level select in the tree
8159
void JOIN::join_free()
8161
SELECT_LEX_UNIT *tmp_unit;
8164
Optimization: if not EXPLAIN and we are done with the JOIN,
8167
bool full= (!select_lex->uncacheable && !thd->lex->describe);
8168
bool can_unlock= full;
8169
DBUG_ENTER("JOIN::join_free");
8173
for (tmp_unit= select_lex->first_inner_unit();
8175
tmp_unit= tmp_unit->next_unit())
8176
for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
8178
Item_subselect *subselect= sl->master_unit()->item;
8179
bool full_local= full && (!subselect || subselect->is_evaluated());
8181
If this join is evaluated, we can fully clean it up and clean up all
8182
its underlying joins even if they are correlated -- they will not be
8183
used any more anyway.
8184
If this join is not yet evaluated, we still must clean it up to
8185
close its table cursors -- it may never get evaluated, as in case of
8186
... HAVING FALSE OR a IN (SELECT ...))
8187
but all table cursors must be closed before the unlock.
8189
sl->cleanup_all_joins(full_local);
8190
/* Can't unlock if at least one JOIN is still needed */
8191
can_unlock= can_unlock && full_local;
8195
We are not using tables anymore
8196
Unlock all tables. We may be in an INSERT .... SELECT statement.
8198
if (can_unlock && lock && thd->lock &&
8199
!(select_options & SELECT_NO_UNLOCK) &&
8200
!select_lex->subquery_in_having &&
8201
(select_lex == (thd->lex->unit.fake_select_lex ?
8202
thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
8205
TODO: unlock tables even if the join isn't top level select in the
8208
mysql_unlock_read_tables(thd, lock); // Don't free join->lock
8217
Free resources of given join.
8219
@param fill true if we should free all resources, call with full==1
8220
should be last, before it this function can be called with
8224
With subquery this function definitely will be called several times,
8225
but even for simple query it can be called several times.
8228
void JOIN::cleanup(bool full)
8230
DBUG_ENTER("JOIN::cleanup");
8236
Only a sorted table may be cached. This sorted table is always the
8237
first non const table in join->table
8239
if (tables > const_tables) // Test for not-const tables
8241
free_io_cache(table[const_tables]);
8242
filesort_free_buffers(table[const_tables],full);
8247
for (tab= join_tab, end= tab+tables; tab != end; tab++)
8253
for (tab= join_tab, end= tab+tables; tab != end; tab++)
8256
tab->table->file->ha_index_or_rnd_end();
8259
cleanup_sj_tmp_tables(this);//
8262
We are not using tables anymore
8263
Unlock all tables. We may be in an INSERT .... SELECT statement.
8268
tmp_table_param.copy_field= 0;
8269
group_fields.delete_elements();
8271
We can't call delete_elements() on copy_funcs as this will cause
8272
problems in free_elements() as some of the elements are then deleted.
8274
tmp_table_param.copy_funcs.empty();
8276
If we have tmp_join and 'this' JOIN is not tmp_join and
8277
tmp_table_param.copy_field's of them are equal then we have to remove
8278
pointer to tmp_table_param.copy_field from tmp_join, because it qill
8279
be removed in tmp_table_param.cleanup().
8283
tmp_join->tmp_table_param.copy_field ==
8284
tmp_table_param.copy_field)
8286
tmp_join->tmp_table_param.copy_field=
8287
tmp_join->tmp_table_param.save_copy_field= 0;
8289
tmp_table_param.cleanup();
8296
Remove the following expressions from ORDER BY and GROUP BY:
8297
Constant expressions @n
8298
Expression that only uses tables that are of type EQ_REF and the reference
8299
is in the ORDER list or if all refereed tables are of the above type.
8301
In the following, the X field can be removed:
8303
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
8304
SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
8307
These can't be optimized:
8309
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
8310
SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
8311
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
8316
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
8318
if (tab->cached_eq_ref_table) // If cached
8319
return tab->eq_ref_table;
8320
tab->cached_eq_ref_table=1;
8321
/* We can skip const tables only if not an outer table */
8322
if (tab->type == JT_CONST && !tab->first_inner)
8323
return (tab->eq_ref_table=1); /* purecov: inspected */
8324
if (tab->type != JT_EQ_REF || tab->table->maybe_null)
8325
return (tab->eq_ref_table=0); // We must use this
8326
Item **ref_item=tab->ref.items;
8327
Item **end=ref_item+tab->ref.key_parts;
8329
table_map map=tab->table->map;
8331
for (; ref_item != end ; ref_item++)
8333
if (! (*ref_item)->const_item())
8334
{ // Not a const ref
8336
for (order=start_order ; order ; order=order->next)
8338
if ((*ref_item)->eq(order->item[0],0))
8344
DBUG_ASSERT(!(order->used & map));
8346
continue; // Used in ORDER BY
8348
if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8349
return (tab->eq_ref_table=0);
8352
/* Check that there was no reference to table before sort order */
8353
for (; found && start_order ; start_order=start_order->next)
8355
if (start_order->used & map)
8360
if (start_order->depend_map & map)
8361
return (tab->eq_ref_table=0);
8363
return tab->eq_ref_table=1;
8368
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
8370
if (specialflag & SPECIAL_SAFE_MODE)
8371
return 0; // skip this optimize /* purecov: inspected */
8372
for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8374
if (tables & 1 && !eq_ref_table(join, order, *tab))
8381
/** Update the dependency map for the tables. */
8383
static void update_depend_map(JOIN *join)
8385
JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
8387
for (; join_tab != end ; join_tab++)
8389
TABLE_REF *ref= &join_tab->ref;
8390
table_map depend_map=0;
8391
Item **item=ref->items;
8393
for (i=0 ; i < ref->key_parts ; i++,item++)
8394
depend_map|=(*item)->used_tables();
8395
ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8396
depend_map&= ~OUTER_REF_TABLE_BIT;
8397
for (JOIN_TAB **tab=join->map2table;
8399
tab++,depend_map>>=1 )
8402
ref->depend_map|=(*tab)->ref.depend_map;
8408
/** Update the dependency map for the sort order. */
8410
static void update_depend_map(JOIN *join, ORDER *order)
8412
for (; order ; order=order->next)
8414
table_map depend_map;
8415
order->item[0]->update_used_tables();
8416
order->depend_map=depend_map=order->item[0]->used_tables();
8417
// Not item_sum(), RAND() and no reference to table outside of sub select
8418
if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
8419
&& !order->item[0]->with_sum_func)
8421
for (JOIN_TAB **tab=join->map2table;
8423
tab++, depend_map>>=1)
8426
order->depend_map|=(*tab)->ref.depend_map;
8434
Remove all constants and check if ORDER only contains simple
8437
simple_order is set to 1 if sort_order only uses fields from head table
8438
and the head table is not a LEFT JOIN table.
8440
@param join Join handler
8441
@param first_order List of SORT or GROUP order
8442
@param cond WHERE statement
8443
@param change_list Set to 1 if we should remove things from list.
8444
If this is not set, then only simple_order is
8446
@param simple_order Set to 1 if we are only using simple expressions
8449
Returns new sort order
8453
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8454
bool change_list, bool *simple_order)
8456
if (join->tables == join->const_tables)
8457
return change_list ? 0 : first_order; // No need to sort
8459
ORDER *order,**prev_ptr;
8460
table_map first_table= join->join_tab[join->const_tables].table->map;
8461
table_map not_const_tables= ~join->const_table_map;
8463
DBUG_ENTER("remove_const");
8465
prev_ptr= &first_order;
8466
*simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8468
/* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8470
update_depend_map(join, first_order);
8471
for (order=first_order; order ; order=order->next)
8473
table_map order_tables=order->item[0]->used_tables();
8474
if (order->item[0]->with_sum_func)
8475
*simple_order=0; // Must do a temp table to sort
8476
else if (!(order_tables & not_const_tables))
8478
if (order->item[0]->with_subselect)
8479
order->item[0]->val_str(&order->item[0]->str_value);
8480
DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8481
continue; // skip const item
8485
if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8490
if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8492
DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8495
if ((ref=order_tables & (not_const_tables ^ first_table)))
8497
if (!(order_tables & first_table) &&
8498
only_eq_ref_tables(join,first_order, ref))
8500
DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8503
*simple_order=0; // Must do a temp table to sort
8508
*prev_ptr= order; // use this entry
8509
prev_ptr= &order->next;
8513
if (prev_ptr == &first_order) // Nothing to sort/group
8515
DBUG_PRINT("exit",("simple_order: %d",(int) *simple_order));
8516
DBUG_RETURN(first_order);
8521
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
8522
List<Item> &fields, bool send_row, ulonglong select_options,
8523
const char *info, Item *having)
8525
DBUG_ENTER("return_zero_rows");
8527
if (select_options & SELECT_DESCRIBE)
8529
select_describe(join, FALSE, FALSE, FALSE, info);
8537
for (TABLE_LIST *table= tables; table; table= table->next_leaf)
8538
mark_as_null_row(table->table); // All fields are NULL
8539
if (having && having->val_int() == 0)
8542
if (!(result->send_fields(fields,
8543
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
8547
List_iterator_fast<Item> it(fields);
8549
while ((item= it++))
8550
item->no_rows_in_result();
8551
result->send_data(fields);
8553
result->send_eof(); // Should be safe
8555
/* Update results for FOUND_ROWS */
8556
join->thd->limit_found_rows= join->thd->examined_row_count= 0;
8561
used only in JOIN::clear
8563
static void clear_tables(JOIN *join)
8566
must clear only the non-const tables, as const tables
8567
are not re-calculated.
8569
for (uint i=join->const_tables ; i < join->tables ; i++)
8570
mark_as_null_row(join->table[i]); // All fields are NULL
8573
/*****************************************************************************
8574
Make som simple condition optimization:
8575
If there is a test 'field = const' change all refs to 'field' to 'const'
8576
Remove all dummy tests 'item = item', 'const op const'.
8577
Remove all 'item is NULL', when item can never be null!
8578
item->marker should be 0 for all items on entry
8579
Return in cond_value FALSE if condition is impossible (1 = 2)
8580
*****************************************************************************/
8582
class COND_CMP :public ilink {
8584
static void *operator new(size_t size)
8586
return (void*) sql_alloc((uint) size);
8588
static void operator delete(void *ptr __attribute__((unused)),
8589
size_t size __attribute__((unused)))
8590
{ TRASH(ptr, size); }
8593
Item_func *cmp_func;
8594
COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
8597
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
8598
template class I_List<COND_CMP>;
8599
template class I_List_iterator<COND_CMP>;
8604
Find the multiple equality predicate containing a field.
8606
The function retrieves the multiple equalities accessed through
8607
the con_equal structure from current level and up looking for
8608
an equality containing field. It stops retrieval as soon as the equality
8609
is found and set up inherited_fl to TRUE if it's found on upper levels.
8611
@param cond_equal multiple equalities to search in
8612
@param field field to look for
8613
@param[out] inherited_fl set up to TRUE if multiple equality is found
8614
on upper levels (not on current level of
8618
- Item_equal for the found multiple equality predicate if a success;
8622
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
8625
Item_equal *item= 0;
8626
bool in_upper_level= FALSE;
8629
List_iterator_fast<Item_equal> li(cond_equal->current_level);
8630
while ((item= li++))
8632
if (item->contains(field))
8635
in_upper_level= TRUE;
8636
cond_equal= cond_equal->upper_levels;
8638
in_upper_level= FALSE;
8640
*inherited_fl= in_upper_level;
8646
Check whether an equality can be used to build multiple equalities.
8648
This function first checks whether the equality (left_item=right_item)
8649
is a simple equality i.e. the one that equates a field with another field
8650
or a constant (field=field_item or field=const_item).
8651
If this is the case the function looks for a multiple equality
8652
in the lists referenced directly or indirectly by cond_equal inferring
8653
the given simple equality. If it doesn't find any, it builds a multiple
8654
equality that covers the predicate, i.e. the predicate can be inferred
8655
from this multiple equality.
8656
The built multiple equality could be obtained in such a way:
8657
create a binary multiple equality equivalent to the predicate, then
8658
merge it, if possible, with one of old multiple equalities.
8659
This guarantees that the set of multiple equalities covering equality
8660
predicates will be minimal.
8663
For the where condition
8665
WHERE a=b AND b=c AND
8668
the check_equality will be called for the following equality
8669
predicates a=b, b=c, b=2 and f=e.
8670
- For a=b it will be called with *cond_equal=(0,[]) and will transform
8671
*cond_equal into (0,[Item_equal(a,b)]).
8672
- For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
8673
and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
8674
- For b=2 it will be called with *cond_equal=(ptr(CE),[])
8675
and will transform *cond_equal into (ptr(CE),[Item_equal(2,a,b,c)]).
8676
- For f=e it will be called with *cond_equal=(ptr(CE), [])
8677
and will transform *cond_equal into (ptr(CE),[Item_equal(f,e)]).
8680
Now only fields that have the same type definitions (verified by
8681
the Field::eq_def method) are placed to the same multiple equalities.
8682
Because of this some equality predicates are not eliminated and
8683
can be used in the constant propagation procedure.
8684
We could weeken the equlity test as soon as at least one of the
8685
equal fields is to be equal to a constant. It would require a
8686
more complicated implementation: we would have to store, in
8687
general case, its own constant for each fields from the multiple
8688
equality. But at the same time it would allow us to get rid
8689
of constant propagation completely: it would be done by the call
8690
to build_equal_items_for_cond.
8693
The implementation does not follow exactly the above rules to
8694
build a new multiple equality for the equality predicate.
8695
If it processes the equality of the form field1=field2, it
8696
looks for multiple equalities me1 containig field1 and me2 containing
8697
field2. If only one of them is found the fuction expands it with
8698
the lacking field. If multiple equalities for both fields are
8699
found they are merged. If both searches fail a new multiple equality
8700
containing just field1 and field2 is added to the existing
8701
multiple equalities.
8702
If the function processes the predicate of the form field1=const,
8703
it looks for a multiple equality containing field1. If found, the
8704
function checks the constant of the multiple equality. If the value
8705
is unknown, it is setup to const. Otherwise the value is compared with
8706
const and the evaluation of the equality predicate is performed.
8707
When expanding/merging equality predicates from the upper levels
8708
the function first copies them for the current level. It looks
8709
acceptable, as this happens rarely. The implementation without
8710
copying would be much more complicated.
8712
@param left_item left term of the quality to be checked
8713
@param right_item right term of the equality to be checked
8714
@param item equality item if the equality originates from a condition
8715
predicate, 0 if the equality is the result of row
8717
@param cond_equal multiple equalities that must hold together with the
8721
TRUE if the predicate is a simple equality predicate to be used
8722
for building multiple equalities
8727
static bool check_simple_equality(Item *left_item, Item *right_item,
8728
Item *item, COND_EQUAL *cond_equal)
8730
if (left_item->type() == Item::REF_ITEM &&
8731
((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
8733
if (((Item_ref*)left_item)->depended_from)
8735
left_item= left_item->real_item();
8737
if (right_item->type() == Item::REF_ITEM &&
8738
((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
8740
if (((Item_ref*)right_item)->depended_from)
8742
right_item= right_item->real_item();
8744
if (left_item->type() == Item::FIELD_ITEM &&
8745
right_item->type() == Item::FIELD_ITEM &&
8746
!((Item_field*)left_item)->depended_from &&
8747
!((Item_field*)right_item)->depended_from)
8749
/* The predicate the form field1=field2 is processed */
8751
Field *left_field= ((Item_field*) left_item)->field;
8752
Field *right_field= ((Item_field*) right_item)->field;
8754
if (!left_field->eq_def(right_field))
8757
/* Search for multiple equalities containing field1 and/or field2 */
8758
bool left_copyfl, right_copyfl;
8759
Item_equal *left_item_equal=
8760
find_item_equal(cond_equal, left_field, &left_copyfl);
8761
Item_equal *right_item_equal=
8762
find_item_equal(cond_equal, right_field, &right_copyfl);
8764
/* As (NULL=NULL) != TRUE we can't just remove the predicate f=f */
8765
if (left_field->eq(right_field)) /* f = f */
8766
return (!(left_field->maybe_null() && !left_item_equal));
8768
if (left_item_equal && left_item_equal == right_item_equal)
8771
The equality predicate is inference of one of the existing
8772
multiple equalities, i.e the condition is already covered
8773
by upper level equalities
8778
bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
8779
item->name < subq_sj_cond_name + 64);
8780
/* Copy the found multiple equalities at the current level if needed */
8783
/* left_item_equal of an upper level contains left_item */
8784
left_item_equal= new Item_equal(left_item_equal);
8785
cond_equal->current_level.push_back(left_item_equal);
8787
left_item_equal->name = item->name;
8791
/* right_item_equal of an upper level contains right_item */
8792
right_item_equal= new Item_equal(right_item_equal);
8793
cond_equal->current_level.push_back(right_item_equal);
8795
right_item_equal->name = item->name;
8798
if (left_item_equal)
8800
/* left item was found in the current or one of the upper levels */
8801
if (! right_item_equal)
8802
left_item_equal->add((Item_field *) right_item);
8805
/* Merge two multiple equalities forming a new one */
8806
left_item_equal->merge(right_item_equal);
8807
/* Remove the merged multiple equality from the list */
8808
List_iterator<Item_equal> li(cond_equal->current_level);
8809
while ((li++) != right_item_equal) {};
8815
/* left item was not found neither the current nor in upper levels */
8816
if (right_item_equal)
8818
right_item_equal->add((Item_field *) left_item);
8820
right_item_equal->name = item->name;
8824
/* None of the fields was found in multiple equalities */
8825
Item_equal *item_equal= new Item_equal((Item_field *) left_item,
8826
(Item_field *) right_item);
8827
cond_equal->current_level.push_back(item_equal);
8829
item_equal->name = item->name;
8836
/* The predicate of the form field=const/const=field is processed */
8837
Item *const_item= 0;
8838
Item_field *field_item= 0;
8839
if (left_item->type() == Item::FIELD_ITEM &&
8840
!((Item_field*)left_item)->depended_from &&
8841
right_item->const_item())
8843
field_item= (Item_field*) left_item;
8844
const_item= right_item;
8846
else if (right_item->type() == Item::FIELD_ITEM &&
8847
!((Item_field*)right_item)->depended_from &&
8848
left_item->const_item())
8850
field_item= (Item_field*) right_item;
8851
const_item= left_item;
8855
field_item->result_type() == const_item->result_type())
8859
if (field_item->result_type() == STRING_RESULT)
8861
CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
8864
Item_func_eq *eq_item;
8865
if ((eq_item= new Item_func_eq(left_item, right_item)))
8867
eq_item->set_cmp_func();
8868
eq_item->quick_fix_field();
8871
if ((cs != ((Item_func *) item)->compare_collation()) ||
8872
!cs->coll->propagate(cs, 0, 0))
8876
Item_equal *item_equal = find_item_equal(cond_equal,
8877
field_item->field, ©fl);
8880
item_equal= new Item_equal(item_equal);
8881
cond_equal->current_level.push_back(item_equal);
8886
The flag cond_false will be set to 1 after this, if item_equal
8887
already contains a constant and its value is not equal to
8888
the value of const_item.
8890
item_equal->add(const_item);
8894
item_equal= new Item_equal(const_item, field_item);
8895
cond_equal->current_level.push_back(item_equal);
8905
Convert row equalities into a conjunction of regular equalities.
8907
The function converts a row equality of the form (E1,...,En)=(E'1,...,E'n)
8908
into a list of equalities E1=E'1,...,En=E'n. For each of these equalities
8909
Ei=E'i the function checks whether it is a simple equality or a row
8910
equality. If it is a simple equality it is used to expand multiple
8911
equalities of cond_equal. If it is a row equality it converted to a
8912
sequence of equalities between row elements. If Ei=E'i is neither a
8913
simple equality nor a row equality the item for this predicate is added
8916
@param thd thread handle
8917
@param left_row left term of the row equality to be processed
8918
@param right_row right term of the row equality to be processed
8919
@param cond_equal multiple equalities that must hold together with the
8921
@param eq_list results of conversions of row equalities that are not
8922
simple enough to form multiple equalities
8925
TRUE if conversion has succeeded (no fatal error)
8930
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8931
COND_EQUAL *cond_equal, List<Item>* eq_list)
8933
uint n= left_row->cols();
8934
for (uint i= 0 ; i < n; i++)
8937
Item *left_item= left_row->element_index(i);
8938
Item *right_item= right_row->element_index(i);
8939
if (left_item->type() == Item::ROW_ITEM &&
8940
right_item->type() == Item::ROW_ITEM)
8942
is_converted= check_row_equality(thd,
8943
(Item_row *) left_item,
8944
(Item_row *) right_item,
8945
cond_equal, eq_list);
8947
thd->lex->current_select->cond_count++;
8951
is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
8952
thd->lex->current_select->cond_count++;
8957
Item_func_eq *eq_item;
8958
if (!(eq_item= new Item_func_eq(left_item, right_item)))
8960
eq_item->set_cmp_func();
8961
eq_item->quick_fix_field();
8962
eq_list->push_back(eq_item);
8970
Eliminate row equalities and form multiple equalities predicates.
8972
This function checks whether the item is a simple equality
8973
i.e. the one that equates a field with another field or a constant
8974
(field=field_item or field=constant_item), or, a row equality.
8975
For a simple equality the function looks for a multiple equality
8976
in the lists referenced directly or indirectly by cond_equal inferring
8977
the given simple equality. If it doesn't find any, it builds/expands
8978
multiple equality that covers the predicate.
8979
Row equalities are eliminated substituted for conjunctive regular
8980
equalities which are treated in the same way as original equality
8983
@param thd thread handle
8984
@param item predicate to process
8985
@param cond_equal multiple equalities that must hold together with the
8987
@param eq_list results of conversions of row equalities that are not
8988
simple enough to form multiple equalities
8991
TRUE if re-writing rules have been applied
8993
FALSE otherwise, i.e.
8994
if the predicate is not an equality,
8995
or, if the equality is neither a simple one nor a row equality,
8996
or, if the procedure fails by a fatal error.
8999
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
9000
List<Item> *eq_list)
9002
if (item->type() == Item::FUNC_ITEM &&
9003
((Item_func*) item)->functype() == Item_func::EQ_FUNC)
9005
Item *left_item= ((Item_func*) item)->arguments()[0];
9006
Item *right_item= ((Item_func*) item)->arguments()[1];
9008
if (left_item->type() == Item::ROW_ITEM &&
9009
right_item->type() == Item::ROW_ITEM)
9011
thd->lex->current_select->cond_count--;
9012
return check_row_equality(thd,
9013
(Item_row *) left_item,
9014
(Item_row *) right_item,
9015
cond_equal, eq_list);
9018
return check_simple_equality(left_item, right_item, item, cond_equal);
9025
Replace all equality predicates in a condition by multiple equality items.
9027
At each 'and' level the function detects items for equality predicates
9028
and replaced them by a set of multiple equality items of class Item_equal,
9029
taking into account inherited equalities from upper levels.
9030
If an equality predicate is used not in a conjunction it's just
9031
replaced by a multiple equality predicate.
9032
For each 'and' level the function set a pointer to the inherited
9033
multiple equalities in the cond_equal field of the associated
9034
object of the type Item_cond_and.
9035
The function also traverses the cond tree and and for each field reference
9036
sets a pointer to the multiple equality item containing the field, if there
9037
is any. If this multiple equality equates fields to a constant the
9038
function replaces the field reference by the constant in the cases
9039
when the field is not of a string type or when the field reference is
9040
just an argument of a comparison predicate.
9041
The function also determines the maximum number of members in
9042
equality lists of each Item_cond_and object assigning it to
9043
thd->lex->current_select->max_equal_elems.
9046
Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
9047
f1=f2, .., fn-1=fn. It substitutes any inference from these
9048
equality predicates that is equivalent to the conjunction.
9049
Thus, =(a1,a2,a3) can substitute for ((a1=a3) AND (a2=a3) AND (a2=a1)) as
9050
it is equivalent to ((a1=a2) AND (a2=a3)).
9051
The function always makes a substitution of all equality predicates occured
9052
in a conjuction for a minimal set of multiple equality predicates.
9053
This set can be considered as a canonical representation of the
9054
sub-conjunction of the equality predicates.
9055
E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by
9056
(=(t1.a,t2.b,t3.c) AND t2.b>5), not by
9057
(=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
9058
while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
9059
(=(t1.a,t2.b) AND =(t3.c=t4.d) AND t2.b>5),
9060
but if additionally =(t4.d,t2.b) is inherited, it
9061
will be replaced by (=(t1.a,t2.b,t3.c,t4.d) AND t2.b>5)
9063
The function performs the substitution in a recursive descent by
9064
the condtion tree, passing to the next AND level a chain of multiple
9065
equality predicates which have been built at the upper levels.
9066
The Item_equal items built at the level are attached to other
9067
non-equality conjucts as a sublist. The pointer to the inherited
9068
multiple equalities is saved in the and condition object (Item_cond_and).
9069
This chain allows us for any field reference occurence easyly to find a
9070
multiple equality that must be held for this occurence.
9071
For each AND level we do the following:
9072
- scan it for all equality predicate (=) items
9073
- join them into disjoint Item_equal() groups
9074
- process the included OR conditions recursively to do the same for
9077
We need to do things in this order as lower AND levels need to know about
9078
all possible Item_equal objects in upper levels.
9080
@param thd thread handle
9081
@param cond condition(expression) where to make replacement
9082
@param inherited path to all inherited multiple equality items
9085
pointer to the transformed condition
9088
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
9089
COND_EQUAL *inherited)
9091
Item_equal *item_equal;
9092
COND_EQUAL cond_equal;
9093
cond_equal.upper_levels= inherited;
9095
if (cond->type() == Item::COND_ITEM)
9098
bool and_level= ((Item_cond*) cond)->functype() ==
9099
Item_func::COND_AND_FUNC;
9100
List<Item> *args= ((Item_cond*) cond)->argument_list();
9102
List_iterator<Item> li(*args);
9108
Retrieve all conjucts of this level detecting the equality
9109
that are subject to substitution by multiple equality items and
9110
removing each such predicate from the conjunction after having
9111
found/created a multiple equality whose inference the predicate is.
9113
while ((item= li++))
9116
PS/SP note: we can safely remove a node from AND-OR
9117
structure here because it's restored before each
9118
re-execution of any prepared statement/stored procedure.
9120
if (check_equality(thd, item, &cond_equal, &eq_list))
9124
List_iterator_fast<Item_equal> it(cond_equal.current_level);
9125
while ((item_equal= it++))
9127
item_equal->fix_length_and_dec();
9128
item_equal->update_used_tables();
9129
set_if_bigger(thd->lex->current_select->max_equal_elems,
9130
item_equal->members());
9133
((Item_cond_and*)cond)->cond_equal= cond_equal;
9134
inherited= &(((Item_cond_and*)cond)->cond_equal);
9137
Make replacement of equality predicates for lower levels
9138
of the condition expression.
9141
while ((item= li++))
9144
if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
9146
/* This replacement happens only for standalone equalities */
9148
This is ok with PS/SP as the replacement is done for
9149
arguments of an AND/OR item, which are restored for each
9152
li.replace(new_item);
9157
args->concat(&eq_list);
9158
args->concat((List<Item> *)&cond_equal.current_level);
9161
else if (cond->type() == Item::FUNC_ITEM)
9165
If an equality predicate forms the whole and level,
9166
we call it standalone equality and it's processed here.
9167
E.g. in the following where condition
9168
WHERE a=5 AND (b=5 or a=c)
9169
(b=5) and (a=c) are standalone equalities.
9170
In general we can't leave alone standalone eqalities:
9171
for WHERE a=b AND c=d AND (b=c OR d=5)
9172
b=c is replaced by =(a,b,c,d).
9174
if (check_equality(thd, cond, &cond_equal, &eq_list))
9176
int n= cond_equal.current_level.elements + eq_list.elements;
9178
return new Item_int((longlong) 1,1);
9181
if ((item_equal= cond_equal.current_level.pop()))
9183
item_equal->fix_length_and_dec();
9184
item_equal->update_used_tables();
9187
item_equal= (Item_equal *) eq_list.pop();
9188
set_if_bigger(thd->lex->current_select->max_equal_elems,
9189
item_equal->members());
9195
Here a new AND level must be created. It can happen only
9196
when a row equality is processed as a standalone predicate.
9198
Item_cond_and *and_cond= new Item_cond_and(eq_list);
9199
and_cond->quick_fix_field();
9200
List<Item> *args= and_cond->argument_list();
9201
List_iterator_fast<Item_equal> it(cond_equal.current_level);
9202
while ((item_equal= it++))
9204
item_equal->fix_length_and_dec();
9205
item_equal->update_used_tables();
9206
set_if_bigger(thd->lex->current_select->max_equal_elems,
9207
item_equal->members());
9209
and_cond->cond_equal= cond_equal;
9210
args->concat((List<Item> *)&cond_equal.current_level);
9216
For each field reference in cond, not from equal item predicates,
9217
set a pointer to the multiple equality it belongs to (if there is any)
9218
as soon the field is not of a string type or the field reference is
9219
an argument of a comparison predicate.
9221
uchar *is_subst_valid= (uchar *) 1;
9222
cond= cond->compile(&Item::subst_argument_checker,
9224
&Item::equal_fields_propagator,
9225
(uchar *) inherited);
9226
cond->update_used_tables();
9233
Build multiple equalities for a condition and all on expressions that
9234
inherit these multiple equalities.
9236
The function first applies the build_equal_items_for_cond function
9237
to build all multiple equalities for condition cond utilizing equalities
9238
referred through the parameter inherited. The extended set of
9239
equalities is returned in the structure referred by the cond_equal_ref
9240
parameter. After this the function calls itself recursively for
9241
all on expressions whose direct references can be found in join_list
9242
and who inherit directly the multiple equalities just having built.
9245
The on expression used in an outer join operation inherits all equalities
9246
from the on expression of the embedding join, if there is any, or
9247
otherwise - from the where condition.
9248
This fact is not obvious, but presumably can be proved.
9249
Consider the following query:
9251
SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t2.a=t4.a
9254
If the on expression in the query inherits =(t1.a,t2.a), then we
9255
can build the multiple equality =(t1.a,t2.a,t3.a,t4.a) that infers
9256
the equality t3.a=t4.a. Although the on expression
9257
t1.a=t3.a AND t2.a=t4.a AND t3.a=t4.a is not equivalent to the one
9258
in the query the latter can be replaced by the former: the new query
9259
will return the same result set as the original one.
9261
Interesting that multiple equality =(t1.a,t2.a,t3.a,t4.a) allows us
9262
to use t1.a=t3.a AND t3.a=t4.a under the on condition:
9264
SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a
9267
This query equivalent to:
9269
SELECT * FROM (t1 LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a),t2
9272
Similarly the original query can be rewritten to the query:
9274
SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
9277
that is equivalent to:
9279
SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
9282
Thus, applying equalities from the where condition we basically
9283
can get more freedom in performing join operations.
9284
Althogh we don't use this property now, it probably makes sense to use
9286
@param thd Thread handler
9287
@param cond condition to build the multiple equalities for
9288
@param inherited path to all inherited multiple equality items
9289
@param join_list list of join tables to which the condition
9291
@param[out] cond_equal_ref pointer to the structure to place built
9295
pointer to the transformed condition containing multiple equalities
9298
static COND *build_equal_items(THD *thd, COND *cond,
9299
COND_EQUAL *inherited,
9300
List<TABLE_LIST> *join_list,
9301
COND_EQUAL **cond_equal_ref)
9303
COND_EQUAL *cond_equal= 0;
9307
cond= build_equal_items_for_cond(thd, cond, inherited);
9308
cond->update_used_tables();
9309
if (cond->type() == Item::COND_ITEM &&
9310
((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
9311
cond_equal= &((Item_cond_and*) cond)->cond_equal;
9312
else if (cond->type() == Item::FUNC_ITEM &&
9313
((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9315
cond_equal= new COND_EQUAL;
9316
cond_equal->current_level.push_back((Item_equal *) cond);
9321
cond_equal->upper_levels= inherited;
9322
inherited= cond_equal;
9324
*cond_equal_ref= cond_equal;
9329
List_iterator<TABLE_LIST> li(*join_list);
9331
while ((table= li++))
9335
List<TABLE_LIST> *nested_join_list= table->nested_join ?
9336
&table->nested_join->join_list : NULL;
9338
We can modify table->on_expr because its old value will
9339
be restored before re-execution of PS/SP.
9341
table->on_expr= build_equal_items(thd, table->on_expr, inherited,
9343
&table->cond_equal);
9353
Compare field items by table order in the execution plan.
9355
field1 considered as better than field2 if the table containing
9356
field1 is accessed earlier than the table containing field2.
9357
The function finds out what of two fields is better according
9360
@param field1 first field item to compare
9361
@param field2 second field item to compare
9362
@param table_join_idx index to tables determining table order
9365
1 if field1 is better than field2
9367
-1 if field2 is better than field1
9372
static int compare_fields_by_table_order(Item_field *field1,
9374
void *table_join_idx)
9378
if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9383
if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9390
JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
9391
cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
9392
return cmp < 0 ? -1 : (cmp ? 1 : 0);
9397
Generate minimal set of simple equalities equivalent to a multiple equality.
9399
The function retrieves the fields of the multiple equality item
9400
item_equal and for each field f:
9401
- if item_equal contains const it generates the equality f=const_item;
9402
- otherwise, if f is not the first field, generates the equality
9403
f=item_equal->get_first().
9404
All generated equality are added to the cond conjunction.
9406
@param cond condition to add the generated equality to
9407
@param upper_levels structure to access multiple equality of upper levels
9408
@param item_equal multiple equality to generate simple equality from
9411
Before generating an equality function checks that it has not
9412
been generated for multiple equalities of the upper levels.
9413
E.g. for the following where condition
9414
WHERE a=5 AND ((a=b AND b=c) OR c>4)
9415
the upper level AND condition will contain =(5,a),
9416
while the lower level AND condition will contain =(5,a,b,c).
9417
When splitting =(5,a,b,c) into a separate equality predicates
9418
we should omit 5=a, as we have it already in the upper level.
9419
The following where condition gives us a more complicated case:
9420
WHERE t1.a=t2.b AND t3.c=t4.d AND (t2.b=t3.c OR t4.e>5 ...) AND ...
9421
Given the tables are accessed in the order t1->t2->t3->t4 for
9422
the selected query execution plan the lower level multiple
9423
equality =(t1.a,t2.b,t3.c,t4.d) formally should be converted to
9424
t1.a=t2.b AND t1.a=t3.c AND t1.a=t4.d. But t1.a=t2.a will be
9425
generated for the upper level. Also t3.c=t4.d will be generated there.
9426
So only t1.a=t3.c should be left in the lower level.
9427
If cond is equal to 0, then not more then one equality is generated
9428
and a pointer to it is returned as the result of the function.
9431
- The condition with generated simple equalities or
9432
a pointer to the simple generated equality, if success.
9436
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
9437
Item_equal *item_equal)
9440
Item_func_eq *eq_item= 0;
9441
if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9442
return new Item_int((longlong) 0,1);
9443
Item *item_const= item_equal->get_const();
9444
Item_equal_iterator it(*item_equal);
9450
head= item_equal->get_first();
9453
Item_field *item_field;
9454
while ((item_field= it++))
9456
Item_equal *upper= item_field->find_item_equal(upper_levels);
9457
Item_field *item= item_field;
9460
if (item_const && upper->get_const())
9464
Item_equal_iterator li(*item_equal);
9465
while ((item= li++) != item_field)
9467
if (item->find_item_equal(upper_levels) == upper)
9472
if (item == item_field)
9475
eq_list.push_back(eq_item);
9476
eq_item= new Item_func_eq(item_field, head);
9479
eq_item->set_cmp_func();
9480
eq_item->quick_fix_field();
9484
if (!cond && !eq_list.head())
9487
return new Item_int((longlong) 1,1);
9492
eq_list.push_back(eq_item);
9494
cond= new Item_cond_and(eq_list);
9497
DBUG_ASSERT(cond->type() == Item::COND_ITEM);
9498
((Item_cond *) cond)->add_at_head(&eq_list);
9501
cond->quick_fix_field();
9502
cond->update_used_tables();
9509
Substitute every field reference in a condition by the best equal field
9510
and eliminate all multiple equality predicates.
9512
The function retrieves the cond condition and for each encountered
9513
multiple equality predicate it sorts the field references in it
9514
according to the order of tables specified by the table_join_idx
9515
parameter. Then it eliminates the multiple equality predicate it
9516
replacing it by the conjunction of simple equality predicates
9517
equating every field from the multiple equality to the first
9518
field in it, or to the constant, if there is any.
9519
After this the function retrieves all other conjuncted
9520
predicates substitute every field reference by the field reference
9521
to the first equal field or equal constant if there are any.
9522
@param cond condition to process
9523
@param cond_equal multiple equalities to take into consideration
9524
@param table_join_idx index to tables determining field preference
9527
At the first glance full sort of fields in multiple equality
9528
seems to be an overkill. Yet it's not the case due to possible
9529
new fields in multiple equality item of lower levels. We want
9530
the order in them to comply with the order of upper levels.
9533
The transformed condition
9536
static COND* substitute_for_best_equal_field(COND *cond,
9537
COND_EQUAL *cond_equal,
9538
void *table_join_idx)
9540
Item_equal *item_equal;
9542
if (cond->type() == Item::COND_ITEM)
9544
List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
9546
bool and_level= ((Item_cond*) cond)->functype() ==
9547
Item_func::COND_AND_FUNC;
9550
cond_equal= &((Item_cond_and *) cond)->cond_equal;
9551
cond_list->disjoin((List<Item> *) &cond_equal->current_level);
9553
List_iterator_fast<Item_equal> it(cond_equal->current_level);
9554
while ((item_equal= it++))
9556
item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9560
List_iterator<Item> li(*cond_list);
9562
while ((item= li++))
9564
Item *new_item =substitute_for_best_equal_field(item, cond_equal,
9567
This works OK with PS/SP re-execution as changes are made to
9568
the arguments of AND/OR items only
9570
if (new_item != item)
9571
li.replace(new_item);
9576
List_iterator_fast<Item_equal> it(cond_equal->current_level);
9577
while ((item_equal= it++))
9579
cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
9580
// This occurs when eliminate_item_equal() founds that cond is
9581
// always false and substitutes it with Item_int 0.
9582
// Due to this, value of item_equal will be 0, so just return it.
9583
if (cond->type() != Item::COND_ITEM)
9587
if (cond->type() == Item::COND_ITEM &&
9588
!((Item_cond*)cond)->argument_list()->elements)
9589
cond= new Item_int((int32)cond->val_bool());
9592
else if (cond->type() == Item::FUNC_ITEM &&
9593
((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9595
item_equal= (Item_equal *) cond;
9596
item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9597
if (cond_equal && cond_equal->current_level.head() == item_equal)
9599
return eliminate_item_equal(0, cond_equal, item_equal);
9602
cond->transform(&Item::replace_equal_field, 0);
9608
Check appearance of new constant items in multiple equalities
9609
of a condition after reading a constant table.
9611
The function retrieves the cond condition and for each encountered
9612
multiple equality checks whether new constants have appeared after
9613
reading the constant (single row) table tab. If so it adjusts
9614
the multiple equality appropriately.
9616
@param cond condition whose multiple equalities are to be checked
9617
@param table constant table that has been read
9620
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
9622
if (!(cond->used_tables() & tab->table->map))
9625
if (cond->type() == Item::COND_ITEM)
9627
List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
9628
List_iterator_fast<Item> li(*cond_list);
9630
while ((item= li++))
9631
update_const_equal_items(item, tab);
9633
else if (cond->type() == Item::FUNC_ITEM &&
9634
((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9636
Item_equal *item_equal= (Item_equal *) cond;
9637
bool contained_const= item_equal->get_const() != NULL;
9638
item_equal->update_const();
9639
if (!contained_const && item_equal->get_const())
9641
/* Update keys for range analysis */
9642
Item_equal_iterator it(*item_equal);
9643
Item_field *item_field;
9644
while ((item_field= it++))
9646
Field *field= item_field->field;
9647
JOIN_TAB *stat= field->table->reginfo.join_tab;
9648
key_map possible_keys= field->key_start;
9649
possible_keys.intersect(field->table->keys_in_use_for_query);
9650
stat[0].const_keys.merge(possible_keys);
9653
For each field in the multiple equality (for which we know that it
9654
is a constant) we have to find its corresponding key part, and set
9655
that key part in const_key_parts.
9657
if (!possible_keys.is_clear_all())
9659
TABLE *tab= field->table;
9661
for (use= stat->keyuse; use && use->table == tab; use++)
9662
if (possible_keys.is_set(use->key) &&
9663
tab->key_info[use->key].key_part[use->keypart].field ==
9665
tab->const_key_parts[use->key]|= use->keypart_map;
9674
change field = field to field = const for each found field = const in the
9679
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
9680
Item *and_father, Item *cond,
9681
Item *field, Item *value)
9683
if (cond->type() == Item::COND_ITEM)
9685
bool and_level= ((Item_cond*) cond)->functype() ==
9686
Item_func::COND_AND_FUNC;
9687
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
9690
change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
9694
if (cond->eq_cmp_result() == Item::COND_OK)
9695
return; // Not a boolean function
9697
Item_bool_func2 *func= (Item_bool_func2*) cond;
9698
Item **args= func->arguments();
9699
Item *left_item= args[0];
9700
Item *right_item= args[1];
9701
Item_func::Functype functype= func->functype();
9703
if (right_item->eq(field,0) && left_item != value &&
9704
right_item->cmp_context == field->cmp_context &&
9705
(left_item->result_type() != STRING_RESULT ||
9706
value->result_type() != STRING_RESULT ||
9707
left_item->collation.collation == value->collation.collation))
9709
Item *tmp=value->clone_item();
9710
tmp->collation.set(right_item->collation);
9714
thd->change_item_tree(args + 1, tmp);
9715
func->update_used_tables();
9716
if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9717
&& and_father != cond && !left_item->const_item())
9721
if ((tmp2=new COND_CMP(and_father,func)))
9722
save_list->push_back(tmp2);
9724
func->set_cmp_func();
9727
else if (left_item->eq(field,0) && right_item != value &&
9728
left_item->cmp_context == field->cmp_context &&
9729
(right_item->result_type() != STRING_RESULT ||
9730
value->result_type() != STRING_RESULT ||
9731
right_item->collation.collation == value->collation.collation))
9733
Item *tmp= value->clone_item();
9734
tmp->collation.set(left_item->collation);
9738
thd->change_item_tree(args, tmp);
9740
func->update_used_tables();
9741
if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9742
&& and_father != cond && !right_item->const_item())
9744
args[0]= args[1]; // For easy check
9745
thd->change_item_tree(args + 1, value);
9748
if ((tmp2=new COND_CMP(and_father,func)))
9749
save_list->push_back(tmp2);
9751
func->set_cmp_func();
9757
Remove additional condition inserted by IN/ALL/ANY transformation.
9759
@param conds condition for processing
9765
static Item *remove_additional_cond(Item* conds)
9767
if (conds->name == in_additional_cond)
9769
if (conds->type() == Item::COND_ITEM)
9771
Item_cond *cnd= (Item_cond*) conds;
9772
List_iterator<Item> li(*(cnd->argument_list()));
9774
while ((item= li++))
9776
if (item->name == in_additional_cond)
9779
if (cnd->argument_list()->elements == 1)
9780
return cnd->argument_list()->head();
9789
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
9790
COND *and_father, COND *cond)
9792
if (cond->type() == Item::COND_ITEM)
9794
bool and_level= ((Item_cond*) cond)->functype() ==
9795
Item_func::COND_AND_FUNC;
9796
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
9798
I_List<COND_CMP> save;
9801
propagate_cond_constants(thd, &save,and_level ? cond : item, item);
9804
{ // Handle other found items
9805
I_List_iterator<COND_CMP> cond_itr(save);
9807
while ((cond_cmp=cond_itr++))
9809
Item **args= cond_cmp->cmp_func->arguments();
9810
if (!args[0]->const_item())
9811
change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
9812
cond_cmp->and_level, args[0], args[1]);
9816
else if (and_father != cond && !cond->marker) // In a AND group
9818
if (cond->type() == Item::FUNC_ITEM &&
9819
(((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
9820
((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
9822
Item_func_eq *func=(Item_func_eq*) cond;
9823
Item **args= func->arguments();
9824
bool left_const= args[0]->const_item();
9825
bool right_const= args[1]->const_item();
9826
if (!(left_const && right_const) &&
9827
args[0]->result_type() == args[1]->result_type())
9831
resolve_const_item(thd, &args[1], args[0]);
9832
func->update_used_tables();
9833
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9836
else if (left_const)
9838
resolve_const_item(thd, &args[0], args[1]);
9839
func->update_used_tables();
9840
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9850
Simplify joins replacing outer joins by inner joins whenever it's
9853
The function, during a retrieval of join_list, eliminates those
9854
outer joins that can be converted into inner join, possibly nested.
9855
It also moves the on expressions for the converted outer joins
9856
and from inner joins to conds.
9857
The function also calculates some attributes for nested joins:
9861
- on_expr_dep_tables
9862
The first two attributes are used to test whether an outer join can
9863
be substituted for an inner join. The third attribute represents the
9864
relation 'to be dependent on' for tables. If table t2 is dependent
9865
on table t1, then in any evaluated execution plan table access to
9866
table t2 must precede access to table t2. This relation is used also
9867
to check whether the query contains invalid cross-references.
9868
The forth attribute is an auxiliary one and is used to calculate
9870
As the attribute dep_tables qualifies possibles orders of tables in the
9871
execution plan, the dependencies required by the straight join
9872
modifiers are reflected in this attribute as well.
9873
The function also removes all braces that can be removed from the join
9874
expression without changing its meaning.
9877
An outer join can be replaced by an inner join if the where condition
9878
or the on expression for an embedding nested join contains a conjunctive
9879
predicate rejecting null values for some attribute of the inner tables.
9883
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9885
the predicate t2.b < 5 rejects nulls.
9886
The query is converted first to:
9888
SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9890
then to the equivalent form:
9892
SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9896
Similarly the following query:
9898
SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9903
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
9907
One conversion might trigger another:
9909
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9910
LEFT JOIN t3 ON t3.b=t2.b
9911
WHERE t3 IS NOT NULL =>
9912
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9913
WHERE t3 IS NOT NULL AND t3.b=t2.b =>
9914
SELECT * FROM t1, t2, t3
9915
WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9918
The function removes all unnecessary braces from the expression
9919
produced by the conversions.
9922
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9924
finally is converted to:
9926
SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9931
It also will remove braces from the following queries:
9933
SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9934
SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9937
The benefit of this simplification procedure is that it might return
9938
a query for which the optimizer can evaluate execution plan with more
9939
join orders. With a left join operation the optimizer does not
9940
consider any plan where one of the inner tables is before some of outer
9944
The function is implemented by a recursive procedure. On the recursive
9945
ascent all attributes are calculated, all outer joins that can be
9946
converted are replaced and then all unnecessary braces are removed.
9947
As join list contains join tables in the reverse order sequential
9948
elimination of outer joins does not require extra recursive calls.
9951
Remove all semi-joins that have are within another semi-join (i.e. have
9952
an "ancestor" semi-join nest)
9955
Here is an example of a join query with invalid cross references:
9957
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
9960
@param join reference to the query info
9961
@param join_list list representation of the join to be converted
9962
@param conds conditions to add on expressions for converted joins
9963
@param top true <=> conds is the where condition
9966
- The new condition, if success
9971
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9975
NESTED_JOIN *nested_join;
9976
TABLE_LIST *prev_table= 0;
9977
List_iterator<TABLE_LIST> li(*join_list);
9978
DBUG_ENTER("simplify_joins");
9981
Try to simplify join operations from join_list.
9982
The most outer join operation is checked for conversion first.
9984
while ((table= li++))
9986
table_map used_tables;
9987
table_map not_null_tables= (table_map) 0;
9989
if ((nested_join= table->nested_join))
9992
If the element of join_list is a nested join apply
9993
the procedure to its nested join list first.
9997
Item *expr= table->on_expr;
9999
If an on expression E is attached to the table,
10000
check all null rejected predicates in this expression.
10001
If such a predicate over an attribute belonging to
10002
an inner table of an embedded outer join is found,
10003
the outer join is converted to an inner join and
10004
the corresponding on expression is added to E.
10006
expr= simplify_joins(join, &nested_join->join_list,
10007
expr, FALSE, in_sj || table->sj_on_expr);
10009
if (!table->prep_on_expr || expr != table->on_expr)
10013
table->on_expr= expr;
10014
table->prep_on_expr= expr->copy_andor_structure(join->thd);
10017
nested_join->used_tables= (table_map) 0;
10018
nested_join->not_null_tables=(table_map) 0;
10019
conds= simplify_joins(join, &nested_join->join_list, conds, top,
10020
in_sj || table->sj_on_expr);
10021
used_tables= nested_join->used_tables;
10022
not_null_tables= nested_join->not_null_tables;
10026
if (!table->prep_on_expr)
10027
table->prep_on_expr= table->on_expr;
10028
used_tables= table->table->map;
10030
not_null_tables= conds->not_null_tables();
10033
if (table->embedding)
10035
table->embedding->nested_join->used_tables|= used_tables;
10036
table->embedding->nested_join->not_null_tables|= not_null_tables;
10039
if (!table->outer_join || (used_tables & not_null_tables))
10042
For some of the inner tables there are conjunctive predicates
10043
that reject nulls => the outer join can be replaced by an inner join.
10045
table->outer_join= 0;
10046
if (table->on_expr)
10048
/* Add ON expression to the WHERE or upper-level ON condition. */
10051
conds= and_conds(conds, table->on_expr);
10052
conds->top_level_item();
10053
/* conds is always a new item as both cond and on_expr existed */
10054
DBUG_ASSERT(!conds->fixed);
10055
conds->fix_fields(join->thd, &conds);
10058
conds= table->on_expr;
10059
table->prep_on_expr= table->on_expr= 0;
10067
Only inner tables of non-convertible outer joins
10068
remain with on_expr.
10070
if (table->on_expr)
10072
table->dep_tables|= table->on_expr->used_tables();
10073
if (table->embedding)
10075
table->dep_tables&= ~table->embedding->nested_join->used_tables;
10077
Embedding table depends on tables used
10078
in embedded on expressions.
10080
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
10083
table->dep_tables&= ~table->table->map;
10088
/* The order of tables is reverse: prev_table follows table */
10089
if (prev_table->straight)
10090
prev_table->dep_tables|= used_tables;
10091
if (prev_table->on_expr)
10093
prev_table->dep_tables|= table->on_expr_dep_tables;
10094
table_map prev_used_tables= prev_table->nested_join ?
10095
prev_table->nested_join->used_tables :
10096
prev_table->table->map;
10098
If on expression contains only references to inner tables
10099
we still make the inner tables dependent on the outer tables.
10100
It would be enough to set dependency only on one outer table
10101
for them. Yet this is really a rare case.
10103
if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
10104
prev_table->dep_tables|= used_tables;
10111
Flatten nested joins that can be flattened.
10112
no ON expression and not a semi-join => can be flattened.
10115
while ((table= li++))
10117
nested_join= table->nested_join;
10118
if (table->sj_on_expr && !in_sj)
10121
If this is a semi-join that is not contained within another semi-join,
10122
leave it intact (otherwise it is flattened)
10124
join->select_lex->sj_nests.push_back(table);
10126
else if (nested_join && !table->on_expr)
10129
List_iterator<TABLE_LIST> it(nested_join->join_list);
10130
while ((tbl= it++))
10132
tbl->embedding= table->embedding;
10133
tbl->join_list= table->join_list;
10135
li.replace(nested_join->join_list);
10138
DBUG_RETURN(conds);
10143
Assign each nested join structure a bit in nested_join_map.
10145
Assign each nested join structure (except "confluent" ones - those that
10146
embed only one element) a bit in nested_join_map.
10148
@param join Join being processed
10149
@param join_list List of tables
10150
@param first_unused Number of first unused bit in nested_join_map before the
10154
This function is called after simplify_joins(), when there are no
10155
redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
10156
we will not run out of bits in nested_join_map.
10159
First unused bit in nested_join_map after the call.
10162
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
10165
List_iterator<TABLE_LIST> li(*join_list);
10167
DBUG_ENTER("build_bitmap_for_nested_joins");
10168
while ((table= li++))
10170
NESTED_JOIN *nested_join;
10171
if ((nested_join= table->nested_join))
10174
It is guaranteed by simplify_joins() function that a nested join
10175
that has only one child is either
10176
- a single-table view (the child is the underlying table), or
10177
- a single-table semi-join nest
10179
We don't assign bits to such sj-nests because
10180
1. it is redundant (a "sequence" of one table cannot be interleaved
10182
2. we could run out bits in nested_join_map otherwise.
10184
if (nested_join->join_list.elements != 1)
10186
/* Don't assign bits to sj-nests */
10187
if (table->on_expr)
10188
nested_join->nj_map= (nested_join_map) 1 << first_unused++;
10189
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
10194
DBUG_RETURN(first_unused);
10199
Set NESTED_JOIN::counter=0 in all nested joins in passed list.
10201
Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
10202
the passed join_list.
10204
@param join_list List of nested joins to process. It may also contain base
10205
tables which will be ignored.
10208
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10210
List_iterator<TABLE_LIST> li(*join_list);
10212
DBUG_ENTER("reset_nj_counters");
10213
while ((table= li++))
10215
NESTED_JOIN *nested_join;
10216
if ((nested_join= table->nested_join))
10218
nested_join->counter_= 0;
10219
reset_nj_counters(&nested_join->join_list);
10227
Check interleaving with an inner tables of an outer join for
10230
Check if table next_tab can be added to current partial join order, and
10231
if yes, record that it has been added.
10233
The function assumes that both current partial join order and its
10234
extension with next_tab are valid wrt table dependencies.
10238
LIMITATIONS ON JOIN ORDER
10239
The nested [outer] joins executioner algorithm imposes these limitations
10241
1. "Outer tables first" - any "outer" table must be before any
10242
corresponding "inner" table.
10243
2. "No interleaving" - tables inside a nested join must form a continuous
10244
sequence in join order (i.e. the sequence must not be interrupted by
10245
tables that are outside of this nested join).
10247
#1 is checked elsewhere, this function checks #2 provided that #1 has
10248
been already checked.
10250
WHY NEED NON-INTERLEAVING
10251
Consider an example:
10253
select * from t0 join t1 left join (t2 join t3) on cond1
10255
The join order "t1 t2 t0 t3" is invalid:
10257
table t0 is outside of the nested join, so WHERE condition for t0 is
10258
attached directly to t0 (without triggers, and it may be used to access
10259
t0). Applying WHERE(t0) to (t2,t0,t3) record is invalid as we may miss
10260
combinations of (t1, t2, t3) that satisfy condition cond1, and produce a
10261
null-complemented (t1, t2.NULLs, t3.NULLs) row, which should not have
10264
If table t0 is not between t2 and t3, the problem doesn't exist:
10265
If t0 is located after (t2,t3), WHERE(t0) is applied after nested join
10266
processing has finished.
10267
If t0 is located before (t2,t3), predicates like WHERE_cond(t0, t2) are
10268
wrapped into condition triggers, which takes care of correct nested
10271
HOW IT IS IMPLEMENTED
10272
The limitations on join order can be rephrased as follows: for valid
10273
join order one must be able to:
10274
1. write down the used tables in the join order on one line.
10275
2. for each nested join, put one '(' and one ')' on the said line
10276
3. write "LEFT JOIN" and "ON (...)" where appropriate
10277
4. get a query equivalent to the query we're trying to execute.
10279
Calls to check_interleaving_with_nj() are equivalent to writing the
10280
above described line from left to right.
10281
A single check_interleaving_with_nj(A,B) call is equivalent to writing
10282
table B and appropriate brackets on condition that table A and
10283
appropriate brackets is the last what was written. Graphically the
10284
transition is as follows:
10286
+---- current position
10288
... last_tab ))) | ( next_tab ) )..) | ...
10290
+- need to move to this
10293
Notes about the position:
10294
The caller guarantees that there is no more then one X-bracket by
10295
checking "!(remaining_tables & s->dependent)" before calling this
10296
function. X-bracket may have a pair in Y-bracket.
10298
When "writing" we store/update this auxilary info about the current
10300
1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10301
joins) we've opened but didn't close.
10302
2. {each NESTED_JOIN structure not simplified away}->counter - number
10303
of this nested join's children that have already been added to to
10304
the partial join order.
10307
@param join Join being processed
10308
@param last_tab Last table in current partial join order (this function is
10309
not called for empty partial join orders)
10310
@param next_tab Table we're going to extend the current partial join with
10313
FALSE Join order extended, nested joins info about current join
10314
order (see NOTE section) updated.
10316
TRUE Requested join order extension not allowed.
10319
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
10321
TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
10322
JOIN *join= last_tab->join;
10324
if (join->cur_embedding_map & ~next_tab->embedding_map)
10327
next_tab is outside of the "pair of brackets" we're currently in.
10334
Do update counters for "pairs of brackets" that we've left (marked as
10335
X,Y,Z in the above picture)
10337
for (;next_emb; next_emb= next_emb->embedding)
10339
next_emb->nested_join->counter_++;
10340
if (next_emb->nested_join->counter_ == 1)
10343
next_emb is the first table inside a nested join we've "entered". In
10344
the picture above, we're looking at the 'X' bracket. Don't exit yet as
10345
X bracket might have Y pair bracket.
10347
join->cur_embedding_map |= next_emb->nested_join->nj_map;
10350
if (next_emb->nested_join->join_list.elements !=
10351
next_emb->nested_join->counter_)
10355
We're currently at Y or Z-bracket as depicted in the above picture.
10356
Mark that we've left it and continue walking up the brackets hierarchy.
10358
join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
10365
Nested joins perspective: Remove the last table from the join order.
10367
Remove the last table from the partial join order and update the nested
10368
joins counters and join->cur_embedding_map. It is ok to call this
10369
function for the first table in join order (for which
10370
check_interleaving_with_nj has not been called)
10372
@param last join table to remove, it is assumed to be the last in current
10373
partial join order.
10376
static void restore_prev_nj_state(JOIN_TAB *last)
10378
TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
10379
JOIN *join= last->join;
10382
if (last_emb->on_expr)
10384
if (!(--last_emb->nested_join->counter_))
10385
join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
10386
else if (last_emb->nested_join->join_list.elements-1 ==
10387
last_emb->nested_join->counter_)
10388
join->cur_embedding_map|= last_emb->nested_join->nj_map;
10392
last_emb= last_emb->embedding;
10399
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10401
TABLE_LIST *emb_sj_nest;
10402
if ((emb_sj_nest= tab->emb_sj_nest))
10404
tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10405
/* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
10406
if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
10407
tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10413
we assume remaining_tables doesnt contain @tab.
10416
static void restore_prev_sj_state(const table_map remaining_tables,
10417
const JOIN_TAB *tab)
10419
TABLE_LIST *emb_sj_nest;
10420
if ((emb_sj_nest= tab->emb_sj_nest))
10422
/* If we're removing the last SJ-inner table, remove the sj-nest */
10423
if ((remaining_tables & emb_sj_nest->sj_inner_tables) ==
10424
(emb_sj_nest->sj_inner_tables & ~tab->table->map))
10426
tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10433
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
10434
Item::cond_result *cond_value)
10436
THD *thd= join->thd;
10437
DBUG_ENTER("optimize_cond");
10440
*cond_value= Item::COND_TRUE;
10444
Build all multiple equality predicates and eliminate equality
10445
predicates that can be inferred from these multiple equalities.
10446
For each reference of a field included into a multiple equality
10447
that occurs in a function set a pointer to the multiple equality
10448
predicate. Substitute a constant instead of this field if the
10449
multiple equality contains a constant.
10451
DBUG_EXECUTE("where", print_where(conds, "original", QT_ORDINARY););
10452
conds= build_equal_items(join->thd, conds, NULL, join_list,
10453
&join->cond_equal);
10454
DBUG_EXECUTE("where",print_where(conds,"after equal_items", QT_ORDINARY););
10456
/* change field = field to field = const for each found field = const */
10457
propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
10459
Remove all instances of item == item
10460
Remove all and-levels where CONST item != CONST item
10462
DBUG_EXECUTE("where",print_where(conds,"after const change", QT_ORDINARY););
10463
conds= remove_eq_conds(thd, conds, cond_value) ;
10464
DBUG_EXECUTE("info",print_where(conds,"after remove", QT_ORDINARY););
10466
DBUG_RETURN(conds);
10471
Remove const and eq items.
10474
Return new item, or NULL if no condition @n
10475
cond_value is set to according:
10476
- COND_OK : query is possible (field = constant)
10477
- COND_TRUE : always true ( 1 = 1 )
10478
- COND_FALSE : always false ( 1 = 2 )
10482
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
10484
if (cond->type() == Item::COND_ITEM)
10486
bool and_level= ((Item_cond*) cond)->functype()
10487
== Item_func::COND_AND_FUNC;
10488
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
10489
Item::cond_result tmp_cond_value;
10490
bool should_fix_fields=0;
10492
*cond_value=Item::COND_UNDEF;
10494
while ((item=li++))
10496
Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
10499
else if (item != new_item)
10501
VOID(li.replace(new_item));
10502
should_fix_fields=1;
10504
if (*cond_value == Item::COND_UNDEF)
10505
*cond_value=tmp_cond_value;
10506
switch (tmp_cond_value) {
10507
case Item::COND_OK: // Not TRUE or FALSE
10508
if (and_level || *cond_value == Item::COND_FALSE)
10509
*cond_value=tmp_cond_value;
10511
case Item::COND_FALSE:
10514
*cond_value=tmp_cond_value;
10515
return (COND*) 0; // Always false
10518
case Item::COND_TRUE:
10521
*cond_value= tmp_cond_value;
10522
return (COND*) 0; // Always true
10525
case Item::COND_UNDEF: // Impossible
10526
break; /* purecov: deadcode */
10529
if (should_fix_fields)
10530
cond->update_used_tables();
10532
if (!((Item_cond*) cond)->argument_list()->elements ||
10533
*cond_value != Item::COND_OK)
10535
if (((Item_cond*) cond)->argument_list()->elements == 1)
10537
item= ((Item_cond*) cond)->argument_list()->head();
10538
((Item_cond*) cond)->argument_list()->empty();
10542
else if (cond->type() == Item::FUNC_ITEM &&
10543
((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
10546
Handles this special case for some ODBC applications:
10547
The are requesting the row that was just updated with a auto_increment
10548
value with this construct:
10550
SELECT * from table_name where auto_increment_column IS NULL
10551
This will be changed to:
10552
SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
10555
Item_func_isnull *func=(Item_func_isnull*) cond;
10556
Item **args= func->arguments();
10557
if (args[0]->type() == Item::FIELD_ITEM)
10559
Field *field=((Item_field*) args[0])->field;
10560
if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
10561
(thd->options & OPTION_AUTO_IS_NULL) &&
10562
(thd->first_successful_insert_id_in_prev_stmt > 0 &&
10563
thd->substitute_null_with_insert_id))
10566
if ((new_cond= new Item_func_eq(args[0],
10567
new Item_int("last_insert_id()",
10568
thd->read_first_successful_insert_id_in_prev_stmt(),
10569
MY_INT64_NUM_DECIMAL_DIGITS))))
10573
Item_func_eq can't be fixed after creation so we do not check
10574
cond->fixed, also it do not need tables so we use 0 as second
10577
cond->fix_fields(thd, &cond);
10580
IS NULL should be mapped to LAST_INSERT_ID only for first row, so
10583
thd->substitute_null_with_insert_id= FALSE;
10585
/* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10586
else if (((field->type() == MYSQL_TYPE_DATE) ||
10587
(field->type() == MYSQL_TYPE_DATETIME)) &&
10588
(field->flags & NOT_NULL_FLAG) &&
10589
!field->table->maybe_null)
10592
if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
10596
Item_func_eq can't be fixed after creation so we do not check
10597
cond->fixed, also it do not need tables so we use 0 as second
10600
cond->fix_fields(thd, &cond);
10604
if (cond->const_item())
10606
*cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10610
else if (cond->const_item() && !cond->is_expensive())
10613
Excluding all expensive functions is too restritive we should exclude only
10614
materialized IN because it is created later than this phase, and cannot be
10615
evaluated at this point.
10616
The condition should be something as (need to fix member access):
10617
!(cond->type() == Item::FUNC_ITEM &&
10618
((Item_func*)cond)->func_name() == "<in_optimizer>" &&
10619
((Item_in_optimizer*)cond)->is_expensive()))
10622
*cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10625
else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
10626
{ // boolan compare function
10627
Item *left_item= ((Item_func*) cond)->arguments()[0];
10628
Item *right_item= ((Item_func*) cond)->arguments()[1];
10629
if (left_item->eq(right_item,1))
10631
if (!left_item->maybe_null ||
10632
((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
10633
return (COND*) 0; // Compare of identical items
10636
*cond_value=Item::COND_OK;
10637
return cond; // Point at next and level
10641
Check if equality can be used in removing components of GROUP BY/DISTINCT
10644
test_if_equality_guarantees_uniqueness()
10645
l the left comparison argument (a field if any)
10646
r the right comparison argument (a const of any)
10649
Checks if an equality predicate can be used to take away
10650
DISTINCT/GROUP BY because it is known to be true for exactly one
10651
distinct value (e.g. <expr> == <const>).
10652
Arguments must be of the same type because e.g.
10653
<string_field> = <int_const> may match more than 1 distinct value from
10655
We must take into consideration and the optimization done for various
10656
string constants when compared to dates etc (see Item_int_with_ref) as
10657
well as the collation of the arguments.
10661
FALSE cannot be used
10664
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
10666
return r->const_item() &&
10667
/* elements must be compared as dates */
10668
(Arg_comparator::can_compare_as_dates(l, r, 0) ||
10669
/* or of the same result type */
10670
(r->result_type() == l->result_type() &&
10671
/* and must have the same collation if compared as strings */
10672
(l->result_type() != STRING_RESULT ||
10673
l->collation.collation == r->collation.collation)));
10677
Return TRUE if the item is a const value in all the WHERE clause.
10681
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
10683
if (cond->type() == Item::COND_ITEM)
10685
bool and_level= (((Item_cond*) cond)->functype()
10686
== Item_func::COND_AND_FUNC);
10687
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
10689
while ((item=li++))
10691
bool res=const_expression_in_where(item, comp_item, const_item);
10692
if (res) // Is a const value
10697
else if (!and_level)
10700
return and_level ? 0 : 1;
10702
else if (cond->eq_cmp_result() != Item::COND_OK)
10703
{ // boolan compare function
10704
Item_func* func= (Item_func*) cond;
10705
if (func->functype() != Item_func::EQUAL_FUNC &&
10706
func->functype() != Item_func::EQ_FUNC)
10708
Item *left_item= ((Item_func*) cond)->arguments()[0];
10709
Item *right_item= ((Item_func*) cond)->arguments()[1];
10710
if (left_item->eq(comp_item,1))
10712
if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10715
return right_item->eq(*const_item, 1);
10716
*const_item=right_item;
10720
else if (right_item->eq(comp_item,1))
10722
if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10725
return left_item->eq(*const_item, 1);
10726
*const_item=left_item;
10734
/****************************************************************************
10735
Create internal temporary table
10736
****************************************************************************/
10739
Create field for temporary table from given field.
10741
@param thd Thread handler
10742
@param org_field field from which new field will be created
10743
@param name New field name
10744
@param table Temporary table
10745
@param item !=NULL if item->result_field should point to new field.
10746
This is relevant for how fill_record() is going to work:
10747
If item != NULL then fill_record() will update
10748
the record in the original table.
10749
If item == NULL then fill_record() will update
10750
the temporary table
10751
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10752
field instead of blob.
10760
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10761
const char *name, TABLE *table,
10762
Item_field *item, uint convert_blob_length)
10767
Make sure that the blob fits into a Field_varstring which has
10770
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10771
(org_field->flags & BLOB_FLAG))
10772
new_field= new Field_varstring(convert_blob_length,
10773
org_field->maybe_null(),
10774
org_field->field_name, table->s,
10775
org_field->charset());
10777
new_field= org_field->new_field(thd->mem_root, table,
10778
table == org_field->table);
10781
new_field->init(table);
10782
new_field->orig_table= org_field->orig_table;
10784
item->result_field= new_field;
10786
new_field->field_name= name;
10787
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10788
if (org_field->maybe_null() || (item && item->maybe_null))
10789
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
10790
if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
10791
org_field->type() == MYSQL_TYPE_VARCHAR)
10792
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10793
else if (org_field->type() == FIELD_TYPE_DOUBLE)
10794
((Field_double *) new_field)->not_fixed= TRUE;
10800
Create field for temporary table using type of given item.
10802
@param thd Thread handler
10803
@param item Item to create a field for
10804
@param table Temporary table
10805
@param copy_func If set and item is a function, store copy of
10807
@param modify_item 1 if item->result_field should point to new
10808
item. This is relevent for how fill_record()
10810
If modify_item is 1 then fill_record() will
10811
update the record in the original table.
10812
If modify_item is 0 then fill_record() will
10813
update the temporary table
10814
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10815
field instead of blob.
10823
static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table,
10824
Item ***copy_func, bool modify_item,
10825
uint convert_blob_length)
10827
bool maybe_null= item->maybe_null;
10830
switch (item->result_type()) {
10832
new_field= new Field_double(item->max_length, maybe_null,
10833
item->name, item->decimals, TRUE);
10837
Select an integer type with the minimal fit precision.
10838
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10839
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
10840
Field_long : make them Field_longlong.
10842
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10843
new_field=new Field_longlong(item->max_length, maybe_null,
10844
item->name, item->unsigned_flag);
10846
new_field=new Field_long(item->max_length, maybe_null,
10847
item->name, item->unsigned_flag);
10849
case STRING_RESULT:
10850
DBUG_ASSERT(item->collation.collation);
10852
enum enum_field_types type;
10854
DATE/TIME fields have STRING_RESULT result type.
10855
To preserve type they needed to be handled separately.
10857
if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
10858
type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_DATE ||
10859
type == MYSQL_TYPE_TIMESTAMP)
10860
new_field= item->tmp_table_field_from_field_type(table, 1);
10862
Make sure that the blob fits into a Field_varstring which has
10865
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10866
convert_blob_length <= Field_varstring::MAX_SIZE &&
10867
convert_blob_length)
10868
new_field= new Field_varstring(convert_blob_length, maybe_null,
10869
item->name, table->s,
10870
item->collation.collation);
10872
new_field= item->make_string_field(table);
10873
new_field->set_derivation(item->collation.derivation);
10875
case DECIMAL_RESULT:
10877
uint8 dec= item->decimals;
10878
uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
10879
uint32 len= item->max_length;
10882
Trying to put too many digits overall in a DECIMAL(prec,dec)
10883
will always throw a warning. We must limit dec to
10884
DECIMAL_MAX_SCALE however to prevent an assert() later.
10889
signed int overflow;
10891
dec= min(dec, DECIMAL_MAX_SCALE);
10894
If the value still overflows the field with the corrected dec,
10895
we'll throw out decimals rather than integers. This is still
10896
bad and of course throws a truncation warning.
10897
+1: for decimal point
10900
overflow= my_decimal_precision_to_length(intg + dec, dec,
10901
item->unsigned_flag) - len;
10904
dec= max(0, dec - overflow); // too long, discard fract
10906
len -= item->decimals - dec; // corrected value fits
10909
new_field= new Field_new_decimal(len, maybe_null, item->name,
10910
dec, item->unsigned_flag);
10915
// This case should never be choosen
10921
new_field->init(table);
10923
if (copy_func && item->is_result_field())
10924
*((*copy_func)++) = item; // Save for copy_funcs
10926
item->set_result_field(new_field);
10927
if (item->type() == Item::NULL_ITEM)
10928
new_field->is_created_from_null_item= TRUE;
10934
Create field for information schema table.
10936
@param thd Thread handler
10937
@param table Temporary table
10938
@param item Item to create a field for
10946
Field *create_tmp_field_for_schema(THD *thd, Item *item, TABLE *table)
10948
if (item->field_type() == MYSQL_TYPE_VARCHAR)
10951
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10952
field= new Field_blob(item->max_length, item->maybe_null,
10953
item->name, item->collation.collation);
10955
field= new Field_varstring(item->max_length, item->maybe_null,
10957
table->s, item->collation.collation);
10959
field->init(table);
10962
return item->tmp_table_field_from_field_type(table, 0);
10967
Create field for temporary table.
10969
@param thd Thread handler
10970
@param table Temporary table
10971
@param item Item to create a field for
10972
@param type Type of item (normally item->type)
10973
@param copy_func If set and item is a function, store copy of item
10975
@param from_field if field will be created using other field as example,
10976
pointer example field will be written here
10977
@param default_field If field has a default value field, store it here
10978
@param group 1 if we are going to do a relative group by on result
10979
@param modify_item 1 if item->result_field should point to new item.
10980
This is relevent for how fill_record() is going to
10982
If modify_item is 1 then fill_record() will update
10983
the record in the original table.
10984
If modify_item is 0 then fill_record() will update
10985
the temporary table
10986
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10987
field instead of blob.
10995
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10996
Item ***copy_func, Field **from_field,
10997
Field **default_field,
10998
bool group, bool modify_item,
10999
bool table_cant_handle_bit_fields,
11000
bool make_copy_field,
11001
uint convert_blob_length)
11004
Item::Type orig_type= type;
11005
Item *orig_item= 0;
11007
if (type != Item::FIELD_ITEM &&
11008
item->real_item()->type() == Item::FIELD_ITEM)
11011
item= item->real_item();
11012
type= Item::FIELD_ITEM;
11016
case Item::SUM_FUNC_ITEM:
11018
Item_sum *item_sum=(Item_sum*) item;
11019
result= item_sum->create_tmp_field(group, table, convert_blob_length);
11021
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
11024
case Item::FIELD_ITEM:
11025
case Item::DEFAULT_VALUE_ITEM:
11027
Item_field *field= (Item_field*) item;
11028
bool orig_modify= modify_item;
11029
if (orig_type == Item::REF_ITEM)
11032
If item have to be able to store NULLs but underlaid field can't do it,
11033
create_tmp_field_from_field() can't be used for tmp field creation.
11035
if (field->maybe_null && !field->field->maybe_null())
11037
result= create_tmp_field_from_item(thd, item, table, NULL,
11038
modify_item, convert_blob_length);
11039
*from_field= field->field;
11040
if (result && modify_item)
11041
field->result_field= result;
11043
else if (table_cant_handle_bit_fields && field->field->type() ==
11046
*from_field= field->field;
11047
result= create_tmp_field_from_item(thd, item, table, copy_func,
11048
modify_item, convert_blob_length);
11049
if (result && modify_item)
11050
field->result_field= result;
11053
result= create_tmp_field_from_field(thd, (*from_field= field->field),
11054
orig_item ? orig_item->name :
11057
modify_item ? field :
11059
convert_blob_length);
11060
if (orig_type == Item::REF_ITEM && orig_modify)
11061
((Item_ref*)orig_item)->set_result_field(result);
11062
if (field->field->eq_def(result))
11063
*default_field= field->field;
11067
case Item::FUNC_ITEM:
11069
case Item::COND_ITEM:
11070
case Item::FIELD_AVG_ITEM:
11071
case Item::FIELD_STD_ITEM:
11072
case Item::SUBSELECT_ITEM:
11073
/* The following can only happen with 'CREATE TABLE ... SELECT' */
11074
case Item::PROC_ITEM:
11075
case Item::INT_ITEM:
11076
case Item::REAL_ITEM:
11077
case Item::DECIMAL_ITEM:
11078
case Item::STRING_ITEM:
11079
case Item::REF_ITEM:
11080
case Item::NULL_ITEM:
11081
case Item::VARBIN_ITEM:
11082
if (make_copy_field)
11084
DBUG_ASSERT(((Item_result_field*)item)->result_field);
11085
*from_field= ((Item_result_field*)item)->result_field;
11087
return create_tmp_field_from_item(thd, item, table,
11088
(make_copy_field ? 0 : copy_func),
11089
modify_item, convert_blob_length);
11090
case Item::TYPE_HOLDER:
11091
result= ((Item_type_holder *)item)->make_field_by_type(table);
11092
result->set_derivation(item->collation.derivation);
11094
default: // Dosen't have to be stored
11100
Set up column usage bitmaps for a temporary table
11103
For temporary tables, we need one bitmap with all columns set and
11104
a tmp_set bitmap to be used by things like filesort.
11107
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
11109
uint field_count= table->s->fields;
11110
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
11112
bitmap_init(&table->tmp_set,
11113
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
11114
field_count, FALSE);
11115
/* write_set and all_set are copies of read_set */
11116
table->def_write_set= table->def_read_set;
11117
table->s->all_set= table->def_read_set;
11118
bitmap_set_all(&table->s->all_set);
11119
table->default_column_bitmaps();
11124
Create a temp table according to a field list.
11126
Given field pointers are changed to point at tmp_table for
11127
send_fields. The table object is self contained: it's
11128
allocated in its own memory root, as well as Field objects
11129
created for table columns.
11130
This function will replace Item_sum items in 'fields' list with
11131
corresponding Item_field items, pointing at the fields in the
11132
temporary table, unless this was prohibited by TRUE
11133
value of argument save_sum_fields. The Item_field objects
11134
are created in THD memory root.
11136
@param thd thread handle
11137
@param param a description used as input to create the table
11138
@param fields list of items that will be used to define
11139
column types of the table (also see NOTES)
11140
@param group TODO document
11141
@param distinct should table rows be distinct
11142
@param save_sum_fields see NOTES
11143
@param select_options
11145
@param table_alias possible name of the temporary table that can
11146
be used for name resolving; can be "".
11149
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
11150
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
11151
#define RATIO_TO_PACK_ROWS 2
11152
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
11155
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
11156
ORDER *group, bool distinct, bool save_sum_fields,
11157
ulonglong select_options, ha_rows rows_limit,
11160
MEM_ROOT *mem_root_save, own_root;
11162
TABLE_SHARE *share;
11163
uint i,field_count,null_count,null_pack_length;
11164
uint copy_func_count= param->func_count;
11165
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
11166
uint blob_count,group_null_items, string_count;
11167
uint temp_pool_slot=MY_BIT_NONE;
11169
ulong reclength, string_total_length;
11170
bool using_unique_constraint= 0;
11171
bool use_packed_rows= 0;
11172
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
11173
char *tmpname,path[FN_REFLEN];
11174
uchar *pos, *group_buff, *bitmaps;
11176
Field **reg_field, **from_field, **default_field;
11178
Copy_field *copy=0;
11180
KEY_PART_INFO *key_part_info;
11182
MI_COLUMNDEF *recinfo;
11183
uint total_uneven_bit_length= 0;
11184
bool force_copy_fields= param->force_copy_fields;
11185
DBUG_ENTER("create_tmp_table");
11186
DBUG_PRINT("enter",
11187
("distinct: %d save_sum_fields: %d rows_limit: %lu group: %d",
11188
(int) distinct, (int) save_sum_fields,
11189
(ulong) rows_limit,test(group)));
11191
status_var_increment(thd->status_var.created_tmp_tables);
11193
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11194
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11196
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11197
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11198
current_pid, temp_pool_slot);
11201
/* if we run out of slots or we are not using tempool */
11202
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11203
thd->thread_id, thd->tmp_table++);
11207
No need to change table name to lower case as we are only creating
11208
MyISAM or HEAP tables here
11210
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11215
if (!param->quick_group)
11216
group=0; // Can't use group key
11217
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
11220
marker == 4 means two things:
11221
- store NULLs in the key, and
11222
- convert BIT fields to 64-bit long, needed because MEMORY tables
11223
can't index BIT fields.
11225
(*tmp->item)->marker= 4;
11226
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11227
using_unique_constraint=1;
11229
if (param->group_length >= MAX_BLOB_WIDTH)
11230
using_unique_constraint=1;
11232
distinct=0; // Can't use distinct
11235
field_count=param->field_count+param->func_count+param->sum_func_count;
11236
hidden_field_count=param->hidden_field_count;
11239
When loose index scan is employed as access method, it already
11240
computes all groups and the result of all aggregate functions. We
11241
make space for the items of the aggregate function in the list of
11242
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11243
these items are stored in the temporary table.
11245
if (param->precomputed_group_by)
11246
copy_func_count+= param->sum_func_count;
11248
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11250
if (!multi_alloc_root(&own_root,
11251
&table, sizeof(*table),
11252
&share, sizeof(*share),
11253
®_field, sizeof(Field*) * (field_count+1),
11254
&default_field, sizeof(Field*) * (field_count),
11255
&blob_field, sizeof(uint)*(field_count+1),
11256
&from_field, sizeof(Field*)*field_count,
11257
©_func, sizeof(*copy_func)*(copy_func_count+1),
11258
¶m->keyinfo, sizeof(*param->keyinfo),
11260
sizeof(*key_part_info)*(param->group_parts+1),
11261
¶m->start_recinfo,
11262
sizeof(*param->recinfo)*(field_count*2+4),
11263
&tmpname, (uint) strlen(path)+1,
11264
&group_buff, (group && ! using_unique_constraint ?
11265
param->group_length : 0),
11266
&bitmaps, bitmap_buffer_size(field_count)*2,
11269
if (temp_pool_slot != MY_BIT_NONE)
11270
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11271
DBUG_RETURN(NULL); /* purecov: inspected */
11273
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11274
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11276
if (temp_pool_slot != MY_BIT_NONE)
11277
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11278
free_root(&own_root, MYF(0)); /* purecov: inspected */
11279
DBUG_RETURN(NULL); /* purecov: inspected */
11281
param->items_to_copy= copy_func;
11282
strmov(tmpname,path);
11283
/* make table according to fields */
11285
bzero((char*) table,sizeof(*table));
11286
bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
11287
bzero((char*) default_field, sizeof(Field*) * (field_count));
11288
bzero((char*) from_field,sizeof(Field*)*field_count);
11290
table->mem_root= own_root;
11291
mem_root_save= thd->mem_root;
11292
thd->mem_root= &table->mem_root;
11294
table->field=reg_field;
11295
table->alias= table_alias;
11296
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11297
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11299
table->temp_pool_slot = temp_pool_slot;
11300
table->copy_blobs= 1;
11301
table->in_use= thd;
11302
table->quick_keys.init();
11303
table->covering_keys.init();
11304
table->keys_in_use_for_query.init();
11307
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11308
share->blob_field= blob_field;
11309
share->blob_ptr_size= portable_sizeof_char_ptr;
11310
share->db_low_byte_first=1; // True for HEAP and MyISAM
11311
share->table_charset= param->table_charset;
11312
share->primary_key= MAX_KEY; // Indicate no primary key
11313
share->keys_for_keyread.init();
11314
share->keys_in_use.init();
11316
/* Calculate which type of fields we will store in the temporary table */
11318
reclength= string_total_length= 0;
11319
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11320
param->using_indirect_summary_function=0;
11322
List_iterator_fast<Item> li(fields);
11324
Field **tmp_from_field=from_field;
11325
while ((item=li++))
11327
Item::Type type=item->type();
11328
if (not_all_columns)
11330
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11332
if (item->used_tables() & OUTER_REF_TABLE_BIT)
11333
item->update_used_tables();
11334
if (type == Item::SUBSELECT_ITEM ||
11335
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
11338
Mark that the we have ignored an item that refers to a summary
11339
function. We need to know this if someone is going to use
11340
DISTINCT on the result.
11342
param->using_indirect_summary_function=1;
11346
if (item->const_item() && (int) hidden_field_count <= 0)
11347
continue; // We don't have to store this
11349
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11350
{ /* Can't calc group yet */
11351
((Item_sum*) item)->result_field=0;
11352
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11354
Item **argp= ((Item_sum*) item)->args + i;
11356
if (!arg->const_item())
11359
create_tmp_field(thd, table, arg, arg->type(), ©_func,
11360
tmp_from_field, &default_field[fieldnr],
11361
group != 0,not_all_columns,
11363
param->convert_blob_length);
11365
goto err; // Should be OOM
11367
reclength+=new_field->pack_length();
11368
if (new_field->flags & BLOB_FLAG)
11370
*blob_field++= fieldnr;
11373
if (new_field->type() == MYSQL_TYPE_BIT)
11374
total_uneven_bit_length+= new_field->field_length & 7;
11375
*(reg_field++)= new_field;
11376
if (new_field->real_type() == MYSQL_TYPE_STRING ||
11377
new_field->real_type() == MYSQL_TYPE_VARCHAR)
11380
string_total_length+= new_field->pack_length();
11382
thd->mem_root= mem_root_save;
11383
thd->change_item_tree(argp, new Item_field(new_field));
11384
thd->mem_root= &table->mem_root;
11385
if (!(new_field->flags & NOT_NULL_FLAG))
11389
new_field->maybe_null() is still false, it will be
11390
changed below. But we have to setup Item_field correctly
11392
(*argp)->maybe_null=1;
11394
new_field->field_index= fieldnr++;
11401
The last parameter to create_tmp_field() is a bit tricky:
11403
We need to set it to 0 in union, to get fill_record() to modify the
11405
We need to set it to 1 on multi-table-update and in select to
11406
write rows to the temporary table.
11407
We here distinguish between UNION and multi-table-updates by the fact
11408
that in the later case group is set to the row pointer.
11410
Field *new_field= (param->schema_table) ?
11411
create_tmp_field_for_schema(thd, item, table) :
11412
create_tmp_field(thd, table, item, type, ©_func,
11413
tmp_from_field, &default_field[fieldnr],
11415
!force_copy_fields &&
11416
(not_all_columns || group !=0),
11418
If item->marker == 4 then we force create_tmp_field
11419
to create a 64-bit longs for BIT fields because HEAP
11420
tables can't index BIT fields directly. We do the same
11421
for distinct, as we want the distinct index to be
11422
usable in this case too.
11424
item->marker == 4 || param->bit_fields_as_long,
11426
param->convert_blob_length);
11430
if (thd->is_fatal_error)
11431
goto err; // Got OOM
11432
continue; // Some kindf of const item
11434
if (type == Item::SUM_FUNC_ITEM)
11435
((Item_sum *) item)->result_field= new_field;
11437
reclength+=new_field->pack_length();
11438
if (!(new_field->flags & NOT_NULL_FLAG))
11440
if (new_field->type() == MYSQL_TYPE_BIT)
11441
total_uneven_bit_length+= new_field->field_length & 7;
11442
if (new_field->flags & BLOB_FLAG)
11444
*blob_field++= fieldnr;
11447
if (item->marker == 4 && item->maybe_null)
11449
group_null_items++;
11450
new_field->flags|= GROUP_FLAG;
11452
new_field->field_index= fieldnr++;
11453
*(reg_field++)= new_field;
11455
if (!--hidden_field_count)
11458
This was the last hidden field; Remember how many hidden fields could
11461
hidden_null_count=null_count;
11463
We need to update hidden_field_count as we may have stored group
11464
functions with constant arguments
11466
param->hidden_field_count= fieldnr;
11470
DBUG_ASSERT(fieldnr == (uint) (reg_field - table->field));
11471
DBUG_ASSERT(field_count >= (uint) (reg_field - table->field));
11472
field_count= fieldnr;
11474
*blob_field= 0; // End marker
11475
share->fields= field_count;
11477
/* If result table is small; use a heap */
11478
/* future: storage engine selection can be made dynamic? */
11479
if (blob_count || using_unique_constraint ||
11480
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11481
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11483
share->db_plugin= ha_lock_engine(0, myisam_hton);
11484
table->file= get_new_handler(share, &table->mem_root,
11487
(param->group_parts > table->file->max_key_parts() ||
11488
param->group_length > table->file->max_key_length()))
11489
using_unique_constraint=1;
11493
share->db_plugin= ha_lock_engine(0, heap_hton);
11494
table->file= get_new_handler(share, &table->mem_root,
11501
if (!using_unique_constraint)
11502
reclength+= group_null_items; // null flag is stored separately
11504
share->blob_fields= blob_count;
11505
if (blob_count == 0)
11507
/* We need to ensure that first byte is not 0 for the delete link */
11508
if (param->hidden_field_count)
11509
hidden_null_count++;
11513
hidden_null_pack_length=(hidden_null_count+7)/8;
11514
null_pack_length= (hidden_null_pack_length +
11515
(null_count + total_uneven_bit_length + 7) / 8);
11516
reclength+=null_pack_length;
11518
reclength=1; // Dummy select
11519
/* Use packed rows if there is blobs or a lot of space to gain */
11520
if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
11521
use_packed_rows= 1;
11523
share->reclength= reclength;
11525
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11526
share->rec_buff_length= alloc_length;
11527
if (!(table->record[0]= (uchar*)
11528
alloc_root(&table->mem_root, alloc_length*3)))
11530
table->record[1]= table->record[0]+alloc_length;
11531
share->default_values= table->record[1]+alloc_length;
11533
copy_func[0]=0; // End marker
11534
param->func_count= copy_func - param->items_to_copy;
11536
setup_tmp_table_column_bitmaps(table, bitmaps);
11538
recinfo=param->start_recinfo;
11539
null_flags=(uchar*) table->record[0];
11540
pos=table->record[0]+ null_pack_length;
11541
if (null_pack_length)
11543
bzero((uchar*) recinfo,sizeof(*recinfo));
11544
recinfo->type=FIELD_NORMAL;
11545
recinfo->length=null_pack_length;
11547
bfill(null_flags,null_pack_length,255); // Set null fields
11549
table->null_flags= (uchar*) table->record[0];
11550
share->null_fields= null_count+ hidden_null_count;
11551
share->null_bytes= null_pack_length;
11553
null_count= (blob_count == 0) ? 1 : 0;
11554
hidden_field_count=param->hidden_field_count;
11555
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11557
Field *field= *reg_field;
11559
bzero((uchar*) recinfo,sizeof(*recinfo));
11561
if (!(field->flags & NOT_NULL_FLAG))
11563
if (field->flags & GROUP_FLAG && !using_unique_constraint)
11566
We have to reserve one byte here for NULL bits,
11567
as this is updated by 'end_update()'
11569
*pos++=0; // Null is stored here
11571
recinfo->type=FIELD_NORMAL;
11573
bzero((uchar*) recinfo,sizeof(*recinfo));
11577
recinfo->null_bit= 1 << (null_count & 7);
11578
recinfo->null_pos= null_count/8;
11580
field->move_field(pos,null_flags+null_count/8,
11581
1 << (null_count & 7));
11585
field->move_field(pos,(uchar*) 0,0);
11586
if (field->type() == MYSQL_TYPE_BIT)
11588
/* We have to reserve place for extra bits among null bits */
11589
((Field_bit*) field)->set_bit_ptr(null_flags + null_count / 8,
11591
null_count+= (field->field_length & 7);
11596
Test if there is a default field value. The test for ->ptr is to skip
11597
'offset' fields generated by initalize_tables
11599
if (default_field[i] && default_field[i]->ptr)
11602
default_field[i] is set only in the cases when 'field' can
11603
inherit the default value that is defined for the field referred
11604
by the Item_field object from which 'field' has been created.
11607
Field *orig_field= default_field[i];
11608
/* Get the value from default_values */
11609
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11610
orig_field->table->record[0]);
11611
orig_field->move_field_offset(diff); // Points now at default_values
11612
if (orig_field->is_real_null())
11616
field->set_notnull();
11617
memcpy(field->ptr, orig_field->ptr, field->pack_length());
11619
orig_field->move_field_offset(-diff); // Back to record[0]
11623
{ /* Not a table Item */
11624
copy->set(field,from_field[i],save_sum_fields);
11627
length=field->pack_length();
11630
/* Make entry for create table */
11631
recinfo->length=length;
11632
if (field->flags & BLOB_FLAG)
11633
recinfo->type= (int) FIELD_BLOB;
11634
else if (use_packed_rows &&
11635
field->real_type() == MYSQL_TYPE_STRING &&
11636
length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11637
recinfo->type=FIELD_SKIP_ENDSPACE;
11639
recinfo->type=FIELD_NORMAL;
11640
if (!--hidden_field_count)
11641
null_count=(null_count+7) & ~7; // move to next byte
11643
// fix table name in field entry
11644
field->table_name= &table->alias;
11647
param->copy_field_end=copy;
11648
param->recinfo=recinfo;
11649
store_record(table,s->default_values); // Make empty default record
11651
if (thd->variables.tmp_table_size == ~ (ulonglong) 0) // No limit
11652
share->max_rows= ~(ha_rows) 0;
11654
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11655
min(thd->variables.tmp_table_size,
11656
thd->variables.max_heap_table_size) :
11657
thd->variables.tmp_table_size) /
11659
set_if_bigger(share->max_rows,1); // For dummy start options
11661
Push the LIMIT clause to the temporary table creation, so that we
11662
materialize only up to 'rows_limit' records instead of all result records.
11664
set_if_smaller(share->max_rows, rows_limit);
11665
param->end_write_records= rows_limit;
11667
keyinfo= param->keyinfo;
11671
DBUG_PRINT("info",("Creating group key in temporary table"));
11672
table->group=group; /* Table is grouped by key */
11673
param->group_buff=group_buff;
11675
share->uniques= test(using_unique_constraint);
11676
table->key_info=keyinfo;
11677
keyinfo->key_part=key_part_info;
11678
keyinfo->flags=HA_NOSAME;
11679
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11680
keyinfo->key_length=0;
11681
keyinfo->rec_per_key=0;
11682
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11683
keyinfo->name= (char*) "group_key";
11684
ORDER *cur_group= group;
11685
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11687
Field *field=(*cur_group->item)->get_tmp_table_field();
11688
bool maybe_null=(*cur_group->item)->maybe_null;
11689
key_part_info->null_bit=0;
11690
key_part_info->field= field;
11691
key_part_info->offset= field->offset(table->record[0]);
11692
key_part_info->length= (uint16) field->key_length();
11693
key_part_info->type= (uint8) field->key_type();
11694
key_part_info->key_type =
11695
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11696
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11697
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11698
0 : FIELDFLAG_BINARY;
11699
if (!using_unique_constraint)
11701
cur_group->buff=(char*) group_buff;
11702
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11707
goto err; /* purecov: inspected */
11711
To be able to group on NULL, we reserved place in group_buff
11712
for the NULL flag just before the column. (see above).
11713
The field data is after this flag.
11714
The NULL flag is updated in 'end_update()' and 'end_write()'
11716
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
11717
key_part_info->null_bit=field->null_bit;
11718
key_part_info->null_offset= (uint) (field->null_ptr -
11719
(uchar*) table->record[0]);
11720
cur_group->buff++; // Pointer to field data
11721
group_buff++; // Skipp null flag
11723
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11724
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11725
group_buff+= cur_group->field->pack_length();
11727
keyinfo->key_length+= key_part_info->length;
11731
if (distinct && field_count != param->hidden_field_count)
11734
Create an unique key or an unique constraint over all columns
11735
that should be in the result. In the temporary table, there are
11736
'param->hidden_field_count' extra columns, whose null bits are stored
11737
in the first 'hidden_null_pack_length' bytes of the row.
11739
DBUG_PRINT("info",("hidden_field_count: %d", param->hidden_field_count));
11744
Special mode for index creation in MyISAM used to support unique
11745
indexes on blobs with arbitrary length. Such indexes cannot be
11750
null_pack_length-=hidden_null_pack_length;
11751
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11752
(share->uniques ? test(null_pack_length) : 0));
11753
table->distinct= 1;
11755
if (!(key_part_info= (KEY_PART_INFO*)
11756
alloc_root(&table->mem_root,
11757
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11759
bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11760
table->key_info=keyinfo;
11761
keyinfo->key_part=key_part_info;
11762
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11763
keyinfo->key_length=(uint16) reclength;
11764
keyinfo->name= (char*) "distinct_key";
11765
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11766
keyinfo->rec_per_key=0;
11769
Create an extra field to hold NULL bits so that unique indexes on
11770
blobs can distinguish NULL from 0. This extra field is not needed
11771
when we do not use UNIQUE indexes for blobs.
11773
if (null_pack_length && share->uniques)
11775
key_part_info->null_bit=0;
11776
key_part_info->offset=hidden_null_pack_length;
11777
key_part_info->length=null_pack_length;
11778
key_part_info->field= new Field_string(table->record[0],
11779
(uint32) key_part_info->length,
11783
NullS, &my_charset_bin);
11784
if (!key_part_info->field)
11786
key_part_info->field->init(table);
11787
key_part_info->key_type=FIELDFLAG_BINARY;
11788
key_part_info->type= HA_KEYTYPE_BINARY;
11791
/* Create a distinct key over the columns we are going to return */
11792
for (i=param->hidden_field_count, reg_field=table->field + i ;
11794
i++, reg_field++, key_part_info++)
11796
key_part_info->null_bit=0;
11797
key_part_info->field= *reg_field;
11798
key_part_info->offset= (*reg_field)->offset(table->record[0]);
11799
key_part_info->length= (uint16) (*reg_field)->pack_length();
11801
The below method of computing the key format length of the
11802
key part is a copy/paste from opt_range.cc, and table.cc.
11803
This should be factored out, e.g. as a method of Field.
11804
In addition it is not clear if any of the Field::*_length
11805
methods is supposed to compute the same length. If so, it
11808
key_part_info->store_length= key_part_info->length;
11810
if ((*reg_field)->real_maybe_null())
11811
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11812
if ((*reg_field)->type() == MYSQL_TYPE_BLOB ||
11813
(*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
11814
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11816
key_part_info->type= (uint8) (*reg_field)->key_type();
11817
key_part_info->key_type =
11818
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11819
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11820
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11821
0 : FIELDFLAG_BINARY;
11825
if (thd->is_fatal_error) // If end of memory
11826
goto err; /* purecov: inspected */
11827
share->db_record_offset= 1;
11828
if (share->db_type() == myisam_hton)
11830
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11831
¶m->recinfo, select_options))
11834
if (open_tmp_table(table))
11837
thd->mem_root= mem_root_save;
11839
DBUG_RETURN(table);
11842
thd->mem_root= mem_root_save;
11843
free_tmp_table(thd,table); /* purecov: inspected */
11844
if (temp_pool_slot != MY_BIT_NONE)
11845
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11846
DBUG_RETURN(NULL); /* purecov: inspected */
11853
Create a temporary table to weed out duplicate rowid combinations
11857
create_duplicate_weedout_tmp_table()
11859
uniq_tuple_length_arg
11863
Create a temporary table to weed out duplicate rowid combinations. The
11864
table has a single column that is a concatenation of all rowids in the
11867
Depending on the needed length, there are two cases:
11869
1. When the length of the column < max_key_length:
11871
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11873
2. Otherwise (not a valid SQL syntax but internally supported):
11875
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11877
The code in this function was produced by extraction of relevant parts
11878
from create_tmp_table().
11885
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
11886
uint uniq_tuple_length_arg,
11887
SJ_TMP_TABLE *sjtbl)
11889
MEM_ROOT *mem_root_save, own_root;
11891
TABLE_SHARE *share;
11892
uint temp_pool_slot=MY_BIT_NONE;
11893
char *tmpname,path[FN_REFLEN];
11895
KEY_PART_INFO *key_part_info;
11900
MI_COLUMNDEF *recinfo, *start_recinfo;
11901
bool using_unique_constraint=FALSE;
11902
bool use_packed_rows= FALSE;
11903
Field *field, *key_field;
11904
uint blob_count, null_pack_length, null_count;
11907
DBUG_ENTER("create_duplicate_weedout_tmp_table");
11910
STEP 1: Get temporary table name
11912
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11913
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11914
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11916
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11917
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11918
current_pid, temp_pool_slot);
11921
/* if we run out of slots or we are not using tempool */
11922
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11923
thd->thread_id, thd->tmp_table++);
11925
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11927
/* STEP 2: Figure if we'll be using a key or blob+constraint */
11928
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11929
using_unique_constraint= TRUE;
11931
/* STEP 3: Allocate memory for temptable description */
11932
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11933
if (!multi_alloc_root(&own_root,
11934
&table, sizeof(*table),
11935
&share, sizeof(*share),
11936
®_field, sizeof(Field*) * (1+1),
11937
&blob_field, sizeof(uint)*2,
11938
&keyinfo, sizeof(*keyinfo),
11939
&key_part_info, sizeof(*key_part_info) * 2,
11941
sizeof(*recinfo)*(1*2+4),
11942
&tmpname, (uint) strlen(path)+1,
11943
&group_buff, (!using_unique_constraint ?
11944
uniq_tuple_length_arg : 0),
11945
&bitmaps, bitmap_buffer_size(1)*2,
11948
if (temp_pool_slot != MY_BIT_NONE)
11949
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11952
strmov(tmpname,path);
11955
/* STEP 4: Create TABLE description */
11956
bzero((char*) table,sizeof(*table));
11957
bzero((char*) reg_field,sizeof(Field*)*2);
11959
table->mem_root= own_root;
11960
mem_root_save= thd->mem_root;
11961
thd->mem_root= &table->mem_root;
11963
table->field=reg_field;
11964
table->alias= "weedout-tmp";
11965
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11966
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11968
table->temp_pool_slot = temp_pool_slot;
11969
table->copy_blobs= 1;
11970
table->in_use= thd;
11971
table->quick_keys.init();
11972
table->covering_keys.init();
11973
table->keys_in_use_for_query.init();
11976
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11977
share->blob_field= blob_field;
11978
share->blob_ptr_size= portable_sizeof_char_ptr;
11979
share->db_low_byte_first=1; // True for HEAP and MyISAM
11980
share->table_charset= NULL;
11981
share->primary_key= MAX_KEY; // Indicate no primary key
11982
share->keys_for_keyread.init();
11983
share->keys_in_use.init();
11987
/* Create the field */
11990
For the sake of uniformity, always use Field_varstring (altough we could
11991
use Field_string for shorter keys)
11993
field= new Field_varstring(uniq_tuple_length_arg, FALSE, "rowids", share,
11997
field->table= table;
11998
field->key_start.init(0);
11999
field->part_of_key.init(0);
12000
field->part_of_sortkey.init(0);
12001
field->unireg_check= Field::NONE;
12002
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
12003
field->reset_fields();
12004
field->init(table);
12005
field->orig_table= NULL;
12007
field->field_index= 0;
12009
*(reg_field++)= field;
12014
share->blob_fields= 0;
12017
uint reclength= field->pack_length();
12018
if (using_unique_constraint)
12020
share->db_plugin= ha_lock_engine(0, myisam_hton);
12021
table->file= get_new_handler(share, &table->mem_root,
12023
DBUG_ASSERT(uniq_tuple_length_arg <= table->file->max_key_length());
12027
share->db_plugin= ha_lock_engine(0, heap_hton);
12028
table->file= get_new_handler(share, &table->mem_root,
12036
null_pack_length= 1;
12037
reclength += null_pack_length;
12039
share->reclength= reclength;
12041
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
12042
share->rec_buff_length= alloc_length;
12043
if (!(table->record[0]= (uchar*)
12044
alloc_root(&table->mem_root, alloc_length*3)))
12046
table->record[1]= table->record[0]+alloc_length;
12047
share->default_values= table->record[1]+alloc_length;
12049
setup_tmp_table_column_bitmaps(table, bitmaps);
12051
recinfo= start_recinfo;
12052
null_flags=(uchar*) table->record[0];
12053
pos=table->record[0]+ null_pack_length;
12054
if (null_pack_length)
12056
bzero((uchar*) recinfo,sizeof(*recinfo));
12057
recinfo->type=FIELD_NORMAL;
12058
recinfo->length=null_pack_length;
12060
bfill(null_flags,null_pack_length,255); // Set null fields
12062
table->null_flags= (uchar*) table->record[0];
12063
share->null_fields= null_count;
12064
share->null_bytes= null_pack_length;
12069
//Field *field= *reg_field;
12071
bzero((uchar*) recinfo,sizeof(*recinfo));
12072
field->move_field(pos,(uchar*) 0,0);
12076
Test if there is a default field value. The test for ->ptr is to skip
12077
'offset' fields generated by initalize_tables
12079
// Initialize the table field:
12080
bzero(field->ptr, field->pack_length());
12082
length=field->pack_length();
12085
/* Make entry for create table */
12086
recinfo->length=length;
12087
if (field->flags & BLOB_FLAG)
12088
recinfo->type= (int) FIELD_BLOB;
12089
else if (use_packed_rows &&
12090
field->real_type() == MYSQL_TYPE_STRING &&
12091
length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
12092
recinfo->type=FIELD_SKIP_ENDSPACE;
12094
recinfo->type=FIELD_NORMAL;
12096
field->table_name= &table->alias;
12099
//param->recinfo=recinfo;
12100
//store_record(table,s->default_values); // Make empty default record
12102
if (thd->variables.tmp_table_size == ~ (ulonglong) 0) // No limit
12103
share->max_rows= ~(ha_rows) 0;
12105
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
12106
min(thd->variables.tmp_table_size,
12107
thd->variables.max_heap_table_size) :
12108
thd->variables.tmp_table_size) /
12110
set_if_bigger(share->max_rows,1); // For dummy start options
12113
//// keyinfo= param->keyinfo;
12116
DBUG_PRINT("info",("Creating group key in temporary table"));
12118
share->uniques= test(using_unique_constraint);
12119
table->key_info=keyinfo;
12120
keyinfo->key_part=key_part_info;
12121
keyinfo->flags=HA_NOSAME;
12122
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
12123
keyinfo->key_length=0;
12124
keyinfo->rec_per_key=0;
12125
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
12126
keyinfo->name= (char*) "weedout_key";
12128
key_part_info->null_bit=0;
12129
key_part_info->field= field;
12130
key_part_info->offset= field->offset(table->record[0]);
12131
key_part_info->length= (uint16) field->key_length();
12132
key_part_info->type= (uint8) field->key_type();
12133
key_part_info->key_type = FIELDFLAG_BINARY;
12134
if (!using_unique_constraint)
12136
if (!(key_field= field->new_key_field(thd->mem_root, table,
12141
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
12143
keyinfo->key_length+= key_part_info->length;
12147
if (thd->is_fatal_error) // If end of memory
12149
share->db_record_offset= 1;
12150
if (share->db_type() == myisam_hton)
12153
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
12156
sjtbl->start_recinfo= start_recinfo;
12157
sjtbl->recinfo= recinfo;
12158
if (open_tmp_table(table))
12161
thd->mem_root= mem_root_save;
12162
DBUG_RETURN(table);
12165
thd->mem_root= mem_root_save;
12166
free_tmp_table(thd,table); /* purecov: inspected */
12167
if (temp_pool_slot != MY_BIT_NONE)
12168
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
12169
DBUG_RETURN(NULL); /* purecov: inspected */
12172
/****************************************************************************/
12175
Create a reduced TABLE object with properly set up Field list from a
12176
list of field definitions.
12178
The created table doesn't have a table handler associated with
12179
it, has no keys, no group/distinct, no copy_funcs array.
12180
The sole purpose of this TABLE object is to use the power of Field
12181
class to read/write data to/from table->record[0]. Then one can store
12182
the record in any container (RB tree, hash, etc).
12183
The table is created in THD mem_root, so are the table's fields.
12184
Consequently, if you don't BLOB fields, you don't need to free it.
12186
@param thd connection handle
12187
@param field_list list of column definitions
12190
0 if out of memory, TABLE object in case of success
12193
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
12195
uint field_count= field_list.elements;
12196
uint blob_count= 0;
12198
Create_field *cdef; /* column definition */
12199
uint record_length= 0;
12200
uint null_count= 0; /* number of columns which may be null */
12201
uint null_pack_length; /* NULL representation array length */
12205
TABLE_SHARE *share;
12207
if (!multi_alloc_root(thd->mem_root,
12208
&table, sizeof(*table),
12209
&share, sizeof(*share),
12210
&field, (field_count + 1) * sizeof(Field*),
12211
&blob_field, (field_count+1) *sizeof(uint),
12212
&bitmaps, bitmap_buffer_size(field_count)*2,
12216
bzero(table, sizeof(*table));
12217
bzero(share, sizeof(*share));
12218
table->field= field;
12220
share->blob_field= blob_field;
12221
share->fields= field_count;
12222
share->blob_ptr_size= portable_sizeof_char_ptr;
12223
setup_tmp_table_column_bitmaps(table, bitmaps);
12225
/* Create all fields and calculate the total length of record */
12226
List_iterator_fast<Create_field> it(field_list);
12227
while ((cdef= it++))
12229
*field= make_field(share, 0, cdef->length,
12230
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
12231
f_maybe_null(cdef->pack_flag) ? 1 : 0,
12232
cdef->pack_flag, cdef->sql_type, cdef->charset,
12233
cdef->unireg_check,
12234
cdef->interval, cdef->field_name);
12237
(*field)->init(table);
12238
record_length+= (*field)->pack_length();
12239
if (! ((*field)->flags & NOT_NULL_FLAG))
12242
if ((*field)->flags & BLOB_FLAG)
12243
share->blob_field[blob_count++]= (uint) (field - table->field);
12247
*field= NULL; /* mark the end of the list */
12248
share->blob_field[blob_count]= 0; /* mark the end of the list */
12249
share->blob_fields= blob_count;
12251
null_pack_length= (null_count + 7)/8;
12252
share->reclength= record_length + null_pack_length;
12253
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12254
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12255
if (!table->record[0])
12258
if (null_pack_length)
12260
table->null_flags= (uchar*) table->record[0];
12261
share->null_fields= null_count;
12262
share->null_bytes= null_pack_length;
12265
table->in_use= thd; /* field->reset() may access table->in_use */
12267
/* Set up field pointers */
12268
uchar *null_pos= table->record[0];
12269
uchar *field_pos= null_pos + share->null_bytes;
12272
for (field= table->field; *field; ++field)
12274
Field *cur_field= *field;
12275
if ((cur_field->flags & NOT_NULL_FLAG))
12276
cur_field->move_field(field_pos);
12279
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12281
if (null_bit == (1 << 8))
12287
cur_field->reset();
12289
field_pos+= cur_field->pack_length();
12294
for (field= table->field; *field; ++field)
12295
delete *field; /* just invokes field destructor */
12300
static bool open_tmp_table(TABLE *table)
12303
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12304
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12306
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12310
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */
12316
Create MyISAM temporary table
12319
create_myisam_tmp_table()
12320
table Table object that descrimes the table to be created
12321
keyinfo Description of the index (there is always one index)
12322
start_recinfo MyISAM's column descriptions
12323
recinfo INOUT End of MyISAM's column descriptions
12324
options Option bits
12327
Create a MyISAM temporary table according to passed description. The is
12328
assumed to have one unique index or constraint.
12330
The passed array or MI_COLUMNDEF structures must have this form:
12332
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12333
when there are many nullable columns)
12335
3. One free MI_COLUMNDEF element (*recinfo points here)
12337
This function may use the free element to create hash column for unique
12345
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
12346
MI_COLUMNDEF *start_recinfo,
12347
MI_COLUMNDEF **recinfo,
12352
MI_UNIQUEDEF uniquedef;
12353
TABLE_SHARE *share= table->s;
12354
DBUG_ENTER("create_myisam_tmp_table");
12357
{ // Get keys for ni_create
12358
bool using_unique_constraint=0;
12359
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12360
sizeof(*seg) * keyinfo->key_parts);
12364
bzero(seg, sizeof(*seg) * keyinfo->key_parts);
12365
if (keyinfo->key_length >= table->file->max_key_length() ||
12366
keyinfo->key_parts > table->file->max_key_parts() ||
12369
/* Can't create a key; Make a unique constraint instead of a key */
12372
using_unique_constraint=1;
12373
bzero((char*) &uniquedef,sizeof(uniquedef));
12374
uniquedef.keysegs=keyinfo->key_parts;
12376
uniquedef.null_are_equal=1;
12378
/* Create extra column for hash value */
12379
bzero((uchar*) *recinfo,sizeof(**recinfo));
12380
(*recinfo)->type= FIELD_CHECK;
12381
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12383
share->reclength+=MI_UNIQUE_HASH_LENGTH;
12387
/* Create an unique key */
12388
bzero((char*) &keydef,sizeof(keydef));
12389
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12390
keydef.keysegs= keyinfo->key_parts;
12393
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12395
Field *field=keyinfo->key_part[i].field;
12397
seg->language= field->charset()->number;
12398
seg->length= keyinfo->key_part[i].length;
12399
seg->start= keyinfo->key_part[i].offset;
12400
if (field->flags & BLOB_FLAG)
12403
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12404
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12405
seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
12406
seg->flag= HA_BLOB_PART;
12407
seg->length=0; // Whole blob in unique constraint
12411
seg->type= keyinfo->key_part[i].type;
12412
/* Tell handler if it can do suffic space compression */
12413
if (field->real_type() == MYSQL_TYPE_STRING &&
12414
keyinfo->key_part[i].length > 4)
12415
seg->flag|= HA_SPACE_PACK;
12417
if (!(field->flags & NOT_NULL_FLAG))
12419
seg->null_bit= field->null_bit;
12420
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12422
We are using a GROUP BY on something that contains NULL
12423
In this case we have to tell MyISAM that two NULL should
12424
on INSERT be regarded at the same value
12426
if (!using_unique_constraint)
12427
keydef.flag|= HA_NULL_ARE_EQUAL;
12431
MI_CREATE_INFO create_info;
12432
bzero((char*) &create_info,sizeof(create_info));
12434
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12436
create_info.data_file_length= ~(ulonglong) 0;
12438
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12439
(uint) (*recinfo-start_recinfo),
12441
share->uniques, &uniquedef,
12443
HA_CREATE_TMP_TABLE)))
12445
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12449
status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12450
share->db_record_offset= 1;
12458
free_tmp_table(THD *thd, TABLE *entry)
12460
MEM_ROOT own_root= entry->mem_root;
12461
const char *save_proc_info;
12462
DBUG_ENTER("free_tmp_table");
12463
DBUG_PRINT("enter",("table: %s",entry->alias));
12465
save_proc_info=thd->proc_info;
12466
thd_proc_info(thd, "removing tmp table");
12470
if (entry->db_stat)
12471
entry->file->ha_drop_table(entry->s->table_name.str);
12473
entry->file->ha_delete_table(entry->s->table_name.str);
12474
delete entry->file;
12478
for (Field **ptr=entry->field ; *ptr ; ptr++)
12480
free_io_cache(entry);
12482
if (entry->temp_pool_slot != MY_BIT_NONE)
12483
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12485
plugin_unlock(0, entry->s->db_plugin);
12487
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12488
thd_proc_info(thd, save_proc_info);
12494
If a HEAP table gets full, create a MyISAM table and copy all rows
12498
bool create_myisam_from_heap(THD *thd, TABLE *table,
12499
MI_COLUMNDEF *start_recinfo,
12500
MI_COLUMNDEF **recinfo,
12501
int error, bool ignore_last_dupp_key_error)
12505
const char *save_proc_info;
12507
DBUG_ENTER("create_myisam_from_heap");
12509
if (table->s->db_type() != heap_hton ||
12510
error != HA_ERR_RECORD_FILE_FULL)
12512
table->file->print_error(error,MYF(0));
12517
new_table.s= &share;
12518
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12519
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12520
new_table.s->db_type())))
12521
DBUG_RETURN(1); // End of memory
12523
save_proc_info=thd->proc_info;
12524
thd_proc_info(thd, "converting HEAP to MyISAM");
12526
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12527
recinfo, thd->lex->select_lex.options |
12530
if (open_tmp_table(&new_table))
12532
if (table->file->indexes_are_disabled())
12533
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12534
table->file->ha_index_or_rnd_end();
12535
table->file->ha_rnd_init(1);
12536
if (table->no_rows)
12538
new_table.file->extra(HA_EXTRA_NO_ROWS);
12539
new_table.no_rows=1;
12542
#ifdef TO_BE_DONE_LATER_IN_4_1
12544
To use start_bulk_insert() (which is new in 4.1) we need to find
12545
all places where a corresponding end_bulk_insert() should be put.
12547
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12548
new_table.file->ha_start_bulk_insert(table->file->stats.records);
12550
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12551
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12555
copy all old rows from heap table to MyISAM table
12556
This is the only code that uses record[1] to read/write but this
12557
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12559
while (!table->file->rnd_next(new_table.record[1]))
12561
write_err= new_table.file->ha_write_row(new_table.record[1]);
12562
DBUG_EXECUTE_IF("raise_error", write_err= HA_ERR_FOUND_DUPP_KEY ;);
12566
/* copy row that filled HEAP table */
12567
if ((write_err=new_table.file->ha_write_row(table->record[0])))
12569
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12570
!ignore_last_dupp_key_error)
12574
/* remove heap table and change to use myisam table */
12575
(void) table->file->ha_rnd_end();
12576
(void) table->file->close(); // This deletes the table !
12577
delete table->file;
12579
plugin_unlock(0, table->s->db_plugin);
12580
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12581
new_table.s= table->s; // Keep old share
12585
table->file->change_table_ptr(table, table->s);
12586
table->use_all_columns();
12587
if (save_proc_info)
12589
const char *new_proc_info=
12590
(!strcmp(save_proc_info,"Copying to tmp table") ?
12591
"Copying to tmp table on disk" : save_proc_info);
12592
thd_proc_info(thd, new_proc_info);
12597
DBUG_PRINT("error",("Got error: %d",write_err));
12598
table->file->print_error(write_err, MYF(0));
12599
(void) table->file->ha_rnd_end();
12600
(void) new_table.file->close();
12602
new_table.file->ha_delete_table(new_table.s->table_name.str);
12604
delete new_table.file;
12605
thd_proc_info(thd, save_proc_info);
12606
table->mem_root= new_table.mem_root;
12613
Rows produced by a join sweep may end up in a temporary table or be sent
12614
to a client. Setup the function of the nested loop join algorithm which
12615
handles final fully constructed and matched records.
12617
@param join join to setup the function for.
12620
end_select function to use. This function can't fail.
12623
Next_select_func setup_end_select_func(JOIN *join)
12625
TABLE *table= join->tmp_table;
12626
TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
12627
Next_select_func end_select;
12629
/* Set up select_end */
12632
if (table->group && tmp_tbl->sum_func_count &&
12633
!tmp_tbl->precomputed_group_by)
12635
if (table->s->keys)
12637
DBUG_PRINT("info",("Using end_update"));
12638
end_select=end_update;
12642
DBUG_PRINT("info",("Using end_unique_update"));
12643
end_select=end_unique_update;
12646
else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
12648
DBUG_PRINT("info",("Using end_write_group"));
12649
end_select=end_write_group;
12653
DBUG_PRINT("info",("Using end_write"));
12654
end_select=end_write;
12655
if (tmp_tbl->precomputed_group_by)
12658
A preceding call to create_tmp_table in the case when loose
12659
index scan is used guarantees that
12660
TMP_TABLE_PARAM::items_to_copy has enough space for the group
12661
by functions. It is OK here to use memcpy since we copy
12662
Item_sum pointers into an array of Item pointers.
12664
memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count,
12666
sizeof(Item*)*tmp_tbl->sum_func_count);
12667
tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0;
12673
if ((join->sort_and_group) &&
12674
!tmp_tbl->precomputed_group_by)
12675
end_select= end_send_group;
12677
end_select= end_send;
12684
Make a join of all tables and write it on socket or to table.
12691
-1 if error should be sent
12695
do_select(JOIN *join,List<Item> *fields,TABLE *table)
12698
enum_nested_loop_state error= NESTED_LOOP_OK;
12699
JOIN_TAB *join_tab= NULL;
12700
DBUG_ENTER("do_select");
12702
join->tmp_table= table; /* Save for easy recursion */
12703
join->fields= fields;
12707
VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
12708
empty_record(table);
12709
if (table->group && join->tmp_table_param.sum_func_count &&
12710
table->s->keys && !table->file->inited)
12711
table->file->ha_index_init(0, 0);
12713
/* Set up select_end */
12714
Next_select_func end_select= setup_end_select_func(join);
12717
join->join_tab[join->tables-1].next_select= end_select;
12719
join_tab=join->join_tab+join->const_tables;
12721
join->send_records=0;
12722
if (join->tables == join->const_tables)
12725
HAVING will be checked after processing aggregate functions,
12726
But WHERE should checkd here (we alredy have read tables)
12728
if (!join->conds || join->conds->val_int())
12730
error= (*end_select)(join, 0, 0);
12731
if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
12732
error= (*end_select)(join, 0, 1);
12735
If we don't go through evaluate_join_record(), do the counting
12736
here. join->send_records is increased on success in end_send(),
12737
so we don't touch it here.
12739
join->examined_rows++;
12740
join->thd->row_count++;
12741
DBUG_ASSERT(join->examined_rows <= 1);
12743
else if (join->send_row_on_empty_set())
12745
List<Item> *columns_list= fields;
12746
rc= join->result->send_data(*columns_list);
12751
DBUG_ASSERT(join->tables);
12752
error= sub_select(join,join_tab,0);
12753
if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
12754
error= sub_select(join,join_tab,1);
12755
if (error == NESTED_LOOP_QUERY_LIMIT)
12756
error= NESTED_LOOP_OK; /* select_limit used */
12758
if (error == NESTED_LOOP_NO_MORE_ROWS)
12759
error= NESTED_LOOP_OK;
12761
if (error == NESTED_LOOP_OK)
12764
Sic: this branch works even if rc != 0, e.g. when
12765
send_data above returns an error.
12767
if (!table) // If sending data to client
12770
The following will unlock all cursors if the command wasn't an
12773
join->join_free(); // Unlock all cursors
12774
if (join->result->send_eof())
12775
rc= 1; // Don't send error
12777
DBUG_PRINT("info",("%ld records output", (long) join->send_records));
12783
int tmp, new_errno= 0;
12784
if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
12786
DBUG_PRINT("error",("extra(HA_EXTRA_NO_CACHE) failed"));
12789
if ((tmp=table->file->ha_index_or_rnd_end()))
12791
DBUG_PRINT("error",("ha_index_or_rnd_end() failed"));
12795
table->file->print_error(new_errno,MYF(0));
12800
DBUG_PRINT("error",("Error: do_select() failed"));
12803
DBUG_RETURN(join->thd->is_error() ? -1 : rc);
12807
enum_nested_loop_state
12808
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
12810
enum_nested_loop_state rc;
12812
if (end_of_records)
12814
rc= flush_cached_records(join,join_tab,FALSE);
12815
if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
12816
rc= sub_select(join,join_tab,end_of_records);
12819
if (join->thd->killed) // If aborted by user
12821
join->thd->send_kill_message();
12822
return NESTED_LOOP_KILLED; /* purecov: inspected */
12824
if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
12826
if (!store_record_in_cache(&join_tab->cache))
12827
return NESTED_LOOP_OK; // There is more room in cache
12828
return flush_cached_records(join,join_tab,FALSE);
12830
rc= flush_cached_records(join, join_tab, TRUE);
12831
if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
12832
rc= sub_select(join, join_tab, end_of_records);
12837
Retrieve records ends with a given beginning from the result of a join.
12839
For a given partial join record consisting of records from the tables
12840
preceding the table join_tab in the execution plan, the function
12841
retrieves all matching full records from the result set and
12842
send them to the result set stream.
12845
The function effectively implements the final (n-k) nested loops
12846
of nested loops join algorithm, where k is the ordinal number of
12847
the join_tab table and n is the total number of tables in the join query.
12848
It performs nested loops joins with all conjunctive predicates from
12849
the where condition pushed as low to the tables as possible.
12852
SELECT * FROM t1,t2,t3
12853
WHERE t1.a=t2.a AND t2.b=t3.b AND t1.a BETWEEN 5 AND 9
12855
the predicate (t1.a BETWEEN 5 AND 9) will be pushed to table t1,
12856
given the selected plan prescribes to nest retrievals of the
12857
joined tables in the following order: t1,t2,t3.
12858
A pushed down predicate are attached to the table which it pushed to,
12859
at the field join_tab->select_cond.
12860
When executing a nested loop of level k the function runs through
12861
the rows of 'join_tab' and for each row checks the pushed condition
12862
attached to the table.
12863
If it is false the function moves to the next row of the
12864
table. If the condition is true the function recursively executes (n-k-1)
12865
remaining embedded nested loops.
12866
The situation becomes more complicated if outer joins are involved in
12867
the execution plan. In this case the pushed down predicates can be
12868
checked only at certain conditions.
12869
Suppose for the query
12871
SELECT * FROM t1 LEFT JOIN (t2,t3) ON t3.a=t1.a
12872
WHERE t1>2 AND (t2.b>5 OR t2.b IS NULL)
12874
the optimizer has chosen a plan with the table order t1,t2,t3.
12875
The predicate P1=t1>2 will be pushed down to the table t1, while the
12876
predicate P2=(t2.b>5 OR t2.b IS NULL) will be attached to the table
12877
t2. But the second predicate can not be unconditionally tested right
12878
after a row from t2 has been read. This can be done only after the
12879
first row with t3.a=t1.a has been encountered.
12880
Thus, the second predicate P2 is supplied with a guarded value that are
12881
stored in the field 'found' of the first inner table for the outer join
12882
(table t2). When the first row with t3.a=t1.a for the current row
12883
of table t1 appears, the value becomes true. For now on the predicate
12884
is evaluated immediately after the row of table t2 has been read.
12885
When the first row with t3.a=t1.a has been encountered all
12886
conditions attached to the inner tables t2,t3 must be evaluated.
12887
Only when all of them are true the row is sent to the output stream.
12888
If not, the function returns to the lowest nest level that has a false
12889
attached condition.
12890
The predicates from on expressions are also pushed down. If in the
12891
the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
12892
then t1.a=t2.a would be pushed down to table t2, and without any
12894
If after the run through all rows of table t2, the first inner table
12895
for the outer join operation, it turns out that no matches are
12896
found for the current row of t1, then current row from table t1
12897
is complemented by nulls for t2 and t3. Then the pushed down predicates
12898
are checked for the composed row almost in the same way as it had
12899
been done for the first row with a match. The only difference is
12900
the predicates from on expressions are not checked.
12905
The function forms output rows for a current partial join of k
12906
tables tables recursively.
12907
For each partial join record ending with a certain row from
12908
join_tab it calls sub_select that builds all possible matching
12909
tails from the result set.
12910
To be able check predicates conditionally items of the class
12911
Item_func_trig_cond are employed.
12912
An object of this class is constructed from an item of class COND
12913
and a pointer to a guarding boolean variable.
12914
When the value of the guard variable is true the value of the object
12915
is the same as the value of the predicate, otherwise it's just returns
12917
To carry out a return to a nested loop level of join table t the pointer
12918
to t is remembered in the field 'return_tab' of the join structure.
12919
Consider the following query:
12923
(t2, t3 LEFT JOIN (t4,t5) ON t5.a=t3.a)
12925
WHERE (t2.b=5 OR t2.b IS NULL) AND (t4.b=2 OR t4.b IS NULL)
12927
Suppose the chosen execution plan dictates the order t1,t2,t3,t4,t5
12928
and suppose for a given joined rows from tables t1,t2,t3 there are
12929
no rows in the result set yet.
12930
When first row from t5 that satisfies the on condition
12931
t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
12932
becomes 'activated', as well the predicate t4.a=t2.a. But
12933
the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
12934
t4.a=t2.a becomes true.
12935
In order not to re-evaluate the predicates that were already evaluated
12936
as attached pushed down predicates, a pointer to the the first
12937
most inner unmatched table is maintained in join_tab->first_unmatched.
12938
Thus, when the first row from t5 with t5.a=t3.a is found
12939
this pointer for t5 is changed from t4 to t2.
12942
@b STRUCTURE @b NOTES
12944
join_tab->first_unmatched points always backwards to the first inner
12945
table of the embedding nested join, if any.
12947
@param join pointer to the structure providing all context info for
12949
@param join_tab the first next table of the execution plan to be retrieved
12950
@param end_records true when we need to perform final steps of retrival
12953
return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
12955
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
12957
enum_nested_loop_state
12958
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
12960
join_tab->table->null_row=0;
12961
if (end_of_records)
12962
return (*join_tab->next_select)(join,join_tab+1,end_of_records);
12965
enum_nested_loop_state rc;
12966
READ_RECORD *info= &join_tab->read_record;
12968
if (join_tab->flush_weedout_table)
12970
do_sj_reset(join_tab->flush_weedout_table);
12973
if (join->resume_nested_loop)
12975
/* If not the last table, plunge down the nested loop */
12976
if (join_tab < join->join_tab + join->tables - 1)
12977
rc= (*join_tab->next_select)(join, join_tab + 1, 0);
12980
join->resume_nested_loop= FALSE;
12981
rc= NESTED_LOOP_OK;
12986
join->return_tab= join_tab;
12988
if (join_tab->last_inner)
12990
/* join_tab is the first inner table for an outer join operation. */
12992
/* Set initial state of guard variables for this table.*/
12994
join_tab->not_null_compl= 1;
12996
/* Set first_unmatched for the last inner table of this group */
12997
join_tab->last_inner->first_unmatched= join_tab;
12999
join->thd->row_count= 0;
13001
error= (*join_tab->read_first_record)(join_tab);
13002
rc= evaluate_join_record(join, join_tab, error);
13006
Note: psergey has added the 2nd part of the following condition; the
13007
change should probably be made in 5.1, too.
13009
while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
13011
error= info->read_record(info);
13012
rc= evaluate_join_record(join, join_tab, error);
13015
if (rc == NESTED_LOOP_NO_MORE_ROWS &&
13016
join_tab->last_inner && !join_tab->found)
13017
rc= evaluate_null_complemented_join_record(join, join_tab);
13019
if (rc == NESTED_LOOP_NO_MORE_ROWS)
13020
rc= NESTED_LOOP_OK;
13028
SemiJoinDuplicateElimination: Weed out duplicate row combinations
13031
do_sj_dups_weedout()
13035
1 The row combination is a duplicate (discard it)
13036
0 The row combination is not a duplicate (continue)
13039
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl)
13042
SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
13043
SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
13044
uchar *ptr= sjtbl->tmp_table->record[0] + 1;
13045
uchar *nulls_ptr= ptr;
13047
/* Put the the rowids tuple into table->record[0]: */
13049
// 1. Store the length
13050
if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
13052
*ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
13057
int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
13061
// 2. Zero the null bytes
13062
if (sjtbl->null_bytes)
13064
bzero(ptr, sjtbl->null_bytes);
13065
ptr += sjtbl->null_bytes;
13068
// 3. Put the rowids
13069
for (uint i=0; tab != tab_end; tab++, i++)
13071
handler *h= tab->join_tab->table->file;
13072
if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
13074
/* It's a NULL-complemented row */
13075
*(nulls_ptr + tab->null_byte) |= tab->null_bit;
13076
bzero(ptr + tab->rowid_offset, h->ref_length);
13080
/* Copy the rowid value */
13081
if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
13082
h->position(tab->join_tab->table->record[0]);
13083
memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
13087
error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
13090
/* create_myisam_from_heap will generate error if needed */
13091
if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
13092
create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo,
13093
&sjtbl->recinfo, error, 1))
13095
//return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
13103
SemiJoinDuplicateElimination: Reset the temporary table
13106
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
13108
if (sj_tbl->tmp_table)
13109
return sj_tbl->tmp_table->file->ha_delete_all_rows();
13114
Process one record of the nested loop join.
13116
This function will evaluate parts of WHERE/ON clauses that are
13117
applicable to the partial record on hand and in case of success
13118
submit this record to the next level of the nested loop.
13121
static enum_nested_loop_state
13122
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
13125
bool not_used_in_distinct=join_tab->not_used_in_distinct;
13126
ha_rows found_records=join->found_records;
13127
COND *select_cond= join_tab->select_cond;
13129
if (error > 0 || (join->thd->is_error())) // Fatal error
13130
return NESTED_LOOP_ERROR;
13132
return NESTED_LOOP_NO_MORE_ROWS;
13133
if (join->thd->killed) // Aborted by user
13135
join->thd->send_kill_message();
13136
return NESTED_LOOP_KILLED; /* purecov: inspected */
13138
DBUG_PRINT("info", ("select cond 0x%lx", (ulong)select_cond));
13139
if (!select_cond || select_cond->val_int())
13142
There is no select condition or the attached pushed down
13143
condition is true => a match is found.
13146
while (join_tab->first_unmatched && found)
13149
The while condition is always false if join_tab is not
13150
the last inner join table of an outer join operation.
13152
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13154
Mark that a match for current outer table is found.
13155
This activates push down conditional predicates attached
13156
to the all inner tables of the outer join.
13158
first_unmatched->found= 1;
13159
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13161
if (tab->table->reginfo.not_exists_optimize)
13162
return NESTED_LOOP_NO_MORE_ROWS;
13163
/* Check all predicates that has just been activated. */
13165
Actually all predicates non-guarded by first_unmatched->found
13166
will be re-evaluated again. It could be fixed, but, probably,
13167
it's not worth doing now.
13169
if (tab->select_cond && !tab->select_cond->val_int())
13171
/* The condition attached to table tab is false */
13172
if (tab == join_tab)
13177
Set a return point if rejected predicate is attached
13178
not to the last table of the current nest level.
13180
join->return_tab= tab;
13181
return NESTED_LOOP_OK;
13186
Check whether join_tab is not the last inner table
13187
for another embedding outer join.
13189
if ((first_unmatched= first_unmatched->first_upper) &&
13190
first_unmatched->last_inner != join_tab)
13191
first_unmatched= 0;
13192
join_tab->first_unmatched= first_unmatched;
13195
JOIN_TAB *return_tab= join->return_tab;
13196
join_tab->found_match= TRUE;
13197
if (join_tab->check_weed_out_table)
13199
int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
13201
return NESTED_LOOP_ERROR;
13203
return NESTED_LOOP_OK;
13205
else if (join_tab->do_firstmatch)
13208
We should return to the join_tab->do_firstmatch after we have
13209
enumerated all the suffixes for current prefix row combination
13211
return_tab= join_tab->do_firstmatch;
13215
It was not just a return to lower loop level when one
13216
of the newly activated predicates is evaluated as false
13217
(See above join->return_tab= tab).
13219
join->examined_rows++;
13220
join->thd->row_count++;
13221
DBUG_PRINT("counts", ("join->examined_rows++: %lu",
13222
(ulong) join->examined_rows));
13226
enum enum_nested_loop_state rc;
13227
/* A match from join_tab is found for the current partial join. */
13228
rc= (*join_tab->next_select)(join, join_tab+1, 0);
13229
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13231
if (return_tab < join->return_tab)
13232
join->return_tab= return_tab;
13234
if (join->return_tab < join_tab)
13235
return NESTED_LOOP_OK;
13237
Test if this was a SELECT DISTINCT query on a table that
13238
was not in the field list; In this case we can abort if
13239
we found a row, as no new rows can be added to the result.
13241
if (not_used_in_distinct && found_records != join->found_records)
13242
return NESTED_LOOP_NO_MORE_ROWS;
13245
join_tab->read_record.file->unlock_row();
13250
The condition pushed down to the table join_tab rejects all rows
13251
with the beginning coinciding with the current partial join.
13253
join->examined_rows++;
13254
join->thd->row_count++;
13255
join_tab->read_record.file->unlock_row();
13257
return NESTED_LOOP_OK;
13264
Construct a NULL complimented partial join record and feed it to the next
13265
level of the nested loop. This function is used in case we have
13266
an OUTER join and no matching record was found.
13269
static enum_nested_loop_state
13270
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
13273
The table join_tab is the first inner table of a outer join operation
13274
and no matches has been found for the current outer row.
13276
JOIN_TAB *last_inner_tab= join_tab->last_inner;
13277
/* Cache variables for faster loop */
13279
for ( ; join_tab <= last_inner_tab ; join_tab++)
13281
/* Change the the values of guard predicate variables. */
13282
join_tab->found= 1;
13283
join_tab->not_null_compl= 0;
13284
/* The outer row is complemented by nulls for each inner tables */
13285
restore_record(join_tab->table,s->default_values); // Make empty record
13286
mark_as_null_row(join_tab->table); // For group by without error
13287
select_cond= join_tab->select_cond;
13288
/* Check all attached conditions for inner table rows. */
13289
if (select_cond && !select_cond->val_int())
13290
return NESTED_LOOP_OK;
13294
The row complemented by nulls might be the first row
13295
of embedding outer joins.
13296
If so, perform the same actions as in the code
13297
for the first regular outer join row above.
13301
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13302
if ((first_unmatched= first_unmatched->first_upper) &&
13303
first_unmatched->last_inner != join_tab)
13304
first_unmatched= 0;
13305
join_tab->first_unmatched= first_unmatched;
13306
if (!first_unmatched)
13308
first_unmatched->found= 1;
13309
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13311
if (tab->select_cond && !tab->select_cond->val_int())
13313
join->return_tab= tab;
13314
return NESTED_LOOP_OK;
13319
The row complemented by nulls satisfies all conditions
13320
attached to inner tables.
13321
Send the row complemented by nulls to be joined with the
13324
return (*join_tab->next_select)(join, join_tab+1, 0);
13328
static enum_nested_loop_state
13329
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13331
enum_nested_loop_state rc= NESTED_LOOP_OK;
13335
join_tab->table->null_row= 0;
13336
if (!join_tab->cache.records)
13337
return NESTED_LOOP_OK; /* Nothing to do */
13339
(void) store_record_in_cache(&join_tab->cache); // Must save this for later
13340
if (join_tab->use_quick == 2)
13342
if (join_tab->select->quick)
13343
{ /* Used quick select last. reset it */
13344
delete join_tab->select->quick;
13345
join_tab->select->quick=0;
13348
/* read through all records */
13349
if ((error=join_init_read_record(join_tab)))
13351
reset_cache_write(&join_tab->cache);
13352
return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13355
for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13357
tmp->status=tmp->table->status;
13358
tmp->table->status=0;
13361
info= &join_tab->read_record;
13364
if (join->thd->killed)
13366
join->thd->send_kill_message();
13367
return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13369
SQL_SELECT *select=join_tab->select;
13370
if (rc == NESTED_LOOP_OK &&
13371
(!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13374
reset_cache_read(&join_tab->cache);
13375
for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13377
read_cached_record(join_tab);
13378
if (!select || !select->skip_record())
13381
if (!join_tab->check_weed_out_table ||
13382
!(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13384
rc= (join_tab->next_select)(join,join_tab+1,0);
13385
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13387
reset_cache_write(&join_tab->cache);
13392
return NESTED_LOOP_ERROR;
13396
} while (!(error=info->read_record(info)));
13399
read_cached_record(join_tab); // Restore current record
13400
reset_cache_write(&join_tab->cache);
13401
if (error > 0) // Fatal error
13402
return NESTED_LOOP_ERROR; /* purecov: inspected */
13403
for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13404
tmp2->table->status=tmp2->status;
13405
return NESTED_LOOP_OK;
13409
/*****************************************************************************
13410
The different ways to read a record
13411
Returns -1 if row was not found, 0 if row was found and 1 on errors
13412
*****************************************************************************/
13414
/** Help function when we get some an error from the table handler. */
13416
int report_error(TABLE *table, int error)
13418
if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13420
table->status= STATUS_GARBAGE;
13421
return -1; // key not found; ok
13424
Locking reads can legally return also these errors, do not
13425
print them to the .err log
13427
if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13428
sql_print_error("Got error %d when reading table '%s'",
13429
error, table->s->path.str);
13430
table->file->print_error(error,MYF(0));
13435
int safe_index_read(JOIN_TAB *tab)
13438
TABLE *table= tab->table;
13439
if ((error=table->file->index_read_map(table->record[0],
13441
make_prev_keypart_map(tab->ref.key_parts),
13442
HA_READ_KEY_EXACT)))
13443
return report_error(table, error);
13449
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
13452
DBUG_ENTER("join_read_const_table");
13453
TABLE *table=tab->table;
13454
table->const_table=1;
13456
table->status=STATUS_NO_RECORD;
13458
if (tab->type == JT_SYSTEM)
13460
if ((error=join_read_system(tab)))
13461
{ // Info for DESCRIBE
13462
tab->info="const row not found";
13463
/* Mark for EXPLAIN that the row was not found */
13464
pos->records_read=0.0;
13465
pos->ref_depend_map= 0;
13466
if (!table->maybe_null || error > 0)
13467
DBUG_RETURN(error);
13472
if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13473
!table->no_keyread &&
13474
(int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
13477
table->file->extra(HA_EXTRA_KEYREAD);
13478
tab->index= tab->ref.key;
13480
error=join_read_const(tab);
13481
if (table->key_read)
13484
table->file->extra(HA_EXTRA_NO_KEYREAD);
13488
tab->info="unique row not found";
13489
/* Mark for EXPLAIN that the row was not found */
13490
pos->records_read=0.0;
13491
pos->ref_depend_map= 0;
13492
if (!table->maybe_null || error > 0)
13493
DBUG_RETURN(error);
13496
if (*tab->on_expr_ref && !table->null_row)
13498
if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
13499
mark_as_null_row(table);
13501
if (!table->null_row)
13502
table->maybe_null=0;
13504
/* Check appearance of new constant items in Item_equal objects */
13505
JOIN *join= tab->join;
13507
update_const_equal_items(join->conds, tab);
13509
for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
13511
TABLE_LIST *embedded;
13512
TABLE_LIST *embedding= tbl;
13515
embedded= embedding;
13516
if (embedded->on_expr)
13517
update_const_equal_items(embedded->on_expr, tab);
13518
embedding= embedded->embedding;
13520
while (embedding &&
13521
embedding->nested_join->join_list.head() == embedded);
13529
join_read_system(JOIN_TAB *tab)
13531
TABLE *table= tab->table;
13533
if (table->status & STATUS_GARBAGE) // If first read
13535
if ((error=table->file->read_first_row(table->record[0],
13536
table->s->primary_key)))
13538
if (error != HA_ERR_END_OF_FILE)
13539
return report_error(table, error);
13540
mark_as_null_row(tab->table);
13541
empty_record(table); // Make empty record
13544
store_record(table,record[1]);
13546
else if (!table->status) // Only happens with left join
13547
restore_record(table,record[1]); // restore old record
13549
return table->status ? -1 : 0;
13554
Read a (constant) table when there is at most one matching row.
13556
@param tab Table to read
13561
-1 Row was not found
13563
1 Got an error (other than row not found) during read
13567
join_read_const(JOIN_TAB *tab)
13570
TABLE *table= tab->table;
13571
if (table->status & STATUS_GARBAGE) // If first read
13574
if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13575
error=HA_ERR_KEY_NOT_FOUND;
13578
error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
13579
(uchar*) tab->ref.key_buff,
13580
make_prev_keypart_map(tab->ref.key_parts),
13581
HA_READ_KEY_EXACT);
13585
table->status= STATUS_NOT_FOUND;
13586
mark_as_null_row(tab->table);
13587
empty_record(table);
13588
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13589
return report_error(table, error);
13592
store_record(table,record[1]);
13594
else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
13597
restore_record(table,record[1]); // restore old record
13600
return table->status ? -1 : 0;
13605
eq_ref access method implementation: "read_first" function
13609
tab JOIN_TAB of the accessed table
13612
This is "read_fist" function for the "ref" access method. The difference
13613
from "ref" is that it has a one-element "cache" (see cmp_buffer_with_ref)
13622
join_read_key(JOIN_TAB *tab)
13625
TABLE *table= tab->table;
13627
if (!table->file->inited)
13629
table->file->ha_index_init(tab->ref.key, tab->sorted);
13632
/* TODO: Why don't we do "Late NULLs Filtering" here? */
13633
if (cmp_buffer_with_ref(tab) ||
13634
(table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
13636
if (tab->ref.key_err)
13638
table->status=STATUS_NOT_FOUND;
13641
error=table->file->index_read_map(table->record[0],
13643
make_prev_keypart_map(tab->ref.key_parts),
13644
HA_READ_KEY_EXACT);
13645
if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13646
return report_error(table, error);
13649
return table->status ? -1 : 0;
13654
ref access method implementation: "read_first" function
13657
join_read_always_key()
13658
tab JOIN_TAB of the accessed table
13661
This is "read_fist" function for the "ref" access method.
13663
The functon must leave the index initialized when it returns.
13664
ref_or_null access implementation depends on that.
13673
join_read_always_key(JOIN_TAB *tab)
13676
TABLE *table= tab->table;
13678
/* Initialize the index first */
13679
if (!table->file->inited)
13680
table->file->ha_index_init(tab->ref.key, tab->sorted);
13682
/* Perform "Late NULLs Filtering" (see internals manual for explanations) */
13683
for (uint i= 0 ; i < tab->ref.key_parts ; i++)
13685
if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
13689
if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13691
if ((error=table->file->index_read_map(table->record[0],
13693
make_prev_keypart_map(tab->ref.key_parts),
13694
HA_READ_KEY_EXACT)))
13696
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13697
return report_error(table, error);
13698
return -1; /* purecov: inspected */
13705
This function is used when optimizing away ORDER BY in
13706
SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
13710
join_read_last_key(JOIN_TAB *tab)
13713
TABLE *table= tab->table;
13715
if (!table->file->inited)
13716
table->file->ha_index_init(tab->ref.key, tab->sorted);
13717
if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13719
if ((error=table->file->index_read_last_map(table->record[0],
13721
make_prev_keypart_map(tab->ref.key_parts))))
13723
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13724
return report_error(table, error);
13725
return -1; /* purecov: inspected */
13733
join_no_more_records(READ_RECORD *info __attribute__((unused)))
13739
join_read_next_same_diff(READ_RECORD *info)
13741
TABLE *table= info->table;
13742
JOIN_TAB *tab=table->reginfo.join_tab;
13743
if (tab->insideout_match_tab->found_match)
13745
KEY *key= tab->table->key_info + tab->index;
13749
/* Save index tuple from record to the buffer */
13750
key_copy(tab->insideout_buf, info->record, key, 0);
13752
if ((error=table->file->index_next_same(table->record[0],
13754
tab->ref.key_length)))
13756
if (error != HA_ERR_END_OF_FILE)
13757
return report_error(table, error);
13758
table->status= STATUS_GARBAGE;
13761
} while (!key_cmp(tab->table->key_info[tab->index].key_part,
13762
tab->insideout_buf, key->key_length));
13763
tab->insideout_match_tab->found_match= 0;
13767
return join_read_next_same(info);
13771
join_read_next_same(READ_RECORD *info)
13774
TABLE *table= info->table;
13775
JOIN_TAB *tab=table->reginfo.join_tab;
13777
if ((error=table->file->index_next_same(table->record[0],
13779
tab->ref.key_length)))
13781
if (error != HA_ERR_END_OF_FILE)
13782
return report_error(table, error);
13783
table->status= STATUS_GARBAGE;
13791
join_read_prev_same(READ_RECORD *info)
13794
TABLE *table= info->table;
13795
JOIN_TAB *tab=table->reginfo.join_tab;
13797
if ((error=table->file->index_prev(table->record[0])))
13798
return report_error(table, error);
13799
if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
13800
tab->ref.key_length))
13802
table->status=STATUS_NOT_FOUND;
13810
join_init_quick_read_record(JOIN_TAB *tab)
13812
if (test_if_quick_select(tab) == -1)
13813
return -1; /* No possible records */
13814
return join_init_read_record(tab);
13818
int rr_sequential(READ_RECORD *info);
13819
int init_read_record_seq(JOIN_TAB *tab)
13821
tab->read_record.read_record= rr_sequential;
13822
if (tab->read_record.file->ha_rnd_init(1))
13824
return (*tab->read_record.read_record)(&tab->read_record);
13828
test_if_quick_select(JOIN_TAB *tab)
13830
delete tab->select->quick;
13831
tab->select->quick=0;
13832
return tab->select->test_quick_select(tab->join->thd, tab->keys,
13833
(table_map) 0, HA_POS_ERROR, 0,
13839
join_init_read_record(JOIN_TAB *tab)
13841
if (tab->select && tab->select->quick && tab->select->quick->reset())
13843
init_read_record(&tab->read_record, tab->join->thd, tab->table,
13845
return (*tab->read_record.read_record)(&tab->read_record);
13850
join_read_first(JOIN_TAB *tab)
13853
TABLE *table=tab->table;
13854
if (!table->key_read && table->covering_keys.is_set(tab->index) &&
13855
!table->no_keyread)
13858
table->file->extra(HA_EXTRA_KEYREAD);
13860
tab->table->status=0;
13861
tab->read_record.table=table;
13862
tab->read_record.file=table->file;
13863
tab->read_record.index=tab->index;
13864
tab->read_record.record=table->record[0];
13865
if (tab->insideout_match_tab)
13867
tab->read_record.do_insideout_scan= tab;
13868
tab->read_record.read_record=join_read_next_different;
13869
tab->insideout_match_tab->found_match= 0;
13873
tab->read_record.read_record=join_read_next;
13874
tab->read_record.do_insideout_scan= 0;
13877
if (!table->file->inited)
13878
table->file->ha_index_init(tab->index, tab->sorted);
13879
if ((error=tab->table->file->index_first(tab->table->record[0])))
13881
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13882
report_error(table, error);
13890
join_read_next_different(READ_RECORD *info)
13892
JOIN_TAB *tab= info->do_insideout_scan;
13893
if (tab->insideout_match_tab->found_match)
13895
KEY *key= tab->table->key_info + tab->index;
13899
/* Save index tuple from record to the buffer */
13900
key_copy(tab->insideout_buf, info->record, key, 0);
13902
if ((error=info->file->index_next(info->record)))
13903
return report_error(info->table, error);
13905
} while (!key_cmp(tab->table->key_info[tab->index].key_part,
13906
tab->insideout_buf, key->key_length));
13907
tab->insideout_match_tab->found_match= 0;
13911
return join_read_next(info);
13916
join_read_next(READ_RECORD *info)
13919
if ((error=info->file->index_next(info->record)))
13920
return report_error(info->table, error);
13926
join_read_last(JOIN_TAB *tab)
13928
TABLE *table=tab->table;
13930
if (!table->key_read && table->covering_keys.is_set(tab->index) &&
13931
!table->no_keyread)
13934
table->file->extra(HA_EXTRA_KEYREAD);
13936
tab->table->status=0;
13937
tab->read_record.read_record=join_read_prev;
13938
tab->read_record.table=table;
13939
tab->read_record.file=table->file;
13940
tab->read_record.index=tab->index;
13941
tab->read_record.record=table->record[0];
13942
if (!table->file->inited)
13943
table->file->ha_index_init(tab->index, 1);
13944
if ((error= tab->table->file->index_last(tab->table->record[0])))
13945
return report_error(table, error);
13951
join_read_prev(READ_RECORD *info)
13954
if ((error= info->file->index_prev(info->record)))
13955
return report_error(info->table, error);
13960
Reading of key with key reference and one part that may be NULL.
13964
join_read_always_key_or_null(JOIN_TAB *tab)
13968
/* First read according to key which is NOT NULL */
13969
*tab->ref.null_ref_key= 0; // Clear null byte
13970
if ((res= join_read_always_key(tab)) >= 0)
13973
/* Then read key with null value */
13974
*tab->ref.null_ref_key= 1; // Set null byte
13975
return safe_index_read(tab);
13980
join_read_next_same_or_null(READ_RECORD *info)
13983
if ((error= join_read_next_same(info)) >= 0)
13985
JOIN_TAB *tab= info->table->reginfo.join_tab;
13987
/* Test if we have already done a read after null key */
13988
if (*tab->ref.null_ref_key)
13989
return -1; // All keys read
13990
*tab->ref.null_ref_key= 1; // Set null byte
13991
return safe_index_read(tab); // then read null keys
13995
/*****************************************************************************
13997
Functions that end one nested loop iteration. Different functions
13998
are used to support GROUP BY clause and to redirect records
13999
to a table (e.g. in case of SELECT into a temporary table) or to the
14003
NESTED_LOOP_OK - the record has been successfully handled
14004
NESTED_LOOP_ERROR - a fatal error (like table corruption)
14006
NESTED_LOOP_KILLED - thread shutdown was requested while processing
14008
NESTED_LOOP_QUERY_LIMIT - the record has been successfully handled;
14009
additionally, the nested loop produced the
14010
number of rows specified in the LIMIT clause
14012
NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
14013
additionally, there is a cursor and the nested
14014
loop algorithm produced the number of rows
14015
that is specified for current cursor fetch
14017
All return values except NESTED_LOOP_OK abort the nested loop.
14018
*****************************************************************************/
14021
static enum_nested_loop_state
14022
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14023
bool end_of_records)
14025
DBUG_ENTER("end_send");
14026
if (!end_of_records)
14029
if (join->having && join->having->val_int() == 0)
14030
DBUG_RETURN(NESTED_LOOP_OK); // Didn't match having
14032
if (join->do_send_rows)
14033
error=join->result->send_data(*join->fields);
14035
DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14036
if (++join->send_records >= join->unit->select_limit_cnt &&
14037
join->do_send_rows)
14039
if (join->select_options & OPTION_FOUND_ROWS)
14041
JOIN_TAB *jt=join->join_tab;
14042
if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
14043
&& !join->send_group_parts && !join->having && !jt->select_cond &&
14044
!(jt->select && jt->select->quick) &&
14045
(jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
14048
/* Join over all rows in table; Return number of found rows */
14049
TABLE *table=jt->table;
14051
join->select_options ^= OPTION_FOUND_ROWS;
14052
if (table->sort.record_pointers ||
14053
(table->sort.io_cache && my_b_inited(table->sort.io_cache)))
14055
/* Using filesort */
14056
join->send_records= table->sort.found_records;
14060
table->file->info(HA_STATUS_VARIABLE);
14061
join->send_records= table->file->stats.records;
14066
join->do_send_rows= 0;
14067
if (join->unit->fake_select_lex)
14068
join->unit->fake_select_lex->select_limit= 0;
14069
DBUG_RETURN(NESTED_LOOP_OK);
14072
DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
14074
else if (join->send_records >= join->fetch_limit)
14077
There is a server side cursor and all rows for
14078
this fetch request are sent.
14080
DBUG_RETURN(NESTED_LOOP_CURSOR_LIMIT);
14084
DBUG_RETURN(NESTED_LOOP_OK);
14089
enum_nested_loop_state
14090
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14091
bool end_of_records)
14094
enum_nested_loop_state ok_code= NESTED_LOOP_OK;
14095
DBUG_ENTER("end_send_group");
14097
if (!join->first_record || end_of_records ||
14098
(idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14100
if (join->first_record ||
14101
(end_of_records && !join->group && !join->group_optimized_away))
14103
if (idx < (int) join->send_group_parts)
14107
if (!join->first_record)
14109
List_iterator_fast<Item> it(*join->fields);
14111
/* No matching rows for group function */
14114
while ((item= it++))
14115
item->no_rows_in_result();
14117
if (join->having && join->having->val_int() == 0)
14118
error= -1; // Didn't satisfy having
14121
if (join->do_send_rows)
14122
error=join->result->send_data(*join->fields) ? 1 : 0;
14123
join->send_records++;
14125
if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
14127
if (join->rollup_send_data((uint) (idx+1)))
14132
DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14133
if (end_of_records)
14134
DBUG_RETURN(NESTED_LOOP_OK);
14135
if (join->send_records >= join->unit->select_limit_cnt &&
14136
join->do_send_rows)
14138
if (!(join->select_options & OPTION_FOUND_ROWS))
14139
DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
14140
join->do_send_rows=0;
14141
join->unit->select_limit_cnt = HA_POS_ERROR;
14143
else if (join->send_records >= join->fetch_limit)
14146
There is a server side cursor and all rows
14147
for this fetch request are sent.
14150
Preventing code duplication. When finished with the group reset
14151
the group functions and copy_fields. We fall through. bug #11904
14153
ok_code= NESTED_LOOP_CURSOR_LIMIT;
14159
if (end_of_records)
14160
DBUG_RETURN(NESTED_LOOP_OK);
14161
join->first_record=1;
14162
VOID(test_if_item_cache_changed(join->group_fields));
14164
if (idx < (int) join->send_group_parts)
14167
This branch is executed also for cursors which have finished their
14168
fetch limit - the reason for ok_code.
14170
copy_fields(&join->tmp_table_param);
14171
if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14172
DBUG_RETURN(NESTED_LOOP_ERROR);
14173
DBUG_RETURN(ok_code);
14176
if (update_sum_func(join->sum_funcs))
14177
DBUG_RETURN(NESTED_LOOP_ERROR);
14178
DBUG_RETURN(NESTED_LOOP_OK);
14183
enum_nested_loop_state
14184
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14185
bool end_of_records)
14187
TABLE *table=join->tmp_table;
14188
DBUG_ENTER("end_write");
14190
if (join->thd->killed) // Aborted by user
14192
join->thd->send_kill_message();
14193
DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */
14195
if (!end_of_records)
14197
copy_fields(&join->tmp_table_param);
14198
copy_funcs(join->tmp_table_param.items_to_copy);
14199
#ifdef TO_BE_DELETED
14200
if (!table->uniques) // If not unique handling
14202
/* Copy null values from group to row */
14204
for (group=table->group ; group ; group=group->next)
14206
Item *item= *group->item;
14207
if (item->maybe_null)
14209
Field *field=item->get_tmp_table_field();
14210
field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
14215
if (!join->having || join->having->val_int())
14218
join->found_records++;
14219
if ((error=table->file->ha_write_row(table->record[0])))
14221
if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
14223
if (create_myisam_from_heap(join->thd, table,
14224
join->tmp_table_param.start_recinfo,
14225
&join->tmp_table_param.recinfo,
14227
DBUG_RETURN(NESTED_LOOP_ERROR); // Not a table_is_full error
14228
table->s->uniques=0; // To ensure rows are the same
14230
if (++join->send_records >= join->tmp_table_param.end_write_records &&
14231
join->do_send_rows)
14233
if (!(join->select_options & OPTION_FOUND_ROWS))
14234
DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
14235
join->do_send_rows=0;
14236
join->unit->select_limit_cnt = HA_POS_ERROR;
14237
DBUG_RETURN(NESTED_LOOP_OK);
14242
DBUG_RETURN(NESTED_LOOP_OK);
14246
/** Group by searching after group record and updating it if possible. */
14248
static enum_nested_loop_state
14249
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14250
bool end_of_records)
14252
TABLE *table=join->tmp_table;
14255
DBUG_ENTER("end_update");
14257
if (end_of_records)
14258
DBUG_RETURN(NESTED_LOOP_OK);
14259
if (join->thd->killed) // Aborted by user
14261
join->thd->send_kill_message();
14262
DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */
14265
join->found_records++;
14266
copy_fields(&join->tmp_table_param); // Groups are copied twice.
14267
/* Make a key of group index */
14268
for (group=table->group ; group ; group=group->next)
14270
Item *item= *group->item;
14271
item->save_org_in_field(group->field);
14272
/* Store in the used key if the field was 0 */
14273
if (item->maybe_null)
14274
group->buff[-1]= (char) group->field->is_null();
14276
if (!table->file->index_read_map(table->record[1],
14277
join->tmp_table_param.group_buff,
14279
HA_READ_KEY_EXACT))
14280
{ /* Update old record */
14281
restore_record(table,record[1]);
14282
update_tmptable_sum_func(join->sum_funcs,table);
14283
if ((error=table->file->ha_update_row(table->record[1],
14284
table->record[0])))
14286
table->file->print_error(error,MYF(0)); /* purecov: inspected */
14287
DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14289
DBUG_RETURN(NESTED_LOOP_OK);
14293
Copy null bits from group key to table
14294
We can't copy all data as the key may have different format
14295
as the row data (for example as with VARCHAR keys)
14297
KEY_PART_INFO *key_part;
14298
for (group=table->group,key_part=table->key_info[0].key_part;
14300
group=group->next,key_part++)
14302
if (key_part->null_bit)
14303
memcpy(table->record[0]+key_part->offset, group->buff, 1);
14305
init_tmptable_sum_functions(join->sum_funcs);
14306
copy_funcs(join->tmp_table_param.items_to_copy);
14307
if ((error=table->file->ha_write_row(table->record[0])))
14309
if (create_myisam_from_heap(join->thd, table,
14310
join->tmp_table_param.start_recinfo,
14311
&join->tmp_table_param.recinfo,
14313
DBUG_RETURN(NESTED_LOOP_ERROR); // Not a table_is_full error
14314
/* Change method to update rows */
14315
table->file->ha_index_init(0, 0);
14316
join->join_tab[join->tables-1].next_select=end_unique_update;
14318
join->send_records++;
14319
DBUG_RETURN(NESTED_LOOP_OK);
14323
/** Like end_update, but this is done with unique constraints instead of keys. */
14325
static enum_nested_loop_state
14326
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14327
bool end_of_records)
14329
TABLE *table=join->tmp_table;
14331
DBUG_ENTER("end_unique_update");
14333
if (end_of_records)
14334
DBUG_RETURN(NESTED_LOOP_OK);
14335
if (join->thd->killed) // Aborted by user
14337
join->thd->send_kill_message();
14338
DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */
14341
init_tmptable_sum_functions(join->sum_funcs);
14342
copy_fields(&join->tmp_table_param); // Groups are copied twice.
14343
copy_funcs(join->tmp_table_param.items_to_copy);
14345
if (!(error=table->file->ha_write_row(table->record[0])))
14346
join->send_records++; // New group
14349
if ((int) table->file->get_dup_key(error) < 0)
14351
table->file->print_error(error,MYF(0)); /* purecov: inspected */
14352
DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14354
if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
14356
table->file->print_error(error,MYF(0)); /* purecov: inspected */
14357
DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14359
restore_record(table,record[1]);
14360
update_tmptable_sum_func(join->sum_funcs,table);
14361
if ((error=table->file->ha_update_row(table->record[1],
14362
table->record[0])))
14364
table->file->print_error(error,MYF(0)); /* purecov: inspected */
14365
DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14368
DBUG_RETURN(NESTED_LOOP_OK);
14373
enum_nested_loop_state
14374
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14375
bool end_of_records)
14377
TABLE *table=join->tmp_table;
14379
DBUG_ENTER("end_write_group");
14381
if (join->thd->killed)
14382
{ // Aborted by user
14383
join->thd->send_kill_message();
14384
DBUG_RETURN(NESTED_LOOP_KILLED); /* purecov: inspected */
14386
if (!join->first_record || end_of_records ||
14387
(idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14389
if (join->first_record || (end_of_records && !join->group))
14391
int send_group_parts= join->send_group_parts;
14392
if (idx < send_group_parts)
14394
if (!join->first_record)
14396
/* No matching rows for group function */
14399
copy_sum_funcs(join->sum_funcs,
14400
join->sum_funcs_end[send_group_parts]);
14401
if (!join->having || join->having->val_int())
14403
int error= table->file->ha_write_row(table->record[0]);
14404
if (error && create_myisam_from_heap(join->thd, table,
14405
join->tmp_table_param.start_recinfo,
14406
&join->tmp_table_param.recinfo,
14408
DBUG_RETURN(NESTED_LOOP_ERROR);
14410
if (join->rollup.state != ROLLUP::STATE_NONE)
14412
if (join->rollup_write_data((uint) (idx+1), table))
14413
DBUG_RETURN(NESTED_LOOP_ERROR);
14415
if (end_of_records)
14416
DBUG_RETURN(NESTED_LOOP_OK);
14421
if (end_of_records)
14422
DBUG_RETURN(NESTED_LOOP_OK);
14423
join->first_record=1;
14424
VOID(test_if_item_cache_changed(join->group_fields));
14426
if (idx < (int) join->send_group_parts)
14428
copy_fields(&join->tmp_table_param);
14429
copy_funcs(join->tmp_table_param.items_to_copy);
14430
if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14431
DBUG_RETURN(NESTED_LOOP_ERROR);
14432
DBUG_RETURN(NESTED_LOOP_OK);
14435
if (update_sum_func(join->sum_funcs))
14436
DBUG_RETURN(NESTED_LOOP_ERROR);
14437
DBUG_RETURN(NESTED_LOOP_OK);
14441
/*****************************************************************************
14442
Remove calculation with tables that aren't yet read. Remove also tests
14443
against fields that are read through key where the table is not a
14445
We can't remove tests that are made against columns which are stored
14447
*****************************************************************************/
14451
1 if right_item is used removable reference key on left_item
14454
static bool test_if_ref(Item_field *left_item,Item *right_item)
14456
Field *field=left_item->field;
14457
// No need to change const test. We also have to keep tests on LEFT JOIN
14458
if (!field->table->const_table && !field->table->maybe_null)
14460
Item *ref_item=part_of_refkey(field->table,field);
14461
if (ref_item && ref_item->eq(right_item,1))
14463
right_item= right_item->real_item();
14464
if (right_item->type() == Item::FIELD_ITEM)
14465
return (field->eq_def(((Item_field *) right_item)->field));
14466
/* remove equalities injected by IN->EXISTS transformation */
14467
else if (right_item->type() == Item::CACHE_ITEM)
14468
return ((Item_cache *)right_item)->eq_def (field);
14469
if (right_item->const_item() && !(right_item->is_null()))
14472
We can remove binary fields and numerical fields except float,
14473
as float comparison isn't 100 % secure
14474
We have to keep normal strings to be able to check for end spaces
14476
sergefp: the above seems to be too restrictive. Counterexample:
14477
create table t100 (v varchar(10), key(v)) default charset=latin1;
14478
insert into t100 values ('a'),('a ');
14479
explain select * from t100 where v='a';
14480
The EXPLAIN shows 'using Where'. Running the query returns both
14481
rows, so it seems there are no problems with endspace in the most
14484
if (field->binary() &&
14485
field->real_type() != MYSQL_TYPE_STRING &&
14486
field->real_type() != MYSQL_TYPE_VARCHAR &&
14487
(field->type() != MYSQL_TYPE_FLOAT || field->decimals() == 0))
14489
return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
14494
return 0; // keep test
14498
@brief Replaces an expression destructively inside the expression tree of
14501
@note Because of current requirements for semijoin flattening, we do not
14502
need to recurse here, hence this function will only examine the top-level
14503
AND conditions. (see JOIN::prepare, comment above the line
14504
'if (do_materialize)'
14506
@param join The top-level query.
14507
@param old_cond The expression to be replaced.
14508
@param new_cond The expression to be substituted.
14509
@param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
14510
the new expression.
14511
@return <code>true</code> if there was an error, <code>false</code> if
14514
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
14515
Item *new_cond, bool do_fix_fields)
14517
if (join->conds == old_cond) {
14518
join->conds= new_cond;
14520
new_cond->fix_fields(join->thd, &join->conds);
14524
if (join->conds->type() == Item::COND_ITEM) {
14525
List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
14527
while ((item= li++))
14528
if (item == old_cond)
14530
li.replace(new_cond);
14532
new_cond->fix_fields(join->thd, li.ref());
14541
Extract a condition that can be checked after reading given table
14544
make_cond_for_table()
14545
cond Condition to analyze
14546
tables Tables for which "current field values" are available
14547
used_table Table that we're extracting the condition for (may
14548
also include PSEUDO_TABLE_BITS
14551
Extract the condition that can be checked after reading the table
14552
specified in 'used_table', given that current-field values for tables
14553
specified in 'tables' bitmap are available.
14555
The function assumes that
14556
- Constant parts of the condition has already been checked.
14557
- Condition that could be checked for tables in 'tables' has already
14560
The function takes into account that some parts of the condition are
14561
guaranteed to be true by employed 'ref' access methods (the code that
14562
does this is located at the end, search down for "EQ_FUNC").
14566
make_cond_for_info_schema uses similar algorithm
14569
Extracted condition
14573
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
14574
bool exclude_expensive_cond)
14576
if (used_table && !(cond->used_tables() & used_table) &&
14578
Exclude constant conditions not checked at optimization time if
14579
the table we are pushing conditions to is the first one.
14580
As a result, such conditions are not considered as already checked
14581
and will be checked at execution time, attached to the first table.
14583
!((used_table & 1) && cond->is_expensive()))
14584
return (COND*) 0; // Already checked
14585
if (cond->type() == Item::COND_ITEM)
14587
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
14589
/* Create new top level AND item */
14590
Item_cond_and *new_cond=new Item_cond_and;
14592
return (COND*) 0; // OOM /* purecov: inspected */
14593
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14595
while ((item=li++))
14597
Item *fix=make_cond_for_table(item,tables,used_table,
14598
exclude_expensive_cond);
14600
new_cond->argument_list()->push_back(fix);
14602
switch (new_cond->argument_list()->elements) {
14604
return (COND*) 0; // Always true
14606
return new_cond->argument_list()->head();
14609
Item_cond_and do not need fix_fields for execution, its parameters
14610
are fixed or do not need fix_fields, too
14612
new_cond->quick_fix_field();
14613
new_cond->used_tables_cache=
14614
((Item_cond_and*) cond)->used_tables_cache &
14621
Item_cond_or *new_cond=new Item_cond_or;
14623
return (COND*) 0; // OOM /* purecov: inspected */
14624
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14626
while ((item=li++))
14628
Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
14630
return (COND*) 0; // Always true
14631
new_cond->argument_list()->push_back(fix);
14634
Item_cond_and do not need fix_fields for execution, its parameters
14635
are fixed or do not need fix_fields, too
14637
new_cond->quick_fix_field();
14638
new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
14639
new_cond->top_level_item();
14645
Because the following test takes a while and it can be done
14646
table_count times, we mark each item that we have examined with the result
14650
if (cond->marker == 3 || (cond->used_tables() & ~tables) ||
14652
When extracting constant conditions, treat expensive conditions as
14653
non-constant, so that they are not evaluated at optimization time.
14655
(!used_table && exclude_expensive_cond && cond->is_expensive()))
14656
return (COND*) 0; // Can't check this yet
14657
if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
14658
return cond; // Not boolean op
14661
Remove equalities that are guaranteed to be true by use of 'ref' access
14664
if (((Item_func*) cond)->functype() == Item_func::EQ_FUNC)
14666
Item *left_item= ((Item_func*) cond)->arguments()[0];
14667
Item *right_item= ((Item_func*) cond)->arguments()[1];
14668
if (left_item->type() == Item::FIELD_ITEM &&
14669
test_if_ref((Item_field*) left_item,right_item))
14671
cond->marker=3; // Checked when read
14674
if (right_item->type() == Item::FIELD_ITEM &&
14675
test_if_ref((Item_field*) right_item,left_item))
14677
cond->marker=3; // Checked when read
14687
part_of_refkey(TABLE *table,Field *field)
14689
if (!table->reginfo.join_tab)
14690
return (Item*) 0; // field from outer non-select (UPDATE,...)
14692
uint ref_parts=table->reginfo.join_tab->ref.key_parts;
14695
KEY_PART_INFO *key_part=
14696
table->key_info[table->reginfo.join_tab->ref.key].key_part;
14699
for (part=0 ; part < ref_parts ; part++)
14701
if (table->reginfo.join_tab->ref.cond_guards[part])
14705
for (part=0 ; part < ref_parts ; part++,key_part++)
14706
if (field->eq(key_part->field) &&
14707
!(key_part->key_part_flag & HA_PART_KEY_SEG))
14708
return table->reginfo.join_tab->ref.items[part];
14715
Test if one can use the key to resolve ORDER BY.
14717
@param order Sort order
14718
@param table Table to sort
14719
@param idx Index to check
14720
@param used_key_parts Return value for used key parts.
14724
used_key_parts is set to correct key parts used if return value != 0
14725
(On other cases, used_key_part may be changed)
14730
0 Key can't be used
14732
-1 Reverse key can be used
14735
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
14736
uint *used_key_parts)
14738
KEY_PART_INFO *key_part,*key_part_end;
14739
key_part=table->key_info[idx].key_part;
14740
key_part_end=key_part+table->key_info[idx].key_parts;
14741
key_part_map const_key_parts=table->const_key_parts[idx];
14743
my_bool on_primary_key= FALSE;
14744
DBUG_ENTER("test_if_order_by_key");
14746
for (; order ; order=order->next, const_key_parts>>=1)
14748
Field *field=((Item_field*) (*order->item)->real_item())->field;
14752
Skip key parts that are constants in the WHERE clause.
14753
These are already skipped in the ORDER BY by const_expression_in_where()
14755
for (; const_key_parts & 1 ; const_key_parts>>= 1)
14758
if (key_part == key_part_end)
14761
We are at the end of the key. Check if the engine has the primary
14762
key as a suffix to the secondary keys. If it has continue to check
14763
the primary key as a suffix.
14765
if (!on_primary_key &&
14766
(table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
14767
table->s->primary_key != MAX_KEY)
14769
on_primary_key= TRUE;
14770
key_part= table->key_info[table->s->primary_key].key_part;
14771
key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
14772
const_key_parts=table->const_key_parts[table->s->primary_key];
14774
for (; const_key_parts & 1 ; const_key_parts>>= 1)
14777
The primary and secondary key parts were all const (i.e. there's
14778
one row). The sorting doesn't matter.
14780
if (key_part == key_part_end && reverse == 0)
14787
if (key_part->field != field)
14790
/* set flag to 1 if we can use read-next on key, else to -1 */
14791
flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
14793
if (reverse && flag != reverse)
14795
reverse=flag; // Remember if reverse
14798
*used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
14799
(uint) (key_part - table->key_info[idx].key_part);
14800
if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
14802
reverse= 0; // Index can't be used
14803
DBUG_RETURN(reverse);
14807
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
14809
uint min_length= (uint) ~0;
14810
uint best= MAX_KEY;
14811
if (!usable_keys->is_clear_all())
14813
for (uint nr=0; nr < table->s->keys ; nr++)
14815
if (usable_keys->is_set(nr))
14817
if (table->key_info[nr].key_length < min_length)
14819
min_length=table->key_info[nr].key_length;
14829
Test if a second key is the subkey of the first one.
14831
@param key_part First key parts
14832
@param ref_key_part Second key parts
14833
@param ref_key_part_end Last+1 part of the second key
14836
Second key MUST be shorter than the first one.
14845
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
14846
KEY_PART_INFO *ref_key_part_end)
14848
for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
14849
if (!key_part->field->eq(ref_key_part->field))
14855
Test if we can use one of the 'usable_keys' instead of 'ref' key
14858
@param ref Number of key, used for WHERE clause
14859
@param usable_keys Keys for testing
14862
- MAX_KEY If we can't use other key
14863
- the number of found key Otherwise
14867
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
14868
const key_map *usable_keys)
14871
uint min_length= (uint) ~0;
14872
uint best= MAX_KEY;
14874
KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
14875
KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
14877
for (nr= 0 ; nr < table->s->keys ; nr++)
14879
if (usable_keys->is_set(nr) &&
14880
table->key_info[nr].key_length < min_length &&
14881
table->key_info[nr].key_parts >= ref_key_parts &&
14882
is_subkey(table->key_info[nr].key_part, ref_key_part,
14883
ref_key_part_end) &&
14884
test_if_order_by_key(order, table, nr, ¬_used))
14886
min_length= table->key_info[nr].key_length;
14895
Check if GROUP BY/DISTINCT can be optimized away because the set is
14896
already known to be distinct.
14898
Used in removing the GROUP BY/DISTINCT of the following types of
14901
SELECT [DISTINCT] <unique_key_cols>... FROM <single_table_ref>
14902
[GROUP BY <unique_key_cols>,...]
14905
If (a,b,c is distinct)
14906
then <any combination of a,b,c>,{whatever} is also distinct
14908
This function checks if all the key parts of any of the unique keys
14909
of the table are referenced by a list : either the select list
14910
through find_field_in_item_list or GROUP BY list through
14911
find_field_in_order_list.
14912
If the above holds and the key parts cannot contain NULLs then we
14913
can safely remove the GROUP BY/DISTINCT,
14914
as no result set can be more distinct than an unique key.
14916
@param table The table to operate on.
14917
@param find_func function to iterate over the list and search
14927
list_contains_unique_index(TABLE *table,
14928
bool (*find_func) (Field *, void *), void *data)
14930
for (uint keynr= 0; keynr < table->s->keys; keynr++)
14932
if (keynr == table->s->primary_key ||
14933
(table->key_info[keynr].flags & HA_NOSAME))
14935
KEY *keyinfo= table->key_info + keynr;
14936
KEY_PART_INFO *key_part, *key_part_end;
14938
for (key_part=keyinfo->key_part,
14939
key_part_end=key_part+ keyinfo->key_parts;
14940
key_part < key_part_end;
14943
if (key_part->field->maybe_null() ||
14944
!find_func(key_part->field, data))
14947
if (key_part == key_part_end)
14956
Helper function for list_contains_unique_index.
14957
Find a field reference in a list of ORDER structures.
14958
Finds a direct reference of the Field in the list.
14960
@param field The field to search for.
14961
@param data ORDER *.The list to search in
14970
find_field_in_order_list (Field *field, void *data)
14972
ORDER *group= (ORDER *) data;
14973
bool part_found= 0;
14974
for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
14976
Item *item= (*tmp_group->item)->real_item();
14977
if (item->type() == Item::FIELD_ITEM &&
14978
((Item_field*) item)->field->eq(field))
14989
Helper function for list_contains_unique_index.
14990
Find a field reference in a dynamic list of Items.
14991
Finds a direct reference of the Field in the list.
14993
@param[in] field The field to search for.
14994
@param[in] data List<Item> *.The list to search in
15003
find_field_in_item_list (Field *field, void *data)
15005
List<Item> *fields= (List<Item> *) data;
15006
bool part_found= 0;
15007
List_iterator<Item> li(*fields);
15010
while ((item= li++))
15012
if (item->type() == Item::FIELD_ITEM &&
15013
((Item_field*) item)->field->eq(field))
15024
Test if we can skip the ORDER BY by using an index.
15027
test_if_skip_sort_order()
15034
If we can use an index, the JOIN_TAB / tab->select struct
15035
is changed to use the index.
15037
The index must cover all fields in <order>, or it will not be considered.
15040
- sergeyp: Results of all index merge selects actually are ordered
15041
by clustered PK values.
15044
0 We have to use filesort to do the sorting
15046
1 We can use an index.
15050
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
15051
bool no_changes, const key_map *map)
15054
uint ref_key_parts;
15055
int order_direction;
15056
uint used_key_parts;
15057
TABLE *table=tab->table;
15058
SQL_SELECT *select=tab->select;
15059
key_map usable_keys;
15060
QUICK_SELECT_I *save_quick= 0;
15061
DBUG_ENTER("test_if_skip_sort_order");
15064
Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
15065
been taken into account.
15069
for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
15071
Item *item= (*tmp_order->item)->real_item();
15072
if (item->type() != Item::FIELD_ITEM)
15074
usable_keys.clear_all();
15077
usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
15078
if (usable_keys.is_clear_all())
15079
DBUG_RETURN(0); // No usable keys
15083
/* Test if constant range in WHERE */
15084
if (tab->ref.key >= 0 && tab->ref.key_parts)
15086
ref_key= tab->ref.key;
15087
ref_key_parts= tab->ref.key_parts;
15088
if (tab->type == JT_REF_OR_NULL)
15091
else if (select && select->quick) // Range found by opt_range
15093
int quick_type= select->quick->get_type();
15094
save_quick= select->quick;
15096
assume results are not ordered when index merge is used
15097
TODO: sergeyp: Results of all index merge selects actually are ordered
15098
by clustered PK values.
15101
if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
15102
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
15103
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
15105
ref_key= select->quick->index;
15106
ref_key_parts= select->quick->used_key_parts;
15112
We come here when there is a REF key.
15114
if (!usable_keys.is_set(ref_key))
15117
We come here when ref_key is not among usable_keys
15121
If using index only read, only consider other possible index only
15124
if (table->covering_keys.is_set(ref_key))
15125
usable_keys.intersect(table->covering_keys);
15126
if (tab->pre_idx_push_select_cond)
15127
tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
15128
if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
15129
&usable_keys)) < MAX_KEY)
15131
/* Found key that can be used to retrieve data in sorted order */
15132
if (tab->ref.key >= 0)
15135
We'll use ref access method on key new_ref_key. In general case
15136
the index search tuple for new_ref_key will be different (e.g.
15137
when one index is defined as (part1, part2, ...) and another as
15138
(part1, part2(N), ...) and the WHERE clause contains
15139
"part1 = const1 AND part2=const2".
15140
So we build tab->ref from scratch here.
15142
KEYUSE *keyuse= tab->keyuse;
15143
while (keyuse->key != new_ref_key && keyuse->table == tab->table)
15146
if (create_ref_for_key(tab->join, tab, keyuse,
15147
tab->join->const_table_map))
15153
The range optimizer constructed QUICK_RANGE for ref_key, and
15154
we want to use instead new_ref_key as the index. We can't
15155
just change the index of the quick select, because this may
15156
result in an incosistent QUICK_SELECT object. Below we
15157
create a new QUICK_SELECT from scratch so that all its
15158
parameres are set correctly by the range optimizer.
15160
key_map new_ref_key_map;
15161
new_ref_key_map.clear_all(); // Force the creation of quick select
15162
new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
15164
if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
15165
(tab->join->select_options &
15166
OPTION_FOUND_ROWS) ?
15168
tab->join->unit->select_limit_cnt,0,
15173
ref_key= new_ref_key;
15176
/* Check if we get the rows in requested sorted order by using the key */
15177
if (usable_keys.is_set(ref_key) &&
15178
(order_direction= test_if_order_by_key(order,table,ref_key,
15180
goto check_reverse_order;
15184
Check whether there is an index compatible with the given order
15185
usage of which is cheaper than usage of the ref_key index (ref_key>=0)
15187
It may be the case if ORDER/GROUP BY is used with LIMIT.
15191
uint best_key_parts= 0;
15192
int best_key_direction= 0;
15193
ha_rows best_records= 0;
15196
bool is_best_covering= FALSE;
15198
JOIN *join= tab->join;
15199
uint tablenr= tab - join->join_tab;
15200
ha_rows table_records= table->file->stats.records;
15201
bool group= join->group && order == join->group_list;
15204
If not used with LIMIT, only use keys if the whole query can be
15205
resolved with a key; This is because filesort() is usually faster than
15206
retrieving all rows through an index.
15208
if (select_limit >= table_records)
15211
filesort() and join cache are usually faster than reading in
15212
index order and not using join cache
15214
if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
15216
keys= *table->file->keys_to_use_for_scanning();
15217
keys.merge(table->covering_keys);
15220
We are adding here also the index specified in FORCE INDEX clause,
15222
This is to allow users to use index in ORDER BY.
15224
if (table->force_index)
15225
keys.merge(group ? table->keys_in_use_for_group_by :
15226
table->keys_in_use_for_order_by);
15227
keys.intersect(usable_keys);
15232
read_time= join->best_positions[tablenr].read_time;
15233
for (uint i= tablenr+1; i < join->tables; i++)
15234
fanout*= join->best_positions[i].records_read; // fanout is always >= 1
15236
for (nr=0; nr < table->s->keys ; nr++)
15239
if (keys.is_set(nr) &&
15240
(direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
15242
bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
15245
Don't use an index scan with ORDER BY without limit.
15246
For GROUP BY without limit always use index scan
15247
if there is a suitable index.
15248
Why we hold to this asymmetry hardly can be explained
15249
rationally. It's easy to demonstrate that using
15250
temporary table + filesort could be cheaper for grouping
15254
select_limit != HA_POS_ERROR ||
15255
(ref_key < 0 && (group || table->force_index)))
15257
double rec_per_key;
15258
double index_scan_time;
15259
KEY *keyinfo= tab->table->key_info+nr;
15260
if (select_limit == HA_POS_ERROR)
15261
select_limit= table_records;
15264
rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
15265
set_if_bigger(rec_per_key, 1);
15267
With a grouping query each group containing on average
15268
rec_per_key records produces only one row that will
15269
be included into the result set.
15271
if (select_limit > table_records/rec_per_key)
15272
select_limit= table_records;
15274
select_limit= (ha_rows) (select_limit*rec_per_key);
15277
If tab=tk is not the last joined table tn then to get first
15278
L records from the result set we can expect to retrieve
15279
only L/fanout(tk,tn) where fanout(tk,tn) says how many
15280
rows in the record set on average will match each row tk.
15281
Usually our estimates for fanouts are too pessimistic.
15282
So the estimate for L/fanout(tk,tn) will be too optimistic
15283
and as result we'll choose an index scan when using ref/range
15284
access + filesort will be cheaper.
15286
select_limit= (ha_rows) (select_limit < fanout ?
15287
1 : select_limit/fanout);
15289
We assume that each of the tested indexes is not correlated
15290
with ref_key. Thus, to select first N records we have to scan
15291
N/selectivity(ref_key) index entries.
15292
selectivity(ref_key) = #scanned_records/#table_records =
15293
table->quick_condition_rows/table_records.
15294
In any case we can't select more than #table_records.
15295
N/(table->quick_condition_rows/table_records) > table_records
15296
<=> N > table->quick_condition_rows.
15298
if (select_limit > table->quick_condition_rows)
15299
select_limit= table_records;
15301
select_limit= (ha_rows) (select_limit *
15302
(double) table_records /
15303
table->quick_condition_rows);
15304
rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
15305
set_if_bigger(rec_per_key, 1);
15307
Here we take into account the fact that rows are
15308
accessed in sequences rec_per_key records in each.
15309
Rows in such a sequence are supposed to be ordered
15310
by rowid/primary key. When reading the data
15311
in a sequence we'll touch not more pages than the
15312
table file contains.
15313
TODO. Use the formula for a disk sweep sequential access
15314
to calculate the cost of accessing data rows for one
15317
index_scan_time= select_limit/rec_per_key *
15318
min(rec_per_key, table->file->scan_time());
15319
if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
15320
index_scan_time < read_time)
15322
ha_rows quick_records= table_records;
15323
if (is_best_covering && !is_covering)
15325
if (table->quick_keys.is_set(nr))
15326
quick_records= table->quick_rows[nr];
15327
if (best_key < 0 ||
15328
(select_limit <= min(quick_records,best_records) ?
15329
keyinfo->key_parts < best_key_parts :
15330
quick_records < best_records))
15333
best_key_parts= keyinfo->key_parts;
15334
best_records= quick_records;
15335
is_best_covering= is_covering;
15336
best_key_direction= direction;
15344
bool quick_created= FALSE;
15345
if (table->quick_keys.is_set(best_key) && best_key != ref_key)
15348
map.clear_all(); // Force the creation of quick select
15349
map.set_bit(best_key); // only best_key.
15351
select->test_quick_select(join->thd, map, 0,
15352
join->select_options & OPTION_FOUND_ROWS ?
15354
join->unit->select_limit_cnt,
15359
if (!quick_created)
15361
tab->index= best_key;
15362
tab->read_first_record= best_key_direction > 0 ?
15363
join_read_first:join_read_last;
15364
tab->type=JT_NEXT; // Read with index_first(), index_next()
15365
if (select && select->quick)
15367
delete select->quick;
15370
if (table->covering_keys.is_set(best_key))
15373
table->file->extra(HA_EXTRA_KEYREAD);
15375
table->file->ha_index_or_rnd_end();
15376
if (join->select_options & SELECT_DESCRIBE)
15379
tab->ref.key_parts= 0;
15380
if (select_limit < table_records)
15381
tab->limit= select_limit;
15384
else if (tab->type != JT_ALL)
15387
We're about to use a quick access to the table.
15388
We need to change the access method so as the quick access
15389
method is actually used.
15391
DBUG_ASSERT(tab->select->quick);
15395
tab->ref.key_parts=0; // Don't use ref key.
15396
tab->read_first_record= join_init_read_record;
15398
TODO: update the number of records in join->best_positions[tablenr]
15402
used_key_parts= best_key_parts;
15403
order_direction= best_key_direction;
15409
check_reverse_order:
15410
if (order_direction == -1) // If ORDER BY ... DESC
15412
if (select && select->quick)
15415
Don't reverse the sort order, if it's already done.
15416
(In some cases test_if_order_by_key() can be called multiple times
15418
if (!select->quick->reverse_sorted())
15420
QUICK_SELECT_DESC *tmp;
15422
int quick_type= select->quick->get_type();
15423
if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
15424
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
15425
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
15426
quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
15429
select->quick= save_quick;
15430
DBUG_RETURN(0); // Use filesort
15433
/* ORDER BY range_key DESC */
15434
tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
15435
used_key_parts, &error);
15439
select->quick= save_quick;
15441
DBUG_RETURN(0); // Reverse sort not supported
15446
else if (tab->type != JT_NEXT &&
15447
tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
15450
SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
15452
Use a traversal function that starts by reading the last row
15453
with key part (A) and then traverse the index backwards.
15455
tab->read_first_record= join_read_last_key;
15456
tab->read_record.read_record= join_read_prev_same;
15459
else if (select && select->quick)
15460
select->quick->sorted= 1;
15466
If not selecting by given key, create an index how records should be read
15469
create_sort_index()
15471
tab Table to sort (in join structure)
15472
order How table should be sorted
15473
filesort_limit Max number of rows that needs to be sorted
15474
select_limit Max number of rows in final output
15475
Used to decide if we should use index or not
15476
is_order_by true if we are sorting on ORDER BY, false if GROUP BY
15477
Used to decide if we should use index or not
15481
- If there is an index that can be used, 'tab' is modified to use
15483
- If no index, create with filesort() an index file that can be used to
15484
retrieve rows in order (should be done with 'read_record').
15485
The sorted data is stored in tab->table and will be freed when calling
15486
free_io_cache(tab->table).
15490
-1 Some fatal error
15495
create_sort_index(THD *thd, JOIN *join, ORDER *order,
15496
ha_rows filesort_limit, ha_rows select_limit,
15500
ha_rows examined_rows;
15502
SQL_SELECT *select;
15504
DBUG_ENTER("create_sort_index");
15506
if (join->tables == join->const_tables)
15507
DBUG_RETURN(0); // One row, no need to sort
15508
tab= join->join_tab + join->const_tables;
15510
select= tab->select;
15513
When there is SQL_BIG_RESULT do not sort using index for GROUP BY,
15514
and thus force sorting on disk unless a group min-max optimization
15515
is going to be used as it is applied now only for one table queries
15516
with covering indexes.
15518
if ((order != join->group_list ||
15519
!(join->select_options & SELECT_BIG_RESULT) ||
15520
(select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
15521
test_if_skip_sort_order(tab,order,select_limit,0,
15522
is_order_by ? &table->keys_in_use_for_order_by :
15523
&table->keys_in_use_for_group_by))
15525
for (ORDER *ord= join->order; ord; ord= ord->next)
15527
if (!(join->sortorder=
15528
make_unireg_sortorder(order, &length, join->sortorder)))
15529
goto err; /* purecov: inspected */
15531
table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
15532
MYF(MY_WME | MY_ZEROFILL));
15533
table->status=0; // May be wrong if quick_select
15535
// If table has a range, move it to select
15536
if (select && !select->quick && tab->ref.key >= 0)
15540
select->quick=tab->quick;
15543
We can only use 'Only index' if quick key is same as ref_key
15544
and in index_merge 'Only index' cannot be used
15546
if (table->key_read && ((uint) tab->ref.key != select->quick->index))
15549
table->file->extra(HA_EXTRA_NO_KEYREAD);
15555
We have a ref on a const; Change this to a range that filesort
15557
For impossible ranges (like when doing a lookup on NULL on a NOT NULL
15558
field, quick will contain an empty record set.
15560
if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref,
15561
tab->found_records))))
15566
/* Fill schema tables with data before filesort if it's necessary */
15567
if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
15568
get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
15571
if (table->s->tmp_table)
15572
table->file->info(HA_STATUS_VARIABLE); // Get record count
15573
table->sort.found_records=filesort(thd, table,join->sortorder, length,
15574
select, filesort_limit, 0,
15576
tab->records= table->sort.found_records; // For SQL_CALC_ROWS
15579
select->cleanup(); // filesort did select
15582
tab->select_cond=0;
15583
tab->last_inner= 0;
15584
tab->first_unmatched= 0;
15585
tab->type=JT_ALL; // Read with normal read_record
15586
tab->read_first_record= join_init_read_record;
15587
tab->join->examined_rows+=examined_rows;
15588
if (table->key_read) // Restore if we used indexes
15591
table->file->extra(HA_EXTRA_NO_KEYREAD);
15593
DBUG_RETURN(table->sort.found_records == HA_POS_ERROR);
15598
/*****************************************************************************
15599
Remove duplicates from tmp table
15600
This should be recoded to add a unique index to the table and remove
15602
Table is a locked single thread table
15603
fields is the number of fields to check (from the end)
15604
*****************************************************************************/
15606
static bool compare_record(TABLE *table, Field **ptr)
15608
for (; *ptr ; ptr++)
15610
if ((*ptr)->cmp_offset(table->s->rec_buff_length))
15616
static bool copy_blobs(Field **ptr)
15618
for (; *ptr ; ptr++)
15620
if ((*ptr)->flags & BLOB_FLAG)
15621
if (((Field_blob *) (*ptr))->copy())
15627
static void free_blobs(Field **ptr)
15629
for (; *ptr ; ptr++)
15631
if ((*ptr)->flags & BLOB_FLAG)
15632
((Field_blob *) (*ptr))->free();
15638
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
15641
ulong reclength,offset;
15643
THD *thd= join->thd;
15644
DBUG_ENTER("remove_duplicates");
15646
entry->reginfo.lock_type=TL_WRITE;
15648
/* Calculate how many saved fields there is in list */
15650
List_iterator<Item> it(fields);
15652
while ((item=it++))
15654
if (item->get_tmp_table_field() && ! item->const_item())
15658
if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
15659
{ // only const items with no OPTION_FOUND_ROWS
15660
join->unit->select_limit_cnt= 1; // Only send first row
15663
Field **first_field=entry->field+entry->s->fields - field_count;
15664
offset= (field_count ?
15665
entry->field[entry->s->fields - field_count]->
15666
offset(entry->record[0]) : 0);
15667
reclength=entry->s->reclength-offset;
15669
free_io_cache(entry); // Safety
15670
entry->file->info(HA_STATUS_VARIABLE);
15671
if (entry->s->db_type() == heap_hton ||
15672
(!entry->s->blob_fields &&
15673
((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
15674
thd->variables.sortbuff_size)))
15675
error=remove_dup_with_hash_index(join->thd, entry,
15676
field_count, first_field,
15677
reclength, having);
15679
error=remove_dup_with_compare(join->thd, entry, first_field, offset,
15682
free_blobs(first_field);
15683
DBUG_RETURN(error);
15687
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
15688
ulong offset, Item *having)
15690
handler *file=table->file;
15691
char *org_record,*new_record;
15694
ulong reclength= table->s->reclength-offset;
15695
DBUG_ENTER("remove_dup_with_compare");
15697
org_record=(char*) (record=table->record[0])+offset;
15698
new_record=(char*) table->record[1]+offset;
15700
file->ha_rnd_init(1);
15701
error=file->rnd_next(record);
15706
thd->send_kill_message();
15712
if (error == HA_ERR_RECORD_DELETED)
15714
if (error == HA_ERR_END_OF_FILE)
15718
if (having && !having->val_int())
15720
if ((error=file->ha_delete_row(record)))
15722
error=file->rnd_next(record);
15725
if (copy_blobs(first_field))
15727
my_message(ER_OUTOFMEMORY, ER(ER_OUTOFMEMORY), MYF(0));
15731
memcpy(new_record,org_record,reclength);
15733
/* Read through rest of file and mark duplicated rows deleted */
15737
if ((error=file->rnd_next(record)))
15739
if (error == HA_ERR_RECORD_DELETED)
15741
if (error == HA_ERR_END_OF_FILE)
15745
if (compare_record(table, first_field) == 0)
15747
if ((error=file->ha_delete_row(record)))
15753
file->position(record); // Remember position
15757
break; // End of file
15758
/* Restart search on next row */
15759
error=file->restart_rnd_next(record,file->ref);
15762
file->extra(HA_EXTRA_NO_CACHE);
15765
file->extra(HA_EXTRA_NO_CACHE);
15767
file->print_error(error,MYF(0));
15773
Generate a hash index for each row to quickly find duplicate rows.
15776
Note that this will not work on tables with blobs!
15779
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
15781
Field **first_field,
15785
uchar *key_buffer, *key_pos, *record=table->record[0];
15787
handler *file= table->file;
15788
ulong extra_length= ALIGN_SIZE(key_length)-key_length;
15789
uint *field_lengths,*field_length;
15791
DBUG_ENTER("remove_dup_with_hash_index");
15793
if (!my_multi_malloc(MYF(MY_WME),
15795
(uint) ((key_length + extra_length) *
15796
(long) file->stats.records),
15798
(uint) (field_count*sizeof(*field_lengths)),
15804
ulong total_length= 0;
15805
for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
15807
uint length= (*ptr)->sort_length();
15808
(*field_length++)= length;
15809
total_length+= length;
15811
DBUG_PRINT("info",("field_count: %u key_length: %lu total_length: %lu",
15812
field_count, key_length, total_length));
15813
DBUG_ASSERT(total_length <= key_length);
15814
key_length= total_length;
15815
extra_length= ALIGN_SIZE(key_length)-key_length;
15818
if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0,
15819
key_length, (hash_get_key) 0, 0, 0))
15821
my_free((char*) key_buffer,MYF(0));
15825
file->ha_rnd_init(1);
15826
key_pos=key_buffer;
15829
uchar *org_key_pos;
15832
thd->send_kill_message();
15836
if ((error=file->rnd_next(record)))
15838
if (error == HA_ERR_RECORD_DELETED)
15840
if (error == HA_ERR_END_OF_FILE)
15844
if (having && !having->val_int())
15846
if ((error=file->ha_delete_row(record)))
15851
/* copy fields to key buffer */
15852
org_key_pos= key_pos;
15853
field_length=field_lengths;
15854
for (Field **ptr= first_field ; *ptr ; ptr++)
15856
(*ptr)->sort_string(key_pos,*field_length);
15857
key_pos+= *field_length++;
15859
/* Check if it exists before */
15860
if (hash_search(&hash, org_key_pos, key_length))
15862
/* Duplicated found ; Remove the row */
15863
if ((error=file->ha_delete_row(record)))
15867
(void) my_hash_insert(&hash, org_key_pos);
15868
key_pos+=extra_length;
15870
my_free((char*) key_buffer,MYF(0));
15872
file->extra(HA_EXTRA_NO_CACHE);
15873
(void) file->ha_rnd_end();
15877
my_free((char*) key_buffer,MYF(0));
15879
file->extra(HA_EXTRA_NO_CACHE);
15880
(void) file->ha_rnd_end();
15882
file->print_error(error,MYF(0));
15887
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
15888
SORT_FIELD *sortorder)
15891
SORT_FIELD *sort,*pos;
15892
DBUG_ENTER("make_unireg_sortorder");
15895
for (ORDER *tmp = order; tmp; tmp=tmp->next)
15898
sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
15899
(max(count, *length) + 1));
15900
pos= sort= sortorder;
15905
for (;order;order=order->next,pos++)
15907
Item *item= order->item[0]->real_item();
15908
pos->field= 0; pos->item= 0;
15909
if (item->type() == Item::FIELD_ITEM)
15910
pos->field= ((Item_field*) item)->field;
15911
else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
15912
pos->field= ((Item_sum*) item)->get_tmp_table_field();
15913
else if (item->type() == Item::COPY_STR_ITEM)
15915
pos->item= ((Item_copy_string*) item)->item;
15918
pos->item= *order->item;
15919
pos->reverse=! order->asc;
15926
/*****************************************************************************
15927
Fill join cache with packed records
15928
Records are stored in tab->cache.buffer and last record in
15929
last record is stored with pointers to blobs to support very big
15931
******************************************************************************/
15934
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
15936
register unsigned int i;
15937
unsigned int length, blobs;
15939
CACHE_FIELD *copy,**blob_ptr;
15941
JOIN_TAB *join_tab;
15942
DBUG_ENTER("join_init_cache");
15944
cache= &tables[table_count].cache;
15945
cache->fields=blobs=0;
15948
for (i=0 ; i < table_count ; i++,join_tab++)
15950
if (!join_tab->used_fieldlength) /* Not calced yet */
15951
calc_used_field_length(thd, join_tab);
15952
cache->fields+=join_tab->used_fields;
15953
blobs+=join_tab->used_blobs;
15955
/* SemiJoinDuplicateElimination: reserve space for rowid */
15956
if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
15959
join_tab->used_fieldlength += join_tab->table->file->ref_length;
15962
if (!(cache->field=(CACHE_FIELD*)
15963
sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
15965
sizeof(CACHE_FIELD*))))
15967
my_free((uchar*) cache->buff,MYF(0)); /* purecov: inspected */
15968
cache->buff=0; /* purecov: inspected */
15969
DBUG_RETURN(1); /* purecov: inspected */
15972
blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
15973
(cache->field+cache->fields+table_count*2);
15976
for (i=0 ; i < table_count ; i++)
15978
uint null_fields=0,used_fields;
15979
Field **f_ptr,*field;
15980
MY_BITMAP *read_set= tables[i].table->read_set;
15981
for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
15986
if (bitmap_is_set(read_set, field->field_index))
15989
length+=field->fill_cache_field(copy);
15990
if (copy->blob_field)
15991
(*blob_ptr++)=copy;
15992
if (field->maybe_null())
15994
copy->get_rowid= NULL;
15998
/* Copy null bits from table */
15999
if (null_fields && tables[i].table->s->null_fields)
16000
{ /* must copy null bits */
16001
copy->str= tables[i].table->null_flags;
16002
copy->length= tables[i].table->s->null_bytes;
16004
copy->blob_field=0;
16005
copy->get_rowid= NULL;
16006
length+=copy->length;
16010
/* If outer join table, copy null_row flag */
16011
if (tables[i].table->maybe_null)
16013
copy->str= (uchar*) &tables[i].table->null_row;
16014
copy->length=sizeof(tables[i].table->null_row);
16016
copy->blob_field=0;
16017
copy->get_rowid= NULL;
16018
length+=copy->length;
16022
/* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
16023
if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
16025
copy->str= tables[i].table->file->ref;
16026
copy->length= tables[i].table->file->ref_length;
16028
copy->blob_field=0;
16029
copy->get_rowid= NULL;
16030
if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
16032
/* We will need to call h->position(): */
16033
copy->get_rowid= tables[i].table;
16034
/* And those after us won't have to: */
16035
tables[i].rowid_keep_flags &= ~((int)JOIN_TAB::CALL_POSITION);
16041
cache->length=length+blobs*sizeof(char*);
16042
cache->blobs=blobs;
16043
*blob_ptr=0; /* End sequentel */
16044
size=max(thd->variables.join_buff_size, cache->length);
16045
if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
16046
DBUG_RETURN(1); /* Don't use cache */ /* purecov: inspected */
16047
cache->end=cache->buff+size;
16048
reset_cache_write(cache);
16054
used_blob_length(CACHE_FIELD **ptr)
16056
uint length,blob_length;
16057
for (length=0 ; *ptr ; ptr++)
16059
(*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
16060
length+=blob_length;
16061
(*ptr)->blob_field->get_ptr(&(*ptr)->str);
16068
store_record_in_cache(JOIN_CACHE *cache)
16072
CACHE_FIELD *copy,*end_field;
16076
end_field=cache->field+cache->fields;
16078
length=cache->length;
16080
length+=used_blob_length(cache->blob_ptr);
16081
if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
16082
cache->ptr_record=cache->records;
16084
There is room in cache. Put record there
16087
for (copy=cache->field ; copy < end_field; copy++)
16089
if (copy->blob_field)
16093
copy->blob_field->get_image(pos, copy->length+sizeof(char*),
16094
copy->blob_field->charset());
16095
pos+=copy->length+sizeof(char*);
16099
copy->blob_field->get_image(pos, copy->length, // blob length
16100
copy->blob_field->charset());
16101
memcpy(pos+copy->length,copy->str,copy->blob_length); // Blob data
16102
pos+=copy->length+copy->blob_length;
16107
// SemiJoinDuplicateElimination: Get the rowid into table->ref:
16108
if (copy->get_rowid)
16109
copy->get_rowid->file->position(copy->get_rowid->record[0]);
16114
for (str=copy->str,end= str+copy->length;
16115
end > str && end[-1] == ' ' ;
16117
length=(uint) (end-str);
16118
memcpy(pos+2, str, length);
16119
int2store(pos, length);
16124
memcpy(pos,copy->str,copy->length);
16130
return last_record || (size_t) (cache->end - pos) < cache->length;
16135
reset_cache_read(JOIN_CACHE *cache)
16137
cache->record_nr=0;
16138
cache->pos=cache->buff;
16142
static void reset_cache_write(JOIN_CACHE *cache)
16144
reset_cache_read(cache);
16146
cache->ptr_record= (uint) ~0;
16151
read_cached_record(JOIN_TAB *tab)
16156
CACHE_FIELD *copy,*end_field;
16158
last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
16159
pos=tab->cache.pos;
16160
for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
16164
if (copy->blob_field)
16168
copy->blob_field->set_image(pos, copy->length+sizeof(char*),
16169
copy->blob_field->charset());
16170
pos+=copy->length+sizeof(char*);
16174
copy->blob_field->set_ptr(pos, pos+copy->length);
16175
pos+=copy->length+copy->blob_field->get_length();
16182
length= uint2korr(pos);
16183
memcpy(copy->str, pos+2, length);
16184
memset(copy->str+length, ' ', copy->length-length);
16189
memcpy(copy->str,pos,copy->length);
16194
tab->cache.pos=pos;
16200
eq_ref: Create the lookup key and check if it is the same as saved key
16203
cmp_buffer_with_ref()
16204
tab Join tab of the accessed table
16207
Used by eq_ref access method: create the index lookup key and check if
16208
we've used this key at previous lookup (If yes, we don't need to repeat
16209
the lookup - the record has been already fetched)
16212
TRUE No cached record for the key, or failed to create the key (due to
16213
out-of-domain error)
16214
FALSE The created key is the same as the previous one (and the record
16215
is already in table->record)
16219
cmp_buffer_with_ref(JOIN_TAB *tab)
16222
if (!tab->ref.disable_cache)
16224
if (!(no_prev_key= tab->ref.key_err))
16226
/* Previous access found a row. Copy its key */
16227
memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
16232
if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
16236
return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
16242
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
16244
enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
16245
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
16246
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
16249
for (store_key **copy=ref->key_copy ; *copy ; copy++)
16251
if ((*copy)->copy() & 1)
16257
thd->count_cuted_fields= save_count_cuted_fields;
16258
dbug_tmp_restore_column_map(table->write_set, old_map);
16263
/*****************************************************************************
16264
Group and order functions
16265
*****************************************************************************/
16268
Resolve an ORDER BY or GROUP BY column reference.
16270
Given a column reference (represented by 'order') from a GROUP BY or ORDER
16271
BY clause, find the actual column it represents. If the column being
16272
resolved is from the GROUP BY clause, the procedure searches the SELECT
16273
list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
16274
the ORDER BY clause, only the SELECT list is being searched.
16276
If 'order' is resolved to an Item, then order->item is set to the found
16277
Item. If there is no item for the found column (that is, it was resolved
16278
into a table field), order->item is 'fixed' and is added to all_fields and
16281
ref_pointer_array and all_fields are updated.
16283
@param[in] thd Pointer to current thread structure
16284
@param[in,out] ref_pointer_array All select, group and order by fields
16285
@param[in] tables List of tables to search in (usually
16287
@param[in] order Column reference to be resolved
16288
@param[in] fields List of fields to search in (usually
16290
@param[in,out] all_fields All select, group and order by fields
16291
@param[in] is_group_field True if order is a GROUP field, false if
16297
TRUE if error occurred
16301
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16302
ORDER *order, List<Item> &fields, List<Item> &all_fields,
16303
bool is_group_field)
16305
Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
16306
Item::Type order_item_type;
16307
Item **select_item; /* The corresponding item from the SELECT clause. */
16308
Field *from_field; /* The corresponding field from the FROM clause. */
16310
enum_resolution_type resolution;
16313
Local SP variables may be int but are expressions, not positions.
16314
(And they can't be used before fix_fields is called for them).
16316
if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
16317
{ /* Order by position */
16318
uint count= (uint) order_item->val_int();
16319
if (!count || count > fields.elements)
16321
my_error(ER_BAD_FIELD_ERROR, MYF(0),
16322
order_item->full_name(), thd->where);
16325
order->item= ref_pointer_array + count - 1;
16326
order->in_field_list= 1;
16327
order->counter= count;
16328
order->counter_used= 1;
16331
/* Lookup the current GROUP/ORDER field in the SELECT clause. */
16332
select_item= find_item_in_list(order_item, fields, &counter,
16333
REPORT_EXCEPT_NOT_FOUND, &resolution);
16335
return TRUE; /* The item is not unique, or some other error occured. */
16338
/* Check whether the resolved field is not ambiguos. */
16339
if (select_item != not_found_item)
16341
Item *view_ref= NULL;
16343
If we have found field not by its alias in select list but by its
16344
original field name, we should additionaly check if we have conflict
16345
for this name (in case if we would perform lookup in all tables).
16347
if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
16348
order_item->fix_fields(thd, order->item))
16351
/* Lookup the current GROUP field in the FROM clause. */
16352
order_item_type= order_item->type();
16353
from_field= (Field*) not_found_field;
16354
if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
16355
order_item_type == Item::REF_ITEM)
16357
from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
16358
NULL, &view_ref, IGNORE_ERRORS, TRUE,
16361
from_field= (Field*) not_found_field;
16364
if (from_field == not_found_field ||
16365
(from_field != view_ref_found ?
16366
/* it is field of base table => check that fields are same */
16367
((*select_item)->type() == Item::FIELD_ITEM &&
16368
((Item_field*) (*select_item))->field->eq(from_field)) :
16370
in is field of view table => check that references on translation
16373
((*select_item)->type() == Item::REF_ITEM &&
16374
view_ref->type() == Item::REF_ITEM &&
16375
((Item_ref *) (*select_item))->ref ==
16376
((Item_ref *) view_ref)->ref)))
16379
If there is no such field in the FROM clause, or it is the same field
16380
as the one found in the SELECT clause, then use the Item created for
16381
the SELECT field. As a result if there was a derived field that
16382
'shadowed' a table field with the same name, the table field will be
16383
chosen over the derived field.
16385
order->item= ref_pointer_array + counter;
16386
order->in_field_list=1;
16392
There is a field with the same name in the FROM clause. This
16393
is the field that will be chosen. In this case we issue a
16394
warning so the user knows that the field from the FROM clause
16395
overshadows the column reference from the SELECT list.
16397
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
16398
ER(ER_NON_UNIQ_ERROR),
16399
((Item_ident*) order_item)->field_name,
16400
current_thd->where);
16404
order->in_field_list=0;
16406
The call to order_item->fix_fields() means that here we resolve
16407
'order_item' to a column from a table in the list 'tables', or to
16408
a column in some outer query. Exactly because of the second case
16409
we come to this point even if (select_item == not_found_item),
16410
inspite of that fix_fields() calls find_item_in_list() one more
16413
We check order_item->fixed because Item_func_group_concat can put
16414
arguments for which fix_fields already was called.
16416
if (!order_item->fixed &&
16417
(order_item->fix_fields(thd, order->item) ||
16418
(order_item= *order->item)->check_cols(1) ||
16419
thd->is_fatal_error))
16420
return TRUE; /* Wrong field. */
16422
uint el= all_fields.elements;
16423
all_fields.push_front(order_item); /* Add new field to field list. */
16424
ref_pointer_array[el]= order_item;
16425
order->item= ref_pointer_array + el;
16431
Change order to point at item in select list.
16433
If item isn't a number and doesn't exits in the select list, add it the
16437
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16438
List<Item> &fields, List<Item> &all_fields, ORDER *order)
16440
thd->where="order clause";
16441
for (; order; order=order->next)
16443
if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
16444
all_fields, FALSE))
16452
Intitialize the GROUP BY list.
16454
@param thd Thread handler
16455
@param ref_pointer_array We store references to all fields that was
16456
not in 'fields' here.
16457
@param fields All fields in the select part. Any item in
16458
'order' that is part of these list is replaced
16459
by a pointer to this fields.
16460
@param all_fields Total list of all unique fields used by the
16461
select. All items in 'order' that was not part
16462
of fields will be added first to this list.
16463
@param order The fields we should do GROUP BY on.
16464
@param hidden_group_fields Pointer to flag that is set to 1 if we added
16465
any fields to all_fields.
16468
change ER_WRONG_FIELD_WITH_GROUP to more detailed
16469
ER_NON_GROUPING_FIELD_USED
16474
1 error (probably out of memory)
16478
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16479
List<Item> &fields, List<Item> &all_fields, ORDER *order,
16480
bool *hidden_group_fields)
16482
*hidden_group_fields=0;
16486
return 0; /* Everything is ok */
16488
uint org_fields=all_fields.elements;
16490
thd->where="group statement";
16491
for (ord= order; ord; ord= ord->next)
16493
if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
16496
(*ord->item)->marker= UNDEF_POS; /* Mark found */
16497
if ((*ord->item)->with_sum_func)
16499
my_error(ER_WRONG_GROUP_FIELD, MYF(0), (*ord->item)->full_name());
16503
/* MODE_ONLY_FULL_GROUP_BY */
16506
Don't allow one to use fields that is not used in GROUP BY
16507
For each select a list of field references that aren't under an
16508
aggregate function is created. Each field in this list keeps the
16509
position of the select list expression which it belongs to.
16511
First we check an expression from the select list against the GROUP BY
16512
list. If it's found there then it's ok. It's also ok if this expression
16513
is a constant or an aggregate function. Otherwise we scan the list
16514
of non-aggregated fields and if we'll find at least one field reference
16515
that belongs to this expression and doesn't occur in the GROUP BY list
16516
we throw an error. If there are no fields in the created list for a
16517
select list expression this means that all fields in it are used under
16518
aggregate functions.
16522
int cur_pos_in_select_list= 0;
16523
List_iterator<Item> li(fields);
16524
List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
16527
while (field && (item=li++))
16529
if (item->type() != Item::SUM_FUNC_ITEM && item->marker >= 0 &&
16530
!item->const_item() &&
16531
!(item->real_item()->type() == Item::FIELD_ITEM &&
16532
item->used_tables() & OUTER_REF_TABLE_BIT))
16536
/* Skip fields from previous expressions. */
16537
if (field->marker < cur_pos_in_select_list)
16539
/* Found a field from the next expression. */
16540
if (field->marker > cur_pos_in_select_list)
16543
Check whether the field occur in the GROUP BY list.
16544
Throw the error later if the field isn't found.
16546
for (ord= order; ord; ord= ord->next)
16547
if ((*ord->item)->eq((Item*)field, 0))
16550
TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
16551
ER_NON_GROUPING_FIELD_USED
16553
my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
16559
cur_pos_in_select_list++;
16562
if (org_fields != all_fields.elements)
16563
*hidden_group_fields=1; // group fields is not used
16568
Create a group by that consist of all non const fields.
16570
Try to use the fields in the order given by 'order' to allow one to
16571
optimize away 'order by'.
16575
create_distinct_group(THD *thd, Item **ref_pointer_array,
16576
ORDER *order_list, List<Item> &fields,
16577
List<Item> &all_fields,
16578
bool *all_order_by_fields_used)
16580
List_iterator<Item> li(fields);
16581
Item *item, **orig_ref_pointer_array= ref_pointer_array;
16582
ORDER *order,*group,**prev;
16584
*all_order_by_fields_used= 1;
16585
while ((item=li++))
16586
item->marker=0; /* Marker that field is not used */
16588
prev= &group; group=0;
16589
for (order=order_list ; order; order=order->next)
16591
if (order->in_field_list)
16593
ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
16598
(*ord->item)->marker=1;
16601
*all_order_by_fields_used= 0;
16605
while ((item=li++))
16607
if (!item->const_item() && !item->with_sum_func && !item->marker)
16610
Don't put duplicate columns from the SELECT list into the
16614
for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
16615
if ((*ord_iter->item)->eq(item, 1))
16618
ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
16622
if (item->type() == Item::FIELD_ITEM &&
16623
item->field_type() == MYSQL_TYPE_BIT)
16626
Because HEAP tables can't index BIT fields we need to use an
16627
additional hidden field for grouping because later it will be
16628
converted to a LONG field. Original field will remain of the
16629
BIT type and will be returned to a client.
16631
Item_field *new_item= new Item_field(thd, (Item_field*)item);
16632
int el= all_fields.elements;
16633
orig_ref_pointer_array[el]= new_item;
16634
all_fields.push_front(new_item);
16635
ord->item= orig_ref_pointer_array + el;
16640
We have here only field_list (not all_field_list), so we can use
16641
simple indexing of ref_pointer_array (order in the array and in the
16644
ord->item= ref_pointer_array;
16651
ref_pointer_array++;
16659
Update join with count of the different type of fields.
16663
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param,
16664
List<Item> &fields, bool reset_with_sum_func)
16666
List_iterator<Item> li(fields);
16669
param->field_count=param->sum_func_count=param->func_count=
16670
param->hidden_field_count=0;
16671
param->quick_group=1;
16672
while ((field=li++))
16674
Item::Type real_type= field->real_item()->type();
16675
if (real_type == Item::FIELD_ITEM)
16676
param->field_count++;
16677
else if (real_type == Item::SUM_FUNC_ITEM)
16679
if (! field->const_item())
16681
Item_sum *sum_item=(Item_sum*) field->real_item();
16682
if (!sum_item->depended_from() ||
16683
sum_item->depended_from() == select_lex)
16685
if (!sum_item->quick_group)
16686
param->quick_group=0; // UDF SUM function
16687
param->sum_func_count++;
16689
for (uint i=0 ; i < sum_item->arg_count ; i++)
16691
if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
16692
param->field_count++;
16694
param->func_count++;
16697
param->func_count++;
16702
param->func_count++;
16703
if (reset_with_sum_func)
16704
field->with_sum_func=0;
16711
Return 1 if second is a subpart of first argument.
16713
If first parts has different direction, change it to second part
16714
(group is sorted like order)
16718
test_if_subpart(ORDER *a,ORDER *b)
16720
for (; a && b; a=a->next,b=b->next)
16722
if ((*a->item)->eq(*b->item,1))
16731
Return table number if there is only one table in sort order
16732
and group and order is compatible, else return 0.
16736
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
16738
table_map map= (table_map) 0;
16739
DBUG_ENTER("get_sort_by_table");
16742
a=b; // Only one need to be given
16746
for (; a && b; a=a->next,b=b->next)
16748
if (!(*a->item)->eq(*b->item,1))
16750
map|=a->item[0]->used_tables();
16752
if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
16755
for (; !(map & tables->table->map); tables= tables->next_leaf) {};
16756
if (map != tables->table->map)
16757
DBUG_RETURN(0); // More than one table
16758
DBUG_PRINT("exit",("sort by table: %d",tables->table->tablenr));
16759
DBUG_RETURN(tables->table);
16764
calc how big buffer we need for comparing group entries.
16768
calc_group_buffer(JOIN *join,ORDER *group)
16770
uint key_length=0, parts=0, null_parts=0;
16774
for (; group ; group=group->next)
16776
Item *group_item= *group->item;
16777
Field *field= group_item->get_tmp_table_field();
16780
enum_field_types type;
16781
if ((type= field->type()) == MYSQL_TYPE_BLOB)
16782
key_length+=MAX_BLOB_WIDTH; // Can't be used as a key
16783
else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
16784
key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
16785
else if (type == MYSQL_TYPE_BIT)
16787
/* Bit is usually stored as a longlong key for group fields */
16788
key_length+= 8; // Big enough
16791
key_length+= field->pack_length();
16795
switch (group_item->result_type()) {
16797
key_length+= sizeof(double);
16800
key_length+= sizeof(longlong);
16802
case DECIMAL_RESULT:
16803
key_length+= my_decimal_get_binary_size(group_item->max_length -
16804
(group_item->decimals ? 1 : 0),
16805
group_item->decimals);
16807
case STRING_RESULT:
16809
enum enum_field_types type= group_item->field_type();
16811
As items represented as DATE/TIME fields in the group buffer
16812
have STRING_RESULT result type, we increase the length
16813
by 8 as maximum pack length of such fields.
16815
if (type == MYSQL_TYPE_TIME ||
16816
type == MYSQL_TYPE_DATE ||
16817
type == MYSQL_TYPE_DATETIME ||
16818
type == MYSQL_TYPE_TIMESTAMP)
16825
Group strings are taken as varstrings and require an length field.
16826
A field is not yet created by create_tmp_field()
16827
and the sizes should match up.
16829
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
16834
/* This case should never be choosen */
16836
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
16840
if (group_item->maybe_null)
16843
join->tmp_table_param.group_length=key_length+null_parts;
16844
join->tmp_table_param.group_parts=parts;
16845
join->tmp_table_param.group_null_parts=null_parts;
16850
allocate group fields or take prepared (cached).
16852
@param main_join join of current select
16853
@param curr_join current join (join of current select or temporary copy
16863
make_group_fields(JOIN *main_join, JOIN *curr_join)
16865
if (main_join->group_fields_cache.elements)
16867
curr_join->group_fields= main_join->group_fields_cache;
16868
curr_join->sort_and_group= 1;
16872
if (alloc_group_fields(curr_join, curr_join->group_list))
16874
main_join->group_fields_cache= curr_join->group_fields;
16881
Get a list of buffers for saveing last group.
16883
Groups are saved in reverse order for easyer check loop.
16887
alloc_group_fields(JOIN *join,ORDER *group)
16891
for (; group ; group=group->next)
16893
Cached_item *tmp=new_Cached_item(join->thd, *group->item, FALSE);
16894
if (!tmp || join->group_fields.push_front(tmp))
16898
join->sort_and_group=1; /* Mark for do_select */
16904
Test if a single-row cache of items changed, and update the cache.
16906
@details Test if a list of items that typically represents a result
16907
row has changed. If the value of some item changed, update the cached
16908
value for this item.
16910
@param list list of <item, cached_value> pairs stored as Cached_item.
16912
@return -1 if no item changed
16913
@return index of the first item that changed
16916
int test_if_item_cache_changed(List<Cached_item> &list)
16918
DBUG_ENTER("test_if_item_cache_changed");
16919
List_iterator<Cached_item> li(list);
16923
for (i=(int) list.elements-1 ; (buff=li++) ; i--)
16928
DBUG_PRINT("info", ("idx: %d", idx));
16934
Setup copy_fields to save fields at start of new group.
16936
Setup copy_fields to save fields at start of new group
16938
Only FIELD_ITEM:s and FUNC_ITEM:s needs to be saved between groups.
16939
Change old item_field to use a new field with points at saved fieldvalue
16940
This function is only called before use of send_fields.
16942
@param thd THD pointer
16943
@param param temporary table parameters
16944
@param ref_pointer_array array of pointers to top elements of filed list
16945
@param res_selected_fields new list of items of select item list
16946
@param res_all_fields new list of all items
16947
@param elements number of elements in select item list
16948
@param all_fields all fields list
16951
In most cases this result will be sent to the user.
16952
This should be changed to use copy_int or copy_real depending
16953
on how the value is to be used: In some cases this may be an
16954
argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
16963
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
16964
Item **ref_pointer_array,
16965
List<Item> &res_selected_fields, List<Item> &res_all_fields,
16966
uint elements, List<Item> &all_fields)
16969
List_iterator_fast<Item> li(all_fields);
16970
Copy_field *copy= NULL;
16971
res_selected_fields.empty();
16972
res_all_fields.empty();
16973
List_iterator_fast<Item> itr(res_all_fields);
16974
List<Item> extra_funcs;
16975
uint i, border= all_fields.elements - elements;
16976
DBUG_ENTER("setup_copy_fields");
16978
if (param->field_count &&
16979
!(copy=param->copy_field= new Copy_field[param->field_count]))
16982
param->copy_funcs.empty();
16983
for (i= 0; (pos= li++); i++)
16987
Item *real_pos= pos->real_item();
16988
if (real_pos->type() == Item::FIELD_ITEM)
16991
if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
16993
if (pos->type() == Item::REF_ITEM)
16995
/* preserve the names of the ref when dereferncing */
16996
Item_ref *ref= (Item_ref *) pos;
16997
item->db_name= ref->db_name;
16998
item->table_name= ref->table_name;
16999
item->name= ref->name;
17002
if (item->field->flags & BLOB_FLAG)
17004
if (!(pos= new Item_copy_string(pos)))
17007
Item_copy_string::copy for function can call
17008
Item_copy_string::val_int for blob via Item_ref.
17009
But if Item_copy_string::copy for blob isn't called before,
17010
it's value will be wrong
17011
so let's insert Item_copy_string for blobs in the beginning of
17013
(to see full test case look at having.test, BUG #4358)
17015
if (param->copy_funcs.push_front(pos))
17021
set up save buffer and change result_field to point at
17024
field= item->field;
17025
item->result_field=field->new_field(thd->mem_root,field->table, 1);
17027
We need to allocate one extra byte for null handling and
17028
another extra byte to not get warnings from purify in
17029
Field_string::val_int
17031
if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
17035
copy->set(tmp, item->result_field);
17036
item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
17038
copy->to_ptr[copy->from_length]= 0;
17044
else if ((real_pos->type() == Item::FUNC_ITEM ||
17045
real_pos->type() == Item::SUBSELECT_ITEM ||
17046
real_pos->type() == Item::CACHE_ITEM ||
17047
real_pos->type() == Item::COND_ITEM) &&
17048
!real_pos->with_sum_func)
17049
{ // Save for send fields
17052
In most cases this result will be sent to the user.
17053
This should be changed to use copy_int or copy_real depending
17054
on how the value is to be used: In some cases this may be an
17055
argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
17057
if (!(pos=new Item_copy_string(pos)))
17059
if (i < border) // HAVING, ORDER and GROUP BY
17061
if (extra_funcs.push_back(pos))
17064
else if (param->copy_funcs.push_back(pos))
17067
res_all_fields.push_back(pos);
17068
ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17071
param->copy_field_end= copy;
17073
for (i= 0; i < border; i++)
17075
itr.sublist(res_selected_fields, elements);
17077
Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
17078
reference used in these will resolve to a item that is already calculated
17080
param->copy_funcs.concat(&extra_funcs);
17086
delete [] param->copy_field; // This is never 0
17087
param->copy_field=0;
17094
Make a copy of all simple SELECT'ed items.
17096
This is done at the start of a new group so that we can retrieve
17097
these later when the group changes.
17101
copy_fields(TMP_TABLE_PARAM *param)
17103
Copy_field *ptr=param->copy_field;
17104
Copy_field *end=param->copy_field_end;
17106
for (; ptr != end; ptr++)
17107
(*ptr->do_copy)(ptr);
17109
List_iterator_fast<Item> it(param->copy_funcs);
17110
Item_copy_string *item;
17111
while ((item = (Item_copy_string*) it++))
17117
Make an array of pointers to sum_functions to speed up
17118
sum_func calculation.
17126
bool JOIN::alloc_func_list()
17128
uint func_count, group_parts;
17129
DBUG_ENTER("alloc_func_list");
17131
func_count= tmp_table_param.sum_func_count;
17133
If we are using rollup, we need a copy of the summary functions for
17136
if (rollup.state != ROLLUP::STATE_NONE)
17137
func_count*= (send_group_parts+1);
17139
group_parts= send_group_parts;
17141
If distinct, reserve memory for possible
17142
disctinct->group_by optimization
17144
if (select_distinct)
17146
group_parts+= fields_list.elements;
17148
If the ORDER clause is specified then it's possible that
17149
it also will be optimized, so reserve space for it too
17154
for (ord= order; ord; ord= ord->next)
17159
/* This must use calloc() as rollup_make_fields depends on this */
17160
sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
17161
sizeof(Item_sum***) * (group_parts+1));
17162
sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
17163
DBUG_RETURN(sum_funcs == 0);
17168
Initialize 'sum_funcs' array with all Item_sum objects.
17170
@param field_list All items
17171
@param send_fields Items in select list
17172
@param before_group_by Set to 1 if this is called before GROUP BY handling
17173
@param recompute Set to TRUE if sum_funcs must be recomputed
17181
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
17182
bool before_group_by, bool recompute)
17184
List_iterator_fast<Item> it(field_list);
17187
DBUG_ENTER("make_sum_func_list");
17189
if (*sum_funcs && !recompute)
17190
DBUG_RETURN(FALSE); /* We have already initialized sum_funcs. */
17193
while ((item=it++))
17195
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17196
(!((Item_sum*) item)->depended_from() ||
17197
((Item_sum *)item)->depended_from() == select_lex))
17198
*func++= (Item_sum*) item;
17200
if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
17202
rollup.state= ROLLUP::STATE_READY;
17203
if (rollup_make_fields(field_list, send_fields, &func))
17204
DBUG_RETURN(TRUE); // Should never happen
17206
else if (rollup.state == ROLLUP::STATE_NONE)
17208
for (uint i=0 ; i <= send_group_parts ;i++)
17209
sum_funcs_end[i]= func;
17211
else if (rollup.state == ROLLUP::STATE_READY)
17212
DBUG_RETURN(FALSE); // Don't put end marker
17213
*func=0; // End marker
17214
DBUG_RETURN(FALSE);
17219
Change all funcs and sum_funcs to fields in tmp table, and create
17220
new list of all items.
17222
@param thd THD pointer
17223
@param ref_pointer_array array of pointers to top elements of filed list
17224
@param res_selected_fields new list of items of select item list
17225
@param res_all_fields new list of all items
17226
@param elements number of elements in select item list
17227
@param all_fields all fields list
17236
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
17237
List<Item> &res_selected_fields,
17238
List<Item> &res_all_fields,
17239
uint elements, List<Item> &all_fields)
17241
List_iterator_fast<Item> it(all_fields);
17242
Item *item_field,*item;
17243
DBUG_ENTER("change_to_use_tmp_fields");
17245
res_selected_fields.empty();
17246
res_all_fields.empty();
17248
uint i, border= all_fields.elements - elements;
17249
for (i= 0; (item= it++); i++)
17253
if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) ||
17254
(item->type() == Item::FUNC_ITEM &&
17255
((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
17259
if (item->type() == Item::FIELD_ITEM)
17261
item_field= item->get_tmp_table_item(thd);
17263
else if ((field= item->get_tmp_table_field()))
17265
if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
17266
item_field= ((Item_sum*) item)->result_item(field);
17268
item_field= (Item*) new Item_field(field);
17270
DBUG_RETURN(TRUE); // Fatal error
17272
if (item->real_item()->type() != Item::FIELD_ITEM)
17273
field->orig_table= 0;
17274
item_field->name= item->name;
17275
if (item->type() == Item::REF_ITEM)
17277
Item_field *ifield= (Item_field *) item_field;
17278
Item_ref *iref= (Item_ref *) item;
17279
ifield->table_name= iref->table_name;
17280
ifield->db_name= iref->db_name;
17283
if (!item_field->name)
17286
String str(buff,sizeof(buff),&my_charset_bin);
17288
item->print(&str, QT_ORDINARY);
17289
item_field->name= sql_strmake(str.ptr(),str.length());
17296
res_all_fields.push_back(item_field);
17297
ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17301
List_iterator_fast<Item> itr(res_all_fields);
17302
for (i= 0; i < border; i++)
17304
itr.sublist(res_selected_fields, elements);
17305
DBUG_RETURN(FALSE);
17310
Change all sum_func refs to fields to point at fields in tmp table.
17311
Change all funcs to be fields in tmp table.
17313
@param thd THD pointer
17314
@param ref_pointer_array array of pointers to top elements of filed list
17315
@param res_selected_fields new list of items of select item list
17316
@param res_all_fields new list of all items
17317
@param elements number of elements in select item list
17318
@param all_fields all fields list
17327
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
17328
List<Item> &res_selected_fields,
17329
List<Item> &res_all_fields, uint elements,
17330
List<Item> &all_fields)
17332
List_iterator_fast<Item> it(all_fields);
17333
Item *item, *new_item;
17334
res_selected_fields.empty();
17335
res_all_fields.empty();
17337
uint i, border= all_fields.elements - elements;
17338
for (i= 0; (item= it++); i++)
17340
res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
17341
ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17345
List_iterator_fast<Item> itr(res_all_fields);
17346
for (i= 0; i < border; i++)
17348
itr.sublist(res_selected_fields, elements);
17350
return thd->is_fatal_error;
17355
/******************************************************************************
17356
Code for calculating functions
17357
******************************************************************************/
17361
Call ::setup for all sum functions.
17363
@param thd thread handler
17364
@param func_ptr sum function list
17372
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
17375
DBUG_ENTER("setup_sum_funcs");
17376
while ((func= *(func_ptr++)))
17378
if (func->setup(thd))
17381
DBUG_RETURN(FALSE);
17386
init_tmptable_sum_functions(Item_sum **func_ptr)
17389
while ((func= *(func_ptr++)))
17390
func->reset_field();
17394
/** Update record 0 in tmp_table from record 1. */
17397
update_tmptable_sum_func(Item_sum **func_ptr,
17398
TABLE *tmp_table __attribute__((unused)))
17401
while ((func= *(func_ptr++)))
17402
func->update_field();
17406
/** Copy result of sum functions to record in tmp_table. */
17409
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
17411
for (; func_ptr != end_ptr ; func_ptr++)
17412
(void) (*func_ptr)->save_in_result_field(1);
17418
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
17420
for (; func_ptr != end_ptr ;func_ptr++)
17422
if ((*func_ptr)->reset())
17425
/* If rollup, calculate the upper sum levels */
17426
for ( ; *func_ptr ; func_ptr++)
17428
if ((*func_ptr)->add())
17436
update_sum_func(Item_sum **func_ptr)
17439
for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
17445
/** Copy result of functions to record in tmp_table. */
17448
copy_funcs(Item **func_ptr)
17451
for (; (func = *func_ptr) ; func_ptr++)
17452
func->save_in_result_field(1);
17457
Create a condition for a const reference and add this to the
17458
currenct select for the table.
17461
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
17463
DBUG_ENTER("add_ref_to_table_cond");
17464
if (!join_tab->ref.key_parts)
17465
DBUG_RETURN(FALSE);
17467
Item_cond_and *cond=new Item_cond_and();
17468
TABLE *table=join_tab->table;
17473
for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
17475
Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
17477
Item *value=join_tab->ref.items[i];
17478
cond->add(new Item_func_equal(new Item_field(field), value));
17480
if (thd->is_fatal_error)
17484
cond->fix_fields(thd, (Item**)&cond);
17485
if (join_tab->select)
17487
error=(int) cond->add(join_tab->select->cond);
17488
join_tab->select_cond=join_tab->select->cond=cond;
17490
else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
17492
join_tab->select_cond=cond;
17494
DBUG_RETURN(error ? TRUE : FALSE);
17499
Free joins of subselect of this select.
17501
@param thd THD pointer
17502
@param select pointer to st_select_lex which subselects joins we will free
17505
void free_underlaid_joins(THD *thd, SELECT_LEX *select)
17507
for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
17509
unit= unit->next_unit())
17513
/****************************************************************************
17515
****************************************************************************/
17518
Replace occurences of group by fields in an expression by ref items.
17520
The function replaces occurrences of group by fields in expr
17521
by ref objects for these fields unless they are under aggregate
17523
The function also corrects value of the the maybe_null attribute
17524
for the items of all subexpressions containing group by fields.
17528
SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
17529
SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
17534
The function recursively traverses the tree of the expr expression,
17535
looks for occurrences of the group by fields that are not under
17536
aggregate functions and replaces them for the corresponding ref items.
17539
This substitution is needed GROUP BY queries with ROLLUP if
17540
SELECT list contains expressions over group by attributes.
17542
@param thd reference to the context
17543
@param expr expression to make replacement
17544
@param group_list list of references to group by items
17545
@param changed out: returns 1 if item contains a replaced field item
17548
- TODO: Some functions are not null-preserving. For those functions
17549
updating of the maybe_null attribute is an overkill.
17557
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
17560
if (expr->arg_count)
17562
Name_resolution_context *context= &thd->lex->current_select->context;
17563
Item **arg,**arg_end;
17564
bool arg_changed= FALSE;
17565
for (arg= expr->arguments(),
17566
arg_end= expr->arguments()+expr->arg_count;
17567
arg != arg_end; arg++)
17570
if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
17573
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17575
if (item->eq(*group_tmp->item,0))
17578
if (!(new_item= new Item_ref(context, group_tmp->item, 0,
17580
return 1; // fatal_error is set
17581
thd->change_item_tree(arg, new_item);
17586
else if (item->type() == Item::FUNC_ITEM)
17588
if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
17594
expr->maybe_null= 1;
17602
/** Allocate memory needed for other rollup functions. */
17604
bool JOIN::rollup_init()
17609
tmp_table_param.quick_group= 0; // Can't create groups in tmp table
17610
rollup.state= ROLLUP::STATE_INITED;
17613
Create pointers to the different sum function groups
17614
These are updated by rollup_make_fields()
17616
tmp_table_param.group_parts= send_group_parts;
17618
if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17620
sizeof(List<Item>) +
17621
ref_pointer_array_size)
17622
* send_group_parts )))
17625
rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17626
rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17627
ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17630
Prepare space for field list for the different levels
17631
These will be filled up in rollup_make_fields()
17633
for (i= 0 ; i < send_group_parts ; i++)
17635
rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17636
List<Item> *rollup_fields= &rollup.fields[i];
17637
rollup_fields->empty();
17638
rollup.ref_pointer_arrays[i]= ref_array;
17639
ref_array+= all_fields.elements;
17641
for (i= 0 ; i < send_group_parts; i++)
17643
for (j=0 ; j < fields_list.elements ; j++)
17644
rollup.fields[i].push_back(rollup.null_items[i]);
17646
List_iterator<Item> it(all_fields);
17648
while ((item= it++))
17651
bool found_in_group= 0;
17653
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17655
if (*group_tmp->item == item)
17657
item->maybe_null= 1;
17659
if (item->const_item())
17662
For ROLLUP queries each constant item referenced in GROUP BY list
17663
is wrapped up into an Item_func object yielding the same value
17664
as the constant item. The objects of the wrapper class are never
17665
considered as constant items and besides they inherit all
17666
properties of the Item_result_field class.
17667
This wrapping allows us to ensure writing constant items
17668
into temporary tables whenever the result of the ROLLUP
17669
operation has to be written into a temporary table, e.g. when
17670
ROLLUP is used together with DISTINCT in the SELECT list.
17671
Usually when creating temporary tables for a intermidiate
17672
result we do not include fields for constant expressions.
17674
Item* new_item= new Item_func_rollup_const(item);
17677
new_item->fix_fields(thd, (Item **) 0);
17678
thd->change_item_tree(it.ref(), new_item);
17679
for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17681
if (*tmp->item == item)
17682
thd->change_item_tree(tmp->item, new_item);
17687
if (item->type() == Item::FUNC_ITEM && !found_in_group)
17689
bool changed= FALSE;
17690
if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17693
We have to prevent creation of a field in a temporary table for
17694
an expression that contains GROUP BY attributes.
17695
Marking the expression item as 'with_sum_func' will ensure this.
17698
item->with_sum_func= 1;
17706
Fill up rollup structures with pointers to fields to use.
17708
Creates copies of item_sum items for each sum level.
17710
@param fields_arg List of all fields (hidden and real ones)
17711
@param sel_fields Pointer to selected fields
17712
@param func Store here a pointer to all fields
17716
In this case func is pointing to next not used element.
17721
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17724
List_iterator_fast<Item> it(fields_arg);
17725
Item *first_field= sel_fields.head();
17729
Create field lists for the different levels
17731
The idea here is to have a separate field list for each rollup level to
17732
avoid all runtime checks of which columns should be NULL.
17734
The list is stored in reverse order to get sum function in such an order
17735
in func that it makes it easy to reset them with init_sum_functions()
17737
Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17739
rollup.fields[0] will contain list where a,b,c is NULL
17740
rollup.fields[1] will contain list where b,c is NULL
17742
rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17744
sum_funcs_end[0] points to all sum functions
17745
sum_funcs_end[1] points to all sum functions, except grand totals
17749
for (level=0 ; level < send_group_parts ; level++)
17752
uint pos= send_group_parts - level -1;
17753
bool real_fields= 0;
17755
List_iterator<Item> new_it(rollup.fields[pos]);
17756
Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17757
ORDER *start_group;
17759
/* Point to first hidden field */
17760
Item **ref_array= ref_array_start + fields_arg.elements-1;
17762
/* Remember where the sum functions ends for the previous level */
17763
sum_funcs_end[pos+1]= *func;
17765
/* Find the start of the group for this level */
17766
for (i= 0, start_group= group_list ;
17768
start_group= start_group->next)
17772
while ((item= it++))
17774
if (item == first_field)
17776
real_fields= 1; // End of hidden fields
17777
ref_array= ref_array_start;
17780
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17781
(!((Item_sum*) item)->depended_from() ||
17782
((Item_sum *)item)->depended_from() == select_lex))
17786
This is a top level summary function that must be replaced with
17787
a sum function that is reset for this level.
17789
NOTE: This code creates an object which is not that nice in a
17790
sub select. Fortunately it's not common to have rollup in
17793
item= item->copy_or_same(thd);
17794
((Item_sum*) item)->make_unique();
17795
*(*func)= (Item_sum*) item;
17800
/* Check if this is something that is part of this group by */
17802
for (group_tmp= start_group, i= pos ;
17803
group_tmp ; group_tmp= group_tmp->next, i++)
17805
if (*group_tmp->item == item)
17808
This is an element that is used by the GROUP BY and should be
17809
set to NULL in this level
17811
Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17814
item->maybe_null= 1; // Value will be null sometimes
17815
null_item->result_field= item->get_tmp_table_field();
17824
(void) new_it++; // Point to next item
17825
new_it.replace(item); // Replace previous
17832
sum_funcs_end[0]= *func; // Point to last function
17837
Send all rollup levels higher than the current one to the client.
17841
SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17844
@param idx Level we are on:
17845
- 0 = Total sum level
17846
- 1 = First group changed (a)
17847
- 2 = Second group changed (a,b)
17852
1 If send_data_failed()
17855
int JOIN::rollup_send_data(uint idx)
17858
for (i= send_group_parts ; i-- > idx ; )
17860
/* Get reference pointers to sum functions in place */
17861
memcpy((char*) ref_pointer_array,
17862
(char*) rollup.ref_pointer_arrays[i],
17863
ref_pointer_array_size);
17864
if ((!having || having->val_int()))
17866
if (send_records < unit->select_limit_cnt && do_send_rows &&
17867
result->send_data(rollup.fields[i]))
17872
/* Restore ref_pointer_array */
17873
set_items_ref_array(current_ref_pointer_array);
17878
Write all rollup levels higher than the current one to a temp table.
17882
SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17885
@param idx Level we are on:
17886
- 0 = Total sum level
17887
- 1 = First group changed (a)
17888
- 2 = Second group changed (a,b)
17889
@param table reference to temp table
17894
1 if write_data_failed()
17897
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17900
for (i= send_group_parts ; i-- > idx ; )
17902
/* Get reference pointers to sum functions in place */
17903
memcpy((char*) ref_pointer_array,
17904
(char*) rollup.ref_pointer_arrays[i],
17905
ref_pointer_array_size);
17906
if ((!having || having->val_int()))
17910
List_iterator_fast<Item> it(rollup.fields[i]);
17911
while ((item= it++))
17913
if (item->type() == Item::NULL_ITEM && item->is_result_field())
17914
item->save_in_result_field(1);
17916
copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17917
if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17919
if (create_myisam_from_heap(thd, table_arg,
17920
tmp_table_param.start_recinfo,
17921
&tmp_table_param.recinfo,
17927
/* Restore ref_pointer_array */
17928
set_items_ref_array(current_ref_pointer_array);
17933
clear results if there are not rows found for group
17934
(end_send_group/end_write_group)
17939
clear_tables(this);
17940
copy_fields(&tmp_table_param);
17944
Item_sum *func, **func_ptr= sum_funcs;
17945
while ((func= *(func_ptr++)))
17953
Send a description about what how the select will be done to stdout.
17956
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17957
bool distinct,const char *message)
17959
List<Item> field_list;
17960
List<Item> item_list;
17961
THD *thd=join->thd;
17962
select_result *result=join->result;
17963
Item *item_null= new Item_null();
17964
CHARSET_INFO *cs= system_charset_info;
17966
DBUG_ENTER("select_describe");
17967
DBUG_PRINT("info", ("Select 0x%lx, type %s, message %s",
17968
(ulong)join->select_lex, join->select_lex->type,
17969
message ? message : "NULL"));
17970
/* Don't log this into the slow query log */
17971
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17972
join->unit->offset_limit_cnt= 0;
17975
NOTE: the number/types of items pushed into item_list must be in sync with
17976
EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17980
item_list.push_back(new Item_int((int32)
17981
join->select_lex->select_number));
17982
item_list.push_back(new Item_string(join->select_lex->type,
17983
strlen(join->select_lex->type), cs));
17984
for (uint i=0 ; i < 7; i++)
17985
item_list.push_back(item_null);
17986
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17987
item_list.push_back(item_null);
17989
item_list.push_back(new Item_string(message,strlen(message),cs));
17990
if (result->send_data(item_list))
17993
else if (join->select_lex == join->unit->fake_select_lex)
17996
here we assume that the query will return at least two rows, so we
17997
show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17998
and no filesort will be actually done, but executing all selects in
17999
the UNION to provide precise EXPLAIN information will hardly be
18002
char table_name_buffer[NAME_LEN];
18005
item_list.push_back(new Item_null);
18007
item_list.push_back(new Item_string(join->select_lex->type,
18008
strlen(join->select_lex->type),
18012
SELECT_LEX *sl= join->unit->first_select();
18013
uint len= 6, lastop= 0;
18014
memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
18015
for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
18018
lastop= my_snprintf(table_name_buffer + len, NAME_LEN - len,
18019
"%u,", sl->select_number);
18021
if (sl || len + lastop >= NAME_LEN)
18023
memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
18029
table_name_buffer[len - 1]= '>'; // change ',' to '>'
18031
item_list.push_back(new Item_string(table_name_buffer, len, cs));
18034
item_list.push_back(new Item_string(join_type_str[JT_ALL],
18035
strlen(join_type_str[JT_ALL]),
18037
/* possible_keys */
18038
item_list.push_back(item_null);
18040
item_list.push_back(item_null);
18042
item_list.push_back(item_null);
18044
item_list.push_back(item_null);
18046
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
18047
item_list.push_back(item_null);
18049
item_list.push_back(item_null);
18051
if (join->unit->global_parameters->order_list.first)
18052
item_list.push_back(new Item_string("Using filesort",
18055
item_list.push_back(new Item_string("", 0, cs));
18057
if (result->send_data(item_list))
18062
table_map used_tables=0;
18063
for (uint i=0 ; i < join->tables ; i++)
18065
JOIN_TAB *tab=join->join_tab+i;
18066
TABLE *table=tab->table;
18067
TABLE_LIST *table_list= tab->table->pos_in_table_list;
18069
char buff1[512], buff2[512], buff3[512];
18070
char keylen_str_buf[64];
18071
String extra(buff, sizeof(buff),cs);
18072
char table_name_buffer[NAME_LEN];
18073
String tmp1(buff1,sizeof(buff1),cs);
18074
String tmp2(buff2,sizeof(buff2),cs);
18075
String tmp3(buff3,sizeof(buff3),cs);
18084
item_list.push_back(new Item_uint((uint32)
18085
join->select_lex->select_number));
18087
item_list.push_back(new Item_string(join->select_lex->type,
18088
strlen(join->select_lex->type),
18090
if (tab->type == JT_ALL && tab->select && tab->select->quick)
18092
quick_type= tab->select->quick->get_type();
18093
if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
18094
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
18095
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
18096
tab->type = JT_INDEX_MERGE;
18098
tab->type = JT_RANGE;
18101
if (table->derived_select_number)
18103
/* Derived table name generation */
18104
int len= my_snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
18106
table->derived_select_number);
18107
item_list.push_back(new Item_string(table_name_buffer, len, cs));
18111
TABLE_LIST *real_table= table->pos_in_table_list;
18112
item_list.push_back(new Item_string(real_table->alias,
18113
strlen(real_table->alias),
18116
/* "type" column */
18117
item_list.push_back(new Item_string(join_type_str[tab->type],
18118
strlen(join_type_str[tab->type]),
18120
/* Build "possible_keys" value and add it to item_list */
18121
if (!tab->keys.is_clear_all())
18124
for (j=0 ; j < table->s->keys ; j++)
18126
if (tab->keys.is_set(j))
18130
tmp1.append(table->key_info[j].name,
18131
strlen(table->key_info[j].name),
18132
system_charset_info);
18137
item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
18139
item_list.push_back(item_null);
18141
/* Build "key", "key_len", and "ref" values and add them to item_list */
18142
if (tab->ref.key_parts)
18144
KEY *key_info=table->key_info+ tab->ref.key;
18145
register uint length;
18146
item_list.push_back(new Item_string(key_info->name,
18147
strlen(key_info->name),
18148
system_charset_info));
18149
length= longlong2str(tab->ref.key_length, keylen_str_buf, 10) -
18151
item_list.push_back(new Item_string(keylen_str_buf, length,
18152
system_charset_info));
18153
for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
18157
tmp2.append((*ref)->name(), strlen((*ref)->name()),
18158
system_charset_info);
18160
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
18162
else if (tab->type == JT_NEXT)
18164
KEY *key_info=table->key_info+ tab->index;
18165
register uint length;
18166
item_list.push_back(new Item_string(key_info->name,
18167
strlen(key_info->name),cs));
18168
length= longlong2str(key_info->key_length, keylen_str_buf, 10) -
18170
item_list.push_back(new Item_string(keylen_str_buf,
18172
system_charset_info));
18173
item_list.push_back(item_null);
18175
else if (tab->select && tab->select->quick)
18177
tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
18178
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
18179
item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
18180
item_list.push_back(item_null);
18184
if (table_list->schema_table &&
18185
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18187
const char *tmp_buff;
18189
if (table_list->has_db_lookup_value)
18191
f_idx= table_list->schema_table->idx_field1;
18192
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
18193
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
18195
if (table_list->has_table_lookup_value)
18197
if (table_list->has_db_lookup_value)
18199
f_idx= table_list->schema_table->idx_field2;
18200
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
18201
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
18204
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
18206
item_list.push_back(item_null);
18209
item_list.push_back(item_null);
18210
item_list.push_back(item_null);
18211
item_list.push_back(item_null);
18214
/* Add "rows" field to item_list. */
18215
if (table_list->schema_table)
18218
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
18219
item_list.push_back(item_null);
18221
item_list.push_back(item_null);
18225
double examined_rows;
18226
if (tab->select && tab->select->quick)
18227
examined_rows= rows2double(tab->select->quick->records);
18228
else if (tab->type == JT_NEXT || tab->type == JT_ALL)
18229
examined_rows= rows2double(tab->limit ? tab->limit :
18230
tab->table->file->records());
18232
examined_rows= join->best_positions[i].records_read;
18234
item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows,
18235
MY_INT64_NUM_DECIMAL_DIGITS));
18237
/* Add "filtered" field to item_list. */
18238
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
18242
f= (float) (100.0 * join->best_positions[i].records_read /
18244
item_list.push_back(new Item_float(f, 2));
18248
/* Build "Extra" field and add it to item_list. */
18249
my_bool key_read=table->key_read;
18250
if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
18251
table->covering_keys.is_set(tab->index))
18253
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
18254
!((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
18258
item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
18259
else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
18261
if (tab->packed_info & TAB_INFO_USING_INDEX)
18262
extra.append(STRING_WITH_LEN("; Using index"));
18263
if (tab->packed_info & TAB_INFO_USING_WHERE)
18264
extra.append(STRING_WITH_LEN("; Using where"));
18265
if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
18266
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18267
/* Skip initial "; "*/
18268
const char *str= extra.ptr();
18269
uint32 len= extra.length();
18275
item_list.push_back(new Item_string(str, len, cs));
18279
uint keyno= MAX_KEY;
18280
if (tab->ref.key_parts)
18281
keyno= tab->ref.key;
18282
else if (tab->select && tab->select->quick)
18283
keyno = tab->select->quick->index;
18285
if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
18286
table->file->pushed_idx_cond)
18287
extra.append(STRING_WITH_LEN("; Using index condition"));
18289
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
18290
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
18291
quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
18293
extra.append(STRING_WITH_LEN("; Using "));
18294
tab->select->quick->add_info_string(&extra);
18298
if (tab->use_quick == 2)
18300
/* 4 bits per 1 hex digit + terminating '\0' */
18301
char buf[MAX_KEY / 4 + 1];
18302
extra.append(STRING_WITH_LEN("; Range checked for each "
18303
"record (index map: 0x"));
18304
extra.append(tab->keys.print(buf));
18307
else if (tab->select->cond)
18309
const COND *pushed_cond= tab->table->file->pushed_cond;
18311
if (thd->variables.engine_condition_pushdown && pushed_cond)
18313
extra.append(STRING_WITH_LEN("; Using where with pushed "
18315
if (thd->lex->describe & DESCRIBE_EXTENDED)
18317
extra.append(STRING_WITH_LEN(": "));
18318
((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
18322
extra.append(STRING_WITH_LEN("; Using where"));
18327
if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
18328
extra.append(STRING_WITH_LEN("; Using index for group-by"));
18330
extra.append(STRING_WITH_LEN("; Using index"));
18332
if (table->reginfo.not_exists_optimize)
18333
extra.append(STRING_WITH_LEN("; Not exists"));
18335
if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
18336
!(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
18337
HA_MRR_USE_DEFAULT_IMPL))
18339
extra.append(STRING_WITH_LEN("; Using MRR"));
18342
if (table_list->schema_table &&
18343
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18345
if (!table_list->table_open_method)
18346
extra.append(STRING_WITH_LEN("; Skip_open_table"));
18347
else if (table_list->table_open_method == OPEN_FRM_ONLY)
18348
extra.append(STRING_WITH_LEN("; Open_frm_only"));
18350
extra.append(STRING_WITH_LEN("; Open_full_table"));
18351
if (table_list->has_db_lookup_value &&
18352
table_list->has_table_lookup_value)
18353
extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18354
else if (table_list->has_db_lookup_value ||
18355
table_list->has_table_lookup_value)
18356
extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18358
extra.append(STRING_WITH_LEN("; Scanned all databases"));
18360
if (need_tmp_table)
18363
extra.append(STRING_WITH_LEN("; Using temporary"));
18368
extra.append(STRING_WITH_LEN("; Using filesort"));
18370
if (distinct & test_all_bits(used_tables,thd->used_tables))
18371
extra.append(STRING_WITH_LEN("; Distinct"));
18373
if (tab->insideout_match_tab)
18375
extra.append(STRING_WITH_LEN("; LooseScan"));
18378
if (tab->flush_weedout_table)
18379
extra.append(STRING_WITH_LEN("; Start temporary"));
18380
else if (tab->check_weed_out_table)
18381
extra.append(STRING_WITH_LEN("; End temporary"));
18382
else if (tab->do_firstmatch)
18384
extra.append(STRING_WITH_LEN("; FirstMatch("));
18385
TABLE *prev_table=tab->do_firstmatch->table;
18386
if (prev_table->derived_select_number)
18388
char namebuf[NAME_LEN];
18389
/* Derived table name generation */
18390
int len= my_snprintf(namebuf, sizeof(namebuf)-1,
18392
prev_table->derived_select_number);
18393
extra.append(namebuf, len);
18396
extra.append(prev_table->pos_in_table_list->alias);
18397
extra.append(STRING_WITH_LEN(")"));
18400
for (uint part= 0; part < tab->ref.key_parts; part++)
18402
if (tab->ref.cond_guards[part])
18404
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18409
if (i > 0 && tab[-1].next_select == sub_select_cache)
18410
extra.append(STRING_WITH_LEN("; Using join buffer"));
18412
/* Skip initial "; "*/
18413
const char *str= extra.ptr();
18414
uint32 len= extra.length();
18420
item_list.push_back(new Item_string(str, len, cs));
18422
// For next iteration
18423
used_tables|=table->map;
18424
if (result->send_data(item_list))
18428
for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18430
unit= unit->next_unit())
18432
if (mysql_explain_union(thd, unit, result))
18439
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18441
DBUG_ENTER("mysql_explain_union");
18443
SELECT_LEX *first= unit->first_select();
18445
for (SELECT_LEX *sl= first;
18447
sl= sl->next_select())
18449
// drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18450
uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18451
sl->type= (((&thd->lex->select_lex)==sl)?
18452
(sl->first_inner_unit() || sl->next_select() ?
18453
"PRIMARY" : "SIMPLE"):
18455
((sl->linkage == DERIVED_TABLE_TYPE) ?
18457
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18458
"DEPENDENT SUBQUERY":
18459
(uncacheable?"UNCACHEABLE SUBQUERY":
18461
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18463
uncacheable?"UNCACHEABLE UNION":
18465
sl->options|= SELECT_DESCRIBE;
18467
if (unit->is_union())
18469
unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18470
unit->fake_select_lex->type= "UNION RESULT";
18471
unit->fake_select_lex->options|= SELECT_DESCRIBE;
18472
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18474
res|= unit->cleanup();
18478
thd->lex->current_select= first;
18479
unit->set_limit(unit->global_parameters);
18480
res= mysql_select(thd, &first->ref_pointer_array,
18481
(TABLE_LIST*) first->table_list.first,
18482
first->with_wild, first->item_list,
18484
first->order_list.elements +
18485
first->group_list.elements,
18486
(ORDER*) first->order_list.first,
18487
(ORDER*) first->group_list.first,
18489
(ORDER*) thd->lex->proc_list.first,
18490
first->options | thd->options | SELECT_DESCRIBE,
18491
result, unit, first);
18493
DBUG_RETURN(res || thd->is_error());
18497
static void print_table_array(THD *thd, String *str, TABLE_LIST **table,
18500
(*table)->print(thd, str, QT_ORDINARY);
18502
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18504
TABLE_LIST *curr= *tbl;
18505
if (curr->outer_join)
18507
/* MySQL converts right to left joins */
18508
str->append(STRING_WITH_LEN(" left join "));
18510
else if (curr->straight)
18511
str->append(STRING_WITH_LEN(" straight_join "));
18512
else if (curr->sj_inner_tables)
18513
str->append(STRING_WITH_LEN(" semi join "));
18515
str->append(STRING_WITH_LEN(" join "));
18516
curr->print(thd, str, QT_ORDINARY);
18519
str->append(STRING_WITH_LEN(" on("));
18520
curr->on_expr->print(str, QT_ORDINARY);
18528
Print joins from the FROM clause.
18529
@param thd thread handler
18530
@param str string where table should be printed
18531
@param tables list of tables in join
18532
@query_type type of the query is being generated
18535
static void print_join(THD *thd,
18537
List<TABLE_LIST> *tables,
18538
enum_query_type query_type)
18540
/* List is reversed => we should reverse it before using */
18541
List_iterator_fast<TABLE_LIST> ti(*tables);
18542
TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
18545
return; // out of memory
18547
for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
18551
If the first table is a semi-join nest, swap it with something that is
18552
not a semi-join nest.
18554
if ((*table)->sj_inner_tables)
18556
TABLE_LIST **end= table + tables->elements;
18557
for (TABLE_LIST **t2= table; t2!=end; t2++)
18559
if (!(*t2)->sj_inner_tables)
18561
TABLE_LIST *tmp= *t2;
18568
DBUG_ASSERT(tables->elements >= 1);
18569
print_table_array(thd, str, table, table + tables->elements);
18574
@brief Print an index hint
18576
@details Prints out the USE|FORCE|IGNORE index hint.
18578
@param thd the current thread
18579
@param[out] str appends the index hint here
18580
@param hint what the hint is (as string : "USE INDEX"|
18581
"FORCE INDEX"|"IGNORE INDEX")
18582
@param hint_length the length of the string in 'hint'
18583
@param indexes a list of index names for the hint
18587
Index_hint::print(THD *thd, String *str)
18591
case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
18592
case INDEX_HINT_USE: str->append(STRING_WITH_LEN("USE INDEX")); break;
18593
case INDEX_HINT_FORCE: str->append(STRING_WITH_LEN("FORCE INDEX")); break;
18595
str->append (STRING_WITH_LEN(" ("));
18596
if (key_name.length)
18598
if (thd && !my_strnncoll(system_charset_info,
18599
(const uchar *)key_name.str, key_name.length,
18600
(const uchar *)primary_key_name,
18601
strlen(primary_key_name)))
18602
str->append(primary_key_name);
18604
append_identifier(thd, str, key_name.str, key_name.length);
18611
Print table as it should be in join list.
18613
@param str string where table should be printed
18616
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
18621
print_join(thd, str, &nested_join->join_list, query_type);
18626
const char *cmp_name; // Name to compare with alias
18631
derived->print(str, query_type);
18633
cmp_name= ""; // Force printing of alias
18639
append_identifier(thd, str, db, db_length);
18644
append_identifier(thd, str, schema_table_name,
18645
strlen(schema_table_name));
18646
cmp_name= schema_table_name;
18650
append_identifier(thd, str, table_name, table_name_length);
18651
cmp_name= table_name;
18654
if (my_strcasecmp(table_alias_charset, cmp_name, alias))
18656
char t_alias_buff[MAX_ALIAS_NAME];
18657
const char *t_alias= alias;
18660
if (lower_case_table_names== 1)
18662
if (alias && alias[0])
18664
strmov(t_alias_buff, alias);
18665
my_casedn_str(files_charset_info, t_alias_buff);
18666
t_alias= t_alias_buff;
18670
append_identifier(thd, str, t_alias, strlen(t_alias));
18675
List_iterator<Index_hint> it(*index_hints);
18678
while ((hint= it++))
18680
str->append (STRING_WITH_LEN(" "));
18681
hint->print (thd, str);
18688
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
18690
/* QQ: thd may not be set for sub queries, but this should be fixed */
18694
str->append(STRING_WITH_LEN("select "));
18696
/* First add options */
18697
if (options & SELECT_STRAIGHT_JOIN)
18698
str->append(STRING_WITH_LEN("straight_join "));
18699
if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
18700
(this == &thd->lex->select_lex))
18701
str->append(STRING_WITH_LEN("high_priority "));
18702
if (options & SELECT_DISTINCT)
18703
str->append(STRING_WITH_LEN("distinct "));
18704
if (options & SELECT_SMALL_RESULT)
18705
str->append(STRING_WITH_LEN("sql_small_result "));
18706
if (options & SELECT_BIG_RESULT)
18707
str->append(STRING_WITH_LEN("sql_big_result "));
18708
if (options & OPTION_BUFFER_RESULT)
18709
str->append(STRING_WITH_LEN("sql_buffer_result "));
18710
if (options & OPTION_FOUND_ROWS)
18711
str->append(STRING_WITH_LEN("sql_calc_found_rows "));
18715
List_iterator_fast<Item> it(item_list);
18717
while ((item= it++))
18723
item->print_item_w_name(str, query_type);
18728
TODO: support USING/FORCE/IGNORE index
18730
if (table_list.elements)
18732
str->append(STRING_WITH_LEN(" from "));
18733
/* go through join tree */
18734
print_join(thd, str, &top_join_list, query_type);
18739
"SELECT 1 FROM DUAL WHERE 2" should not be printed as
18740
"SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
18742
str->append(STRING_WITH_LEN(" from DUAL "));
18746
Item *cur_where= where;
18748
cur_where= join->conds;
18749
if (cur_where || cond_value != Item::COND_UNDEF)
18751
str->append(STRING_WITH_LEN(" where "));
18753
cur_where->print(str, query_type);
18755
str->append(cond_value != Item::COND_FALSE ? "1" : "0");
18759
if (group_list.elements)
18761
str->append(STRING_WITH_LEN(" group by "));
18762
print_order(str, (ORDER *) group_list.first, query_type);
18766
str->append(STRING_WITH_LEN(" with cube"));
18769
str->append(STRING_WITH_LEN(" with rollup"));
18772
; //satisfy compiler
18777
Item *cur_having= having;
18779
cur_having= join->having;
18781
if (cur_having || having_value != Item::COND_UNDEF)
18783
str->append(STRING_WITH_LEN(" having "));
18785
cur_having->print(str, query_type);
18787
str->append(having_value != Item::COND_FALSE ? "1" : "0");
18790
if (order_list.elements)
18792
str->append(STRING_WITH_LEN(" order by "));
18793
print_order(str, (ORDER *) order_list.first, query_type);
18797
print_limit(thd, str, query_type);
18799
// PROCEDURE unsupported here
18804
change select_result object of JOIN.
18806
@param res new select_result object
18814
bool JOIN::change_result(select_result *res)
18816
DBUG_ENTER("JOIN::change_result");
18818
if (result->prepare(fields_list, select_lex->master_unit()) ||
18819
result->prepare2())
18823
DBUG_RETURN(FALSE);
18827
@} (end of group Query_Optimizer)