20
20
mysql_select and join optimization
22
23
@defgroup Query_Optimizer Query Optimizer
32
#include "drizzled/sql_select.h" /* include join.h */
34
#include "drizzled/error.h"
35
#include "drizzled/gettext.h"
36
#include "drizzled/util/test.h"
37
#include "drizzled/name_resolution_context_state.h"
38
#include "drizzled/nested_join.h"
39
#include "drizzled/probes.h"
40
#include "drizzled/show.h"
41
#include "drizzled/item/cache.h"
42
#include "drizzled/item/cmpfunc.h"
43
#include "drizzled/item/copy_string.h"
44
#include "drizzled/item/uint.h"
45
#include "drizzled/cached_item.h"
46
#include "drizzled/sql_base.h"
47
#include "drizzled/field/blob.h"
48
#include "drizzled/check_stack_overrun.h"
49
#include "drizzled/lock.h"
50
#include "drizzled/item/outer_ref.h"
51
#include "drizzled/index_hint.h"
52
#include "drizzled/records.h"
53
#include "drizzled/internal/iocache.h"
54
#include "drizzled/drizzled.h"
56
#include "drizzled/sql_union.h"
57
#include "drizzled/optimizer/key_field.h"
58
#include "drizzled/optimizer/position.h"
59
#include "drizzled/optimizer/sargable_param.h"
60
#include "drizzled/optimizer/key_use.h"
61
#include "drizzled/optimizer/range.h"
62
#include "drizzled/optimizer/quick_range_select.h"
63
#include "drizzled/optimizer/quick_ror_intersect_select.h"
65
#include "drizzled/filesort.h"
72
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
73
static COND *build_equal_items(Session *session, COND *cond,
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,
74
102
COND_EQUAL *inherited,
75
List<TableList> *join_list,
103
List<TABLE_LIST> *join_list,
76
104
COND_EQUAL **cond_equal_ref);
78
static Item* part_of_refkey(Table *form,Field *field);
79
static bool cmp_buffer_with_ref(JoinTable *tab);
80
static void change_cond_ref_to_const(Session *session,
81
vector<COND_CMP>& save_list,
86
static bool copy_blobs(Field **ptr);
88
static bool eval_const_cond(COND *cond)
90
return ((Item_func*) cond)->val_int() ? true : false;
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
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
173
ha_rows select_limit, bool no_changes,
175
static bool list_contains_unique_index(TABLE *table,
176
bool (*find_func) (Field *, void *), void *data);
177
static bool find_field_in_item_list (Field *field, void *data);
178
static bool find_field_in_order_list (Field *field, void *data);
179
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
180
ha_rows filesort_limit, ha_rows select_limit,
182
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
184
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
185
ulong offset,Item *having);
186
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
187
uint field_count, Field **first_field,
189
ulong key_length,Item *having);
190
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
191
static ulong used_blob_length(CACHE_FIELD **ptr);
192
static bool store_record_in_cache(JOIN_CACHE *cache);
193
static void reset_cache_read(JOIN_CACHE *cache);
194
static void reset_cache_write(JOIN_CACHE *cache);
195
static void read_cached_record(JOIN_TAB *tab);
196
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
197
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
198
ORDER *order, List<Item> &fields,
199
List<Item> &all_fields,
200
bool *all_order_by_fields_used);
201
static bool test_if_subpart(ORDER *a,ORDER *b);
202
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
203
static void calc_group_buffer(JOIN *join,ORDER *group);
204
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
205
static bool alloc_group_fields(JOIN *join,ORDER *group);
206
// Create list for using with tempory table
207
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
208
List<Item> &new_list1,
209
List<Item> &new_list2,
210
uint elements, List<Item> &items);
211
// Create list for using with tempory table
212
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
213
List<Item> &new_list1,
214
List<Item> &new_list2,
215
uint elements, List<Item> &items);
216
static void init_tmptable_sum_functions(Item_sum **func);
217
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
218
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
219
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
220
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
221
static bool init_sum_functions(Item_sum **func, Item_sum **end);
222
static bool update_sum_func(Item_sum **func);
223
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
224
bool distinct, const char *message=NullS);
225
static Item *remove_additional_cond(Item* conds);
226
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
227
static bool test_if_ref(Item_field *left_item,Item *right_item);
228
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
229
Item *new_cond, bool fix_fields);
94
232
This is used to mark equalities that were made from i-th IN-equality.
261
391
ref->outer_ref= new_ref;
262
392
ref->ref= &ref->outer_ref;
264
if (!ref->fixed && ref->fix_fields(session, 0))
394
if (!ref->fixed && ref->fix_fields(thd, 0))
266
session->used_tables|= item->used_tables();
396
thd->used_tables|= item->used_tables();
401
#define MAGIC_IN_WHERE_TOP_LEVEL 10
403
Function to setup clauses without sum functions.
405
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
409
List<Item> &all_fields,
412
ORDER *group, bool *hidden_group_fields)
415
nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
417
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
418
res= setup_conds(thd, tables, leaves, conds);
420
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
421
res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
423
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
424
res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
425
group, hidden_group_fields);
426
thd->lex->allow_sum_func= save_allow_sum_func;
271
430
/*****************************************************************************
272
431
Check fields, find best join, do the select and output fields.
273
432
mysql_select assumes that all tables are already opened
274
433
*****************************************************************************/
436
Prepare of whole select (including sub queries in future).
439
Add check of calculation of GROUP functions and fields:
440
SELECT COUNT(*)+table.col1 from table1;
448
JOIN::prepare(Item ***rref_pointer_array,
449
TABLE_LIST *tables_init,
450
uint wild_num, COND *conds_init, uint og_num,
451
ORDER *order_init, ORDER *group_init,
453
ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
454
SELECT_LEX_UNIT *unit_arg)
456
// to prevent double initialization on EXPLAIN
462
group_list= group_init;
464
proc_param= proc_param_init;
465
tables_list= tables_init;
466
select_lex= select_lex_arg;
467
select_lex->join= this;
468
join_list= &select_lex->top_join_list;
469
union_part= unit_arg->is_union();
471
thd->lex->current_select->is_item_list_lookup= 1;
473
If we have already executed SELECT, then it have not sense to prevent
474
its table from update (see unique_table())
476
if (thd->derived_tables_processing)
477
select_lex->exclude_from_table_unique_test= true;
479
/* Check that all tables, fields, conds and order are ok */
481
if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
482
setup_tables_and_check_access(thd, &select_lex->context, join_list,
483
tables_list, &select_lex->leaf_tables,
487
TABLE_LIST *table_ptr;
488
for (table_ptr= select_lex->leaf_tables;
490
table_ptr= table_ptr->next_leaf)
493
if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
494
select_lex->setup_ref_array(thd, og_num) ||
495
setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
497
setup_without_group(thd, (*rref_pointer_array), tables_list,
498
select_lex->leaf_tables, fields_list,
499
all_fields, &conds, order, group_list,
500
&hidden_group_fields))
501
return(-1); /* purecov: inspected */
503
ref_pointer_array= *rref_pointer_array;
507
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
508
thd->where="having clause";
509
thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
510
select_lex->having_fix_field= 1;
511
bool having_fix_rc= (!having->fixed &&
512
(having->fix_fields(thd, &having) ||
513
having->check_cols(1)));
514
select_lex->having_fix_field= 0;
515
if (having_fix_rc || thd->is_error())
516
return(-1); /* purecov: inspected */
517
thd->lex->allow_sum_func= save_allow_sum_func;
520
if (!thd->lex->view_prepare_mode)
522
Item_subselect *subselect;
523
Item_in_subselect *in_subs= NULL;
525
Are we in a subquery predicate?
526
TODO: the block below will be executed for every PS execution without need.
528
if ((subselect= select_lex->master_unit()->item))
530
bool do_semijoin= !test(thd->variables.optimizer_switch &
531
OPTIMIZER_SWITCH_NO_SEMIJOIN);
532
if (subselect->substype() == Item_subselect::IN_SUBS)
533
in_subs= (Item_in_subselect*)subselect;
536
Check if we're in subquery that is a candidate for flattening into a
537
semi-join (which is done done in flatten_subqueries()). The
539
1. Subquery predicate is an IN/=ANY subq predicate
540
2. Subquery is a single SELECT (not a UNION)
541
3. Subquery does not have GROUP BY or ORDER BY
542
4. Subquery does not use aggregate functions or HAVING
543
5. Subquery predicate is at the AND-top-level of ON/WHERE clause
544
6. No execution method was already chosen (by a prepared statement).
546
(*). We are not in a subquery of a single table UPDATE/DELETE that
547
doesn't have a JOIN (TODO: We should handle this at some
548
point by switching to multi-table UPDATE/DELETE)
550
(**). We're not in a confluent table-less subquery, like
554
!select_lex->master_unit()->first_select()->next_select() && // 2
555
!select_lex->group_list.elements && !order && // 3
556
!having && !select_lex->with_sum_func && // 4
557
thd->thd_marker && // 5
558
select_lex->outer_select()->join && // (*)
559
select_lex->master_unit()->first_select()->leaf_tables && // (**)
561
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
564
if (!in_subs->left_expr->fixed &&
565
in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
570
Check that the right part of the subselect contains no more than one
571
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
573
if (subselect->substype() == Item_subselect::IN_SUBS &&
574
(select_lex->item_list.elements !=
575
((Item_in_subselect*)subselect)->left_expr->cols()))
577
my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
582
/* Register the subquery for further processing */
583
select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
584
in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
588
bool do_materialize= !test(thd->variables.optimizer_switch &
589
OPTIMIZER_SWITCH_NO_MATERIALIZATION);
591
Check if the subquery predicate can be executed via materialization.
592
The required conditions are:
593
1. Subquery predicate is an IN/=ANY subq predicate
594
2. Subquery is a single SELECT (not a UNION)
595
3. Subquery is not a table-less query. In this case there is no
596
point in materializing.
597
4. Subquery predicate is a top-level predicate
598
(this implies it is not negated)
599
TODO: this is a limitation that should be lifeted once we
600
implement correct NULL semantics (WL#3830)
601
5. Subquery is non-correlated
603
This is an overly restrictive condition. It can be extended to:
604
(Subquery is non-correlated ||
605
Subquery is correlated to any query outer to IN predicate ||
606
(Subquery is correlated to the immediate outer query &&
607
Subquery !contains {GROUP BY, ORDER BY [LIMIT],
608
aggregate functions) && subquery predicate is not under "NOT IN"))
609
6. No execution method was already chosen (by a prepared statement).
611
(*) The subquery must be part of a SELECT statement. The current
612
condition also excludes multi-table update statements.
614
We have to determine whether we will perform subquery materialization
615
before calling the IN=>EXISTS transformation, so that we know whether to
616
perform the whole transformation or only that part of it which wraps
617
Item_in_subselect in an Item_in_optimizer.
619
if (do_materialize &&
621
!select_lex->master_unit()->first_select()->next_select() && // 2
622
select_lex->master_unit()->first_select()->leaf_tables && // 3
623
thd->lex->sql_command == SQLCOM_SELECT) // *
625
if (in_subs->is_top_level_item() && // 4
626
!in_subs->is_correlated && // 5
627
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
628
in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
631
Item_subselect::trans_res trans_res;
632
if ((trans_res= subselect->select_transformer(this)) !=
633
Item_subselect::RES_OK)
635
select_lex->fix_prepare_information(thd, &conds, &having);
636
return((trans_res == Item_subselect::RES_ERROR));
642
select_lex->fix_prepare_information(thd, &conds, &having);
647
for (ord= order; ord; ord= ord->next)
649
Item *item= *ord->item;
650
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
651
item->split_sum_func(thd, ref_pointer_array, all_fields);
655
if (having && having->with_sum_func)
656
having->split_sum_func2(thd, ref_pointer_array, all_fields,
658
if (select_lex->inner_sum_func_list)
660
Item_sum *end=select_lex->inner_sum_func_list;
661
Item_sum *item_sum= end;
664
item_sum= item_sum->next;
665
item_sum->split_sum_func2(thd, ref_pointer_array,
666
all_fields, item_sum->ref_by, false);
667
} while (item_sum != end);
670
if (select_lex->inner_refs_list.elements &&
671
fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
675
Check if there are references to un-aggregated columns when computing
676
aggregate functions with implicit grouping (there is no GROUP BY).
678
MODE_ONLY_FULL_GROUP_BY is enabled here by default
680
if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
682
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
683
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
687
/* Caclulate the number of groups */
689
for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
694
goto err; /* purecov: inspected */
696
if (result && result->prepare(fields_list, unit_arg))
697
goto err; /* purecov: inspected */
699
/* Init join struct */
700
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
701
ref_pointer_array_size= all_fields.elements*sizeof(Item*);
702
this->group= group_list != 0;
705
#ifdef RESTRICTED_GROUP
706
if (sum_func_count && !group_list && (func_count || field_count))
708
my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
712
if (select_lex->olap == ROLLUP_TYPE && rollup_init())
714
if (alloc_func_list())
720
return(-1); /* purecov: inspected */
725
Remove the predicates pushed down into the subquery
728
JOIN::remove_subq_pushed_predicates()
729
where IN Must be NULL
730
OUT The remaining WHERE condition, or NULL
733
Given that this join will be executed using (unique|index)_subquery,
734
without "checking NULL", remove the predicates that were pushed down
737
If the subquery compares scalar values, we can remove the condition that
738
was wrapped into trig_cond (it will be checked when needed by the subquery
741
If the subquery compares row values, we need to keep the wrapped
742
equalities in the WHERE clause: when the left (outer) tuple has both NULL
743
and non-NULL values, we'll do a full table scan and will rely on the
744
equalities corresponding to non-NULL parts of left tuple to filter out
745
non-matching records.
747
TODO: We can remove the equalities that will be guaranteed to be true by the
748
fact that subquery engine will be using index lookup. This must be done only
749
for cases where there are no conversion errors of significance, e.g. 257
750
that is searched in a byte. But this requires homogenization of the return
751
codes of all Field*::store() methods.
754
void JOIN::remove_subq_pushed_predicates(Item **where)
756
if (conds->type() == Item::FUNC_ITEM &&
757
((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
758
((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
759
((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
760
test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
761
((Item_func *)conds)->arguments()[0]))
277
770
Index lookup-based subquery: save some flags for EXPLAIN output
809
Check if the table's rowid is included in the temptable
812
sj_table_is_included()
814
join_tab The table to be checked
817
SemiJoinDuplicateElimination: check the table's rowid should be included
818
in the temptable. This is so if
820
1. The table is not embedded within some semi-join nest
821
2. The has been pulled out of a semi-join nest, or
823
3. The table is functionally dependent on some previous table
825
[4. This is also true for constant tables that can't be
826
NULL-complemented but this function is not called for such tables]
829
true - Include table's rowid
833
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
835
if (join_tab->emb_sj_nest)
838
/* Check if this table is functionally dependent on the tables that
839
are within the same outer join nest
841
TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
842
if (join_tab->type == JT_EQ_REF)
844
Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
846
while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
848
JOIN_TAB *ref_tab= join->join_tab + idx;
849
if (embedding == ref_tab->table->pos_in_table_list->embedding)
852
/* Ok, functionally dependent */
855
/* Not functionally dependent => need to include*/
860
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
861
SJ_TMP_TABLE *sjtbl);
865
Setup the strategies to eliminate semi-join duplicates.
868
setup_semijoin_dups_elimination()
870
options Join options (needed to see if join buffering will be
872
no_jbuf_after Another bit of information re where join buffering will
876
Setup the strategies to eliminate semi-join duplicates. ATM there are 3
879
1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
881
2. FirstMatch (pick only the 1st matching row combination of inner tables)
882
3. InsideOut (scanning the sj-inner table in a way that groups duplicates
883
together and picking the 1st one)
885
The join order has "duplicate-generating ranges", and every range is
886
served by one strategy or a combination of FirstMatch with with some
889
"Duplicate-generating range" is defined as a range within the join order
890
that contains all of the inner tables of a semi-join. All ranges must be
891
disjoint, if tables of several semi-joins are interleaved, then the ranges
892
are joined together, which is equivalent to converting
893
SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
895
SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
898
Applicability conditions are as follows:
900
DuplicateWeedout strategy
901
~~~~~~~~~~~~~~~~~~~~~~~~~
903
(ot|nt)* [ it ((it|ot|nt)* (it|ot))] (nt)*
904
+------+ +=========================+ +---+
907
(1) - Prefix of OuterTables (those that participate in
908
IN-equality and/or are correlated with subquery) and outer
909
Noncorrelated Tables.
910
(2) - The handled range. The range starts with the first sj-inner
911
table, and covers all sj-inner and outer tables
912
Within the range, Inner, Outer, outer Noncorrelated tables
913
may follow in any order.
914
(3) - The suffix of outer Noncorrelated tables.
919
(ot|nt)* [ it ((it|nt)* it) ] (nt)*
920
+------+ +==================+ +---+
923
(1) - Prefix of outer and non-correlated tables
924
(2) - The handled range, which may contain only inner and
925
non-correlated tables.
926
(3) - The suffix of outer Noncorrelated tables.
931
(ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ] (ot|nt)*
932
+--------+ +===========+ +=============+ +------+
935
(1) - Prefix that may contain any outer tables. The prefix must contain
936
all the non-trivially correlated outer tables. (non-trivially means
937
that the correlation is not just through the IN-equality).
939
(2) - Inner table for which the InsideOut scan is performed.
941
(3) - The remainder of the duplicate-generating range. It is served by
942
application of FirstMatch strategy, with the exception that
943
outer IN-correlated tables are considered to be non-correlated.
945
(4) - THe suffix of outer and outer non-correlated tables.
947
If several strategies are applicable, their relative priorities are:
952
This function walks over the join order and sets up the strategies by
953
setting appropriate members in join_tab structures.
957
true Out of memory error
961
int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, uint no_jbuf_after)
963
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
966
0 - invalid (EOF marker),
968
2 - Temptable (maybe confluent),
969
3 - Temptable with join buffering
972
uint start_idx; /* Left range bound */
973
uint end_idx; /* Right range bound */
975
For Temptable strategy: Bitmap of all outer and correlated tables from
976
all involved join nests.
978
table_map outer_tables;
979
} dups_ranges [MAX_TABLES];
981
TABLE_LIST *emb_insideout_nest= NULL;
982
table_map emb_sj_map= 0; /* A bitmap of sj-nests (that is, their sj-inner
983
tables) whose ranges we're in */
984
table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
985
table_map range_start_map= 0; /* table_map at current range start */
986
bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
991
First pass: locate the duplicate-generating ranges and pick the strategies.
993
for (i=join->const_tables ; i < join->tables ; i++)
995
JOIN_TAB *tab=join->join_tab+i;
996
TABLE *table=tab->table;
997
cur_map |= table->map;
999
if (tab->emb_sj_nest) // Encountered an sj-inner table
1003
dups_ranges[cur_range].start_idx= i;
1004
range_start_map= cur_map & ~table->map;
1006
Remember if this is a possible start of range that is covered by
1007
the InsideOut strategy (the reason that it is not covered could
1008
be that it overlaps with anther semi-join's range. we don't
1009
support InsideOut for joined ranges)
1011
if (join->best_positions[i].use_insideout_scan)
1012
emb_insideout_nest= tab->emb_sj_nest;
1015
emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1016
emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1018
if (tab->emb_sj_nest != emb_insideout_nest)
1021
Two different semi-joins interleave. This cannot be handled by
1024
emb_insideout_nest= NULL;
1028
if (emb_sj_map) /* We're in duplicate-generating range */
1030
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1031
tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1032
i <= no_jbuf_after && !dealing_with_jbuf)
1035
This table uses join buffering, which makes use of FirstMatch or
1036
InsideOut strategies impossible for the current and (we assume)
1037
preceding duplicate-producing ranges.
1038
That is, for the join order:
1040
x x [ x x] x [x x x] x [x x X* x] x
1042
+-----+ +-----+ | join buffering use
1045
we'll have to remove r1 and r2 and use duplicate-elimination
1046
strategy that spans all the tables, starting from the very 1st
1049
dealing_with_jbuf= true;
1050
emb_insideout_nest= false;
1053
Absorb all preceding duplicate-eliminating ranges. Their strategies
1056
for (int prev_range= 0; prev_range < cur_range; prev_range++)
1058
dups_ranges[cur_range].outer_tables |=
1059
dups_ranges[prev_range].outer_tables;
1061
dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1062
dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1067
Check if we are at the end of duplicate-producing range. We are if
1069
1. It's an InsideOut range (which presumes all correlated tables are
1070
in the prefix), and all inner tables are in the join order prefix,
1072
2. It's a DuplicateElimination range (possibly covering several
1073
SJ-nests), and all inner, outer, and correlated tables of all
1074
sj-nests are in the join order prefix.
1076
bool end_of_range= false;
1077
if (emb_insideout_nest &&
1078
bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1080
/* Save that this range is handled with InsideOut: */
1081
dups_ranges[cur_range].strategy= 1;
1084
else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1087
This is a complete range to be handled with either DuplicateWeedout
1090
dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1092
This will hold tables from within the range that need to be put
1093
into the join buffer before we can use the FirstMatch on its tail.
1095
dups_ranges[cur_range].outer_tables= emb_outer_tables &
1102
dups_ranges[cur_range].end_idx= i+1;
1103
emb_sj_map= emb_outer_tables= 0;
1104
emb_insideout_nest= NULL;
1105
dealing_with_jbuf= false;
1106
dups_ranges[++cur_range].strategy= 0;
1111
THD *thd= join->thd;
1112
SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1114
Second pass: setup the chosen strategies
1116
for (int j= 0; j < cur_range; j++)
1118
JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1120
if (dups_ranges[j].strategy == 1) // InsideOut strategy
1122
tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1125
else // DuplicateWeedout strategy
1127
SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1128
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1129
uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1130
uint jt_null_bits= 0; // # null bits in tuple bytes
1131
SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1132
uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1133
JOIN_TAB *last_outer_tab= tab - 1;
1135
Walk through the range and remember
1136
- tables that need their rowids to be put into temptable
1137
- the last outer table
1139
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1141
if (sj_table_is_included(join, tab))
1143
last_tab->join_tab= tab;
1144
last_tab->rowid_offset= jt_rowid_offset;
1145
jt_rowid_offset += tab->table->file->ref_length;
1146
if (tab->table->maybe_null)
1148
last_tab->null_byte= jt_null_bits / 8;
1149
last_tab->null_bit= jt_null_bits++;
1152
tab->table->prepare_for_position();
1153
tab->rowid_keep_flags= rowid_keep_flags;
1155
cur_map |= tab->table->map;
1156
if (!tab->emb_sj_nest && bitmap_covers(cur_map,
1157
dups_ranges[j].outer_tables))
1158
last_outer_tab= tab;
1161
if (jt_rowid_offset) /* Temptable has at least one rowid */
1163
SJ_TMP_TABLE *sjtbl;
1164
uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1165
if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1166
!(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1168
memcpy(sjtbl->tabs, sjtabs, tabs_size);
1169
sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1170
sjtbl->rowid_len= jt_rowid_offset;
1171
sjtbl->null_bits= jt_null_bits;
1172
sjtbl->null_bytes= (jt_null_bits + 7)/8;
1174
*next_sjtbl_ptr= sjtbl;
1175
next_sjtbl_ptr= &(sjtbl->next);
1179
create_duplicate_weedout_tmp_table(thd,
1184
join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1185
join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1187
tab= last_outer_tab + 1;
1188
jump_to= last_outer_tab;
1191
/* Create the FirstMatch tail */
1192
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1194
if (tab->emb_sj_nest)
1195
tab->do_firstmatch= jump_to;
1204
static void cleanup_sj_tmp_tables(JOIN *join)
1206
for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
1207
sj_tbl= sj_tbl->next)
1209
if (sj_tbl->tmp_table)
1211
free_tmp_table(join->thd, sj_tbl->tmp_table);
1214
join->sj_tmp_tables= NULL;
1217
uint make_join_orderinfo(JOIN *join);
1220
global select optimisation.
1223
error code saved in field 'error'
1234
// to prevent double initialization on EXPLAIN
1239
thd_proc_info(thd, "optimizing");
1240
row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1241
unit->select_limit_cnt);
1242
/* select_limit is used to decide if we are likely to scan the whole table */
1243
select_limit= unit->select_limit_cnt;
1244
if (having || (select_options & OPTION_FOUND_ROWS))
1245
select_limit= HA_POS_ERROR;
1246
do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1247
// Ignore errors of execution if option IGNORE present
1248
if (thd->lex->ignore)
1249
thd->lex->current_select->no_error= 1;
1251
#ifdef HAVE_REF_TO_FIELDS // Not done yet
1252
/* Add HAVING to WHERE if possible */
1253
if (having && !group_list && !sum_func_count)
1260
else if ((conds=new Item_cond_and(conds,having)))
1263
Item_cond_and can't be fixed after creation, so we do not check
1266
conds->fix_fields(thd, &conds);
1267
conds->change_ref_to_fields(thd, tables_list);
1268
conds->top_level_item();
1273
SELECT_LEX *sel= thd->lex->current_select;
1274
if (sel->first_cond_optimization)
1277
The following code will allocate the new items in a permanent
1278
MEMROOT for prepared statements and stored procedures.
1280
sel->first_cond_optimization= 0;
1282
/* Convert all outer joins to inner joins if possible */
1283
conds= simplify_joins(this, join_list, conds, true, false);
1284
build_bitmap_for_nested_joins(join_list, 0);
1286
sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;
1289
conds= optimize_cond(this, conds, join_list, &cond_value);
1290
if (thd->is_error())
1297
having= optimize_cond(this, having, join_list, &having_value);
1298
if (thd->is_error())
1303
if (select_lex->where)
1304
select_lex->cond_value= cond_value;
1305
if (select_lex->having)
1306
select_lex->having_value= having_value;
1308
if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
1309
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1310
{ /* Impossible cond */
1311
zero_result_cause= having_value == Item::COND_FALSE ?
1312
"Impossible HAVING" : "Impossible WHERE";
1318
/* Optimize count(*), min() and max() */
1319
if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1323
opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1324
to the WHERE conditions,
1325
or 1 if all items were resolved,
1326
or 0, or an error number HA_ERR_...
1328
if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1330
if (res == HA_ERR_KEY_NOT_FOUND)
1332
zero_result_cause= "No matching min/max row";
1343
zero_result_cause= "No matching min/max row";
1347
zero_result_cause= "Select tables optimized away";
1348
tables_list= 0; // All tables resolved
1350
Extract all table-independent conditions and replace the WHERE
1351
clause with them. All other conditions were computed by opt_sum_query
1352
and the MIN/MAX/COUNT function(s) have been replaced by constants,
1353
so there is no need to compute the whole WHERE clause again.
1354
Notice that make_cond_for_table() will always succeed to remove all
1355
computed conditions, because opt_sum_query() is applicable only to
1357
Preserve conditions for EXPLAIN.
1359
if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1361
COND *table_independent_conds=
1362
make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1363
conds= table_independent_conds;
1372
error= -1; // Error is sent to client
1373
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1375
/* Calculate how to do the join */
1376
thd_proc_info(thd, "statistics");
1377
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1378
thd->is_fatal_error)
1383
/* Remove distinct if only const tables */
1384
select_distinct= select_distinct && (const_tables != tables);
1385
thd_proc_info(thd, "preparing");
1386
if (result->initialize_tables(this))
1388
return(1); // error == -1
1390
if (const_table_map != found_const_table_map &&
1391
!(select_options & SELECT_DESCRIBE) &&
1393
!(conds->used_tables() & RAND_TABLE_BIT) ||
1394
select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1396
zero_result_cause= "no matching row in const table";
1400
if (!(thd->options & OPTION_BIG_SELECTS) &&
1401
best_read > (double) thd->variables.max_join_size &&
1402
!(select_options & SELECT_DESCRIBE))
1403
{ /* purecov: inspected */
1404
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1408
if (const_tables && !thd->locked_tables &&
1409
!(select_options & SELECT_NO_UNLOCK))
1410
mysql_unlock_some_tables(thd, table, const_tables);
1411
if (!conds && outer_join)
1413
/* Handle the case where we have an OUTER JOIN without a WHERE */
1414
conds=new Item_int((longlong) 1,1); // Always true
1416
select= make_select(*table, const_table_map,
1417
const_table_map, conds, 1, &error);
1419
{ /* purecov: inspected */
1420
error= -1; /* purecov: inspected */
1424
reset_nj_counters(join_list);
1425
make_outerjoin_info(this);
1428
Among the equal fields belonging to the same multiple equality
1429
choose the one that is to be retrieved first and substitute
1430
all references to these in where condition for a reference for
1435
conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1436
conds->update_used_tables();
1440
Permorm the the optimization on fields evaluation mentioned above
1441
for all on expressions.
1443
for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1445
if (*tab->on_expr_ref)
1447
*tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1450
(*tab->on_expr_ref)->update_used_tables();
1454
if (conds &&!outer_join && const_table_map != found_const_table_map &&
1455
(select_options & SELECT_DESCRIBE) &&
1456
select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1458
conds=new Item_int((longlong) 0,1); // Always false
1460
if (make_join_select(this, select, conds))
1463
"Impossible WHERE noticed after reading const tables";
1464
return(0); // error == 0
1467
error= -1; /* if goto err */
1469
/* Optimize distinct away if possible */
1471
ORDER *org_order= order;
1472
order=remove_const(this, order,conds,1, &simple_order);
1473
if (thd->is_error())
1480
If we are using ORDER BY NULL or ORDER BY const_expression,
1481
return result in any order (even if we are using a GROUP BY)
1483
if (!order && org_order)
1487
Check if we can optimize away GROUP BY/DISTINCT.
1488
We can do that if there are no aggregate functions, the
1489
fields in DISTINCT clause (if present) and/or columns in GROUP BY
1490
(if present) contain direct references to all key parts of
1491
an unique index (in whatever order) and if the key parts of the
1492
unique index cannot contain NULLs.
1493
Note that the unique keys for DISTINCT and GROUP BY should not
1494
be the same (as long as they are unique).
1496
The FROM clause must contain a single non-constant table.
1498
if (tables - const_tables == 1 && (group_list || select_distinct) &&
1499
!tmp_table_param.sum_func_count &&
1500
(!join_tab[const_tables].select ||
1501
!join_tab[const_tables].select->quick ||
1502
join_tab[const_tables].select->quick->get_type() !=
1503
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1506
list_contains_unique_index(join_tab[const_tables].table,
1507
find_field_in_order_list,
1508
(void *) group_list))
1511
We have found that grouping can be removed since groups correspond to
1512
only one row anyway, but we still have to guarantee correct result
1513
order. The line below effectively rewrites the query from GROUP BY
1514
<fields> to ORDER BY <fields>. There are two exceptions:
1515
- if skip_sort_order is set (see above), then we can simply skip
1517
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1518
with the GROUP BY ones, i.e. either one is a prefix of another.
1519
We only check if the ORDER BY is a prefix of GROUP BY. In this case
1520
test_if_subpart() copies the ASC/DESC attributes from the original
1522
If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1525
if (!order || test_if_subpart(group_list, order))
1526
order= skip_sort_order ? 0 : group_list;
1528
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1529
rewritten to IGNORE INDEX FOR ORDER BY(fields).
1531
join_tab->table->keys_in_use_for_order_by=
1532
join_tab->table->keys_in_use_for_group_by;
1536
if (select_distinct &&
1537
list_contains_unique_index(join_tab[const_tables].table,
1538
find_field_in_item_list,
1539
(void *) &fields_list))
1544
if (group_list || tmp_table_param.sum_func_count)
1546
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1549
else if (select_distinct && tables - const_tables == 1)
1552
We are only using one table. In this case we change DISTINCT to a
1554
- The GROUP BY can be done through indexes (no sort) and the ORDER
1555
BY only uses selected fields.
1556
(In this case we can later optimize away GROUP BY and ORDER BY)
1557
- We are scanning the whole table without LIMIT
1559
- We are using CALC_FOUND_ROWS
1560
- We are using an ORDER BY that can't be optimized away.
1562
We don't want to use this optimization when we are using LIMIT
1563
because in this case we can just create a temporary table that
1564
holds LIMIT rows and stop when this table is full.
1566
JOIN_TAB *tab= &join_tab[const_tables];
1567
bool all_order_fields_used;
1569
skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
1570
&tab->table->keys_in_use_for_order_by);
1571
if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1572
order, fields_list, all_fields,
1573
&all_order_fields_used)))
1575
bool skip_group= (skip_sort_order &&
1576
test_if_skip_sort_order(tab, group_list, select_limit, 1,
1577
&tab->table->keys_in_use_for_group_by) != 0);
1578
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1579
if ((skip_group && all_order_fields_used) ||
1580
select_limit == HA_POS_ERROR ||
1581
(order && !skip_sort_order))
1583
/* Change DISTINCT to GROUP BY */
1586
if (all_order_fields_used)
1588
if (order && skip_sort_order)
1591
Force MySQL to read the table in sorted order to get result in
1594
tmp_table_param.quick_group=0;
1598
group=1; // For end_write_group
1603
else if (thd->is_fatal_error) // End of memory
1608
ORDER *old_group_list;
1609
group_list= remove_const(this, (old_group_list= group_list), conds,
1610
rollup.state == ROLLUP::STATE_NONE,
1612
if (thd->is_error())
1617
if (old_group_list && !group_list)
1620
if (!group_list && group)
1622
order=0; // The output has only one row
1624
select_distinct= 0; // No need in distinct for 1 row
1625
group_optimized_away= 1;
1628
calc_group_buffer(this, group_list);
1629
send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1631
if (test_if_subpart(group_list, order) ||
1632
(!group_list && tmp_table_param.sum_func_count))
1635
// Can't use sort on head table if using row cache
1645
Check if we need to create a temporary table.
1646
This has to be done if all tables are not already read (const tables)
1647
and one of the following conditions holds:
1648
- We are using DISTINCT (simple distinct's are already optimized away)
1649
- We are using an ORDER BY or GROUP BY on fields not in the first table
1650
- We are using different ORDER BY and GROUP BY orders
1651
- The user wants us to buffer the result.
1653
need_tmp= (const_tables != tables &&
1654
((select_distinct || !simple_order || !simple_group) ||
1655
(group_list && order) ||
1656
test(select_options & OPTION_BUFFER_RESULT)));
1658
uint no_jbuf_after= make_join_orderinfo(this);
1659
ulonglong select_opts_for_readinfo=
1660
(select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1662
sj_tmp_tables= NULL;
1663
if (!select_lex->sj_nests.is_empty())
1664
setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1667
// No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1668
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1671
/* Create all structures needed for materialized subquery execution. */
1672
if (setup_subquery_materialization())
1676
is this simple IN subquery?
1678
if (!group_list && !order &&
1679
unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1680
tables == 1 && conds &&
1686
if (join_tab[0].type == JT_EQ_REF &&
1687
join_tab[0].ref.items[0]->name == in_left_expr_name)
1689
remove_subq_pushed_predicates(&where);
1690
save_index_subquery_explain_info(join_tab, where);
1691
join_tab[0].type= JT_UNIQUE_SUBQUERY;
1695
subselect_uniquesubquery_engine(thd,
1700
else if (join_tab[0].type == JT_REF &&
1701
join_tab[0].ref.items[0]->name == in_left_expr_name)
1703
remove_subq_pushed_predicates(&where);
1704
save_index_subquery_explain_info(join_tab, where);
1705
join_tab[0].type= JT_INDEX_SUBQUERY;
1709
subselect_indexsubquery_engine(thd,
1716
} else if (join_tab[0].type == JT_REF_OR_NULL &&
1717
join_tab[0].ref.items[0]->name == in_left_expr_name &&
1718
having->name == in_having_cond)
1720
join_tab[0].type= JT_INDEX_SUBQUERY;
1722
conds= remove_additional_cond(conds);
1723
save_index_subquery_explain_info(join_tab, conds);
1725
change_engine(new subselect_indexsubquery_engine(thd,
1735
Need to tell handlers that to play it safe, it should fetch all
1736
columns of the primary key of the tables: this is because MySQL may
1737
build row pointers for the rows, and for all columns of the primary key
1738
the read set has not necessarily been set by the server code.
1740
if (need_tmp || select_distinct || group_list || order)
1742
for (uint i = const_tables; i < tables; i++)
1743
join_tab[i].table->prepare_for_position();
1746
if (const_tables != tables)
1749
Because filesort always does a full table scan or a quick range scan
1750
we must add the removed reference to the select for the table.
1751
We only need to do this when we have a simple_order or simple_group
1752
as in other cases the join is done before the sort.
1754
if ((order || group_list) &&
1755
(join_tab[const_tables].type != JT_ALL) &&
1756
(join_tab[const_tables].type != JT_REF_OR_NULL) &&
1757
((order && simple_order) || (group_list && simple_group)))
1759
if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1764
if (!(select_options & SELECT_BIG_RESULT) &&
1767
!test_if_skip_sort_order(&join_tab[const_tables], group_list,
1768
unit->select_limit_cnt, 0,
1769
&join_tab[const_tables].table->
1770
keys_in_use_for_group_by))) ||
1772
tmp_table_param.quick_group)
1774
need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort
1779
Force using of tmp table if sorting by a SP or UDF function due to
1780
their expensive and probably non-deterministic nature.
1782
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1784
Item *item= *tmp_order->item;
1785
if (item->is_expensive())
1787
/* Force tmp table without sort */
1788
need_tmp=1; simple_order=simple_group=0;
1796
if (select_options & SELECT_DESCRIBE)
1804
The loose index scan access method guarantees that all grouping or
1805
duplicate row elimination (for distinct) is already performed
1806
during data retrieval, and that all MIN/MAX functions are already
1807
computed for each group. Thus all MIN/MAX functions should be
1808
treated as regular functions, and there is no need to perform
1809
grouping in the main execution loop.
1810
Notice that currently loose index scan is applicable only for
1811
single table queries, thus it is sufficient to test only the first
1812
join_tab element of the plan for its access method.
1814
if (join_tab->is_using_loose_index_scan())
1815
tmp_table_param.precomputed_group_by= true;
1817
/* Create a tmp table if distinct or if the sort is too complicated */
1820
thd_proc_info(thd, "Creating tmp table");
1822
init_items_ref_array();
1824
tmp_table_param.hidden_field_count= (all_fields.elements -
1825
fields_list.elements);
1826
ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1829
Pushing LIMIT to the temporary table creation is not applicable
1830
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1831
there are aggregate functions, because in all these cases we need
1834
ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1836
!thd->lex->current_select->with_sum_func) ?
1837
select_limit : HA_POS_ERROR;
1839
if (!(exec_tmp_table1=
1840
create_tmp_table(thd, &tmp_table_param, all_fields,
1842
group_list ? 0 : select_distinct,
1843
group_list && simple_group,
1852
We don't have to store rows in temp table that doesn't match HAVING if:
1853
- we are sorting the table and writing complete group rows to the
1855
- We are using DISTINCT without resolving the distinct as a GROUP BY
1858
If having is not handled here, it will be checked before the row
1859
is sent to the client.
1862
(sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1865
/* if group or order on first table, sort first */
1866
if (group_list && simple_group)
1868
thd_proc_info(thd, "Sorting for group");
1869
if (create_sort_index(thd, this, group_list,
1870
HA_POS_ERROR, HA_POS_ERROR, false) ||
1871
alloc_group_fields(this, group_list) ||
1872
make_sum_func_list(all_fields, fields_list, 1) ||
1873
setup_sum_funcs(thd, sum_funcs))
1881
if (make_sum_func_list(all_fields, fields_list, 0) ||
1882
setup_sum_funcs(thd, sum_funcs))
1887
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1889
thd_proc_info(thd, "Sorting for order");
1890
if (create_sort_index(thd, this, order,
1891
HA_POS_ERROR, HA_POS_ERROR, true))
1900
Optimize distinct when used on some of the tables
1901
SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1902
In this case we can stop scanning t2 when we have found one t1.a
1905
if (exec_tmp_table1->distinct)
1907
table_map used_tables= thd->used_tables;
1908
JOIN_TAB *last_join_tab= join_tab+tables-1;
1911
if (used_tables & last_join_tab->table->map)
1913
last_join_tab->not_used_in_distinct=1;
1914
} while (last_join_tab-- != join_tab);
1915
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1916
if (order && skip_sort_order)
1918
/* Should always succeed */
1919
if (test_if_skip_sort_order(&join_tab[const_tables],
1920
order, unit->select_limit_cnt, 0,
1921
&join_tab[const_tables].table->
1922
keys_in_use_for_order_by))
1928
If this join belongs to an uncacheable subquery save
1931
if (select_lex->uncacheable && !is_top_level_join() &&
1932
init_save_join_tab())
1933
return(-1); /* purecov: inspected */
1942
Restore values in temporary join.
1944
void JOIN::restore_tmp()
1946
memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1953
unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1954
select_lex->offset_limit->val_uint() :
1959
if (exec_tmp_table1)
1961
exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1962
exec_tmp_table1->file->ha_delete_all_rows();
1963
free_io_cache(exec_tmp_table1);
1964
filesort_free_buffers(exec_tmp_table1,0);
1966
if (exec_tmp_table2)
1968
exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1969
exec_tmp_table2->file->ha_delete_all_rows();
1970
free_io_cache(exec_tmp_table2);
1971
filesort_free_buffers(exec_tmp_table2,0);
1974
set_items_ref_array(items0);
1977
memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1982
/* Reset of sum functions */
1985
Item_sum *func, **func_ptr= sum_funcs;
1986
while ((func= *(func_ptr++)))
1994
@brief Save the original join layout
1996
@details Saves the original join layout so it can be reused in
1997
re-execution and for EXPLAIN.
1999
@return Operation status
2001
@retval 1 error occurred.
2005
JOIN::init_save_join_tab()
2007
if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
2008
return 1; /* purecov: inspected */
2009
error= 0; // Ensure that tmp_join.error= 0
2016
JOIN::save_join_tab()
2018
if (!join_tab_save && select_lex->master_unit()->uncacheable)
2020
if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2021
sizeof(JOIN_TAB) * tables)))
2032
Note, that create_sort_index calls test_if_skip_sort_order and may
2033
finally replace sorting with index scan if there is a LIMIT clause in
2034
the query. It's never shown in EXPLAIN!
2037
When can we have here thd->net.report_error not zero?
2042
List<Item> *columns_list= &fields_list;
2045
thd_proc_info(thd, "executing");
2047
(void) result->prepare2(); // Currently, this cannot fail.
2049
if (!tables_list && (tables || !select_lex->with_sum_func))
2050
{ // Only test of functions
2051
if (select_options & SELECT_DESCRIBE)
2052
select_describe(this, false, false, false,
2053
(zero_result_cause?zero_result_cause:"No tables used"));
2056
result->send_fields(*columns_list,
2057
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2059
We have to test for 'conds' here as the WHERE may not be constant
2060
even if we don't have any tables for prepared statements or if
2061
conds uses something like 'rand()'.
2063
if (cond_value != Item::COND_FALSE &&
2064
(!conds || conds->val_int()) &&
2065
(!having || having->val_int()))
2067
if (do_send_rows && result->send_data(fields_list))
2071
error= (int) result->send_eof();
2072
send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2073
thd->sent_row_count);
2078
error=(int) result->send_eof();
2082
/* Single select (without union) always returns 0 or 1 row */
2083
thd->limit_found_rows= send_records;
2084
thd->examined_row_count= 0;
2088
Don't reset the found rows count if there're no tables as
2089
FOUND_ROWS() may be called. Never reset the examined row count here.
2090
It must be accumulated from all join iterations of all join parts.
2093
thd->limit_found_rows= 0;
2095
if (zero_result_cause)
2097
(void) return_zero_rows(this, result, select_lex->leaf_tables,
2099
send_row_on_empty_set(),
2106
if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2107
get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2110
if (select_options & SELECT_DESCRIBE)
2113
Check if we managed to optimize ORDER BY away and don't use temporary
2114
table to resolve ORDER BY: in that case, we only may need to do
2115
filesort for GROUP BY.
2117
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2120
Reset 'order' to 'group_list' and reinit variables describing
2124
simple_order= simple_group;
2128
(order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2129
(const_tables == tables ||
2130
((simple_order || skip_sort_order) &&
2131
test_if_skip_sort_order(&join_tab[const_tables], order,
2133
&join_tab[const_tables].table->
2134
keys_in_use_for_query))))
2137
select_describe(this, need_tmp,
2138
order != 0 && !skip_sort_order,
2140
!tables ? "No tables used" : NullS);
2144
JOIN *curr_join= this;
2145
List<Item> *curr_all_fields= &all_fields;
2146
List<Item> *curr_fields_list= &fields_list;
2147
TABLE *curr_tmp_table= 0;
2149
Initialize examined rows here because the values from all join parts
2150
must be accumulated in examined_row_count. Hence every join
2151
iteration must count from zero.
2153
curr_join->examined_rows= 0;
2155
/* Create a tmp table if distinct or if the sort is too complicated */
2161
We are in a non cacheable sub query. Get the saved join structure
2163
(curr_join may have been modified during last exection and we need
2166
curr_join= tmp_join;
2168
curr_tmp_table= exec_tmp_table1;
2170
/* Copy data to the temporary table */
2171
thd_proc_info(thd, "Copying to tmp table");
2172
if (!curr_join->sort_and_group &&
2173
curr_join->const_tables != curr_join->tables)
2174
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2175
if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2180
curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2182
if (curr_join->having)
2183
curr_join->having= curr_join->tmp_having= 0; // Allready done
2185
/* Change sum_fields reference to calculated fields in tmp_table */
2186
curr_join->all_fields= *curr_all_fields;
2189
items1= items0 + all_fields.elements;
2190
if (sort_and_group || curr_tmp_table->group)
2192
if (change_to_use_tmp_fields(thd, items1,
2193
tmp_fields_list1, tmp_all_fields1,
2194
fields_list.elements, all_fields))
2199
if (change_refs_to_tmp_fields(thd, items1,
2200
tmp_fields_list1, tmp_all_fields1,
2201
fields_list.elements, all_fields))
2204
curr_join->tmp_all_fields1= tmp_all_fields1;
2205
curr_join->tmp_fields_list1= tmp_fields_list1;
2206
curr_join->items1= items1;
2208
curr_all_fields= &tmp_all_fields1;
2209
curr_fields_list= &tmp_fields_list1;
2210
curr_join->set_items_ref_array(items1);
2212
if (sort_and_group || curr_tmp_table->group)
2214
curr_join->tmp_table_param.field_count+=
2215
curr_join->tmp_table_param.sum_func_count+
2216
curr_join->tmp_table_param.func_count;
2217
curr_join->tmp_table_param.sum_func_count=
2218
curr_join->tmp_table_param.func_count= 0;
2222
curr_join->tmp_table_param.field_count+=
2223
curr_join->tmp_table_param.func_count;
2224
curr_join->tmp_table_param.func_count= 0;
2227
if (curr_tmp_table->group)
2228
{ // Already grouped
2229
if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2230
curr_join->order= curr_join->group_list; /* order by group */
2231
curr_join->group_list= 0;
2235
If we have different sort & group then we must sort the data by group
2236
and copy it to another tmp table
2237
This code is also used if we are using distinct something
2238
we haven't been able to store in the temporary table yet
2239
like SEC_TO_TIME(SUM(...)).
2242
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))
2243
{ /* Must copy to another table */
2244
/* Free first data from old join */
2245
curr_join->join_free();
2246
if (make_simple_join(curr_join, curr_tmp_table))
2248
calc_group_buffer(curr_join, group_list);
2249
count_field_types(select_lex, &curr_join->tmp_table_param,
2250
curr_join->tmp_all_fields1,
2251
curr_join->select_distinct && !curr_join->group_list);
2252
curr_join->tmp_table_param.hidden_field_count=
2253
(curr_join->tmp_all_fields1.elements-
2254
curr_join->tmp_fields_list1.elements);
2257
if (exec_tmp_table2)
2258
curr_tmp_table= exec_tmp_table2;
2261
/* group data to new table */
2264
If the access method is loose index scan then all MIN/MAX
2265
functions are precomputed, and should be treated as regular
2266
functions. See extended comment in JOIN::exec.
2268
if (curr_join->join_tab->is_using_loose_index_scan())
2269
curr_join->tmp_table_param.precomputed_group_by= true;
2271
if (!(curr_tmp_table=
2272
exec_tmp_table2= create_tmp_table(thd,
2273
&curr_join->tmp_table_param,
2276
curr_join->select_distinct &&
2277
!curr_join->group_list,
2278
1, curr_join->select_options,
2282
curr_join->exec_tmp_table2= exec_tmp_table2;
2284
if (curr_join->group_list)
2286
thd_proc_info(thd, "Creating sort index");
2287
if (curr_join->join_tab == join_tab && save_join_tab())
2291
if (create_sort_index(thd, curr_join, curr_join->group_list,
2292
HA_POS_ERROR, HA_POS_ERROR, false) ||
2293
make_group_fields(this, curr_join))
2297
sortorder= curr_join->sortorder;
2300
thd_proc_info(thd, "Copying to group table");
2302
if (curr_join != this)
2306
curr_join->sum_funcs= sum_funcs2;
2307
curr_join->sum_funcs_end= sum_funcs_end2;
2311
curr_join->alloc_func_list();
2312
sum_funcs2= curr_join->sum_funcs;
2313
sum_funcs_end2= curr_join->sum_funcs_end;
2316
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2319
curr_join->group_list= 0;
2320
if (!curr_join->sort_and_group &&
2321
curr_join->const_tables != curr_join->tables)
2322
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2323
if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2324
(tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2329
end_read_record(&curr_join->join_tab->read_record);
2330
curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2331
curr_join->join_tab[0].table= 0; // Table is freed
2333
// No sum funcs anymore
2336
items2= items1 + all_fields.elements;
2337
if (change_to_use_tmp_fields(thd, items2,
2338
tmp_fields_list2, tmp_all_fields2,
2339
fields_list.elements, tmp_all_fields1))
2341
curr_join->tmp_fields_list2= tmp_fields_list2;
2342
curr_join->tmp_all_fields2= tmp_all_fields2;
2344
curr_fields_list= &curr_join->tmp_fields_list2;
2345
curr_all_fields= &curr_join->tmp_all_fields2;
2346
curr_join->set_items_ref_array(items2);
2347
curr_join->tmp_table_param.field_count+=
2348
curr_join->tmp_table_param.sum_func_count;
2349
curr_join->tmp_table_param.sum_func_count= 0;
2351
if (curr_tmp_table->distinct)
2352
curr_join->select_distinct=0; /* Each row is unique */
2354
curr_join->join_free(); /* Free quick selects */
2355
if (curr_join->select_distinct && ! curr_join->group_list)
2357
thd_proc_info(thd, "Removing duplicates");
2358
if (curr_join->tmp_having)
2359
curr_join->tmp_having->update_used_tables();
2360
if (remove_duplicates(curr_join, curr_tmp_table,
2361
*curr_fields_list, curr_join->tmp_having))
2363
curr_join->tmp_having=0;
2364
curr_join->select_distinct=0;
2366
curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2367
if (make_simple_join(curr_join, curr_tmp_table))
2369
calc_group_buffer(curr_join, curr_join->group_list);
2370
count_field_types(select_lex, &curr_join->tmp_table_param,
2371
*curr_all_fields, 0);
2375
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2377
if (make_group_fields(this, curr_join))
2384
init_items_ref_array();
2385
items3= ref_pointer_array + (all_fields.elements*4);
2386
setup_copy_fields(thd, &curr_join->tmp_table_param,
2387
items3, tmp_fields_list3, tmp_all_fields3,
2388
curr_fields_list->elements, *curr_all_fields);
2389
tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2390
tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2391
tmp_table_param.save_copy_field_end=
2392
curr_join->tmp_table_param.copy_field_end;
2393
curr_join->tmp_all_fields3= tmp_all_fields3;
2394
curr_join->tmp_fields_list3= tmp_fields_list3;
2398
curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2399
curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2400
curr_join->tmp_table_param.copy_field_end=
2401
tmp_table_param.save_copy_field_end;
2403
curr_fields_list= &tmp_fields_list3;
2404
curr_all_fields= &tmp_all_fields3;
2405
curr_join->set_items_ref_array(items3);
2407
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2409
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2410
thd->is_fatal_error)
2413
if (curr_join->group_list || curr_join->order)
2415
thd_proc_info(thd, "Sorting result");
2416
/* If we have already done the group, add HAVING to sorted table */
2417
if (curr_join->tmp_having && ! curr_join->group_list &&
2418
! curr_join->sort_and_group)
2420
// Some tables may have been const
2421
curr_join->tmp_having->update_used_tables();
2422
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2423
table_map used_tables= (curr_join->const_table_map |
2424
curr_table->table->map);
2426
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2429
if (sort_table_cond)
2431
if (!curr_table->select)
2432
if (!(curr_table->select= new SQL_SELECT))
2434
if (!curr_table->select->cond)
2435
curr_table->select->cond= sort_table_cond;
2436
else // This should never happen
2438
if (!(curr_table->select->cond=
2439
new Item_cond_and(curr_table->select->cond,
2443
Item_cond_and do not need fix_fields for execution, its parameters
2444
are fixed or do not need fix_fields, too
2446
curr_table->select->cond->quick_fix_field();
2448
curr_table->select_cond= curr_table->select->cond;
2449
curr_table->select_cond->top_level_item();
2450
curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2457
curr_join->select_limit= HA_POS_ERROR;
2461
We can abort sorting after thd->select_limit rows if we there is no
2462
WHERE clause for any tables after the sorted one.
2464
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2465
JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2466
for (; curr_table < end_table ; curr_table++)
2469
table->keyuse is set in the case there was an original WHERE clause
2470
on the table that was optimized away.
2472
if (curr_table->select_cond ||
2473
(curr_table->keyuse && !curr_table->first_inner))
2475
/* We have to sort all rows */
2476
curr_join->select_limit= HA_POS_ERROR;
2481
if (curr_join->join_tab == join_tab && save_join_tab())
2486
Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2487
chose FILESORT to be faster than INDEX SCAN or there is no
2488
suitable index present.
2489
Note, that create_sort_index calls test_if_skip_sort_order and may
2490
finally replace sorting with index scan if there is a LIMIT clause in
2491
the query. XXX: it's never shown in EXPLAIN!
2492
OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2494
if (create_sort_index(thd, curr_join,
2495
curr_join->group_list ?
2496
curr_join->group_list : curr_join->order,
2497
curr_join->select_limit,
2498
(select_options & OPTION_FOUND_ROWS ?
2499
HA_POS_ERROR : unit->select_limit_cnt),
2500
curr_join->group_list ? true : false))
2502
sortorder= curr_join->sortorder;
2503
if (curr_join->const_tables != curr_join->tables &&
2504
!curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2507
If no IO cache exists for the first table then we are using an
2508
INDEX SCAN and no filesort. Thus we should not remove the sorted
2509
attribute on the INDEX SCAN.
2515
/* XXX: When can we have here thd->is_error() not zero? */
2516
if (thd->is_error())
2518
error= thd->is_error();
2521
curr_join->having= curr_join->tmp_having;
2522
curr_join->fields= curr_fields_list;
2525
thd_proc_info(thd, "Sending data");
2526
result->send_fields(*curr_fields_list,
2527
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2528
error= do_select(curr_join, curr_fields_list, NULL);
2529
thd->limit_found_rows= curr_join->send_records;
2532
/* Accumulate the counts from all join iterations of all join parts. */
2533
thd->examined_row_count+= curr_join->examined_rows;
2536
With EXPLAIN EXTENDED we have to restore original ref_array
2537
for a derived table which is always materialized.
2538
Otherwise we would not be able to print the query correctly.
2541
(thd->lex->describe & DESCRIBE_EXTENDED) &&
2542
select_lex->linkage == DERIVED_TABLE_TYPE)
2543
set_items_ref_array(items0);
2553
Return error that hold JOIN.
2559
select_lex->join= 0;
2563
if (join_tab != tmp_join->join_tab)
2565
JOIN_TAB *tab, *end;
2566
for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2569
tmp_join->tmp_join= 0;
2570
tmp_table_param.copy_field=0;
2571
return(tmp_join->destroy());
2576
if (exec_tmp_table1)
2577
free_tmp_table(thd, exec_tmp_table1);
2578
if (exec_tmp_table2)
2579
free_tmp_table(thd, exec_tmp_table2);
2581
delete_dynamic(&keyuse);
312
2588
An entry point to single-unit select (a select without UNION).
314
@param session thread Cursor
2590
@param thd thread handler
315
2591
@param rref_pointer_array a reference to ref_pointer_array of
316
2592
the top-level select_lex for this query
317
2593
@param tables list of all tables used in this query.
318
2594
The tables have been pre-opened.
319
@param wild_num number of wildcards used in the top level
2595
@param wild_num number of wildcards used in the top level
320
2596
select of this query.
321
2597
For example statement
322
2598
SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
443
session->set_proc_info("end");
2722
thd_proc_info(thd, "end");
444
2723
err|= select_lex->cleanup();
445
return(err || session->is_error());
2724
return(err || thd->is_error());
447
2726
return(join->error);
450
inline Item *and_items(Item* cond, Item *item)
2730
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
2731
Item_in_subselect* const *el2)
2733
return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
2734
( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2738
inline Item * and_items(Item* cond, Item *item)
452
2740
return (cond? (new Item_cond_and(cond, item)) : item);
2744
static TABLE_LIST *alloc_join_nest(THD *thd)
2747
if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2748
sizeof(NESTED_JOIN))))
2750
tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl +
2751
ALIGN_SIZE(sizeof(TABLE_LIST)));
2756
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2758
List_iterator<TABLE_LIST> it(*tlist);
2760
while ((table= it++))
2763
table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
2764
if (table->nested_join)
2765
fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
2771
Convert a subquery predicate into a TABLE_LIST semi-join nest
2774
convert_subq_to_sj()
2775
parent_join Parent join, the one that has subq_pred in its WHERE/ON
2777
subq_pred Subquery predicate to be converted
2780
Convert a subquery predicate into a TABLE_LIST semi-join nest. All the
2781
prerequisites are already checked, so the conversion is always successfull.
2783
Prepared Statements: the transformation is permanent:
2784
- Changes in TABLE_LIST structures are naturally permanent
2785
- Item tree changes are performed on statement MEM_ROOT:
2786
= we activate statement MEM_ROOT
2787
= this function is called before the first fix_prepare_information
2790
This is intended because the criteria for subquery-to-sj conversion remain
2791
constant for the lifetime of the Prepared Statement.
2795
true Out of memory error
2798
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2800
SELECT_LEX *parent_lex= parent_join->select_lex;
2801
TABLE_LIST *emb_tbl_nest= NULL;
2802
List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2803
THD *thd= parent_join->thd;
2806
1. Find out where to put the predicate into.
2807
Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2809
if ((void*)subq_pred->expr_join_nest != (void*)1)
2811
if (subq_pred->expr_join_nest->nested_join)
2816
... [LEFT] JOIN ( ... ) ON (subquery AND whatever) ...
2818
The sj-nest will be inserted into the brackets nest.
2820
emb_tbl_nest= subq_pred->expr_join_nest;
2821
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2823
else if (!subq_pred->expr_join_nest->outer_join)
2828
... INNER JOIN tblX ON (subquery AND whatever) ...
2830
The sj-nest will be tblX's "sibling", i.e. another child of its
2831
parent. This is ok because tblX is joined as an inner join.
2833
emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2835
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2837
else if (!subq_pred->expr_join_nest->nested_join)
2839
TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;
2840
TABLE_LIST *wrap_nest;
2844
... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2846
we'll need to convert it into:
2848
... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2850
|<----- wrap_nest ---->|
2852
Q: other subqueries may be pointing to this element. What to do?
2853
A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2854
But we'll need to fix other pointers.
2855
A2: Another way: have TABLE_LIST::next_ptr so the following
2856
subqueries know the table has been nested.
2857
A3: changes in the TABLE_LIST::outer_join will make everything work
2860
if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2864
wrap_nest->embedding= outer_tbl->embedding;
2865
wrap_nest->join_list= outer_tbl->join_list;
2866
wrap_nest->alias= (char*) "(sj-wrap)";
2868
wrap_nest->nested_join->join_list.empty();
2869
wrap_nest->nested_join->join_list.push_back(outer_tbl);
2871
outer_tbl->embedding= wrap_nest;
2872
outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2875
wrap_nest will take place of outer_tbl, so move the outer join flag
2878
wrap_nest->outer_join= outer_tbl->outer_join;
2879
outer_tbl->outer_join= 0;
2881
wrap_nest->on_expr= outer_tbl->on_expr;
2882
outer_tbl->on_expr= NULL;
2884
List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2888
if (tbl == outer_tbl)
2890
li.replace(wrap_nest);
2895
Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
2896
semi-join nest into it
2898
emb_join_list= &wrap_nest->nested_join->join_list;
2899
emb_tbl_nest= wrap_nest;
2903
TABLE_LIST *sj_nest;
2904
NESTED_JOIN *nested_join;
2905
if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2909
nested_join= sj_nest->nested_join;
2911
sj_nest->join_list= emb_join_list;
2912
sj_nest->embedding= emb_tbl_nest;
2913
sj_nest->alias= (char*) "(sj-nest)";
2914
/* Nests do not participate in those 'chains', so: */
2915
/* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2916
emb_join_list->push_back(sj_nest);
2919
nested_join->used_tables and nested_join->not_null_tables are
2920
initialized in simplify_joins().
2924
2. Walk through subquery's top list and set 'embedding' to point to the
2927
st_select_lex *subq_lex= subq_pred->unit->first_select();
2928
nested_join->join_list.empty();
2929
List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
2930
TABLE_LIST *tl, *last_leaf;
2933
tl->embedding= sj_nest;
2934
tl->join_list= &nested_join->join_list;
2935
nested_join->join_list.push_back(tl);
2939
Reconnect the next_leaf chain.
2940
TODO: Do we have to put subquery's tables at the end of the chain?
2941
Inserting them at the beginning would be a bit faster.
2942
NOTE: We actually insert them at the front! That's because the order is
2943
reversed in this list.
2945
for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
2946
tl->next_leaf= subq_lex->leaf_tables;
2950
Same as above for next_local chain
2951
(a theory: a next_local chain always starts with ::leaf_tables
2952
because view's tables are inserted after the view)
2954
for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
2955
tl->next_local= subq_lex->leaf_tables;
2957
/* A theory: no need to re-connect the next_global chain */
2959
/* 3. Remove the original subquery predicate from the WHERE/ON */
2961
// The subqueries were replaced for Item_int(1) earlier
2962
subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
2963
/*TODO: also reset the 'with_subselect' there. */
2965
/* n. Adjust the parent_join->tables counter */
2966
uint table_no= parent_join->tables;
2967
/* n. Walk through child's tables and adjust table->map */
2968
for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2970
tl->table->tablenr= table_no;
2971
tl->table->map= ((table_map)1) << table_no;
2972
SELECT_LEX *old_sl= tl->select_lex;
2973
tl->select_lex= parent_join->select_lex;
2974
for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2975
emb->select_lex= parent_join->select_lex;
2977
parent_join->tables += subq_lex->join->tables;
2980
Put the subquery's WHERE into semi-join's sj_on_expr
2981
Add the subquery-induced equalities too.
2983
SELECT_LEX *save_lex= thd->lex->current_select;
2984
thd->lex->current_select=subq_lex;
2985
if (!subq_pred->left_expr->fixed &&
2986
subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
2988
thd->lex->current_select=save_lex;
2990
sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2991
sj_nest->nested_join->sj_depends_on= subq_pred->used_tables() |
2992
subq_pred->left_expr->used_tables();
2993
sj_nest->sj_on_expr= subq_lex->where;
2996
Create the IN-equalities and inject them into semi-join's ON expression.
2997
Additionally, for InsideOut strategy
2998
- Record the number of IN-equalities.
2999
- Create list of pointers to (oe1, ..., ieN). We'll need the list to
3000
see which of the expressions are bound and which are not (for those
3001
we'll produce a distinct stream of (ie_i1,...ie_ik).
3003
(TODO: can we just create a list of pointers and hope the expressions
3004
will not substitute themselves on fix_fields()? or we need to wrap
3005
them into Item_direct_view_refs and store pointers to those. The
3006
pointers to Item_direct_view_refs are guaranteed to be stable as
3007
Item_direct_view_refs doesn't substitute itself with anything in
3008
Item_direct_view_ref::fix_fields.
3010
sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
3011
sj_nest->nested_join->sj_outer_expr_list.empty();
3013
if (subq_pred->left_expr->cols() == 1)
3015
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3017
Item *item_eq= new Item_func_eq(subq_pred->left_expr,
3018
subq_lex->ref_pointer_array[0]);
3019
item_eq->name= (char*)subq_sj_cond_name;
3020
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3024
for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3026
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3029
new Item_func_eq(subq_pred->left_expr->element_index(i),
3030
subq_lex->ref_pointer_array[i]);
3031
item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3032
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3035
/* Fix the created equality and AND */
3036
sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3039
Walk through sj nest's WHERE and ON expressions and call
3040
item->fix_table_changes() for all items.
3042
sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3043
fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3046
/* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3047
subq_lex->master_unit()->exclude_level();
3049
/* Inject sj_on_expr into the parent's WHERE or ON */
3052
emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
3053
sj_nest->sj_on_expr);
3054
emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3058
/* Inject into the WHERE */
3059
parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3060
parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3061
parent_join->select_lex->where= parent_join->conds;
3069
Convert candidate subquery predicates to semi-joins
3072
JOIN::flatten_subqueries()
3075
Convert candidate subquery predicates to semi-joins.
3082
bool JOIN::flatten_subqueries()
3084
Item_in_subselect **in_subq;
3085
Item_in_subselect **in_subq_end;
3087
if (sj_subselects.elements() == 0)
3090
/* 1. Fix children subqueries */
3091
for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
3092
in_subq != in_subq_end; in_subq++)
3094
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3095
child_join->outer_tables = child_join->tables;
3096
if (child_join->flatten_subqueries())
3098
(*in_subq)->sj_convert_priority=
3099
(*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3102
//dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3104
2. Pick which subqueries to convert:
3105
sort the subquery array
3106
- prefer correlated subqueries over uncorrelated;
3107
- prefer subqueries that have greater number of outer tables;
3109
sj_subselects.sort(subq_sj_candidate_cmp);
3110
// #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3111
/* Replace all subqueries to be flattened with Item_int(1) */
3112
for (in_subq= sj_subselects.front();
3113
in_subq != in_subq_end &&
3114
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3117
if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3121
for (in_subq= sj_subselects.front();
3122
in_subq != in_subq_end &&
3123
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3126
if (convert_subq_to_sj(this, *in_subq))
3130
/* 3. Finalize those we didn't convert */
3131
for (; in_subq!= in_subq_end; in_subq++)
3133
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3134
Item_subselect::trans_res res;
3135
(*in_subq)->changed= 0;
3136
(*in_subq)->fixed= 0;
3137
res= (*in_subq)->select_transformer(child_join);
3138
if (res == Item_subselect::RES_ERROR)
3141
(*in_subq)->changed= 1;
3142
(*in_subq)->fixed= 1;
3144
Item *substitute= (*in_subq)->substitution;
3145
bool do_fix_fields= !(*in_subq)->substitution->fixed;
3146
if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3149
//if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3152
sj_subselects.clear();
3158
Setup for execution all subqueries of a query, for which the optimizer
3159
chose hash semi-join.
3161
@details Iterate over all subqueries of the query, and if they are under an
3162
IN predicate, and the optimizer chose to compute it via hash semi-join:
3163
- try to initialize all data structures needed for the materialized execution
3164
of the IN predicate,
3165
- if this fails, then perform the IN=>EXISTS transformation which was
3166
previously blocked during JOIN::prepare.
3168
This method is part of the "code generation" query processing phase.
3170
This phase must be called after substitute_for_best_equal_field() because
3171
that function may replace items with other items from a multiple equality,
3172
and we need to reference the correct items in the index access method of the
3175
@return Operation status
3176
@retval false success.
3177
@retval true error occurred.
3180
bool JOIN::setup_subquery_materialization()
3182
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3183
un= un->next_unit())
3185
for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3187
Item_subselect *subquery_predicate= sl->master_unit()->item;
3188
if (subquery_predicate &&
3189
subquery_predicate->substype() == Item_subselect::IN_SUBS)
3191
Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3192
if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3193
in_subs->setup_engine())
3203
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3206
find_eq_ref_candidate()
3207
table Table to be checked
3208
sj_inner_tables Bitmap of inner tables. eq_ref(inner_table) doesn't
3212
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3215
Check again if it is feasible to factor common parts with constant table
3219
true - There exists an eq_ref(outer-tables) candidate
3223
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3225
KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3230
while (1) /* For each key */
3233
KEY *keyinfo= table->key_info + key;
3234
key_part_map bound_parts= 0;
3235
if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
3237
do /* For all equalities on all key parts */
3239
/* Check if this is "t.keypart = expr(outer_tables) */
3240
if (!(keyuse->used_tables & sj_inner_tables) &&
3241
!(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3243
bound_parts |= 1 << keyuse->keypart;
3246
} while (keyuse->key == key && keyuse->table == table);
3248
if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3250
if (keyuse->table != table)
3258
if (keyuse->table != table)
3261
while (keyuse->key == key);
3270
Pull tables out of semi-join nests, if possible
3273
pull_out_semijoin_tables()
3274
join The join where to do the semi-join flattening
3277
Try to pull tables out of semi-join nests.
3280
When this function is called, the join may have several semi-join nests
3281
(possibly within different semi-join nests), but it is guaranteed that
3282
one semi-join nest does not contain another.
3285
A table can be pulled out of the semi-join nest if
3286
- It is a constant table
3290
* Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3292
* Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3293
* Semi-join nests TABLE_LIST::sj_inner_tables
3295
This operation is (and should be) performed at each PS execution since
3296
tables may become/cease to be constant across PS reexecutions.
3300
1 - Out of memory error
3303
int pull_out_semijoin_tables(JOIN *join)
3305
TABLE_LIST *sj_nest;
3306
List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3308
/* Try pulling out of the each of the semi-joins */
3309
while ((sj_nest= sj_list_it++))
3311
/* Action #1: Mark the constant tables to be pulled out */
3312
table_map pulled_tables= 0;
3314
List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3316
while ((tbl= child_li++))
3320
tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3321
if (tbl->table->map & join->const_table_map)
3323
pulled_tables |= tbl->table->map;
3329
Action #2: Find which tables we can pull out based on
3330
update_ref_and_keys() data. Note that pulling one table out can allow
3331
us to pull out some other tables too.
3333
bool pulled_a_table;
3336
pulled_a_table= false;
3338
while ((tbl= child_li++))
3340
if (tbl->table && !(pulled_tables & tbl->table->map))
3342
if (find_eq_ref_candidate(tbl->table,
3343
sj_nest->nested_join->used_tables &
3346
pulled_a_table= true;
3347
pulled_tables |= tbl->table->map;
3351
} while (pulled_a_table);
3354
if ((sj_nest)->nested_join->used_tables == pulled_tables)
3356
(sj_nest)->sj_inner_tables= 0;
3357
while ((tbl= child_li++))
3360
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3365
/* Record the bitmap of inner tables, mark the inner tables */
3366
table_map inner_tables=(sj_nest)->nested_join->used_tables &
3368
(sj_nest)->sj_inner_tables= inner_tables;
3369
while ((tbl= child_li++))
3373
if (inner_tables & tbl->table->map)
3374
tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3376
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
455
3384
/*****************************************************************************
456
Create JoinTableS, make a guess about the table types,
3385
Create JOIN_TABS, make a guess about the table types,
457
3386
Approximate how many records will be used in each table
458
3387
*****************************************************************************/
459
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
3390
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3392
const key_map *keys,ha_rows limit)
462
if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3395
if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
463
3396
return(0); // Fatal error flag is set
466
3399
select->head=table;
467
3400
table->reginfo.impossible_range=0;
468
if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
3401
if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
469
3402
limit, 0, false)) == 1)
470
3403
return(select->quick->records);
471
3404
if (error == -1)
477
3410
return(HA_POS_ERROR); /* This shouldn't happend */
3414
This structure is used to collect info on potentially sargable
3415
predicates in order to check whether they become sargable after
3416
reading const tables.
3417
We form a bitmap of indexes that can be used for sargable predicates.
3418
Only such indexes are involved in range analysis.
3420
typedef struct st_sargable_param
3422
Field *field; /* field against which to check sargability */
3423
Item **arg_value; /* values of potential keys for lookups */
3424
uint num_values; /* number of values in the above array */
3428
Calculate the best possible join and initialize the join structure.
3437
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3438
DYNAMIC_ARRAY *keyuse_array)
3442
uint i,table_count,const_count,key;
3443
table_map found_const_table_map, all_table_map, found_ref, refs;
3444
key_map const_ref, eq_part;
3445
TABLE **table_vector;
3446
JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3447
KEYUSE *keyuse,*start_keyuse;
3448
table_map outer_join=0;
3449
SARGABLE_PARAM *sargables= 0;
3450
JOIN_TAB *stat_vector[MAX_TABLES+1];
3452
table_count=join->tables;
3453
stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3454
stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3455
table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3456
if (!stat || !stat_ref || !table_vector)
3457
return(1); // Eom /* purecov: inspected */
3459
join->best_ref=stat_vector;
3461
stat_end=stat+table_count;
3462
found_const_table_map= all_table_map=0;
3467
s++, tables= tables->next_leaf, i++)
3469
TABLE_LIST *embedding= tables->embedding;
3472
s->const_keys.init();
3473
s->checked_keys.init();
3474
s->needed_reg.init();
3475
table_vector[i]=s->table=table=tables->table;
3476
table->pos_in_table_list= tables;
3477
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3480
table->file->print_error(error, MYF(0));
3483
table->quick_keys.clear_all();
3484
table->reginfo.join_tab=s;
3485
table->reginfo.not_exists_optimize=0;
3486
bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
3487
all_table_map|= table->map;
3489
s->info=0; // For describe
3491
s->dependent= tables->dep_tables;
3492
s->key_dependent= 0;
3493
if (tables->schema_table)
3494
table->file->stats.records= 2;
3495
table->quick_condition_rows= table->file->stats.records;
3497
s->on_expr_ref= &tables->on_expr;
3498
if (*s->on_expr_ref)
3500
/* s is the only inner table of an outer join */
3501
if (!table->file->stats.records && !embedding)
3503
s->dependent= 0; // Ignore LEFT JOIN depend.
3504
set_position(join,const_count++,s,(KEYUSE*) 0);
3507
outer_join|= table->map;
3508
s->embedding_map= 0;
3509
for (;embedding; embedding= embedding->embedding)
3510
s->embedding_map|= embedding->nested_join->nj_map;
3513
if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3515
/* s belongs to a nested join, maybe to several embedded joins */
3516
s->embedding_map= 0;
3519
NESTED_JOIN *nested_join= embedding->nested_join;
3520
s->embedding_map|=nested_join->nj_map;
3521
s->dependent|= embedding->dep_tables;
3522
embedding= embedding->embedding;
3523
outer_join|= nested_join->used_tables;
3528
if ((table->s->system || table->file->stats.records <= 1) &&
3530
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3531
!table->fulltext_searched && !join->no_const_tables)
3533
set_position(join,const_count++,s,(KEYUSE*) 0);
3537
join->outer_join=outer_join;
3539
if (join->outer_join)
3542
Build transitive closure for relation 'to be dependent on'.
3543
This will speed up the plan search for many cases with outer joins,
3544
as well as allow us to catch illegal cross references/
3545
Warshall's algorithm is used to build the transitive closure.
3546
As we use bitmaps to represent the relation the complexity
3547
of the algorithm is O((number of tables)^2).
3549
for (i= 0, s= stat ; i < table_count ; i++, s++)
3551
for (uint j= 0 ; j < table_count ; j++)
3553
table= stat[j].table;
3554
if (s->dependent & table->map)
3555
s->dependent |= table->reginfo.join_tab->dependent;
3558
s->table->maybe_null= 1;
3560
/* Catch illegal cross references for outer joins */
3561
for (i= 0, s= stat ; i < table_count ; i++, s++)
3563
if (s->dependent & s->table->map)
3565
join->tables=0; // Don't use join->table
3566
my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3569
s->key_dependent= s->dependent;
3573
if (conds || outer_join)
3574
if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3575
conds, join->cond_equal,
3576
~outer_join, join->select_lex, &sargables))
3579
/* Read tables with 0 or 1 rows (system tables) */
3580
join->const_table_map= 0;
3582
for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3589
join->const_table_map|=s->table->map;
3590
if ((tmp=join_read_const_table(s, p_pos)))
3593
return(1); // Fatal error
3596
found_const_table_map|= s->table->map;
3599
/* loop until no more const tables are found */
3603
more_const_tables_found:
3608
We only have to loop from stat_vector + const_count as
3609
set_position() will move all const_tables first in stat_vector
3612
for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3617
If equi-join condition by a key is null rejecting and after a
3618
substitution of a const table the key value happens to be null
3619
then we can state that there are no matches for this equi-join.
3621
if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3624
When performing an outer join operation if there are no matching rows
3625
for the single row of the outer table all the inner tables are to be
3626
null complemented and thus considered as constant tables.
3627
Here we apply this consideration to the case of outer join operations
3628
with a single inner table only because the case with nested tables
3629
would require a more thorough analysis.
3630
TODO. Apply single row substitution to null complemented inner tables
3631
for nested outer join operations.
3633
while (keyuse->table == table)
3635
if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3636
keyuse->val->is_null() && keyuse->null_rejecting)
3639
mark_as_null_row(table);
3640
found_const_table_map|= table->map;
3641
join->const_table_map|= table->map;
3642
set_position(join,const_count++,s,(KEYUSE*) 0);
3643
goto more_const_tables_found;
3649
if (s->dependent) // If dependent on some table
3651
// All dep. must be constants
3652
if (s->dependent & ~(found_const_table_map))
3654
if (table->file->stats.records <= 1L &&
3655
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3656
!table->pos_in_table_list->embedding)
3660
join->const_table_map|=table->map;
3661
set_position(join,const_count++,s,(KEYUSE*) 0);
3662
if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3665
return(1); // Fatal error
3668
found_const_table_map|= table->map;
3672
/* check if table can be read by key or table only uses const refs */
3673
if ((keyuse=s->keyuse))
3676
while (keyuse->table == table)
3678
start_keyuse=keyuse;
3680
s->keys.set_bit(key); // QQ: remove this ?
3683
const_ref.clear_all();
3684
eq_part.clear_all();
3687
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3689
if (!((~found_const_table_map) & keyuse->used_tables))
3690
const_ref.set_bit(keyuse->keypart);
3692
refs|=keyuse->used_tables;
3693
eq_part.set_bit(keyuse->keypart);
3696
} while (keyuse->table == table && keyuse->key == key);
3698
if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3699
!table->fulltext_searched &&
3700
!table->pos_in_table_list->embedding)
3702
if ((table->key_info[key].flags & (HA_NOSAME))
3705
if (const_ref == eq_part)
3706
{ // Found everything for ref.
3710
join->const_table_map|=table->map;
3711
set_position(join,const_count++,s,start_keyuse);
3712
if (create_ref_for_key(join, s, start_keyuse,
3713
found_const_table_map))
3715
if ((tmp=join_read_const_table(s,
3716
join->positions+const_count-1)))
3719
return(1); // Fatal error
3722
found_const_table_map|= table->map;
3726
found_ref|= refs; // Table is const if all refs are const
3728
else if (const_ref == eq_part)
3729
s->const_keys.set_bit(key);
3734
} while (join->const_table_map & found_ref && ref_changed);
3737
Update info on indexes that can be used for search lookups as
3738
reading const tables may has added new sargable predicates.
3740
if (const_count && sargables)
3742
for( ; sargables->field ; sargables++)
3744
Field *field= sargables->field;
3745
JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3746
key_map possible_keys= field->key_start;
3747
possible_keys.intersect(field->table->keys_in_use_for_query);
3749
for (uint j=0; j < sargables->num_values; j++)
3750
is_const&= sargables->arg_value[j]->const_item();
3752
join_tab[0].const_keys.merge(possible_keys);
3756
if (pull_out_semijoin_tables(join))
3759
/* Calc how many (possible) matched records in each table */
3761
for (s=stat ; s < stat_end ; s++)
3763
if (s->type == JT_SYSTEM || s->type == JT_CONST)
3765
/* Only one matching row */
3766
s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3769
/* Approximate found rows and time to read them */
3770
s->found_records=s->records=s->table->file->stats.records;
3771
s->read_time=(ha_rows) s->table->file->scan_time();
3774
Set a max range of how many seeks we can expect when using keys
3775
This is can't be to high as otherwise we are likely to use
3778
s->worst_seeks= min((double) s->found_records / 10,
3779
(double) s->read_time*3);
3780
if (s->worst_seeks < 2.0) // Fix for small tables
3784
Add to stat->const_keys those indexes for which all group fields or
3785
all select distinct fields participate in one index.
3787
add_group_and_distinct_keys(join, s);
3789
if (!s->const_keys.is_clear_all() &&
3790
!s->table->pos_in_table_list->embedding)
3794
select= make_select(s->table, found_const_table_map,
3795
found_const_table_map,
3796
*s->on_expr_ref ? *s->on_expr_ref : conds,
3800
records= get_quick_record_count(join->thd, select, s->table,
3801
&s->const_keys, join->row_limit);
3802
s->quick=select->quick;
3803
s->needed_reg=select->needed_reg;
3805
if (records == 0 && s->table->reginfo.impossible_range)
3808
Impossible WHERE or ON expression
3809
In case of ON, we mark that the we match one empty NULL row.
3810
In case of WHERE, don't set found_const_table_map to get the
3811
caller to abort with a zero row result.
3813
join->const_table_map|= s->table->map;
3814
set_position(join,const_count++,s,(KEYUSE*) 0);
3816
if (*s->on_expr_ref)
3818
/* Generate empty row */
3819
s->info= "Impossible ON condition";
3820
found_const_table_map|= s->table->map;
3822
mark_as_null_row(s->table); // All fields are NULL
3825
if (records != HA_POS_ERROR)
3827
s->found_records=records;
3828
s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3834
join->join_tab=stat;
3835
join->map2table=stat_ref;
3836
join->table= join->all_tables=table_vector;
3837
join->const_tables=const_count;
3838
join->found_const_table_map=found_const_table_map;
3840
/* Find an optimal join order of the non-constant tables. */
3841
if (join->const_tables != join->tables)
3843
optimize_keyuse(join, keyuse_array);
3844
if (choose_plan(join, all_table_map & ~join->const_table_map))
3849
memcpy((uchar*) join->best_positions,(uchar*) join->positions,
3850
sizeof(POSITION)*join->const_tables);
3851
join->best_read=1.0;
3853
/* Generate an execution plan from the found optimal join order. */
3854
return(join->thd->killed || get_best_combination(join));
480
3858
/*****************************************************************************
481
3859
Check with keys are used and with tables references with tables
482
3860
Updates in stat:
485
3863
keyuse Pointer to possible keys
486
3864
*****************************************************************************/
3866
/// Used when finding key fields
3867
typedef struct key_field_t {
3869
Item *val; ///< May be empty if diff constant
3871
uint optimize; // KEY_OPTIMIZE_*
3874
If true, the condition this struct represents will not be satisfied
3877
bool null_rejecting;
3878
bool *cond_guard; /* See KEYUSE::cond_guard */
3879
uint sj_pred_no; /* See KEYUSE::sj_pred_no */
3883
Merge new key definitions to old ones, remove those not used in both.
3885
This is called for OR between different levels.
3887
To be able to do 'ref_or_null' we merge a comparison of a column
3888
and 'column IS NULL' to one test. This is useful for sub select queries
3889
that are internally transformed to something like:.
3892
SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
3895
KEY_FIELD::null_rejecting is processed as follows: @n
3896
result has null_rejecting=true if it is set for both ORed references.
3898
- (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true
3899
- (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3902
The result of this is that we're missing some 'ref' accesses.
3903
OptimizerTeam: Fix this
3907
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3910
if (start == new_fields)
3911
return start; // Impossible or
3912
if (new_fields == end)
3913
return start; // No new fields, skip all
3915
KEY_FIELD *first_free=new_fields;
3917
/* Mark all found fields in old array */
3918
for (; new_fields != end ; new_fields++)
3920
for (KEY_FIELD *old=start ; old != first_free ; old++)
3922
if (old->field == new_fields->field)
3925
NOTE: below const_item() call really works as "!used_tables()", i.e.
3926
it can return false where it is feasible to make it return true.
3928
The cause is as follows: Some of the tables are already known to be
3929
const tables (the detection code is in make_join_statistics(),
3930
above the update_ref_and_keys() call), but we didn't propagate
3931
information about this: TABLE::const_table is not set to true, and
3932
Item::update_used_tables() hasn't been called for each item.
3933
The result of this is that we're missing some 'ref' accesses.
3934
TODO: OptimizerTeam: Fix this
3936
if (!new_fields->val->const_item())
3939
If the value matches, we can use the key reference.
3940
If not, we keep it until we have examined all new values
3942
if (old->val->eq(new_fields->val, old->field->binary()))
3944
old->level= and_level;
3945
old->optimize= ((old->optimize & new_fields->optimize &
3946
KEY_OPTIMIZE_EXISTS) |
3947
((old->optimize | new_fields->optimize) &
3948
KEY_OPTIMIZE_REF_OR_NULL));
3949
old->null_rejecting= (old->null_rejecting &&
3950
new_fields->null_rejecting);
3953
else if (old->eq_func && new_fields->eq_func &&
3954
old->val->eq_by_collation(new_fields->val,
3955
old->field->binary(),
3956
old->field->charset()))
3959
old->level= and_level;
3960
old->optimize= ((old->optimize & new_fields->optimize &
3961
KEY_OPTIMIZE_EXISTS) |
3962
((old->optimize | new_fields->optimize) &
3963
KEY_OPTIMIZE_REF_OR_NULL));
3964
old->null_rejecting= (old->null_rejecting &&
3965
new_fields->null_rejecting);
3967
else if (old->eq_func && new_fields->eq_func &&
3968
((old->val->const_item() && old->val->is_null()) ||
3969
new_fields->val->is_null()))
3971
/* field = expression OR field IS NULL */
3972
old->level= and_level;
3973
old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3975
Remember the NOT NULL value unless the value does not depend
3978
if (!old->val->used_tables() && old->val->is_null())
3979
old->val= new_fields->val;
3980
/* The referred expression can be NULL: */
3981
old->null_rejecting= 0;
3986
We are comparing two different const. In this case we can't
3987
use a key-lookup on this so it's better to remove the value
3988
and let the range optimzier handle it
3990
if (old == --first_free) // If last item
3992
*old= *first_free; // Remove old value
3993
old--; // Retry this value
3998
/* Remove all not used items */
3999
for (KEY_FIELD *old=start ; old != first_free ;)
4001
if (old->level != and_level)
4002
{ // Not used in all levels
4003
if (old == --first_free)
4005
*old= *first_free; // Remove old value
4015
Add a possible key to array of possible keys if it's usable as a key
4017
@param key_fields Pointer to add key, if usable
4018
@param and_level And level, to be stored in KEY_FIELD
4019
@param cond Condition predicate
4020
@param field Field used in comparision
4021
@param eq_func True if we used =, <=> or IS NULL
4022
@param value Value used for comparison with field
4023
@param usable_tables Tables which can be used for key optimization
4024
@param sargables IN/OUT Array of found sargable candidates
4027
If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4028
table, we store this to be able to do not exists optimization later.
4031
*key_fields is incremented if we stored a key in the array
4035
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4036
Field *field, bool eq_func, Item **value, uint num_values,
4037
table_map usable_tables, SARGABLE_PARAM **sargables)
4039
uint exists_optimize= 0;
4040
if (!(field->flags & PART_KEY_FLAG))
4042
// Don't remove column IS NULL on a LEFT JOIN table
4043
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4044
!field->table->maybe_null || field->null_ptr)
4045
return; // Not a key. Skip it
4046
exists_optimize= KEY_OPTIMIZE_EXISTS;
4047
assert(num_values == 1);
4051
table_map used_tables=0;
4053
for (uint i=0; i<num_values; i++)
4055
used_tables|=(value[i])->used_tables();
4056
if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4061
if (!(usable_tables & field->table->map))
4063
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4064
!field->table->maybe_null || field->null_ptr)
4065
return; // Can't use left join optimize
4066
exists_optimize= KEY_OPTIMIZE_EXISTS;
4070
JOIN_TAB *stat=field->table->reginfo.join_tab;
4071
key_map possible_keys=field->key_start;
4072
possible_keys.intersect(field->table->keys_in_use_for_query);
4073
stat[0].keys.merge(possible_keys); // Add possible keys
4076
Save the following cases:
4078
Field LIKE constant where constant doesn't start with a wildcard
4079
Field = field2 where field2 is in a different table
4086
stat[0].key_dependent|=used_tables;
4089
for (uint i=0; i<num_values; i++)
4091
if (!(is_const&= value[i]->const_item()))
4095
stat[0].const_keys.merge(possible_keys);
4099
Save info to be able check whether this predicate can be
4100
considered as sargable for range analisis after reading const tables.
4101
We do not save info about equalities as update_const_equal_items
4102
will take care of updating info on keys from sargable equalities.
4105
(*sargables)->field= field;
4106
(*sargables)->arg_value= value;
4107
(*sargables)->num_values= num_values;
4110
We can't always use indexes when comparing a string index to a
4111
number. cmp_type() is checked to allow compare of dates to numbers.
4112
eq_func is NEVER true when num_values > 1
4117
Additional optimization: if we're processing
4118
"t.key BETWEEN c1 AND c1" then proceed as if we were processing
4120
TODO: This is a very limited fix. A more generic fix is possible.
4121
There are 2 options:
4122
A) Make equality propagation code be able to handle BETWEEN
4123
(including cases like t1.key BETWEEN t2.key AND t3.key)
4124
B) Make range optimizer to infer additional "t.key = c" equalities
4125
and use them in equality propagation process (see details in
4128
if ((cond->functype() != Item_func::BETWEEN) ||
4129
((Item_func_between*) cond)->negated ||
4130
!value[0]->eq(value[1], field->binary()))
4135
if (field->result_type() == STRING_RESULT)
4137
if ((*value)->result_type() != STRING_RESULT)
4139
if (field->cmp_type() != (*value)->result_type())
4145
We can't use indexes if the effective collation
4146
of the operation differ from the field collation.
4148
if (field->cmp_type() == STRING_RESULT &&
4149
((Field_str*)field)->charset() != cond->compare_collation())
4156
For the moment eq_func is always true. This slot is reserved for future
4157
extensions where we want to remembers other things than just eq comparisons
4160
/* Store possible eq field */
4161
(*key_fields)->field= field;
4162
(*key_fields)->eq_func= eq_func;
4163
(*key_fields)->val= *value;
4164
(*key_fields)->level= and_level;
4165
(*key_fields)->optimize= exists_optimize;
4167
If the condition has form "tbl.keypart = othertbl.field" and
4168
othertbl.field can be NULL, there will be no matches if othertbl.field
4170
We use null_rejecting in add_not_null_conds() to add
4171
'othertbl.field IS NOT NULL' to tab->select_cond.
4173
(*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4174
cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4175
((*value)->type() == Item::FIELD_ITEM) &&
4176
((Item_field*)*value)->field->maybe_null());
4177
(*key_fields)->cond_guard= NULL;
4178
(*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
4179
cond->name < subq_sj_cond_name + 64)?
4180
cond->name - subq_sj_cond_name: UINT_MAX;
4185
Add possible keys to array of possible keys originated from a simple
4188
@param key_fields Pointer to add key, if usable
4189
@param and_level And level, to be stored in KEY_FIELD
4190
@param cond Condition predicate
4191
@param field Field used in comparision
4192
@param eq_func True if we used =, <=> or IS NULL
4193
@param value Value used for comparison with field
4194
Is NULL for BETWEEN and IN
4195
@param usable_tables Tables which can be used for key optimization
4196
@param sargables IN/OUT Array of found sargable candidates
4199
If field items f1 and f2 belong to the same multiple equality and
4200
a key is added for f1, the the same key is added for f2.
4203
*key_fields is incremented if we stored a key in the array
4207
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4208
Item_func *cond, Item_field *field_item,
4209
bool eq_func, Item **val,
4210
uint num_values, table_map usable_tables,
4211
SARGABLE_PARAM **sargables)
4213
Field *field= field_item->field;
4214
add_key_field(key_fields, and_level, cond, field,
4215
eq_func, val, num_values, usable_tables, sargables);
4216
Item_equal *item_equal= field_item->item_equal;
4220
Add to the set of possible key values every substitution of
4221
the field for an equal field included into item_equal
4223
Item_equal_iterator it(*item_equal);
4225
while ((item= it++))
4227
if (!field->eq(item->field))
4229
add_key_field(key_fields, and_level, cond, item->field,
4230
eq_func, val, num_values, usable_tables,
4238
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4239
COND *cond, table_map usable_tables,
4240
SARGABLE_PARAM **sargables)
4242
if (cond->type() == Item_func::COND_ITEM)
4244
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4245
KEY_FIELD *org_key_fields= *key_fields;
4247
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4251
add_key_fields(join, key_fields, and_level, item, usable_tables,
4253
for (; org_key_fields != *key_fields ; org_key_fields++)
4254
org_key_fields->level= *and_level;
4259
add_key_fields(join, key_fields, and_level, li++, usable_tables,
4264
KEY_FIELD *start_key_fields= *key_fields;
4266
add_key_fields(join, key_fields, and_level, item, usable_tables,
4268
*key_fields=merge_key_fields(org_key_fields,start_key_fields,
4269
*key_fields,++(*and_level));
4276
Subquery optimization: Conditions that are pushed down into subqueries
4277
are wrapped into Item_func_trig_cond. We process the wrapped condition
4278
but need to set cond_guard for KEYUSE elements generated from it.
4281
if (cond->type() == Item::FUNC_ITEM &&
4282
((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4284
Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4285
if (!join->group_list && !join->order &&
4287
join->unit->item->substype() == Item_subselect::IN_SUBS &&
4288
!join->unit->is_union())
4290
KEY_FIELD *save= *key_fields;
4291
add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4293
// Indicate that this ref access candidate is for subquery lookup:
4294
for (; save != *key_fields; save++)
4295
save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4301
/* If item is of type 'field op field/constant' add it to key_fields */
4302
if (cond->type() != Item::FUNC_ITEM)
4304
Item_func *cond_func= (Item_func*) cond;
4305
switch (cond_func->select_optimize()) {
4306
case Item_func::OPTIMIZE_NONE:
4308
case Item_func::OPTIMIZE_KEY:
4312
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4313
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4315
values= cond_func->arguments()+1;
4316
if (cond_func->functype() == Item_func::NE_FUNC &&
4317
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4318
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4320
assert(cond_func->functype() != Item_func::IN_FUNC ||
4321
cond_func->argument_count() != 2);
4322
add_key_equal_fields(key_fields, *and_level, cond_func,
4323
(Item_field*) (cond_func->key_item()->real_item()),
4325
cond_func->argument_count()-1,
4326
usable_tables, sargables);
4328
if (cond_func->functype() == Item_func::BETWEEN)
4330
values= cond_func->arguments();
4331
for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4333
Item_field *field_item;
4334
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4336
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4338
field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4339
add_key_equal_fields(key_fields, *and_level, cond_func,
4340
field_item, 0, values, 1, usable_tables,
4347
case Item_func::OPTIMIZE_OP:
4349
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4350
cond_func->functype() == Item_func::EQUAL_FUNC);
4352
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4353
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4355
add_key_equal_fields(key_fields, *and_level, cond_func,
4356
(Item_field*) (cond_func->arguments()[0])->real_item(),
4358
cond_func->arguments()+1, 1, usable_tables,
4361
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4362
cond_func->functype() != Item_func::LIKE_FUNC &&
4363
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4365
add_key_equal_fields(key_fields, *and_level, cond_func,
4366
(Item_field*) (cond_func->arguments()[1])->real_item(),
4368
cond_func->arguments(),1,usable_tables,
4373
case Item_func::OPTIMIZE_NULL:
4374
/* column_name IS [NOT] NULL */
4375
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4376
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4378
Item *tmp=new Item_null;
4379
if (unlikely(!tmp)) // Should never be true
4381
add_key_equal_fields(key_fields, *and_level, cond_func,
4382
(Item_field*) (cond_func->arguments()[0])->real_item(),
4383
cond_func->functype() == Item_func::ISNULL_FUNC,
4384
&tmp, 1, usable_tables, sargables);
4387
case Item_func::OPTIMIZE_EQUAL:
4388
Item_equal *item_equal= (Item_equal *) cond;
4389
Item *const_item= item_equal->get_const();
4390
Item_equal_iterator it(*item_equal);
4395
For each field field1 from item_equal consider the equality
4396
field1=const_item as a condition allowing an index access of the table
4397
with field1 by the keys value of field1.
4399
while ((item= it++))
4401
add_key_field(key_fields, *and_level, cond_func, item->field,
4402
true, &const_item, 1, usable_tables, sargables);
4408
Consider all pairs of different fields included into item_equal.
4409
For each of them (field1, field1) consider the equality
4410
field1=field2 as a condition allowing an index access of the table
4411
with field1 by the keys value of field2.
4413
Item_equal_iterator fi(*item_equal);
4414
while ((item= fi++))
4416
Field *field= item->field;
4417
while ((item= it++))
4419
if (!field->eq(item->field))
4421
add_key_field(key_fields, *and_level, cond_func, field,
4422
true, (Item **) &item, 1, usable_tables,
490
4434
Add all keys with uses 'field' for some keypart.
492
4436
If field->and_level != and_level then only mark key_part as const_part.
494
uint32_t max_part_bit(key_part_map bits)
4440
max_part_bit(key_part_map bits)
497
4443
for (found=0; bits & 1 ; found++,bits>>=1) ;
501
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
4448
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4450
Field *field=key_field->field;
4451
TABLE *form= field->table;
4454
if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4456
for (uint key=0 ; key < form->s->keys ; key++)
4458
if (!(form->keys_in_use_for_query.is_set(key)))
4461
uint key_parts= (uint) form->key_info[key].key_parts;
4462
for (uint part=0 ; part < key_parts ; part++)
4464
if (field->eq(form->key_info[key].key_part[part].field))
4466
keyuse.table= field->table;
4467
keyuse.val = key_field->val;
4469
keyuse.keypart=part;
4470
keyuse.keypart_map= (key_part_map) 1 << part;
4471
keyuse.used_tables=key_field->val->used_tables();
4472
keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4473
keyuse.null_rejecting= key_field->null_rejecting;
4474
keyuse.cond_guard= key_field->cond_guard;
4475
keyuse.sj_pred_no= key_field->sj_pred_no;
4476
VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4484
sort_keyuse(KEYUSE *a,KEYUSE *b)
504
if (a->getTable()->tablenr != b->getTable()->tablenr)
505
return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
506
if (a->getKey() != b->getKey())
507
return static_cast<int>((a->getKey() - b->getKey()));
508
if (a->getKeypart() != b->getKeypart())
509
return static_cast<int>((a->getKeypart() - b->getKeypart()));
4487
if (a->table->tablenr != b->table->tablenr)
4488
return (int) (a->table->tablenr - b->table->tablenr);
4489
if (a->key != b->key)
4490
return (int) (a->key - b->key);
4491
if (a->keypart != b->keypart)
4492
return (int) (a->keypart - b->keypart);
510
4493
// Place const values before other ones
511
if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
512
test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
4494
if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4495
test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
514
4497
/* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
515
return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
516
(b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
4498
return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4499
(b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4504
Add to KEY_FIELD array all 'ref' access candidates within nested join.
4506
This function populates KEY_FIELD array with entries generated from the
4507
ON condition of the given nested join, and does the same for nested joins
4508
contained within this nested join.
4510
@param[in] nested_join_table Nested join pseudo-table to process
4511
@param[in,out] end End of the key field array
4512
@param[in,out] and_level And-level
4513
@param[in,out] sargables Array of found sargable candidates
4517
We can add accesses to the tables that are direct children of this nested
4518
join (1), and are not inner tables w.r.t their neighbours (2).
4520
Example for #1 (outer brackets pair denotes nested join this function is
4523
... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4527
... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4529
In examples 1-2 for condition cond, we can add 'ref' access candidates to
4533
... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4535
Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4538
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4539
KEY_FIELD **end, uint *and_level,
4540
SARGABLE_PARAM **sargables)
4542
List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4543
List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4544
bool have_another = false;
4545
table_map tables= 0;
4547
assert(nested_join_table->nested_join);
4549
while ((table= li++) || (have_another && (li=li2, have_another=false,
4552
if (table->nested_join)
4554
if (!table->on_expr)
4556
/* It's a semi-join nest. Walk into it as if it wasn't a nest */
4559
li= List_iterator<TABLE_LIST>(table->nested_join->join_list);
4562
add_key_fields_for_nj(join, table, end, and_level, sargables);
4565
if (!table->on_expr)
4566
tables |= table->table->map;
4568
if (nested_join_table->on_expr)
4569
add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
521
4575
Update keyuse array with all possible keys we can use to fetch rows.
524
@param[out] keyuse Put here ordered array of KeyUse structures
4578
@param[out] keyuse Put here ordered array of KEYUSE structures
525
4579
@param join_tab Array in tablenr_order
526
4580
@param tables Number of tables in join
527
4581
@param cond WHERE condition (note that the function analyzes
776
4835
/* Intersect the keys of all group fields. */
777
4836
cur_item= indexed_fields_it++;
778
possible_keys|= cur_item->field->part_of_key;
4837
possible_keys.merge(cur_item->field->part_of_key);
779
4838
while ((cur_item= indexed_fields_it++))
781
possible_keys&= cur_item->field->part_of_key;
784
if (possible_keys.any())
785
join_tab->const_keys|= possible_keys;
789
Compare two JoinTable objects based on the number of accessed records.
791
@param ptr1 pointer to first JoinTable object
792
@param ptr2 pointer to second JoinTable object
4840
possible_keys.intersect(cur_item->field->part_of_key);
4843
if (!possible_keys.is_clear_all())
4844
join_tab->const_keys.merge(possible_keys);
4848
/*****************************************************************************
4849
Go through all combinations of not marked tables and find the one
4850
which uses least records
4851
*****************************************************************************/
4853
/** Save const tables first as used tables. */
4856
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4858
join->positions[idx].table= table;
4859
join->positions[idx].key=key;
4860
join->positions[idx].records_read=1.0; /* This is a const table */
4861
join->positions[idx].ref_depend_map= 0;
4863
/* Move the const table as down as possible in best_ref */
4864
JOIN_TAB **pos=join->best_ref+idx+1;
4865
JOIN_TAB *next=join->best_ref[idx];
4866
for (;next != table ; pos++)
4868
JOIN_TAB *tmp=pos[0];
4872
join->best_ref[idx]=table;
4877
Given a semi-join nest, find out which of the IN-equalities are bound
4880
get_bound_sj_equalities()
4881
sj_nest Semi-join nest
4882
remaining_tables Tables that are not yet bound
4885
Given a semi-join nest, find out which of the IN-equalities have their
4886
left part expression bound (i.e. the said expression doesn't refer to
4887
any of remaining_tables and can be evaluated).
4890
Bitmap of bound IN-equalities.
4893
ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest,
4894
table_map remaining_tables)
4896
List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4900
while ((item= li++))
4903
Q: should this take into account equality propagation and how?
4904
A: If e->outer_side is an Item_field, walk over the equality
4905
class and see if there is an element that is bound?
4906
(this is an optional feature)
4908
if (!(item->used_tables() & remaining_tables))
4918
Find the best access path for an extension of a partial execution
4919
plan and add this path to the plan.
4921
The function finds the best access path to table 's' from the passed
4922
partial plan where an access path is the general term for any means to
4923
access the data in 's'. An access path may use either an index or a scan,
4924
whichever is cheaper. The input partial plan is passed via the array
4925
'join->positions' of length 'idx'. The chosen access method for 's' and its
4926
cost are stored in 'join->positions[idx]'.
4928
@param join pointer to the structure providing all context info
4930
@param s the table to be joined by the function
4931
@param thd thread for the connection that submitted the query
4932
@param remaining_tables set of tables not included into the partial plan yet
4933
@param idx the length of the partial plan
4934
@param record_count estimate for the number of records returned by the
4936
@param read_time the cost of the partial plan
4943
best_access_path(JOIN *join,
4946
table_map remaining_tables,
4948
double record_count,
4949
double read_time __attribute__((__unused__)))
4951
KEYUSE *best_key= 0;
4952
uint best_max_key_part= 0;
4953
my_bool found_constraint= 0;
4954
double best= DBL_MAX;
4955
double best_time= DBL_MAX;
4956
double records= DBL_MAX;
4957
table_map best_ref_depends_map= 0;
4960
uint best_is_sj_inside_out= 0;
4963
{ /* Use key if possible */
4964
TABLE *table= s->table;
4965
KEYUSE *keyuse,*start_key=0;
4966
double best_records= DBL_MAX;
4967
uint max_key_part=0;
4968
ulonglong bound_sj_equalities= 0;
4969
bool try_sj_inside_out= false;
4971
Discover the bound equalites. We need to do this, if
4972
1. The next table is an SJ-inner table, and
4973
2. It is the first table from that semijoin, and
4974
3. We're not within a semi-join range (i.e. all semi-joins either have
4975
all or none of their tables in join_table_map), except
4976
s->emb_sj_nest (which we've just entered).
4977
3. All correlation references from this sj-nest are bound
4979
if (s->emb_sj_nest && // (1)
4980
s->emb_sj_nest->sj_in_exprs < 64 &&
4981
((remaining_tables & s->emb_sj_nest->sj_inner_tables) == // (2)
4982
s->emb_sj_nest->sj_inner_tables) && // (2)
4983
join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables && // (3)
4984
!(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4986
/* This table is an InsideOut scan candidate */
4987
bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
4989
try_sj_inside_out= true;
4992
/* Test how we can use keys */
4993
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key
4994
for (keyuse=s->keyuse ; keyuse->table == table ;)
4996
key_part_map found_part= 0;
4997
table_map found_ref= 0;
4998
uint key= keyuse->key;
4999
KEY *keyinfo= table->key_info+key;
5000
/* Bitmap of keyparts where the ref access is over 'keypart=const': */
5001
key_part_map const_part= 0;
5002
/* The or-null keypart in ref-or-null access: */
5003
key_part_map ref_or_null_part= 0;
5005
/* Calculate how many key segments of the current key we can use */
5007
ulonglong handled_sj_equalities=0;
5008
key_part_map sj_insideout_map= 0;
5010
do /* For each keypart */
5012
uint keypart= keyuse->keypart;
5013
table_map best_part_found_ref= 0;
5014
double best_prev_record_reads= DBL_MAX;
5016
do /* For each way to access the keypart */
5020
if 1. expression doesn't refer to forward tables
5021
2. we won't get two ref-or-null's
5023
if (!(remaining_tables & keyuse->used_tables) &&
5024
!(ref_or_null_part && (keyuse->optimize &
5025
KEY_OPTIMIZE_REF_OR_NULL)))
5027
found_part|= keyuse->keypart_map;
5028
if (!(keyuse->used_tables & ~join->const_table_map))
5029
const_part|= keyuse->keypart_map;
5031
double tmp2= prev_record_reads(join, idx, (found_ref |
5032
keyuse->used_tables));
5033
if (tmp2 < best_prev_record_reads)
5035
best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5036
best_prev_record_reads= tmp2;
5038
if (rec > keyuse->ref_table_rows)
5039
rec= keyuse->ref_table_rows;
5041
If there is one 'key_column IS NULL' expression, we can
5042
use this ref_or_null optimisation of this field
5044
if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5045
ref_or_null_part |= keyuse->keypart_map;
5048
if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5050
if (!(remaining_tables & keyuse->used_tables))
5051
bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5054
handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5055
sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5060
} while (keyuse->table == table && keyuse->key == key &&
5061
keyuse->keypart == keypart);
5062
found_ref|= best_part_found_ref;
5063
} while (keyuse->table == table && keyuse->key == key);
5066
Assume that that each key matches a proportional part of table.
5068
if (!found_part && !handled_sj_equalities)
5069
continue; // Nothing usable found
5071
if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5072
rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables
5074
bool sj_inside_out_scan= false;
5076
found_constraint= 1;
5078
Check if InsideOut scan is applicable:
5079
1. All IN-equalities are either "bound" or "handled"
5080
2. Index keyparts are
5083
if (try_sj_inside_out &&
5084
table->covering_keys.is_set(key) &&
5085
(handled_sj_equalities | bound_sj_equalities) == // (1)
5086
PREV_BITS(ulonglong, s->emb_sj_nest->sj_in_exprs)) // (1)
5088
uint n_fixed_parts= max_part_bit(found_part);
5089
if (n_fixed_parts != keyinfo->key_parts &&
5090
(PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5091
PREV_BITS(uint, keyinfo->key_parts))
5094
Not all parts are fixed. Produce bitmap of remaining bits and
5095
check if all of them are covered.
5097
sj_inside_out_scan= true;
5101
It's a confluent ref scan.
5103
That is, all found KEYUSE elements refer to IN-equalities,
5104
and there is really no ref access because there is no
5105
t.keypart0 = {bound expression}
5107
Calculate the cost of complete loose index scan.
5109
records= (double)s->table->file->stats.records;
5111
/* The cost is entire index scan cost (divided by 2) */
5112
best_time= s->table->file->index_only_read_time(key, records);
5114
/* Now figure how many different keys we will get */
5116
if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5117
records= records / rpc;
5124
Check if we found full key
5126
if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5129
max_key_part= (uint) ~0;
5130
if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5132
tmp = prev_record_reads(join, idx, found_ref);
5138
{ /* We found a const key */
5140
ReuseRangeEstimateForRef-1:
5141
We get here if we've found a ref(const) (c_i are constants):
5142
"(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond]
5144
If range optimizer was able to construct a "range"
5145
access on this index, then its condition "quick_cond" was
5146
eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5147
from the range optimizer.
5149
Proof of (*): By properties of range and ref optimizers
5150
quick_cond will be equal or tighther than ref_const_cond.
5151
ref_const_cond already covers "smallest" possible interval -
5152
a singlepoint interval over all keyparts. Therefore,
5153
quick_cond is equivalent to ref_const_cond (if it was an
5154
empty interval we wouldn't have got here).
5156
if (table->quick_keys.is_set(key))
5157
records= (double) table->quick_rows[key];
5160
/* quick_range couldn't use key! */
5161
records= (double) s->records/rec;
5166
if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5167
{ /* Prefer longer keys */
5169
((double) s->records / (double) rec *
5171
((double) (table->s->max_key_length-keyinfo->key_length) /
5172
(double) table->s->max_key_length)));
5174
records=2.0; /* Can't be as good as a unique */
5177
ReuseRangeEstimateForRef-2: We get here if we could not reuse
5178
E(#rows) from range optimizer. Make another try:
5180
If range optimizer produced E(#rows) for a prefix of the ref
5181
access we're considering, and that E(#rows) is lower then our
5182
current estimate, make an adjustment. The criteria of when we
5183
can make an adjustment is a special case of the criteria used
5184
in ReuseRangeEstimateForRef-3.
5186
if (table->quick_keys.is_set(key) &&
5187
const_part & (1 << table->quick_key_parts[key]) &&
5188
table->quick_n_ranges[key] == 1 &&
5189
records > (double) table->quick_rows[key])
5191
records= (double) table->quick_rows[key];
5194
/* Limit the number of matched rows */
5196
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5197
if (table->covering_keys.is_set(key))
5199
/* we can use only index tree */
5200
tmp= record_count * table->file->index_only_read_time(key, tmp);
5203
tmp= record_count*min(tmp,s->worst_seeks);
5209
Use as much key-parts as possible and a uniq key is better
5210
than a not unique key
5211
Set tmp to (previous record count) * (records / combination)
5213
if ((found_part & 1) &&
5214
(!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5215
found_part == PREV_BITS(uint,keyinfo->key_parts)))
5217
max_key_part= max_part_bit(found_part);
5219
ReuseRangeEstimateForRef-3:
5220
We're now considering a ref[or_null] access via
5221
(t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
5222
(same-as-above but with one cond replaced
5223
with "t.keypart_i IS NULL")] (**)
5225
Try re-using E(#rows) from "range" optimizer:
5226
We can do so if "range" optimizer used the same intervals as
5227
in (**). The intervals used by range optimizer may be not
5228
available at this point (as "range" access might have choosen to
5229
create quick select over another index), so we can't compare
5230
them to (**). We'll make indirect judgements instead.
5231
The sufficient conditions for re-use are:
5232
(C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5233
this is not satisfied we have no way to know which ranges
5234
will be actually scanned by 'ref' until we execute the
5236
(C2) max #key parts in 'range' access == K == max_key_part (this
5237
is apparently a necessary requirement)
5239
We also have a property that "range optimizer produces equal or
5240
tighter set of scan intervals than ref(const) optimizer". Each
5241
of the intervals in (**) are "tightest possible" intervals when
5242
one limits itself to using keyparts 1..K (which we do in #2).
5243
From here it follows that range access used either one, or
5244
both of the (I1) and (I2) intervals:
5246
(t.keypart1=c1 AND ... AND t.keypartK=eK) (I1)
5247
(same-as-above but with one cond replaced
5248
with "t.keypart_i IS NULL") (I2)
5250
The remaining part is to exclude the situation where range
5251
optimizer used one interval while we're considering
5252
ref-or-null and looking for estimate for two intervals. This
5253
is done by last limitation:
5255
(C3) "range optimizer used (have ref_or_null?2:1) intervals"
5257
if (table->quick_keys.is_set(key) && !found_ref && //(C1)
5258
table->quick_key_parts[key] == max_key_part && //(C2)
5259
table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5261
tmp= records= (double) table->quick_rows[key];
5265
/* Check if we have statistic about the distribution */
5266
if ((records= keyinfo->rec_per_key[max_key_part-1]))
5269
Fix for the case where the index statistics is too
5271
(1) We're considering ref(const) and there is quick select
5273
(2) and that quick select uses more keyparts (i.e. it will
5274
scan equal/smaller interval then this ref(const))
5275
(3) and E(#rows) for quick select is higher then our
5278
We'll use E(#rows) from quick select.
5280
Q: Why do we choose to use 'ref'? Won't quick select be
5281
cheaper in some cases ?
5282
TODO: figure this out and adjust the plan choice if needed.
5284
if (!found_ref && table->quick_keys.is_set(key) && // (1)
5285
table->quick_key_parts[key] > max_key_part && // (2)
5286
records < (double)table->quick_rows[key]) // (3)
5287
records= (double)table->quick_rows[key];
5294
Assume that the first key part matches 1% of the file
5295
and that the whole key matches 10 (duplicates) or 1
5297
Assume also that more key matches proportionally more
5299
This gives the formula:
5300
records = (x * (b-a) + a*c-b)/(c-1)
5302
b = records matched by whole key
5303
a = records matched by first key part (1% of all records?)
5304
c = number of key parts in key
5305
x = used key parts (1 <= x <= c)
5308
if (!(rec_per_key=(double)
5309
keyinfo->rec_per_key[keyinfo->key_parts-1]))
5310
rec_per_key=(double) s->records/rec+1;
5314
else if (rec_per_key/(double) s->records >= 0.01)
5318
double a=s->records*0.01;
5319
if (keyinfo->key_parts > 1)
5320
tmp= (max_key_part * (rec_per_key - a) +
5321
a*keyinfo->key_parts - rec_per_key)/
5322
(keyinfo->key_parts-1);
5325
set_if_bigger(tmp,1.0);
5327
records = (ulong) tmp;
5330
if (ref_or_null_part)
5332
/* We need to do two key searches to find key */
5338
ReuseRangeEstimateForRef-4: We get here if we could not reuse
5339
E(#rows) from range optimizer. Make another try:
5341
If range optimizer produced E(#rows) for a prefix of the ref
5342
access we're considering, and that E(#rows) is lower then our
5343
current estimate, make the adjustment.
5345
The decision whether we can re-use the estimate from the range
5346
optimizer is the same as in ReuseRangeEstimateForRef-3,
5347
applied to first table->quick_key_parts[key] key parts.
5349
if (table->quick_keys.is_set(key) &&
5350
table->quick_key_parts[key] <= max_key_part &&
5351
const_part & (1 << table->quick_key_parts[key]) &&
5352
table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5354
records > (double) table->quick_rows[key])
5356
tmp= records= (double) table->quick_rows[key];
5360
/* Limit the number of matched rows */
5361
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5362
if (table->covering_keys.is_set(key))
5364
/* we can use only index tree */
5365
tmp= record_count * table->file->index_only_read_time(key, tmp);
5368
tmp= record_count * min(tmp,s->worst_seeks);
5371
tmp= best_time; // Do nothing
5374
if (sj_inside_out_scan && !start_key)
5382
if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5384
best_time= tmp + records/(double) TIME_FOR_COMPARE;
5386
best_records= records;
5387
best_key= start_key;
5388
best_max_key_part= max_key_part;
5389
best_ref_depends_map= found_ref;
5390
best_is_sj_inside_out= sj_inside_out_scan;
5393
records= best_records;
5397
Don't test table scan if it can't be better.
5398
Prefer key lookup if we would use the same key for scanning.
5400
Don't do a table scan on InnoDB tables, if we can read the used
5401
parts of the row from any of the used index.
5402
This is because table scans uses index and we would not win
5403
anything by using a table scan.
5405
A word for word translation of the below if-statement in sergefp's
5406
understanding: we check if we should use table scan if:
5407
(1) The found 'ref' access produces more records than a table scan
5408
(or index scan, or quick select), or 'ref' is more expensive than
5410
(2) This doesn't hold: the best way to perform table scan is to to perform
5411
'range' access using index IDX, and the best way to perform 'ref'
5412
access is to use the same index IDX, with the same or more key parts.
5413
(note: it is not clear how this rule is/should be extended to
5414
index_merge quick selects)
5415
(3) See above note about InnoDB.
5416
(4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5417
path, but there is no quick select)
5418
If the condition in the above brackets holds, then the only possible
5419
"table scan" access method is ALL/index (there is no quick select).
5420
Since we have a 'ref' access path, and FORCE INDEX instructs us to
5421
choose it over ALL/index, there is no need to consider a full table
5424
if ((records >= s->found_records || best > s->read_time) && // (1)
5425
!(s->quick && best_key && s->quick->index == best_key->key && // (2)
5426
best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5427
!((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3)
5428
! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5429
!(s->table->force_index && best_key && !s->quick)) // (4)
5430
{ // Check full join
5431
ha_rows rnd_records= s->found_records;
5433
If there is a filtering condition on the table (i.e. ref analyzer found
5434
at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5435
preceding this table in the join order we're now considering), then
5436
assume that 25% of the rows will be filtered out by this condition.
5438
This heuristic is supposed to force tables used in exprZ to be before
5439
this table in join order.
5441
if (found_constraint)
5442
rnd_records-= rnd_records/4;
5445
If applicable, get a more accurate estimate. Don't use the two
5448
if (s->table->quick_condition_rows != s->found_records)
5449
rnd_records= s->table->quick_condition_rows;
5452
Range optimizer never proposes a RANGE if it isn't better
5453
than FULL: so if RANGE is present, it's always preferred to FULL.
5454
Here we estimate its cost.
5460
- read record range through 'quick'
5461
- skip rows which does not satisfy WHERE constraints
5463
We take into account possible use of join cache for ALL/index
5464
access (see first else-branch below), but we don't take it into
5465
account here for range/index_merge access. Find out why this is so.
5468
(s->quick->read_time +
5469
(s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5473
/* Estimate cost of reading table. */
5474
tmp= s->table->file->scan_time();
5475
if (s->table->map & join->outer_join) // Can't use join cache
5478
For each record we have to:
5479
- read the whole table record
5480
- skip rows which does not satisfy join condition
5484
(s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5488
/* We read the table as many times as join buffer becomes full. */
5489
tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5491
(double) thd->variables.join_buff_size));
5493
We don't make full cartesian product between rows in the scanned
5494
table and existing records because we skip all rows from the
5495
scanned table, which does not satisfy join condition when
5496
we read the table (see flush_cached_records for details). Here we
5497
take into account cost to read and skip these records.
5499
tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5504
We estimate the cost of evaluating WHERE clause for found records
5505
as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5506
tmp give us total cost of using TABLE SCAN
5508
if (best == DBL_MAX ||
5509
(tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5510
best + record_count/(double) TIME_FOR_COMPARE*records))
5513
If the table has a range (s->quick is set) make_join_select()
5514
will ensure that this will be used
5517
records= rows2double(rnd_records);
5519
/* range/index_merge/ALL/index access method are "independent", so: */
5520
best_ref_depends_map= 0;
5521
best_is_sj_inside_out= false;
5525
/* Update the cost information for the current partial plan */
5526
join->positions[idx].records_read= records;
5527
join->positions[idx].read_time= best;
5528
join->positions[idx].key= best_key;
5529
join->positions[idx].table= s;
5530
join->positions[idx].ref_depend_map= best_ref_depends_map;
5531
join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5534
idx == join->const_tables &&
5535
s->table == join->sort_by_table &&
5536
join->unit->select_limit_cnt >= records)
5537
join->sort_by_table= (TABLE*) 1; // Must use temporary table
5544
Selects and invokes a search strategy for an optimal query plan.
5546
The function checks user-configurable parameters that control the search
5547
strategy for an optimal plan, selects the search method and then invokes
5548
it. Each specific optimization procedure stores the final optimal plan in
5549
the array 'join->best_positions', and the cost of the plan in
5552
@param join pointer to the structure providing all context info for
5554
@param join_tables set of the tables in the query
5557
'MAX_TABLES+2' denotes the old implementation of find_best before
5558
the greedy version. Will be removed when greedy_search is approved.
5567
choose_plan(JOIN *join, table_map join_tables)
5569
uint search_depth= join->thd->variables.optimizer_search_depth;
5570
uint prune_level= join->thd->variables.optimizer_prune_level;
5571
bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5573
join->cur_embedding_map= 0;
5574
reset_nj_counters(join->join_list);
5576
if (SELECT_STRAIGHT_JOIN option is set)
5577
reorder tables so dependent tables come after tables they depend
5578
on, otherwise keep tables in the order they were specified in the query
5580
Apply heuristic: pre-sort all access plans with respect to the number of
5583
my_qsort(join->best_ref + join->const_tables,
5584
join->tables - join->const_tables, sizeof(JOIN_TAB*),
5585
straight_join ? join_tab_cmp_straight : join_tab_cmp);
5586
join->cur_emb_sj_nests= 0;
5589
optimize_straight_join(join, join_tables);
5593
if (search_depth == MAX_TABLES+2)
5595
TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5596
the greedy version. Will be removed when greedy_search is approved.
5598
join->best_read= DBL_MAX;
5599
if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5604
if (search_depth == 0)
5605
/* Automatically determine a reasonable value for 'search_depth' */
5606
search_depth= determine_search_depth(join);
5607
if (greedy_search(join, join_tables, search_depth, prune_level))
5613
Store the cost of this query into a user variable
5614
Don't update last_query_cost for statements that are not "flat joins" :
5615
i.e. they have subqueries, unions or call stored procedures.
5616
TODO: calculate a correct cost for a query with subqueries and UNIONs.
5618
if (join->thd->lex->is_single_level_stmt())
5619
join->thd->status_var.last_query_cost= join->best_read;
5625
Compare two JOIN_TAB objects based on the number of accessed records.
5627
@param ptr1 pointer to first JOIN_TAB object
5628
@param ptr2 pointer to second JOIN_TAB object
795
5631
The order relation implemented by join_tab_cmp() is not transitive,
5685
Heuristic procedure to automatically guess a reasonable degree of
5686
exhaustiveness for the greedy search procedure.
5688
The procedure estimates the optimization time and selects a search depth
5689
big enough to result in a near-optimal QEP, that doesn't take too long to
5690
find. If the number of tables in the query exceeds some constant, then
5691
search_depth is set to this constant.
5693
@param join pointer to the structure providing all context info for
5697
This is an extremely simplistic implementation that serves as a stub for a
5698
more advanced analysis of the join. Ideally the search depth should be
5699
determined by learning from previous query optimizations, because it will
5700
depend on the CPU power (and other factors).
5703
this value should be determined dynamically, based on statistics:
5704
uint max_tables_for_exhaustive_opt= 7;
5707
this value could be determined by some mapping of the form:
5708
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5711
A positive integer that specifies the search depth (and thus the
5712
exhaustiveness) of the depth-first search algorithm used by
5717
determine_search_depth(JOIN *join)
5719
uint table_count= join->tables - join->const_tables;
5721
/* TODO: this value should be determined dynamically, based on statistics: */
5722
uint max_tables_for_exhaustive_opt= 7;
5724
if (table_count <= max_tables_for_exhaustive_opt)
5725
search_depth= table_count+1; // use exhaustive for small number of tables
5728
TODO: this value could be determined by some mapping of the form:
5729
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5731
search_depth= max_tables_for_exhaustive_opt; // use greedy search
5733
return search_depth;
5738
Select the best ways to access the tables in a query without reordering them.
5740
Find the best access paths for each query table and compute their costs
5741
according to their order in the array 'join->best_ref' (thus without
5742
reordering the join tables). The function calls sequentially
5743
'best_access_path' for each table in the query to select the best table
5744
access method. The final optimal plan is stored in the array
5745
'join->best_positions', and the corresponding cost in 'join->best_read'.
5747
@param join pointer to the structure providing all context info for
5749
@param join_tables set of the tables in the query
5752
This function can be applied to:
5753
- queries with STRAIGHT_JOIN
5754
- internally to compute the cost of an arbitrary QEP
5756
Thus 'optimize_straight_join' can be used at any stage of the query
5757
optimization process to finalize a QEP as it is.
5761
optimize_straight_join(JOIN *join, table_map join_tables)
5764
uint idx= join->const_tables;
5765
double record_count= 1.0;
5766
double read_time= 0.0;
5768
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5770
/* Find the best access method from 's' to the current partial plan */
5771
advance_sj_state(join_tables, s);
5772
best_access_path(join, s, join->thd, join_tables, idx,
5773
record_count, read_time);
5774
/* compute the cost of the new plan extended with 's' */
5775
record_count*= join->positions[idx].records_read;
5776
read_time+= join->positions[idx].read_time;
5777
join_tables&= ~(s->table->map);
5781
read_time+= record_count / (double) TIME_FOR_COMPARE;
5782
if (join->sort_by_table &&
5783
join->sort_by_table != join->positions[join->const_tables].table->table)
5784
read_time+= record_count; // We have to make a temp table
5785
memcpy((uchar*) join->best_positions, (uchar*) join->positions,
5786
sizeof(POSITION)*idx);
5787
join->best_read= read_time;
5792
Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5794
The search procedure uses a hybrid greedy/exhaustive search with controlled
5795
exhaustiveness. The search is performed in N = card(remaining_tables)
5796
steps. Each step evaluates how promising is each of the unoptimized tables,
5797
selects the most promising table, and extends the current partial QEP with
5798
that table. Currenly the most 'promising' table is the one with least
5799
expensive extension.\
5801
There are two extreme cases:
5802
-# When (card(remaining_tables) < search_depth), the estimate finds the
5803
best complete continuation of the partial QEP. This continuation can be
5804
used directly as a result of the search.
5805
-# When (search_depth == 1) the 'best_extension_by_limited_search'
5806
consideres the extension of the current QEP with each of the remaining
5809
All other cases are in-between these two extremes. Thus the parameter
5810
'search_depth' controlls the exhaustiveness of the search. The higher the
5811
value, the longer the optimizaton time and possibly the better the
5812
resulting plan. The lower the value, the fewer alternative plans are
5813
estimated, but the more likely to get a bad QEP.
5815
All intermediate and final results of the procedure are stored in 'join':
5816
- join->positions : modified for every partial QEP that is explored
5817
- join->best_positions: modified for the current best complete QEP
5818
- join->best_read : modified for the current best complete QEP
5819
- join->best_ref : might be partially reordered
5821
The final optimal plan is stored in 'join->best_positions', and its
5822
corresponding cost in 'join->best_read'.
5825
The following pseudocode describes the algorithm of 'greedy_search':
5828
procedure greedy_search
5829
input: remaining_tables
5834
(t, a) = best_extension(pplan, remaining_tables);
5835
pplan = concat(pplan, (t, a));
5836
remaining_tables = remaining_tables - t;
5837
} while (remaining_tables != {})
5842
where 'best_extension' is a placeholder for a procedure that selects the
5843
most "promising" of all tables in 'remaining_tables'.
5844
Currently this estimate is performed by calling
5845
'best_extension_by_limited_search' to evaluate all extensions of the
5846
current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5847
mainly depends on that of 'best_extension_by_limited_search'.
5850
If 'best_extension()' == 'best_extension_by_limited_search()', then the
5851
worst-case complexity of this algorithm is <=
5852
O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5853
complexity of greedy_search is O(N!).
5856
In the future, 'greedy_search' might be extended to support other
5857
implementations of 'best_extension', e.g. some simpler quadratic procedure.
5859
@param join pointer to the structure providing all context info
5861
@param remaining_tables set of tables not included into the partial plan yet
5862
@param search_depth controlls the exhaustiveness of the search
5863
@param prune_level the pruning heuristics that should be applied during
5873
greedy_search(JOIN *join,
5874
table_map remaining_tables,
5878
double record_count= 1.0;
5879
double read_time= 0.0;
5880
uint idx= join->const_tables; // index into 'join->best_ref'
5882
uint size_remain; // cardinality of remaining_tables
5884
JOIN_TAB *best_table; // the next plan node to be added to the curr QEP
5886
/* number of tables that remain to be optimized */
5887
size_remain= my_count_bits(remaining_tables);
5890
/* Find the extension of the current QEP with the lowest cost */
5891
join->best_read= DBL_MAX;
5892
if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5893
read_time, search_depth, prune_level))
5896
if (size_remain <= search_depth)
5899
'join->best_positions' contains a complete optimal extension of the
5900
current partial QEP.
5905
/* select the first table in the optimal extension as most promising */
5906
best_pos= join->best_positions[idx];
5907
best_table= best_pos.table;
5909
Each subsequent loop of 'best_extension_by_limited_search' uses
5910
'join->positions' for cost estimates, therefore we have to update its
5913
join->positions[idx]= best_pos;
5915
/* find the position of 'best_table' in 'join->best_ref' */
5917
JOIN_TAB *pos= join->best_ref[best_idx];
5918
while (pos && best_table != pos)
5919
pos= join->best_ref[++best_idx];
5920
assert((pos != NULL)); // should always find 'best_table'
5921
/* move 'best_table' at the first free position in the array of joins */
5922
swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5924
/* compute the cost of the new plan extended with 'best_table' */
5925
record_count*= join->positions[idx].records_read;
5926
read_time+= join->positions[idx].read_time;
5928
remaining_tables&= ~(best_table->table->map);
5936
Find a good, possibly optimal, query execution plan (QEP) by a possibly
5939
The procedure searches for the optimal ordering of the query tables in set
5940
'remaining_tables' of size N, and the corresponding optimal access paths to
5941
each table. The choice of a table order and an access path for each table
5942
constitutes a query execution plan (QEP) that fully specifies how to
5945
The maximal size of the found plan is controlled by the parameter
5946
'search_depth'. When search_depth == N, the resulting plan is complete and
5947
can be used directly as a QEP. If search_depth < N, the found plan consists
5948
of only some of the query tables. Such "partial" optimal plans are useful
5949
only as input to query optimization procedures, and cannot be used directly
5952
The algorithm begins with an empty partial plan stored in 'join->positions'
5953
and a set of N tables - 'remaining_tables'. Each step of the algorithm
5954
evaluates the cost of the partial plan extended by all access plans for
5955
each of the relations in 'remaining_tables', expands the current partial
5956
plan with the access plan that results in lowest cost of the expanded
5957
partial plan, and removes the corresponding relation from
5958
'remaining_tables'. The algorithm continues until it either constructs a
5959
complete optimal plan, or constructs an optimal plartial plan with size =
5962
The final optimal plan is stored in 'join->best_positions'. The
5963
corresponding cost of the optimal plan is in 'join->best_read'.
5966
The procedure uses a recursive depth-first search where the depth of the
5967
recursion (and thus the exhaustiveness of the search) is controlled by the
5968
parameter 'search_depth'.
5971
The pseudocode below describes the algorithm of
5972
'best_extension_by_limited_search'. The worst-case complexity of this
5973
algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
5974
the complexity of greedy_search is O(N!).
5977
procedure best_extension_by_limited_search(
5978
pplan in, // in, partial plan of tables-joined-so-far
5979
pplan_cost, // in, cost of pplan
5980
remaining_tables, // in, set of tables not referenced in pplan
5981
best_plan_so_far, // in/out, best plan found so far
5982
best_plan_so_far_cost,// in/out, cost of best_plan_so_far
5983
search_depth) // in, maximum size of the plans being considered
5985
for each table T from remaining_tables
5987
// Calculate the cost of using table T as above
5988
cost = complex-series-of-calculations;
5990
// Add the cost to the cost so far.
5993
if (pplan_cost >= best_plan_so_far_cost)
5994
// pplan_cost already too great, stop search
5997
pplan= expand pplan by best_access_method;
5998
remaining_tables= remaining_tables - table T;
5999
if (remaining_tables is not an empty set
6003
best_extension_by_limited_search(pplan, pplan_cost,
6006
best_plan_so_far_cost,
6011
best_plan_so_far_cost= pplan_cost;
6012
best_plan_so_far= pplan;
6019
When 'best_extension_by_limited_search' is called for the first time,
6020
'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6021
The actual implementation provides a way to optionally use pruning
6022
heuristic (controlled by the parameter 'prune_level') to reduce the search
6023
space by skipping some partial plans.
6026
The parameter 'search_depth' provides control over the recursion
6027
depth, and thus the size of the resulting optimal plan.
6029
@param join pointer to the structure providing all context info
6031
@param remaining_tables set of tables not included into the partial plan yet
6032
@param idx length of the partial QEP in 'join->positions';
6033
since a depth-first search is used, also corresponds
6034
to the current depth of the search tree;
6035
also an index in the array 'join->best_ref';
6036
@param record_count estimate for the number of records returned by the
6038
@param read_time the cost of the best partial plan
6039
@param search_depth maximum depth of the recursion and thus size of the
6041
(0 < search_depth <= join->tables+1).
6042
@param prune_level pruning heuristics that should be applied during
6044
(values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6053
best_extension_by_limited_search(JOIN *join,
6054
table_map remaining_tables,
6056
double record_count,
6061
THD *thd= join->thd;
6062
if (thd->killed) // Abort
6066
'join' is a partial plan with lower cost than the best plan so far,
6067
so continue expanding it further with the tables in 'remaining_tables'.
6070
double best_record_count= DBL_MAX;
6071
double best_read_time= DBL_MAX;
6073
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6075
table_map real_table_bit= s->table->map;
6076
if ((remaining_tables & real_table_bit) &&
6077
!(remaining_tables & s->dependent) &&
6078
(!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6080
double current_record_count, current_read_time;
6081
advance_sj_state(remaining_tables, s);
6084
psergey-insideout-todo:
6085
when best_access_path() detects it could do an InsideOut scan or
6086
some other scan, have it return an insideout scan and a flag that
6087
requests to "fork" this loop iteration. (Q: how does that behave
6088
when the depth is insufficient??)
6090
/* Find the best access method from 's' to the current partial plan */
6091
best_access_path(join, s, thd, remaining_tables, idx,
6092
record_count, read_time);
6093
/* Compute the cost of extending the plan with 's' */
6094
current_record_count= record_count * join->positions[idx].records_read;
6095
current_read_time= read_time + join->positions[idx].read_time;
6097
/* Expand only partial plans with lower cost than the best QEP so far */
6098
if ((current_read_time +
6099
current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6101
restore_prev_nj_state(s);
6102
restore_prev_sj_state(remaining_tables, s);
6107
Prune some less promising partial plans. This heuristic may miss
6108
the optimal QEPs, thus it results in a non-exhaustive search.
6110
if (prune_level == 1)
6112
if (best_record_count > current_record_count ||
6113
best_read_time > current_read_time ||
6114
(idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6116
if (best_record_count >= current_record_count &&
6117
best_read_time >= current_read_time &&
6118
/* TODO: What is the reasoning behind this condition? */
6119
(!(s->key_dependent & remaining_tables) ||
6120
join->positions[idx].records_read < 2.0))
6122
best_record_count= current_record_count;
6123
best_read_time= current_read_time;
6128
restore_prev_nj_state(s);
6129
restore_prev_sj_state(remaining_tables, s);
6134
if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6135
{ /* Recursively expand the current partial plan */
6136
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6137
if (best_extension_by_limited_search(join,
6138
remaining_tables & ~real_table_bit,
6140
current_record_count,
6145
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6149
'join' is either the best partial QEP with 'search_depth' relations,
6150
or the best complete QEP so far, whichever is smaller.
6152
current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6153
if (join->sort_by_table &&
6154
join->sort_by_table !=
6155
join->positions[join->const_tables].table->table)
6156
/* We have to make a temp table */
6157
current_read_time+= current_record_count;
6158
if ((search_depth == 1) || (current_read_time < join->best_read))
6160
memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6161
sizeof(POSITION) * (idx + 1));
6162
join->best_read= current_read_time - 0.001;
6165
restore_prev_nj_state(s);
6166
restore_prev_sj_state(remaining_tables, s);
6175
- TODO: this function is here only temporarily until 'greedy_search' is
6176
tested and accepted.
6183
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6186
THD *thd= join->thd;
6191
read_time+=record_count/(double) TIME_FOR_COMPARE;
6192
if (join->sort_by_table &&
6193
join->sort_by_table !=
6194
join->positions[join->const_tables].table->table)
6195
read_time+=record_count; // We have to make a temp table
6196
if (read_time < join->best_read)
6198
memcpy((uchar*) join->best_positions,(uchar*) join->positions,
6199
sizeof(POSITION)*idx);
6200
join->best_read= read_time - 0.001;
6204
if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6205
return(false); /* Found better before */
6208
double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6209
for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6211
table_map real_table_bit=s->table->map;
6212
if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6213
(!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6215
double records, best;
6216
advance_sj_state(rest_tables, s);
6217
best_access_path(join, s, thd, rest_tables, idx, record_count,
6219
records= join->positions[idx].records_read;
6220
best= join->positions[idx].read_time;
6222
Go to the next level only if there hasn't been a better key on
6223
this level! This will cut down the search for a lot simple cases!
6225
double current_record_count=record_count*records;
6226
double current_read_time=read_time+best;
6227
if (best_record_count > current_record_count ||
6228
best_read_time > current_read_time ||
6229
(idx == join->const_tables && s->table == join->sort_by_table))
6231
if (best_record_count >= current_record_count &&
6232
best_read_time >= current_read_time &&
6233
(!(s->key_dependent & rest_tables) || records < 2.0))
6235
best_record_count=current_record_count;
6236
best_read_time=current_read_time;
6238
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6239
if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6240
current_record_count,current_read_time))
6242
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6244
restore_prev_nj_state(s);
6245
restore_prev_sj_state(rest_tables, s);
6246
if (join->select_options & SELECT_STRAIGHT_JOIN)
6247
break; // Don't test all combinations
844
6255
Find how much space the prevous read not const tables takes in cache.
846
void calc_used_field_length(Session *, JoinTable *join_tab)
6258
static void calc_used_field_length(THD *thd __attribute__((__unused__)),
848
uint32_t null_fields,blobs,fields,rec_length;
6261
uint null_fields,blobs,fields,rec_length;
849
6262
Field **f_ptr,*field;
6263
MY_BITMAP *read_set= join_tab->table->read_set;;
851
6265
null_fields= blobs= fields= rec_length=0;
852
for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
6266
for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
854
if (field->isReadSet())
6268
if (bitmap_is_set(read_set, field->field_index))
856
uint32_t flags=field->flags;
6270
uint flags=field->flags;
858
6272
rec_length+=field->pack_length();
859
6273
if (flags & BLOB_FLAG)
861
6275
if (!(flags & NOT_NULL_FLAG))
865
6279
if (null_fields)
866
rec_length+=(join_tab->table->getNullFields() + 7)/8;
6280
rec_length+=(join_tab->table->s->null_fields+7)/8;
867
6281
if (join_tab->table->maybe_null)
868
rec_length+=sizeof(bool);
6282
rec_length+=sizeof(my_bool);
871
uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
872
(join_tab->table->getRecordLength()- rec_length));
873
rec_length+= max((uint32_t)4,blob_length);
875
join_tab->used_fields= fields;
876
join_tab->used_fieldlength= rec_length;
877
join_tab->used_blobs= blobs;
880
StoredKey *get_store_key(Session *session,
881
optimizer::KeyUse *keyuse,
882
table_map used_tables,
883
KeyPartInfo *key_part,
884
unsigned char *key_buff,
887
Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
888
if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
890
return new store_key_const_item(session,
892
key_buff + maybe_null,
893
maybe_null ? key_buff : 0,
897
else if (key_use_val->type() == Item::FIELD_ITEM ||
898
(key_use_val->type() == Item::REF_ITEM &&
899
key_use_val->ref_type() == Item_ref::OUTER_REF &&
900
(*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
901
key_use_val->real_item()->type() == Item::FIELD_ITEM))
903
return new store_key_field(session,
905
key_buff + maybe_null,
906
maybe_null ? key_buff : 0,
908
((Item_field*) key_use_val->real_item())->field,
909
key_use_val->full_name());
911
return new store_key_item(session,
913
key_buff + maybe_null,
914
maybe_null ? key_buff : 0,
6285
uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6286
(join_tab->table->s->reclength- rec_length));
6287
rec_length+=(uint) max(4,blob_length);
6289
join_tab->used_fields=fields;
6290
join_tab->used_fieldlength=rec_length;
6291
join_tab->used_blobs=blobs;
6296
cache_record_length(JOIN *join,uint idx)
6299
JOIN_TAB **pos,**end;
6302
for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6306
JOIN_TAB *join_tab= *pos;
6307
if (!join_tab->used_fieldlength) /* Not calced yet */
6308
calc_used_field_length(thd, join_tab);
6309
length+=join_tab->used_fieldlength;
6316
Get the number of different row combinations for subset of partial join
6320
join The join structure
6321
idx Number of tables in the partial join order (i.e. the
6322
partial join order is in join->positions[0..idx-1])
6323
found_ref Bitmap of tables for which we need to find # of distinct
6327
Given a partial join order (in join->positions[0..idx-1]) and a subset of
6328
tables within that join order (specified in found_ref), find out how many
6329
distinct row combinations of subset tables will be in the result of the
6332
This is used as follows: Suppose we have a table accessed with a ref-based
6333
method. The ref access depends on current rows of tables in found_ref.
6334
We want to count # of different ref accesses. We assume two ref accesses
6335
will be different if at least one of access parameters is different.
6336
Example: consider a query
6338
SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6341
t1, ref access on t1.key=c1
6342
t2, ref access on t2.key=c2
6343
t3, ref access on t3.key=t1.field
6345
For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6346
For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6347
For t3: n_ref_scans = records_read(t1)*records_read(t2)
6348
n_distinct_ref_scans = #records_read(t1)
6350
The reason for having this function (at least the latest version of it)
6351
is that we need to account for buffering in join execution.
6353
An edge-case example: if we have a non-first table in join accessed via
6354
ref(const) or ref(param) where there is a small number of different
6355
values of param, then the access will likely hit the disk cache and will
6356
not require any disk seeks.
6358
The proper solution would be to assume an LRU disk cache of some size,
6359
calculate probability of cache hits, etc. For now we just count
6360
identical ref accesses as one.
6363
Expected number of row combinations
6367
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6370
POSITION *pos_end= join->positions - 1;
6371
for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6373
if (pos->table->table->map & found_ref)
6375
found_ref|= pos->ref_depend_map;
6377
For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
6378
with no matching row we will get position[t2].records_read==0.
6379
Actually the size of output is one null-complemented row, therefore
6380
we will use value of 1 whenever we get records_read==0.
6383
- the above case can't occur if inner part of outer join has more
6384
than one table: table with no matches will not be marked as const.
6386
- Ideally we should add 1 to records_read for every possible null-
6387
complemented row. We're not doing it because: 1. it will require
6388
non-trivial code and add overhead. 2. The value of records_read
6389
is an inprecise estimate and adding 1 (or, in the worst case,
6390
#max_nested_outer_joins=64-1) will not make it any more precise.
6392
if (pos->records_read > DBL_EPSILON)
6393
found*= pos->records_read;
920
This function is only called for const items on fields which are keys.
923
returns 1 if there was some conversion made when the field was stored.
6401
Set up join struct according to best position.
925
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
928
Table *table= field->getTable();
929
Session *session= table->in_use;
930
ha_rows cuted_fields=session->cuted_fields;
933
we should restore old value of count_cuted_fields because
934
store_val_in_field can be called from mysql_insert
935
with select_insert, which make count_cuted_fields= 1
937
enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
938
session->count_cuted_fields= check_flag;
939
error= item->save_in_field(field, 1);
940
session->count_cuted_fields= old_count_cuted_fields;
941
return error || cuted_fields != session->cuted_fields;
944
inline void add_cond_and_fix(Item **e1, Item *e2)
6405
get_best_combination(JOIN *join)
6408
table_map used_tables;
6409
JOIN_TAB *join_tab,*j;
6414
table_count=join->tables;
6415
if (!(join->join_tab=join_tab=
6416
(JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6421
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read
6422
for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
949
if ((res= new Item_cond_and(*e1, e2)))
6425
*j= *join->best_positions[tablenr].table;
6426
form=join->table[tablenr]=j->table;
6427
used_tables|= form->map;
6428
form->reginfo.join_tab=j;
6429
if (!*j->on_expr_ref)
6430
form->reginfo.not_exists_optimize=0; // Only with LEFT JOIN
6431
if (j->type == JT_CONST)
6432
continue; // Handled in make_join_stat..
6437
if (j->type == JT_SYSTEM)
6439
if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
952
res->quick_fix_field();
6442
if (tablenr != join->const_tables)
6445
else if (create_ref_for_key(join, j, keyuse, used_tables))
6446
return(true); // Something went wrong
6449
for (i=0 ; i < table_count ; i++)
6450
join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6451
update_depend_map(join);
959
bool create_ref_for_key(Join *join,
961
optimizer::KeyUse *org_keyuse,
962
table_map used_tables)
6456
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6457
table_map used_tables)
964
optimizer::KeyUse *keyuse= org_keyuse;
965
Session *session= join->session;
970
KeyInfo *keyinfo= NULL;
6459
KEYUSE *keyuse=org_keyuse;
6460
THD *thd= join->thd;
6461
uint keyparts,length,key;
972
6465
/* Use best key from find_best */
974
key= keyuse->getKey();
975
keyinfo= table->key_info + key;
6468
keyinfo=table->key_info+key;
979
uint32_t found_part_ref_or_null= 0;
6472
uint found_part_ref_or_null= 0;
981
6474
Calculate length for the used key
982
6475
Stop if there is a missing key part or when we find second key_part
6862
Fill in outer join related info for the execution plan structure.
6864
For each outer join operation left after simplification of the
6865
original query the function set up the following pointers in the linear
6866
structure join->join_tab representing the selected execution plan.
6867
The first inner table t0 for the operation is set to refer to the last
6868
inner table tk through the field t0->last_inner.
6869
Any inner table ti for the operation are set to refer to the first
6870
inner table ti->first_inner.
6871
The first inner table t0 for the operation is set to refer to the
6872
first inner table of the embedding outer join operation, if there is any,
6873
through the field t0->first_upper.
6874
The on expression for the outer join operation is attached to the
6875
corresponding first inner table through the field t0->on_expr_ref.
6876
Here ti are structures of the JOIN_TAB type.
6878
EXAMPLE. For the query:
6882
(t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6883
ON (t1.a=t2.a AND t1.b=t3.b)
6887
given the execution plan with the table order t1,t2,t3,t4
6888
is selected, the following references will be set;
6889
t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6890
t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6891
on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
6892
*t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6894
@param join reference to the info fully describing the query
6897
The function assumes that the simplification procedure has been
6898
already applied to the join query (see simplify_joins).
6899
This function can be called only after the execution plan
6904
make_outerjoin_info(JOIN *join)
6906
for (uint i=join->const_tables ; i < join->tables ; i++)
6908
JOIN_TAB *tab=join->join_tab+i;
6909
TABLE *table=tab->table;
6910
TABLE_LIST *tbl= table->pos_in_table_list;
6911
TABLE_LIST *embedding= tbl->embedding;
6913
if (tbl->outer_join)
6916
Table tab is the only one inner table for outer join.
6917
(Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6918
is in the query above.)
6920
tab->last_inner= tab->first_inner= tab;
6921
tab->on_expr_ref= &tbl->on_expr;
6922
tab->cond_equal= tbl->cond_equal;
6924
tab->first_upper= embedding->nested_join->first_nested;
6926
for ( ; embedding ; embedding= embedding->embedding)
6928
/* Ignore sj-nests: */
6929
if (!embedding->on_expr)
6931
NESTED_JOIN *nested_join= embedding->nested_join;
6932
if (!nested_join->counter_)
6935
Table tab is the first inner table for nested_join.
6936
Save reference to it in the nested join structure.
6938
nested_join->first_nested= tab;
6939
tab->on_expr_ref= &embedding->on_expr;
6940
tab->cond_equal= tbl->cond_equal;
6941
if (embedding->embedding)
6942
tab->first_upper= embedding->embedding->nested_join->first_nested;
6944
if (!tab->first_inner)
6945
tab->first_inner= nested_join->first_nested;
6946
if (++nested_join->counter_ < nested_join->join_list.elements)
6948
/* Table tab is the last inner table for nested join. */
6949
nested_join->first_nested->last_inner= tab;
6957
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6959
THD *thd= join->thd;
6962
add_not_null_conds(join);
6963
table_map used_tables;
6964
if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6965
{ /* there may be a select without a cond. */
6966
if (join->tables > 1)
6967
cond->update_used_tables(); // Tablenr may have changed
6968
if (join->const_tables == join->tables &&
6969
thd->lex->current_select->master_unit() ==
6970
&thd->lex->unit) // not upper level SELECT
6971
join->const_table_map|=RAND_TABLE_BIT;
6972
{ // Check const tables
6974
make_cond_for_table(cond,
6975
join->const_table_map,
6977
for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6978
tab < join->join_tab+join->tables ; tab++)
6980
if (*tab->on_expr_ref)
6982
JOIN_TAB *cond_tab= tab->first_inner;
6983
COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6984
join->const_table_map,
6988
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6991
tmp->quick_fix_field();
6992
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6993
new Item_cond_and(cond_tab->select_cond,
6995
if (!cond_tab->select_cond)
6997
cond_tab->select_cond->quick_fix_field();
7000
if (const_cond && !const_cond->val_int())
7002
return(1); // Impossible const condition
7006
used_tables=((select->const_tables=join->const_table_map) |
7007
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7008
for (uint i=join->const_tables ; i < join->tables ; i++)
7010
JOIN_TAB *tab=join->join_tab+i;
7012
first_inner is the X in queries like:
7013
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7015
JOIN_TAB *first_inner_tab= tab->first_inner;
7016
table_map current_map= tab->table->map;
7017
bool use_quick_range=0;
7021
Following force including random expression in last table condition.
7022
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7024
if (i == join->tables-1)
7025
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7026
used_tables|=current_map;
7028
if (tab->type == JT_REF && tab->quick &&
7029
(uint) tab->ref.key == tab->quick->index &&
7030
tab->ref.key_length < tab->quick->max_used_key_length)
7032
/* Range uses longer key; Use this instead of ref on key */
7037
tab->ref.key_parts=0; // Don't use ref key.
7038
join->best_positions[i].records_read= rows2double(tab->quick->records);
7040
We will use join cache here : prevent sorting of the first
7041
table only and sort at the end.
7043
if (i != join->const_tables && join->tables > join->const_tables + 1)
7049
tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7050
if (cond && !tmp && tab->quick)
7052
if (tab->type != JT_ALL)
7055
Don't use the quick method
7056
We come here in the case where we have 'key=constant' and
7057
the test is removed by make_cond_for_table()
7065
Hack to handle the case where we only refer to a table
7066
in the ON part of an OUTER JOIN. In this case we want the code
7067
below to check if we should use 'quick' instead.
7069
tmp= new Item_int((longlong) 1,1); // Always true
7073
if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7074
tab->type == JT_EQ_REF)
7076
SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7077
thd->memdup((uchar*) select,
7080
return(1); // End of memory
7082
If tab is an inner table of an outer join operation,
7083
add a match guard to the pushed down predicate.
7084
The guard will turn the predicate on only after
7085
the first match for outer tables is encountered.
7090
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7091
a cond, so neutralize the hack above.
7093
if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7095
tab->select_cond=sel->cond=tmp;
7096
/* Push condition to storage engine if this is enabled
7097
and the condition is not guarded */
7098
tab->table->file->pushed_cond= NULL;
7099
if (thd->variables.engine_condition_pushdown)
7102
make_cond_for_table(tmp, current_map, current_map, 0);
7105
/* Push condition to handler */
7106
if (!tab->table->file->cond_push(push_cond))
7107
tab->table->file->pushed_cond= push_cond;
7112
tab->select_cond= sel->cond= NULL;
7114
sel->head=tab->table;
7117
/* Use quick key read if it's a constant and it's not used
7119
if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7120
&& (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7122
sel->quick=tab->quick; // Use value from get_quick_...
7123
sel->quick_keys.clear_all();
7124
sel->needed_reg.clear_all();
7132
uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7133
if (i == join->const_tables && ref_key)
7135
if (!tab->const_keys.is_clear_all() &&
7136
tab->table->reginfo.impossible_range)
7139
else if (tab->type == JT_ALL && ! use_quick_range)
7141
if (!tab->const_keys.is_clear_all() &&
7142
tab->table->reginfo.impossible_range)
7143
return(1); // Impossible range
7145
We plan to scan all rows.
7146
Check again if we should use an index.
7147
We could have used an column from a previous table in
7148
the index if we are using limit and this is the first table
7151
if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7152
(!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)))
7154
/* Join with outer join condition */
7155
COND *orig_cond=sel->cond;
7156
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7159
We can't call sel->cond->fix_fields,
7160
as it will break tab->on_expr if it's AND condition
7161
(fix_fields currently removes extra AND/OR levels).
7162
Yet attributes of the just built condition are not needed.
7163
Thus we call sel->cond->quick_fix_field for safety.
7165
if (sel->cond && !sel->cond->fixed)
7166
sel->cond->quick_fix_field();
7168
if (sel->test_quick_select(thd, tab->keys,
7169
used_tables & ~ current_map,
7170
(join->select_options &
7173
join->unit->select_limit_cnt), 0,
7177
Before reporting "Impossible WHERE" for the whole query
7178
we have to check isn't it only "impossible ON" instead
7180
sel->cond=orig_cond;
7181
if (!*tab->on_expr_ref ||
7182
sel->test_quick_select(thd, tab->keys,
7183
used_tables & ~ current_map,
7184
(join->select_options &
7187
join->unit->select_limit_cnt),0,
7189
return(1); // Impossible WHERE
7192
sel->cond=orig_cond;
7194
/* Fix for EXPLAIN */
7196
join->best_positions[i].records_read= (double)sel->quick->records;
7200
sel->needed_reg=tab->needed_reg;
7201
sel->quick_keys.clear_all();
7203
if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7204
!sel->needed_reg.is_subset(tab->checked_keys))
7206
tab->keys=sel->quick_keys;
7207
tab->keys.merge(sel->needed_reg);
7208
tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7209
(select->quick_keys.is_clear_all() ||
7211
(select->quick->records >= 100L)))) ?
7213
sel->read_tables= used_tables & ~current_map;
7215
if (i != join->const_tables && tab->use_quick != 2)
7216
{ /* Read with cache */
7218
(tmp=make_cond_for_table(cond,
7219
join->const_table_map |
7223
tab->cache.select=(SQL_SELECT*)
7224
thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7225
tab->cache.select->cond=tmp;
7226
tab->cache.select->read_tables=join->const_table_map;
7233
Push down conditions from all on expressions.
7234
Each of these conditions are guarded by a variable
7235
that turns if off just before null complemented row for
7236
outer joins is formed. Thus, the condition from an
7237
'on expression' are guaranteed not to be checked for
7238
the null complemented row.
7241
/* First push down constant conditions from on expressions */
7242
for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7243
join_tab < join->join_tab+join->tables ; join_tab++)
7245
if (*join_tab->on_expr_ref)
7247
JOIN_TAB *cond_tab= join_tab->first_inner;
7248
COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7249
join->const_table_map,
7253
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7256
tmp->quick_fix_field();
7257
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7258
new Item_cond_and(cond_tab->select_cond,tmp);
7259
if (!cond_tab->select_cond)
7261
cond_tab->select_cond->quick_fix_field();
7265
/* Push down non-constant conditions from on expressions */
7266
JOIN_TAB *last_tab= tab;
7267
while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7270
Table tab is the last inner table of an outer join.
7271
An on expression is always attached to it.
7273
COND *on_expr= *first_inner_tab->on_expr_ref;
7275
table_map used_tables2= (join->const_table_map |
7276
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7277
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7279
current_map= tab->table->map;
7280
used_tables2|= current_map;
7281
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7285
JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7287
First add the guards for match variables of
7288
all embedding outer join operations.
7290
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7295
Now add the guard turning the predicate off for
7296
the null complemented row.
7298
tmp_cond= new Item_func_trig_cond(tmp_cond,
7302
tmp_cond->quick_fix_field();
7303
/* Add the predicate to other pushed down predicates */
7304
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7305
new Item_cond_and(cond_tab->select_cond,
7307
if (!cond_tab->select_cond)
7309
cond_tab->select_cond->quick_fix_field();
7312
first_inner_tab= first_inner_tab->first_upper;
7321
Check if given expression uses only table fields covered by the given index
7324
uses_index_fields_only()
7325
item Expression to check
7326
tbl The table having the index
7327
keyno The index number
7328
other_tbls_ok true <=> Fields of other non-const tables are allowed
7331
Check if given expression only uses fields covered by index #keyno in the
7332
table tbl. The expression can use any fields in any other tables.
7334
The expression is guaranteed not to be AND or OR - those constructs are
7335
handled outside of this function.
7342
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno,
7345
if (item->const_item())
7349
Don't push down the triggered conditions. Nested outer joins execution
7350
code may need to evaluate a condition several times (both triggered and
7351
untriggered), and there is no way to put thi
7352
TODO: Consider cloning the triggered condition and using the copies for:
7353
1. push the first copy down, to have most restrictive index condition
7355
2. Put the second copy into tab->select_cond.
7357
if (item->type() == Item::FUNC_ITEM &&
7358
((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7361
if (!(item->used_tables() & tbl->map))
7362
return other_tbls_ok;
7364
Item::Type item_type= item->type();
7365
switch (item_type) {
7366
case Item::FUNC_ITEM:
7368
/* This is a function, apply condition recursively to arguments */
7369
Item_func *item_func= (Item_func*)item;
7371
Item **item_end= (item_func->arguments()) + item_func->argument_count();
7372
for (child= item_func->arguments(); child != item_end; child++)
7374
if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
7379
case Item::COND_ITEM:
7381
/* This is a function, apply condition recursively to arguments */
7382
List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7386
if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7391
case Item::FIELD_ITEM:
7393
Item_field *item_field= (Item_field*)item;
7394
if (item_field->field->table != tbl)
7396
return item_field->field->part_of_key.is_set(keyno);
7398
case Item::REF_ITEM:
7399
return uses_index_fields_only(item->real_item(), tbl, keyno,
7402
return false; /* Play it safe, don't push unknown non-const items */
1211
7407
#define ICP_COND_USES_INDEX_ONLY 10
1217
void JoinTable::cleanup()
7410
Get a part of the condition that can be checked using only index fields
7413
make_cond_for_index()
7414
cond The source condition
7415
table The table that is partially available
7416
keyno The index in the above table. Only fields covered by the index
7418
other_tbls_ok true <=> Fields of other non-const tables are allowed
7421
Get a part of the condition that can be checked when for the given table
7422
we have values only of fields covered by some index. The condition may
7423
refer to other tables, it is assumed that we have values of all of their
7427
make_cond_for_index(
7428
"cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7431
"cond(t1.field) AND cond(t2.key2)"
7434
Index condition, or NULL if no condition could be inferred.
7437
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7442
if (cond->type() == Item::COND_ITEM)
7445
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7447
Item_cond_and *new_cond=new Item_cond_and;
7450
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7454
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7456
new_cond->argument_list()->push_back(fix);
7457
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7459
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7460
cond->marker= ICP_COND_USES_INDEX_ONLY;
7461
switch (new_cond->argument_list()->elements) {
7465
return new_cond->argument_list()->head();
7467
new_cond->quick_fix_field();
7473
Item_cond_or *new_cond=new Item_cond_or;
7476
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7480
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7483
new_cond->argument_list()->push_back(fix);
7484
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7486
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7487
cond->marker= ICP_COND_USES_INDEX_ONLY;
7488
new_cond->quick_fix_field();
7489
new_cond->top_level_item();
7494
if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
7496
cond->marker= ICP_COND_USES_INDEX_ONLY;
7501
Item *make_cond_remainder(Item *cond, bool exclude_index)
7503
if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7504
return 0; /* Already checked */
7506
if (cond->type() == Item::COND_ITEM)
7508
table_map tbl_map= 0;
7509
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7511
/* Create new top level AND item */
7512
Item_cond_and *new_cond=new Item_cond_and;
7515
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7519
Item *fix= make_cond_remainder(item, exclude_index);
7522
new_cond->argument_list()->push_back(fix);
7523
tbl_map |= fix->used_tables();
7526
switch (new_cond->argument_list()->elements) {
7530
return new_cond->argument_list()->head();
7532
new_cond->quick_fix_field();
7533
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7539
Item_cond_or *new_cond=new Item_cond_or;
7542
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7546
Item *fix= make_cond_remainder(item, false);
7549
new_cond->argument_list()->push_back(fix);
7550
tbl_map |= fix->used_tables();
7552
new_cond->quick_fix_field();
7553
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7554
new_cond->top_level_item();
7563
Try to extract and push the index condition
7567
tab A join tab that has tab->table->file and its condition
7569
keyno Index for which extract and push the condition
7570
other_tbls_ok true <=> Fields of other non-const tables are allowed
7573
Try to extract and push the index condition down to table handler
7576
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7579
if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7580
tab->join->thd->variables.engine_condition_pushdown)
7582
idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7587
tab->pre_idx_push_select_cond= tab->select_cond;
7588
Item *idx_remainder_cond=
7589
tab->table->file->idx_cond_push(keyno, idx_cond);
7592
Disable eq_ref's "lookup cache" if we've pushed down an index
7594
TODO: This check happens to work on current ICP implementations, but
7595
there may exist a compliant implementation that will not work
7596
correctly with it. Sort this out when we stabilize the condition
7599
if (idx_remainder_cond != idx_cond)
7600
tab->ref.disable_cache= true;
7602
Item *row_cond= make_cond_remainder(tab->select_cond, true);
7606
if (!idx_remainder_cond)
7607
tab->select_cond= row_cond;
7610
tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7611
tab->select_cond->quick_fix_field();
7612
((Item_cond_and*)tab->select_cond)->used_tables_cache=
7613
row_cond->used_tables() | idx_remainder_cond->used_tables();
7617
tab->select_cond= idx_remainder_cond;
7620
tab->select->cond= tab->select_cond;
7630
Determine if the set is already ordered for ORDER BY, so it can
7631
disable join cache because it will change the ordering of the results.
7632
Code handles sort table that is at any location (not only first after
7633
the const tables) despite the fact that it's currently prohibited.
7634
We must disable join cache if the first non-const table alone is
7635
ordered. If there is a temp table the ordering is done as a last
7636
operation and doesn't prevent join cache usage.
7638
uint make_join_orderinfo(JOIN *join)
7642
return join->tables;
7644
for (i=join->const_tables ; i < join->tables ; i++)
7646
JOIN_TAB *tab=join->join_tab+i;
7647
TABLE *table=tab->table;
7648
if ((table == join->sort_by_table &&
7649
(!join->order || join->skip_sort_order)) ||
7650
(join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7660
Plan refinement stage: do various set ups for the executioner
7663
make_join_readinfo()
7664
join Join being processed
7665
options Join's options (checking for SELECT_DESCRIBE,
7666
SELECT_NO_JOIN_CACHE)
7667
no_jbuf_after Don't use join buffering after table with this number.
7670
Plan refinement stage: do various set ups for the executioner
7671
- set up use of join buffering
7672
- push index conditions
7673
- increment counters
7678
true - Out of memory
7682
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
7685
bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7688
for (i=join->const_tables ; i < join->tables ; i++)
7690
JOIN_TAB *tab=join->join_tab+i;
7691
TABLE *table=tab->table;
7692
bool using_join_cache;
7693
tab->read_record.table= table;
7694
tab->read_record.file=table->file;
7695
tab->next_select=sub_select; /* normal select */
7697
TODO: don't always instruct first table's ref/range access method to
7698
produce sorted output.
7700
tab->sorted= sorted;
7701
sorted= 0; // only first must be sorted
7702
if (tab->insideout_match_tab)
7704
if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7709
switch (tab->type) {
7710
case JT_SYSTEM: // Only happens with left join
7711
table->status=STATUS_NO_RECORD;
7712
tab->read_first_record= join_read_system;
7713
tab->read_record.read_record= join_no_more_records;
7715
case JT_CONST: // Only happens with left join
7716
table->status=STATUS_NO_RECORD;
7717
tab->read_first_record= join_read_const;
7718
tab->read_record.read_record= join_no_more_records;
7719
if (table->covering_keys.is_set(tab->ref.key) &&
7723
table->file->extra(HA_EXTRA_KEYREAD);
7727
table->status=STATUS_NO_RECORD;
7730
delete tab->select->quick;
7731
tab->select->quick=0;
7735
tab->read_first_record= join_read_key;
7736
tab->read_record.read_record= join_no_more_records;
7737
if (table->covering_keys.is_set(tab->ref.key) &&
7741
table->file->extra(HA_EXTRA_KEYREAD);
7744
push_index_cond(tab, tab->ref.key, true);
7746
case JT_REF_OR_NULL:
7748
table->status=STATUS_NO_RECORD;
7751
delete tab->select->quick;
7752
tab->select->quick=0;
7756
if (table->covering_keys.is_set(tab->ref.key) &&
7760
table->file->extra(HA_EXTRA_KEYREAD);
7763
push_index_cond(tab, tab->ref.key, true);
7764
if (tab->type == JT_REF)
7766
tab->read_first_record= join_read_always_key;
7767
tab->read_record.read_record= tab->insideout_match_tab?
7768
join_read_next_same_diff : join_read_next_same;
7772
tab->read_first_record= join_read_always_key_or_null;
7773
tab->read_record.read_record= join_read_next_same_or_null;
7778
If previous table use cache
7779
If the incoming data set is already sorted don't use cache.
7781
table->status=STATUS_NO_RECORD;
7782
using_join_cache= false;
7783
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7784
tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7785
!tab->insideout_match_tab)
7787
if ((options & SELECT_DESCRIBE) ||
7788
!join_init_cache(join->thd,join->join_tab+join->const_tables,
7789
i-join->const_tables))
7791
using_join_cache= true;
7792
tab[-1].next_select=sub_select_cache; /* Patch previous */
7795
/* These init changes read_record */
7796
if (tab->use_quick == 2)
7798
join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7799
tab->read_first_record= join_init_quick_read_record;
7801
status_var_increment(join->thd->status_var.select_range_check_count);
7805
tab->read_first_record= join_init_read_record;
7806
if (i == join->const_tables)
7808
if (tab->select && tab->select->quick)
7811
status_var_increment(join->thd->status_var.select_range_count);
7815
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7817
status_var_increment(join->thd->status_var.select_scan_count);
7822
if (tab->select && tab->select->quick)
7825
status_var_increment(join->thd->status_var.select_full_range_join_count);
7829
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7831
status_var_increment(join->thd->status_var.select_full_join_count);
7834
if (!table->no_keyread)
7836
if (tab->select && tab->select->quick &&
7837
tab->select->quick->index != MAX_KEY && //not index_merge
7838
table->covering_keys.is_set(tab->select->quick->index))
7841
table->file->extra(HA_EXTRA_KEYREAD);
7843
else if (!table->covering_keys.is_clear_all() &&
7844
!(tab->select && tab->select->quick))
7845
{ // Only read index tree
7846
if (!tab->insideout_match_tab)
7849
See bug #26447: "Using the clustered index for a table scan
7850
is always faster than using a secondary index".
7852
if (table->s->primary_key != MAX_KEY &&
7853
table->file->primary_key_is_clustered())
7854
tab->index= table->s->primary_key;
7856
tab->index=find_shortest_key(table, & table->covering_keys);
7858
tab->read_first_record= join_read_first;
7859
tab->type=JT_NEXT; // Read with index_first / index_next
7862
if (tab->select && tab->select->quick &&
7863
tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
7864
push_index_cond(tab, tab->select->quick->index, !using_join_cache);
7868
break; /* purecov: deadcode */
7871
abort(); /* purecov: deadcode */
7874
join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7880
Give error if we some tables are done with a full join.
7882
This is used by multi_table_update and multi_table_delete when running
7885
@param join Join condition
7890
1 Error (full join used)
7893
bool error_if_full_join(JOIN *join)
7895
for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
7899
if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
7901
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7902
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7914
void JOIN_TAB::cleanup()
1225
size_t size= cache.end - cache.buff;
1226
global_join_buffer.sub(size);
2511
9641
if (!(left_const && right_const) &&
2512
9642
args[0]->result_type() == args[1]->result_type())
2516
resolve_const_item(session, &args[1], args[0]);
2517
func->update_used_tables();
2518
change_cond_ref_to_const(session, save_list, and_father, and_father,
2521
else if (left_const)
2523
resolve_const_item(session, &args[0], args[1]);
2524
func->update_used_tables();
2525
change_cond_ref_to_const(session, save_list, and_father, and_father,
9646
resolve_const_item(thd, &args[1], args[0]);
9647
func->update_used_tables();
9648
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9651
else if (left_const)
9653
resolve_const_item(thd, &args[0], args[1]);
9654
func->update_used_tables();
9655
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9665
Simplify joins replacing outer joins by inner joins whenever it's
9668
The function, during a retrieval of join_list, eliminates those
9669
outer joins that can be converted into inner join, possibly nested.
9670
It also moves the on expressions for the converted outer joins
9671
and from inner joins to conds.
9672
The function also calculates some attributes for nested joins:
9676
- on_expr_dep_tables
9677
The first two attributes are used to test whether an outer join can
9678
be substituted for an inner join. The third attribute represents the
9679
relation 'to be dependent on' for tables. If table t2 is dependent
9680
on table t1, then in any evaluated execution plan table access to
9681
table t2 must precede access to table t2. This relation is used also
9682
to check whether the query contains invalid cross-references.
9683
The forth attribute is an auxiliary one and is used to calculate
9685
As the attribute dep_tables qualifies possibles orders of tables in the
9686
execution plan, the dependencies required by the straight join
9687
modifiers are reflected in this attribute as well.
9688
The function also removes all braces that can be removed from the join
9689
expression without changing its meaning.
9692
An outer join can be replaced by an inner join if the where condition
9693
or the on expression for an embedding nested join contains a conjunctive
9694
predicate rejecting null values for some attribute of the inner tables.
9698
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9700
the predicate t2.b < 5 rejects nulls.
9701
The query is converted first to:
9703
SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9705
then to the equivalent form:
9707
SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9711
Similarly the following query:
9713
SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9718
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
9722
One conversion might trigger another:
9724
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9725
LEFT JOIN t3 ON t3.b=t2.b
9726
WHERE t3 IS NOT NULL =>
9727
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9728
WHERE t3 IS NOT NULL AND t3.b=t2.b =>
9729
SELECT * FROM t1, t2, t3
9730
WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9733
The function removes all unnecessary braces from the expression
9734
produced by the conversions.
9737
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9739
finally is converted to:
9741
SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9746
It also will remove braces from the following queries:
9748
SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9749
SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9752
The benefit of this simplification procedure is that it might return
9753
a query for which the optimizer can evaluate execution plan with more
9754
join orders. With a left join operation the optimizer does not
9755
consider any plan where one of the inner tables is before some of outer
9759
The function is implemented by a recursive procedure. On the recursive
9760
ascent all attributes are calculated, all outer joins that can be
9761
converted are replaced and then all unnecessary braces are removed.
9762
As join list contains join tables in the reverse order sequential
9763
elimination of outer joins does not require extra recursive calls.
9766
Remove all semi-joins that have are within another semi-join (i.e. have
9767
an "ancestor" semi-join nest)
9770
Here is an example of a join query with invalid cross references:
9772
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
9775
@param join reference to the query info
9776
@param join_list list representation of the join to be converted
9777
@param conds conditions to add on expressions for converted joins
9778
@param top true <=> conds is the where condition
9781
- The new condition, if success
9786
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9790
NESTED_JOIN *nested_join;
9791
TABLE_LIST *prev_table= 0;
9792
List_iterator<TABLE_LIST> li(*join_list);
9795
Try to simplify join operations from join_list.
9796
The most outer join operation is checked for conversion first.
9798
while ((table= li++))
9800
table_map used_tables;
9801
table_map not_null_tables= (table_map) 0;
9803
if ((nested_join= table->nested_join))
9806
If the element of join_list is a nested join apply
9807
the procedure to its nested join list first.
9811
Item *expr= table->on_expr;
9813
If an on expression E is attached to the table,
9814
check all null rejected predicates in this expression.
9815
If such a predicate over an attribute belonging to
9816
an inner table of an embedded outer join is found,
9817
the outer join is converted to an inner join and
9818
the corresponding on expression is added to E.
9820
expr= simplify_joins(join, &nested_join->join_list,
9821
expr, false, in_sj || table->sj_on_expr);
9823
if (!table->prep_on_expr || expr != table->on_expr)
9827
table->on_expr= expr;
9828
table->prep_on_expr= expr->copy_andor_structure(join->thd);
9831
nested_join->used_tables= (table_map) 0;
9832
nested_join->not_null_tables=(table_map) 0;
9833
conds= simplify_joins(join, &nested_join->join_list, conds, top,
9834
in_sj || table->sj_on_expr);
9835
used_tables= nested_join->used_tables;
9836
not_null_tables= nested_join->not_null_tables;
9840
if (!table->prep_on_expr)
9841
table->prep_on_expr= table->on_expr;
9842
used_tables= table->table->map;
9844
not_null_tables= conds->not_null_tables();
9847
if (table->embedding)
9849
table->embedding->nested_join->used_tables|= used_tables;
9850
table->embedding->nested_join->not_null_tables|= not_null_tables;
9853
if (!table->outer_join || (used_tables & not_null_tables))
9856
For some of the inner tables there are conjunctive predicates
9857
that reject nulls => the outer join can be replaced by an inner join.
9859
table->outer_join= 0;
9862
/* Add ON expression to the WHERE or upper-level ON condition. */
9865
conds= and_conds(conds, table->on_expr);
9866
conds->top_level_item();
9867
/* conds is always a new item as both cond and on_expr existed */
9868
assert(!conds->fixed);
9869
conds->fix_fields(join->thd, &conds);
9872
conds= table->on_expr;
9873
table->prep_on_expr= table->on_expr= 0;
9881
Only inner tables of non-convertible outer joins
9882
remain with on_expr.
9886
table->dep_tables|= table->on_expr->used_tables();
9887
if (table->embedding)
9889
table->dep_tables&= ~table->embedding->nested_join->used_tables;
9891
Embedding table depends on tables used
9892
in embedded on expressions.
9894
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9897
table->dep_tables&= ~table->table->map;
9902
/* The order of tables is reverse: prev_table follows table */
9903
if (prev_table->straight)
9904
prev_table->dep_tables|= used_tables;
9905
if (prev_table->on_expr)
9907
prev_table->dep_tables|= table->on_expr_dep_tables;
9908
table_map prev_used_tables= prev_table->nested_join ?
9909
prev_table->nested_join->used_tables :
9910
prev_table->table->map;
9912
If on expression contains only references to inner tables
9913
we still make the inner tables dependent on the outer tables.
9914
It would be enough to set dependency only on one outer table
9915
for them. Yet this is really a rare case.
9917
if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9918
prev_table->dep_tables|= used_tables;
9925
Flatten nested joins that can be flattened.
9926
no ON expression and not a semi-join => can be flattened.
9929
while ((table= li++))
9931
nested_join= table->nested_join;
9932
if (table->sj_on_expr && !in_sj)
9935
If this is a semi-join that is not contained within another semi-join,
9936
leave it intact (otherwise it is flattened)
9938
join->select_lex->sj_nests.push_back(table);
9940
else if (nested_join && !table->on_expr)
9943
List_iterator<TABLE_LIST> it(nested_join->join_list);
9946
tbl->embedding= table->embedding;
9947
tbl->join_list= table->join_list;
9949
li.replace(nested_join->join_list);
9957
Assign each nested join structure a bit in nested_join_map.
9959
Assign each nested join structure (except "confluent" ones - those that
9960
embed only one element) a bit in nested_join_map.
9962
@param join Join being processed
9963
@param join_list List of tables
9964
@param first_unused Number of first unused bit in nested_join_map before the
9968
This function is called after simplify_joins(), when there are no
9969
redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9970
we will not run out of bits in nested_join_map.
9973
First unused bit in nested_join_map after the call.
9976
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
9979
List_iterator<TABLE_LIST> li(*join_list);
9981
while ((table= li++))
9983
NESTED_JOIN *nested_join;
9984
if ((nested_join= table->nested_join))
9987
It is guaranteed by simplify_joins() function that a nested join
9988
that has only one child is either
9989
- a single-table view (the child is the underlying table), or
9990
- a single-table semi-join nest
9992
We don't assign bits to such sj-nests because
9993
1. it is redundant (a "sequence" of one table cannot be interleaved
9995
2. we could run out bits in nested_join_map otherwise.
9997
if (nested_join->join_list.elements != 1)
9999
/* Don't assign bits to sj-nests */
10000
if (table->on_expr)
10001
nested_join->nj_map= (nested_join_map) 1 << first_unused++;
10002
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
10007
return(first_unused);
10012
Set NESTED_JOIN::counter=0 in all nested joins in passed list.
10014
Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
10015
the passed join_list.
10017
@param join_list List of nested joins to process. It may also contain base
10018
tables which will be ignored.
10021
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10023
List_iterator<TABLE_LIST> li(*join_list);
10025
while ((table= li++))
10027
NESTED_JOIN *nested_join;
10028
if ((nested_join= table->nested_join))
10030
nested_join->counter_= 0;
10031
reset_nj_counters(&nested_join->join_list);
2534
10039
Check interleaving with an inner tables of an outer join for
2535
10040
extension table.
2537
Check if table next_tab can be added to current partial join order, and
10042
Check if table next_tab can be added to current partial join order, and
2538
10043
if yes, record that it has been added.
2540
10045
The function assumes that both current partial join order and its
2541
10046
extension with next_tab are valid wrt table dependencies.
2545
LIMITATIONS ON JOIN order_st
10050
LIMITATIONS ON JOIN ORDER
2546
10051
The nested [outer] joins executioner algorithm imposes these limitations
2547
10052
on join order:
2548
1. "Outer tables first" - any "outer" table must be before any
10053
1. "Outer tables first" - any "outer" table must be before any
2549
10054
corresponding "inner" table.
2550
10055
2. "No interleaving" - tables inside a nested join must form a continuous
2551
sequence in join order (i.e. the sequence must not be interrupted by
10056
sequence in join order (i.e. the sequence must not be interrupted by
2552
10057
tables that are outside of this nested join).
2554
10059
#1 is checked elsewhere, this function checks #2 provided that #1 has
2555
10060
been already checked.
2557
10062
WHY NEED NON-INTERLEAVING
2558
Consider an example:
10063
Consider an example:
2560
10065
select * from t0 join t1 left join (t2 join t3) on cond1
2943
10519
if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2946
return right_item->eq(*const_item, 1);
2947
*const_item=right_item;
10522
return right_item->eq(*const_item, 1);
10523
*const_item=right_item;
2951
10527
else if (right_item->eq(comp_item,1))
2953
10529
if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2956
return left_item->eq(*const_item, 1);
2957
*const_item=left_item;
10532
return left_item->eq(*const_item, 1);
10533
*const_item=left_item;
10541
/****************************************************************************
10542
Create internal temporary table
10543
****************************************************************************/
10546
Create field for temporary table from given field.
10548
@param thd Thread handler
10549
@param org_field field from which new field will be created
10550
@param name New field name
10551
@param table Temporary table
10552
@param item !=NULL if item->result_field should point to new field.
10553
This is relevant for how fill_record() is going to work:
10554
If item != NULL then fill_record() will update
10555
the record in the original table.
10556
If item == NULL then fill_record() will update
10557
the temporary table
10558
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10559
field instead of blob.
10567
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10568
const char *name, TABLE *table,
10569
Item_field *item, uint convert_blob_length)
10574
Make sure that the blob fits into a Field_varstring which has
10577
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10578
(org_field->flags & BLOB_FLAG))
10579
new_field= new Field_varstring(convert_blob_length,
10580
org_field->maybe_null(),
10581
org_field->field_name, table->s,
10582
org_field->charset());
10584
new_field= org_field->new_field(thd->mem_root, table,
10585
table == org_field->table);
10588
new_field->init(table);
10589
new_field->orig_table= org_field->orig_table;
10591
item->result_field= new_field;
10593
new_field->field_name= name;
10594
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10595
if (org_field->maybe_null() || (item && item->maybe_null))
10596
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
10597
if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
10598
org_field->type() == MYSQL_TYPE_VARCHAR)
10599
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10600
else if (org_field->type() == FIELD_TYPE_DOUBLE)
10601
((Field_double *) new_field)->not_fixed= true;
10607
Create field for temporary table using type of given item.
10609
@param thd Thread handler
10610
@param item Item to create a field for
10611
@param table Temporary table
10612
@param copy_func If set and item is a function, store copy of
10614
@param modify_item 1 if item->result_field should point to new
10615
item. This is relevent for how fill_record()
10617
If modify_item is 1 then fill_record() will
10618
update the record in the original table.
10619
If modify_item is 0 then fill_record() will
10620
update the temporary table
10621
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10622
field instead of blob.
10630
static Field *create_tmp_field_from_item(THD *thd __attribute__((__unused__)),
10631
Item *item, TABLE *table,
10632
Item ***copy_func, bool modify_item,
10633
uint convert_blob_length)
10635
bool maybe_null= item->maybe_null;
10638
switch (item->result_type()) {
10640
new_field= new Field_double(item->max_length, maybe_null,
10641
item->name, item->decimals, true);
10645
Select an integer type with the minimal fit precision.
10646
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10647
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
10648
Field_long : make them Field_longlong.
10650
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10651
new_field=new Field_longlong(item->max_length, maybe_null,
10652
item->name, item->unsigned_flag);
10654
new_field=new Field_long(item->max_length, maybe_null,
10655
item->name, item->unsigned_flag);
10657
case STRING_RESULT:
10658
assert(item->collation.collation);
10660
enum enum_field_types type;
10662
DATE/TIME fields have STRING_RESULT result type.
10663
To preserve type they needed to be handled separately.
10665
if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
10666
type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_NEWDATE ||
10667
type == MYSQL_TYPE_TIMESTAMP)
10668
new_field= item->tmp_table_field_from_field_type(table, 1);
10670
Make sure that the blob fits into a Field_varstring which has
10673
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10674
convert_blob_length <= Field_varstring::MAX_SIZE &&
10675
convert_blob_length)
10676
new_field= new Field_varstring(convert_blob_length, maybe_null,
10677
item->name, table->s,
10678
item->collation.collation);
10680
new_field= item->make_string_field(table);
10681
new_field->set_derivation(item->collation.derivation);
10683
case DECIMAL_RESULT:
10685
uint8 dec= item->decimals;
10686
uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
10687
uint32 len= item->max_length;
10690
Trying to put too many digits overall in a DECIMAL(prec,dec)
10691
will always throw a warning. We must limit dec to
10692
DECIMAL_MAX_SCALE however to prevent an assert() later.
10697
signed int overflow;
10699
dec= min(dec, DECIMAL_MAX_SCALE);
10702
If the value still overflows the field with the corrected dec,
10703
we'll throw out decimals rather than integers. This is still
10704
bad and of course throws a truncation warning.
10705
+1: for decimal point
10708
overflow= my_decimal_precision_to_length(intg + dec, dec,
10709
item->unsigned_flag) - len;
10712
dec= max(0, dec - overflow); // too long, discard fract
10714
len -= item->decimals - dec; // corrected value fits
10717
new_field= new Field_new_decimal(len, maybe_null, item->name,
10718
dec, item->unsigned_flag);
10723
// This case should never be choosen
10729
new_field->init(table);
10731
if (copy_func && item->is_result_field())
10732
*((*copy_func)++) = item; // Save for copy_funcs
10734
item->set_result_field(new_field);
10735
if (item->type() == Item::NULL_ITEM)
10736
new_field->is_created_from_null_item= true;
10742
Create field for information schema table.
10744
@param thd Thread handler
10745
@param table Temporary table
10746
@param item Item to create a field for
10754
Field *create_tmp_field_for_schema(THD *thd __attribute__((__unused__)),
10755
Item *item, TABLE *table)
10757
if (item->field_type() == MYSQL_TYPE_VARCHAR)
10760
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10761
field= new Field_blob(item->max_length, item->maybe_null,
10762
item->name, item->collation.collation);
10764
field= new Field_varstring(item->max_length, item->maybe_null,
10766
table->s, item->collation.collation);
10768
field->init(table);
10771
return item->tmp_table_field_from_field_type(table, 0);
10776
Create field for temporary table.
10778
@param thd Thread handler
10779
@param table Temporary table
10780
@param item Item to create a field for
10781
@param type Type of item (normally item->type)
10782
@param copy_func If set and item is a function, store copy of item
10784
@param from_field if field will be created using other field as example,
10785
pointer example field will be written here
10786
@param default_field If field has a default value field, store it here
10787
@param group 1 if we are going to do a relative group by on result
10788
@param modify_item 1 if item->result_field should point to new item.
10789
This is relevent for how fill_record() is going to
10791
If modify_item is 1 then fill_record() will update
10792
the record in the original table.
10793
If modify_item is 0 then fill_record() will update
10794
the temporary table
10795
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10796
field instead of blob.
10804
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10805
Item ***copy_func, Field **from_field,
10806
Field **default_field,
10807
bool group, bool modify_item,
10808
bool table_cant_handle_bit_fields __attribute__((__unused__)),
10809
bool make_copy_field,
10810
uint convert_blob_length)
10813
Item::Type orig_type= type;
10814
Item *orig_item= 0;
10816
if (type != Item::FIELD_ITEM &&
10817
item->real_item()->type() == Item::FIELD_ITEM)
10820
item= item->real_item();
10821
type= Item::FIELD_ITEM;
10825
case Item::SUM_FUNC_ITEM:
10827
Item_sum *item_sum=(Item_sum*) item;
10828
result= item_sum->create_tmp_field(group, table, convert_blob_length);
10830
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10833
case Item::FIELD_ITEM:
10834
case Item::DEFAULT_VALUE_ITEM:
10836
Item_field *field= (Item_field*) item;
10837
bool orig_modify= modify_item;
10838
if (orig_type == Item::REF_ITEM)
10841
If item have to be able to store NULLs but underlaid field can't do it,
10842
create_tmp_field_from_field() can't be used for tmp field creation.
10844
if (field->maybe_null && !field->field->maybe_null())
10846
result= create_tmp_field_from_item(thd, item, table, NULL,
10847
modify_item, convert_blob_length);
10848
*from_field= field->field;
10849
if (result && modify_item)
10850
field->result_field= result;
10853
result= create_tmp_field_from_field(thd, (*from_field= field->field),
10854
orig_item ? orig_item->name :
10857
modify_item ? field :
10859
convert_blob_length);
10860
if (orig_type == Item::REF_ITEM && orig_modify)
10861
((Item_ref*)orig_item)->set_result_field(result);
10862
if (field->field->eq_def(result))
10863
*default_field= field->field;
10867
case Item::FUNC_ITEM:
10869
case Item::COND_ITEM:
10870
case Item::FIELD_AVG_ITEM:
10871
case Item::FIELD_STD_ITEM:
10872
case Item::SUBSELECT_ITEM:
10873
/* The following can only happen with 'CREATE TABLE ... SELECT' */
10874
case Item::PROC_ITEM:
10875
case Item::INT_ITEM:
10876
case Item::REAL_ITEM:
10877
case Item::DECIMAL_ITEM:
10878
case Item::STRING_ITEM:
10879
case Item::REF_ITEM:
10880
case Item::NULL_ITEM:
10881
case Item::VARBIN_ITEM:
10882
if (make_copy_field)
10884
assert(((Item_result_field*)item)->result_field);
10885
*from_field= ((Item_result_field*)item)->result_field;
10887
return create_tmp_field_from_item(thd, item, table,
10888
(make_copy_field ? 0 : copy_func),
10889
modify_item, convert_blob_length);
10890
case Item::TYPE_HOLDER:
10891
result= ((Item_type_holder *)item)->make_field_by_type(table);
10892
result->set_derivation(item->collation.derivation);
10894
default: // Dosen't have to be stored
10900
Set up column usage bitmaps for a temporary table
10903
For temporary tables, we need one bitmap with all columns set and
10904
a tmp_set bitmap to be used by things like filesort.
10907
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10909
uint field_count= table->s->fields;
10910
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10912
bitmap_init(&table->tmp_set,
10913
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10914
field_count, false);
10915
/* write_set and all_set are copies of read_set */
10916
table->def_write_set= table->def_read_set;
10917
table->s->all_set= table->def_read_set;
10918
bitmap_set_all(&table->s->all_set);
10919
table->default_column_bitmaps();
10924
Create a temp table according to a field list.
10926
Given field pointers are changed to point at tmp_table for
10927
send_fields. The table object is self contained: it's
10928
allocated in its own memory root, as well as Field objects
10929
created for table columns.
10930
This function will replace Item_sum items in 'fields' list with
10931
corresponding Item_field items, pointing at the fields in the
10932
temporary table, unless this was prohibited by true
10933
value of argument save_sum_fields. The Item_field objects
10934
are created in THD memory root.
10936
@param thd thread handle
10937
@param param a description used as input to create the table
10938
@param fields list of items that will be used to define
10939
column types of the table (also see NOTES)
10940
@param group TODO document
10941
@param distinct should table rows be distinct
10942
@param save_sum_fields see NOTES
10943
@param select_options
10945
@param table_alias possible name of the temporary table that can
10946
be used for name resolving; can be "".
10949
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10950
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
10951
#define RATIO_TO_PACK_ROWS 2
10952
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
10955
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10956
ORDER *group, bool distinct, bool save_sum_fields,
10957
ulonglong select_options, ha_rows rows_limit,
10960
MEM_ROOT *mem_root_save, own_root;
10962
TABLE_SHARE *share;
10963
uint i,field_count,null_count,null_pack_length;
10964
uint copy_func_count= param->func_count;
10965
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
10966
uint blob_count,group_null_items, string_count;
10967
uint temp_pool_slot=MY_BIT_NONE;
10969
ulong reclength, string_total_length;
10970
bool using_unique_constraint= 0;
10971
bool use_packed_rows= 0;
10972
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10973
char *tmpname,path[FN_REFLEN];
10974
uchar *pos, *group_buff, *bitmaps;
10976
Field **reg_field, **from_field, **default_field;
10978
Copy_field *copy=0;
10980
KEY_PART_INFO *key_part_info;
10982
MI_COLUMNDEF *recinfo;
10983
uint total_uneven_bit_length= 0;
10984
bool force_copy_fields= param->force_copy_fields;
10986
status_var_increment(thd->status_var.created_tmp_tables);
10988
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10989
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10991
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10992
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10993
current_pid, temp_pool_slot);
10996
/* if we run out of slots or we are not using tempool */
10997
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10998
thd->thread_id, thd->tmp_table++);
11002
No need to change table name to lower case as we are only creating
11003
MyISAM or HEAP tables here
11005
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11010
if (!param->quick_group)
11011
group=0; // Can't use group key
11012
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
11015
marker == 4 means two things:
11016
- store NULLs in the key, and
11017
- convert BIT fields to 64-bit long, needed because MEMORY tables
11018
can't index BIT fields.
11020
(*tmp->item)->marker= 4;
11021
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11022
using_unique_constraint=1;
11024
if (param->group_length >= MAX_BLOB_WIDTH)
11025
using_unique_constraint=1;
11027
distinct=0; // Can't use distinct
11030
field_count=param->field_count+param->func_count+param->sum_func_count;
11031
hidden_field_count=param->hidden_field_count;
11034
When loose index scan is employed as access method, it already
11035
computes all groups and the result of all aggregate functions. We
11036
make space for the items of the aggregate function in the list of
11037
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11038
these items are stored in the temporary table.
11040
if (param->precomputed_group_by)
11041
copy_func_count+= param->sum_func_count;
11043
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11045
if (!multi_alloc_root(&own_root,
11046
&table, sizeof(*table),
11047
&share, sizeof(*share),
11048
®_field, sizeof(Field*) * (field_count+1),
11049
&default_field, sizeof(Field*) * (field_count),
11050
&blob_field, sizeof(uint)*(field_count+1),
11051
&from_field, sizeof(Field*)*field_count,
11052
©_func, sizeof(*copy_func)*(copy_func_count+1),
11053
¶m->keyinfo, sizeof(*param->keyinfo),
11055
sizeof(*key_part_info)*(param->group_parts+1),
11056
¶m->start_recinfo,
11057
sizeof(*param->recinfo)*(field_count*2+4),
11058
&tmpname, (uint) strlen(path)+1,
11059
&group_buff, (group && ! using_unique_constraint ?
11060
param->group_length : 0),
11061
&bitmaps, bitmap_buffer_size(field_count)*2,
11064
if (temp_pool_slot != MY_BIT_NONE)
11065
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11066
return(NULL); /* purecov: inspected */
11068
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11069
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11071
if (temp_pool_slot != MY_BIT_NONE)
11072
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11073
free_root(&own_root, MYF(0)); /* purecov: inspected */
11074
return(NULL); /* purecov: inspected */
11076
param->items_to_copy= copy_func;
11077
strmov(tmpname,path);
11078
/* make table according to fields */
11080
bzero((char*) table,sizeof(*table));
11081
bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
11082
bzero((char*) default_field, sizeof(Field*) * (field_count));
11083
bzero((char*) from_field,sizeof(Field*)*field_count);
11085
table->mem_root= own_root;
11086
mem_root_save= thd->mem_root;
11087
thd->mem_root= &table->mem_root;
11089
table->field=reg_field;
11090
table->alias= table_alias;
11091
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11092
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11094
table->temp_pool_slot = temp_pool_slot;
11095
table->copy_blobs= 1;
11096
table->in_use= thd;
11097
table->quick_keys.init();
11098
table->covering_keys.init();
11099
table->keys_in_use_for_query.init();
11102
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11103
share->blob_field= blob_field;
11104
share->blob_ptr_size= portable_sizeof_char_ptr;
11105
share->db_low_byte_first=1; // True for HEAP and MyISAM
11106
share->table_charset= param->table_charset;
11107
share->primary_key= MAX_KEY; // Indicate no primary key
11108
share->keys_for_keyread.init();
11109
share->keys_in_use.init();
11111
/* Calculate which type of fields we will store in the temporary table */
11113
reclength= string_total_length= 0;
11114
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11115
param->using_indirect_summary_function=0;
11117
List_iterator_fast<Item> li(fields);
11119
Field **tmp_from_field=from_field;
11120
while ((item=li++))
11122
Item::Type type=item->type();
11123
if (not_all_columns)
11125
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11127
if (item->used_tables() & OUTER_REF_TABLE_BIT)
11128
item->update_used_tables();
11129
if (type == Item::SUBSELECT_ITEM ||
11130
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
11133
Mark that the we have ignored an item that refers to a summary
11134
function. We need to know this if someone is going to use
11135
DISTINCT on the result.
11137
param->using_indirect_summary_function=1;
11141
if (item->const_item() && (int) hidden_field_count <= 0)
11142
continue; // We don't have to store this
11144
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11145
{ /* Can't calc group yet */
11146
((Item_sum*) item)->result_field=0;
11147
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11149
Item **argp= ((Item_sum*) item)->args + i;
11151
if (!arg->const_item())
11154
create_tmp_field(thd, table, arg, arg->type(), ©_func,
11155
tmp_from_field, &default_field[fieldnr],
11156
group != 0,not_all_columns,
11158
param->convert_blob_length);
11160
goto err; // Should be OOM
11162
reclength+=new_field->pack_length();
11163
if (new_field->flags & BLOB_FLAG)
11165
*blob_field++= fieldnr;
11168
*(reg_field++)= new_field;
11169
if (new_field->real_type() == MYSQL_TYPE_STRING ||
11170
new_field->real_type() == MYSQL_TYPE_VARCHAR)
11173
string_total_length+= new_field->pack_length();
11175
thd->mem_root= mem_root_save;
11176
thd->change_item_tree(argp, new Item_field(new_field));
11177
thd->mem_root= &table->mem_root;
11178
if (!(new_field->flags & NOT_NULL_FLAG))
11182
new_field->maybe_null() is still false, it will be
11183
changed below. But we have to setup Item_field correctly
11185
(*argp)->maybe_null=1;
11187
new_field->field_index= fieldnr++;
11194
The last parameter to create_tmp_field() is a bit tricky:
11196
We need to set it to 0 in union, to get fill_record() to modify the
11198
We need to set it to 1 on multi-table-update and in select to
11199
write rows to the temporary table.
11200
We here distinguish between UNION and multi-table-updates by the fact
11201
that in the later case group is set to the row pointer.
11203
Field *new_field= (param->schema_table) ?
11204
create_tmp_field_for_schema(thd, item, table) :
11205
create_tmp_field(thd, table, item, type, ©_func,
11206
tmp_from_field, &default_field[fieldnr],
11208
!force_copy_fields &&
11209
(not_all_columns || group !=0),
11211
If item->marker == 4 then we force create_tmp_field
11212
to create a 64-bit longs for BIT fields because HEAP
11213
tables can't index BIT fields directly. We do the same
11214
for distinct, as we want the distinct index to be
11215
usable in this case too.
11217
item->marker == 4 || param->bit_fields_as_long,
11219
param->convert_blob_length);
11223
if (thd->is_fatal_error)
11224
goto err; // Got OOM
11225
continue; // Some kindf of const item
11227
if (type == Item::SUM_FUNC_ITEM)
11228
((Item_sum *) item)->result_field= new_field;
11230
reclength+=new_field->pack_length();
11231
if (!(new_field->flags & NOT_NULL_FLAG))
11233
if (new_field->flags & BLOB_FLAG)
11235
*blob_field++= fieldnr;
11238
if (item->marker == 4 && item->maybe_null)
11240
group_null_items++;
11241
new_field->flags|= GROUP_FLAG;
11243
new_field->field_index= fieldnr++;
11244
*(reg_field++)= new_field;
11246
if (!--hidden_field_count)
11249
This was the last hidden field; Remember how many hidden fields could
11252
hidden_null_count=null_count;
11254
We need to update hidden_field_count as we may have stored group
11255
functions with constant arguments
11257
param->hidden_field_count= fieldnr;
11261
assert(fieldnr == (uint) (reg_field - table->field));
11262
assert(field_count >= (uint) (reg_field - table->field));
11263
field_count= fieldnr;
11265
*blob_field= 0; // End marker
11266
share->fields= field_count;
11268
/* If result table is small; use a heap */
11269
/* future: storage engine selection can be made dynamic? */
11270
if (blob_count || using_unique_constraint ||
11271
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11272
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11274
share->db_plugin= ha_lock_engine(0, myisam_hton);
11275
table->file= get_new_handler(share, &table->mem_root,
11278
(param->group_parts > table->file->max_key_parts() ||
11279
param->group_length > table->file->max_key_length()))
11280
using_unique_constraint=1;
11284
share->db_plugin= ha_lock_engine(0, heap_hton);
11285
table->file= get_new_handler(share, &table->mem_root,
11292
if (!using_unique_constraint)
11293
reclength+= group_null_items; // null flag is stored separately
11295
share->blob_fields= blob_count;
11296
if (blob_count == 0)
11298
/* We need to ensure that first byte is not 0 for the delete link */
11299
if (param->hidden_field_count)
11300
hidden_null_count++;
11304
hidden_null_pack_length=(hidden_null_count+7)/8;
11305
null_pack_length= (hidden_null_pack_length +
11306
(null_count + total_uneven_bit_length + 7) / 8);
11307
reclength+=null_pack_length;
11309
reclength=1; // Dummy select
11310
/* Use packed rows if there is blobs or a lot of space to gain */
11311
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)))
11312
use_packed_rows= 1;
11314
share->reclength= reclength;
11316
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11317
share->rec_buff_length= alloc_length;
11318
if (!(table->record[0]= (uchar*)
11319
alloc_root(&table->mem_root, alloc_length*3)))
11321
table->record[1]= table->record[0]+alloc_length;
11322
share->default_values= table->record[1]+alloc_length;
11324
copy_func[0]=0; // End marker
11325
param->func_count= copy_func - param->items_to_copy;
11327
setup_tmp_table_column_bitmaps(table, bitmaps);
11329
recinfo=param->start_recinfo;
11330
null_flags=(uchar*) table->record[0];
11331
pos=table->record[0]+ null_pack_length;
11332
if (null_pack_length)
11334
bzero((uchar*) recinfo,sizeof(*recinfo));
11335
recinfo->type=FIELD_NORMAL;
11336
recinfo->length=null_pack_length;
11338
bfill(null_flags,null_pack_length,255); // Set null fields
11340
table->null_flags= (uchar*) table->record[0];
11341
share->null_fields= null_count+ hidden_null_count;
11342
share->null_bytes= null_pack_length;
11344
null_count= (blob_count == 0) ? 1 : 0;
11345
hidden_field_count=param->hidden_field_count;
11346
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11348
Field *field= *reg_field;
11350
bzero((uchar*) recinfo,sizeof(*recinfo));
11352
if (!(field->flags & NOT_NULL_FLAG))
11354
if (field->flags & GROUP_FLAG && !using_unique_constraint)
11357
We have to reserve one byte here for NULL bits,
11358
as this is updated by 'end_update()'
11360
*pos++=0; // Null is stored here
11362
recinfo->type=FIELD_NORMAL;
11364
bzero((uchar*) recinfo,sizeof(*recinfo));
11368
recinfo->null_bit= 1 << (null_count & 7);
11369
recinfo->null_pos= null_count/8;
11371
field->move_field(pos,null_flags+null_count/8,
11372
1 << (null_count & 7));
11376
field->move_field(pos,(uchar*) 0,0);
11380
Test if there is a default field value. The test for ->ptr is to skip
11381
'offset' fields generated by initalize_tables
11383
if (default_field[i] && default_field[i]->ptr)
11386
default_field[i] is set only in the cases when 'field' can
11387
inherit the default value that is defined for the field referred
11388
by the Item_field object from which 'field' has been created.
11391
Field *orig_field= default_field[i];
11392
/* Get the value from default_values */
11393
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11394
orig_field->table->record[0]);
11395
orig_field->move_field_offset(diff); // Points now at default_values
11396
if (orig_field->is_real_null())
11400
field->set_notnull();
11401
memcpy(field->ptr, orig_field->ptr, field->pack_length());
11403
orig_field->move_field_offset(-diff); // Back to record[0]
11407
{ /* Not a table Item */
11408
copy->set(field,from_field[i],save_sum_fields);
11411
length=field->pack_length();
11414
/* Make entry for create table */
11415
recinfo->length=length;
11416
if (field->flags & BLOB_FLAG)
11417
recinfo->type= (int) FIELD_BLOB;
11418
else if (use_packed_rows &&
11419
field->real_type() == MYSQL_TYPE_STRING &&
11420
length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11421
recinfo->type=FIELD_SKIP_ENDSPACE;
11423
recinfo->type=FIELD_NORMAL;
11424
if (!--hidden_field_count)
11425
null_count=(null_count+7) & ~7; // move to next byte
11427
// fix table name in field entry
11428
field->table_name= &table->alias;
11431
param->copy_field_end=copy;
11432
param->recinfo=recinfo;
11433
store_record(table,s->default_values); // Make empty default record
11435
if (thd->variables.tmp_table_size == ~ (ulonglong) 0) // No limit
11436
share->max_rows= ~(ha_rows) 0;
11438
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11439
min(thd->variables.tmp_table_size,
11440
thd->variables.max_heap_table_size) :
11441
thd->variables.tmp_table_size) /
11443
set_if_bigger(share->max_rows,1); // For dummy start options
11445
Push the LIMIT clause to the temporary table creation, so that we
11446
materialize only up to 'rows_limit' records instead of all result records.
11448
set_if_smaller(share->max_rows, rows_limit);
11449
param->end_write_records= rows_limit;
11451
keyinfo= param->keyinfo;
11455
table->group=group; /* Table is grouped by key */
11456
param->group_buff=group_buff;
11458
share->uniques= test(using_unique_constraint);
11459
table->key_info=keyinfo;
11460
keyinfo->key_part=key_part_info;
11461
keyinfo->flags=HA_NOSAME;
11462
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11463
keyinfo->key_length=0;
11464
keyinfo->rec_per_key=0;
11465
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11466
keyinfo->name= (char*) "group_key";
11467
ORDER *cur_group= group;
11468
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11470
Field *field=(*cur_group->item)->get_tmp_table_field();
11471
bool maybe_null=(*cur_group->item)->maybe_null;
11472
key_part_info->null_bit=0;
11473
key_part_info->field= field;
11474
key_part_info->offset= field->offset(table->record[0]);
11475
key_part_info->length= (uint16) field->key_length();
11476
key_part_info->type= (uint8) field->key_type();
11477
key_part_info->key_type =
11478
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11479
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11480
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11481
0 : FIELDFLAG_BINARY;
11482
if (!using_unique_constraint)
11484
cur_group->buff=(char*) group_buff;
11485
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11490
goto err; /* purecov: inspected */
11494
To be able to group on NULL, we reserved place in group_buff
11495
for the NULL flag just before the column. (see above).
11496
The field data is after this flag.
11497
The NULL flag is updated in 'end_update()' and 'end_write()'
11499
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
11500
key_part_info->null_bit=field->null_bit;
11501
key_part_info->null_offset= (uint) (field->null_ptr -
11502
(uchar*) table->record[0]);
11503
cur_group->buff++; // Pointer to field data
11504
group_buff++; // Skipp null flag
11506
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11507
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11508
group_buff+= cur_group->field->pack_length();
11510
keyinfo->key_length+= key_part_info->length;
11514
if (distinct && field_count != param->hidden_field_count)
11517
Create an unique key or an unique constraint over all columns
11518
that should be in the result. In the temporary table, there are
11519
'param->hidden_field_count' extra columns, whose null bits are stored
11520
in the first 'hidden_null_pack_length' bytes of the row.
11525
Special mode for index creation in MyISAM used to support unique
11526
indexes on blobs with arbitrary length. Such indexes cannot be
11531
null_pack_length-=hidden_null_pack_length;
11532
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11533
(share->uniques ? test(null_pack_length) : 0));
11534
table->distinct= 1;
11536
if (!(key_part_info= (KEY_PART_INFO*)
11537
alloc_root(&table->mem_root,
11538
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11540
bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11541
table->key_info=keyinfo;
11542
keyinfo->key_part=key_part_info;
11543
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11544
keyinfo->key_length=(uint16) reclength;
11545
keyinfo->name= (char*) "distinct_key";
11546
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11547
keyinfo->rec_per_key=0;
11550
Create an extra field to hold NULL bits so that unique indexes on
11551
blobs can distinguish NULL from 0. This extra field is not needed
11552
when we do not use UNIQUE indexes for blobs.
11554
if (null_pack_length && share->uniques)
11556
key_part_info->null_bit=0;
11557
key_part_info->offset=hidden_null_pack_length;
11558
key_part_info->length=null_pack_length;
11559
key_part_info->field= new Field_string(table->record[0],
11560
(uint32) key_part_info->length,
11564
NullS, &my_charset_bin);
11565
if (!key_part_info->field)
11567
key_part_info->field->init(table);
11568
key_part_info->key_type=FIELDFLAG_BINARY;
11569
key_part_info->type= HA_KEYTYPE_BINARY;
11572
/* Create a distinct key over the columns we are going to return */
11573
for (i=param->hidden_field_count, reg_field=table->field + i ;
11575
i++, reg_field++, key_part_info++)
11577
key_part_info->null_bit=0;
11578
key_part_info->field= *reg_field;
11579
key_part_info->offset= (*reg_field)->offset(table->record[0]);
11580
key_part_info->length= (uint16) (*reg_field)->pack_length();
11582
The below method of computing the key format length of the
11583
key part is a copy/paste from opt_range.cc, and table.cc.
11584
This should be factored out, e.g. as a method of Field.
11585
In addition it is not clear if any of the Field::*_length
11586
methods is supposed to compute the same length. If so, it
11589
key_part_info->store_length= key_part_info->length;
11591
if ((*reg_field)->real_maybe_null())
11592
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11593
if ((*reg_field)->type() == MYSQL_TYPE_BLOB ||
11594
(*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
11595
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11597
key_part_info->type= (uint8) (*reg_field)->key_type();
11598
key_part_info->key_type =
11599
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11600
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11601
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11602
0 : FIELDFLAG_BINARY;
11606
if (thd->is_fatal_error) // If end of memory
11607
goto err; /* purecov: inspected */
11608
share->db_record_offset= 1;
11609
if (share->db_type() == myisam_hton)
11611
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11612
¶m->recinfo, select_options))
11615
if (open_tmp_table(table))
11618
thd->mem_root= mem_root_save;
11623
thd->mem_root= mem_root_save;
11624
free_tmp_table(thd,table); /* purecov: inspected */
11625
if (temp_pool_slot != MY_BIT_NONE)
11626
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11627
return(NULL); /* purecov: inspected */
11634
Create a temporary table to weed out duplicate rowid combinations
11638
create_duplicate_weedout_tmp_table()
11640
uniq_tuple_length_arg
11644
Create a temporary table to weed out duplicate rowid combinations. The
11645
table has a single column that is a concatenation of all rowids in the
11648
Depending on the needed length, there are two cases:
11650
1. When the length of the column < max_key_length:
11652
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11654
2. Otherwise (not a valid SQL syntax but internally supported):
11656
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11658
The code in this function was produced by extraction of relevant parts
11659
from create_tmp_table().
11666
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
11667
uint uniq_tuple_length_arg,
11668
SJ_TMP_TABLE *sjtbl)
11670
MEM_ROOT *mem_root_save, own_root;
11672
TABLE_SHARE *share;
11673
uint temp_pool_slot=MY_BIT_NONE;
11674
char *tmpname,path[FN_REFLEN];
11676
KEY_PART_INFO *key_part_info;
11681
MI_COLUMNDEF *recinfo, *start_recinfo;
11682
bool using_unique_constraint=false;
11683
bool use_packed_rows= false;
11684
Field *field, *key_field;
11685
uint blob_count, null_pack_length, null_count;
11690
STEP 1: Get temporary table name
11692
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11693
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11694
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11696
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11697
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11698
current_pid, temp_pool_slot);
11701
/* if we run out of slots or we are not using tempool */
11702
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11703
thd->thread_id, thd->tmp_table++);
11705
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11707
/* STEP 2: Figure if we'll be using a key or blob+constraint */
11708
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11709
using_unique_constraint= true;
11711
/* STEP 3: Allocate memory for temptable description */
11712
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11713
if (!multi_alloc_root(&own_root,
11714
&table, sizeof(*table),
11715
&share, sizeof(*share),
11716
®_field, sizeof(Field*) * (1+1),
11717
&blob_field, sizeof(uint)*2,
11718
&keyinfo, sizeof(*keyinfo),
11719
&key_part_info, sizeof(*key_part_info) * 2,
11721
sizeof(*recinfo)*(1*2+4),
11722
&tmpname, (uint) strlen(path)+1,
11723
&group_buff, (!using_unique_constraint ?
11724
uniq_tuple_length_arg : 0),
11725
&bitmaps, bitmap_buffer_size(1)*2,
11728
if (temp_pool_slot != MY_BIT_NONE)
11729
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11732
strmov(tmpname,path);
11735
/* STEP 4: Create TABLE description */
11736
bzero((char*) table,sizeof(*table));
11737
bzero((char*) reg_field,sizeof(Field*)*2);
11739
table->mem_root= own_root;
11740
mem_root_save= thd->mem_root;
11741
thd->mem_root= &table->mem_root;
11743
table->field=reg_field;
11744
table->alias= "weedout-tmp";
11745
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11746
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11748
table->temp_pool_slot = temp_pool_slot;
11749
table->copy_blobs= 1;
11750
table->in_use= thd;
11751
table->quick_keys.init();
11752
table->covering_keys.init();
11753
table->keys_in_use_for_query.init();
11756
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11757
share->blob_field= blob_field;
11758
share->blob_ptr_size= portable_sizeof_char_ptr;
11759
share->db_low_byte_first=1; // True for HEAP and MyISAM
11760
share->table_charset= NULL;
11761
share->primary_key= MAX_KEY; // Indicate no primary key
11762
share->keys_for_keyread.init();
11763
share->keys_in_use.init();
11767
/* Create the field */
11770
For the sake of uniformity, always use Field_varstring (altough we could
11771
use Field_string for shorter keys)
11773
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11777
field->table= table;
11778
field->key_start.init(0);
11779
field->part_of_key.init(0);
11780
field->part_of_sortkey.init(0);
11781
field->unireg_check= Field::NONE;
11782
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11783
field->reset_fields();
11784
field->init(table);
11785
field->orig_table= NULL;
11787
field->field_index= 0;
11789
*(reg_field++)= field;
11794
share->blob_fields= 0;
11797
uint reclength= field->pack_length();
11798
if (using_unique_constraint)
11800
share->db_plugin= ha_lock_engine(0, myisam_hton);
11801
table->file= get_new_handler(share, &table->mem_root,
11803
assert(uniq_tuple_length_arg <= table->file->max_key_length());
11807
share->db_plugin= ha_lock_engine(0, heap_hton);
11808
table->file= get_new_handler(share, &table->mem_root,
11816
null_pack_length= 1;
11817
reclength += null_pack_length;
11819
share->reclength= reclength;
11821
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11822
share->rec_buff_length= alloc_length;
11823
if (!(table->record[0]= (uchar*)
11824
alloc_root(&table->mem_root, alloc_length*3)))
11826
table->record[1]= table->record[0]+alloc_length;
11827
share->default_values= table->record[1]+alloc_length;
11829
setup_tmp_table_column_bitmaps(table, bitmaps);
11831
recinfo= start_recinfo;
11832
null_flags=(uchar*) table->record[0];
11833
pos=table->record[0]+ null_pack_length;
11834
if (null_pack_length)
11836
bzero((uchar*) recinfo,sizeof(*recinfo));
11837
recinfo->type=FIELD_NORMAL;
11838
recinfo->length=null_pack_length;
11840
bfill(null_flags,null_pack_length,255); // Set null fields
11842
table->null_flags= (uchar*) table->record[0];
11843
share->null_fields= null_count;
11844
share->null_bytes= null_pack_length;
11849
//Field *field= *reg_field;
11851
bzero((uchar*) recinfo,sizeof(*recinfo));
11852
field->move_field(pos,(uchar*) 0,0);
11856
Test if there is a default field value. The test for ->ptr is to skip
11857
'offset' fields generated by initalize_tables
11859
// Initialize the table field:
11860
bzero(field->ptr, field->pack_length());
11862
length=field->pack_length();
11865
/* Make entry for create table */
11866
recinfo->length=length;
11867
if (field->flags & BLOB_FLAG)
11868
recinfo->type= (int) FIELD_BLOB;
11869
else if (use_packed_rows &&
11870
field->real_type() == MYSQL_TYPE_STRING &&
11871
length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11872
recinfo->type=FIELD_SKIP_ENDSPACE;
11874
recinfo->type=FIELD_NORMAL;
11876
field->table_name= &table->alias;
11879
//param->recinfo=recinfo;
11880
//store_record(table,s->default_values); // Make empty default record
11882
if (thd->variables.tmp_table_size == ~ (ulonglong) 0) // No limit
11883
share->max_rows= ~(ha_rows) 0;
11885
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11886
min(thd->variables.tmp_table_size,
11887
thd->variables.max_heap_table_size) :
11888
thd->variables.tmp_table_size) /
11890
set_if_bigger(share->max_rows,1); // For dummy start options
11893
//// keyinfo= param->keyinfo;
11897
share->uniques= test(using_unique_constraint);
11898
table->key_info=keyinfo;
11899
keyinfo->key_part=key_part_info;
11900
keyinfo->flags=HA_NOSAME;
11901
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11902
keyinfo->key_length=0;
11903
keyinfo->rec_per_key=0;
11904
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11905
keyinfo->name= (char*) "weedout_key";
11907
key_part_info->null_bit=0;
11908
key_part_info->field= field;
11909
key_part_info->offset= field->offset(table->record[0]);
11910
key_part_info->length= (uint16) field->key_length();
11911
key_part_info->type= (uint8) field->key_type();
11912
key_part_info->key_type = FIELDFLAG_BINARY;
11913
if (!using_unique_constraint)
11915
if (!(key_field= field->new_key_field(thd->mem_root, table,
11920
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11922
keyinfo->key_length+= key_part_info->length;
11926
if (thd->is_fatal_error) // If end of memory
11928
share->db_record_offset= 1;
11929
if (share->db_type() == myisam_hton)
11932
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11935
sjtbl->start_recinfo= start_recinfo;
11936
sjtbl->recinfo= recinfo;
11937
if (open_tmp_table(table))
11940
thd->mem_root= mem_root_save;
11944
thd->mem_root= mem_root_save;
11945
free_tmp_table(thd,table); /* purecov: inspected */
11946
if (temp_pool_slot != MY_BIT_NONE)
11947
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11948
return(NULL); /* purecov: inspected */
11951
/****************************************************************************/
11954
Create a reduced TABLE object with properly set up Field list from a
11955
list of field definitions.
11957
The created table doesn't have a table handler associated with
11958
it, has no keys, no group/distinct, no copy_funcs array.
11959
The sole purpose of this TABLE object is to use the power of Field
11960
class to read/write data to/from table->record[0]. Then one can store
11961
the record in any container (RB tree, hash, etc).
11962
The table is created in THD mem_root, so are the table's fields.
11963
Consequently, if you don't BLOB fields, you don't need to free it.
11965
@param thd connection handle
11966
@param field_list list of column definitions
11969
0 if out of memory, TABLE object in case of success
11972
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11974
uint field_count= field_list.elements;
11975
uint blob_count= 0;
11977
Create_field *cdef; /* column definition */
11978
uint record_length= 0;
11979
uint null_count= 0; /* number of columns which may be null */
11980
uint null_pack_length; /* NULL representation array length */
11984
TABLE_SHARE *share;
11986
if (!multi_alloc_root(thd->mem_root,
11987
&table, sizeof(*table),
11988
&share, sizeof(*share),
11989
&field, (field_count + 1) * sizeof(Field*),
11990
&blob_field, (field_count+1) *sizeof(uint),
11991
&bitmaps, bitmap_buffer_size(field_count)*2,
11995
bzero(table, sizeof(*table));
11996
bzero(share, sizeof(*share));
11997
table->field= field;
11999
share->blob_field= blob_field;
12000
share->fields= field_count;
12001
share->blob_ptr_size= portable_sizeof_char_ptr;
12002
setup_tmp_table_column_bitmaps(table, bitmaps);
12004
/* Create all fields and calculate the total length of record */
12005
List_iterator_fast<Create_field> it(field_list);
12006
while ((cdef= it++))
12008
*field= make_field(share, 0, cdef->length,
12009
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
12010
f_maybe_null(cdef->pack_flag) ? 1 : 0,
12011
cdef->pack_flag, cdef->sql_type, cdef->charset,
12012
cdef->unireg_check,
12013
cdef->interval, cdef->field_name);
12016
(*field)->init(table);
12017
record_length+= (*field)->pack_length();
12018
if (! ((*field)->flags & NOT_NULL_FLAG))
12021
if ((*field)->flags & BLOB_FLAG)
12022
share->blob_field[blob_count++]= (uint) (field - table->field);
12026
*field= NULL; /* mark the end of the list */
12027
share->blob_field[blob_count]= 0; /* mark the end of the list */
12028
share->blob_fields= blob_count;
12030
null_pack_length= (null_count + 7)/8;
12031
share->reclength= record_length + null_pack_length;
12032
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12033
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12034
if (!table->record[0])
12037
if (null_pack_length)
12039
table->null_flags= (uchar*) table->record[0];
12040
share->null_fields= null_count;
12041
share->null_bytes= null_pack_length;
12044
table->in_use= thd; /* field->reset() may access table->in_use */
12046
/* Set up field pointers */
12047
uchar *null_pos= table->record[0];
12048
uchar *field_pos= null_pos + share->null_bytes;
12051
for (field= table->field; *field; ++field)
12053
Field *cur_field= *field;
12054
if ((cur_field->flags & NOT_NULL_FLAG))
12055
cur_field->move_field(field_pos);
12058
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12060
if (null_bit == (1 << 8))
12066
cur_field->reset();
12068
field_pos+= cur_field->pack_length();
12073
for (field= table->field; *field; ++field)
12074
delete *field; /* just invokes field destructor */
12079
static bool open_tmp_table(TABLE *table)
12082
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12083
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12085
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12089
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */
12095
Create MyISAM temporary table
12098
create_myisam_tmp_table()
12099
table Table object that descrimes the table to be created
12100
keyinfo Description of the index (there is always one index)
12101
start_recinfo MyISAM's column descriptions
12102
recinfo INOUT End of MyISAM's column descriptions
12103
options Option bits
12106
Create a MyISAM temporary table according to passed description. The is
12107
assumed to have one unique index or constraint.
12109
The passed array or MI_COLUMNDEF structures must have this form:
12111
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12112
when there are many nullable columns)
12114
3. One free MI_COLUMNDEF element (*recinfo points here)
12116
This function may use the free element to create hash column for unique
12124
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
12125
MI_COLUMNDEF *start_recinfo,
12126
MI_COLUMNDEF **recinfo,
12131
MI_UNIQUEDEF uniquedef;
12132
TABLE_SHARE *share= table->s;
12135
{ // Get keys for ni_create
12136
bool using_unique_constraint=0;
12137
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12138
sizeof(*seg) * keyinfo->key_parts);
12142
bzero(seg, sizeof(*seg) * keyinfo->key_parts);
12143
if (keyinfo->key_length >= table->file->max_key_length() ||
12144
keyinfo->key_parts > table->file->max_key_parts() ||
12147
/* Can't create a key; Make a unique constraint instead of a key */
12150
using_unique_constraint=1;
12151
bzero((char*) &uniquedef,sizeof(uniquedef));
12152
uniquedef.keysegs=keyinfo->key_parts;
12154
uniquedef.null_are_equal=1;
12156
/* Create extra column for hash value */
12157
bzero((uchar*) *recinfo,sizeof(**recinfo));
12158
(*recinfo)->type= FIELD_CHECK;
12159
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12161
share->reclength+=MI_UNIQUE_HASH_LENGTH;
12165
/* Create an unique key */
12166
bzero((char*) &keydef,sizeof(keydef));
12167
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12168
keydef.keysegs= keyinfo->key_parts;
12171
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12173
Field *field=keyinfo->key_part[i].field;
12175
seg->language= field->charset()->number;
12176
seg->length= keyinfo->key_part[i].length;
12177
seg->start= keyinfo->key_part[i].offset;
12178
if (field->flags & BLOB_FLAG)
12181
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12182
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12183
seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
12184
seg->flag= HA_BLOB_PART;
12185
seg->length=0; // Whole blob in unique constraint
12189
seg->type= keyinfo->key_part[i].type;
12190
/* Tell handler if it can do suffic space compression */
12191
if (field->real_type() == MYSQL_TYPE_STRING &&
12192
keyinfo->key_part[i].length > 4)
12193
seg->flag|= HA_SPACE_PACK;
12195
if (!(field->flags & NOT_NULL_FLAG))
12197
seg->null_bit= field->null_bit;
12198
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12200
We are using a GROUP BY on something that contains NULL
12201
In this case we have to tell MyISAM that two NULL should
12202
on INSERT be regarded at the same value
12204
if (!using_unique_constraint)
12205
keydef.flag|= HA_NULL_ARE_EQUAL;
12209
MI_CREATE_INFO create_info;
12210
bzero((char*) &create_info,sizeof(create_info));
12212
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12214
create_info.data_file_length= ~(ulonglong) 0;
12216
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12217
(uint) (*recinfo-start_recinfo),
12219
share->uniques, &uniquedef,
12221
HA_CREATE_TMP_TABLE)))
12223
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12227
status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12228
share->db_record_offset= 1;
12236
free_tmp_table(THD *thd, TABLE *entry)
12238
MEM_ROOT own_root= entry->mem_root;
12239
const char *save_proc_info;
12241
save_proc_info=thd->proc_info;
12242
thd_proc_info(thd, "removing tmp table");
12246
if (entry->db_stat)
12247
entry->file->ha_drop_table(entry->s->table_name.str);
12249
entry->file->ha_delete_table(entry->s->table_name.str);
12250
delete entry->file;
12254
for (Field **ptr=entry->field ; *ptr ; ptr++)
12256
free_io_cache(entry);
12258
if (entry->temp_pool_slot != MY_BIT_NONE)
12259
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12261
plugin_unlock(0, entry->s->db_plugin);
12263
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12264
thd_proc_info(thd, save_proc_info);
12270
If a HEAP table gets full, create a MyISAM table and copy all rows
12274
bool create_myisam_from_heap(THD *thd, TABLE *table,
12275
MI_COLUMNDEF *start_recinfo,
12276
MI_COLUMNDEF **recinfo,
12277
int error, bool ignore_last_dupp_key_error)
12281
const char *save_proc_info;
12284
if (table->s->db_type() != heap_hton ||
12285
error != HA_ERR_RECORD_FILE_FULL)
12287
table->file->print_error(error,MYF(0));
12292
new_table.s= &share;
12293
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12294
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12295
new_table.s->db_type())))
12296
return(1); // End of memory
12298
save_proc_info=thd->proc_info;
12299
thd_proc_info(thd, "converting HEAP to MyISAM");
12301
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12302
recinfo, thd->lex->select_lex.options |
12305
if (open_tmp_table(&new_table))
12307
if (table->file->indexes_are_disabled())
12308
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12309
table->file->ha_index_or_rnd_end();
12310
table->file->ha_rnd_init(1);
12311
if (table->no_rows)
12313
new_table.file->extra(HA_EXTRA_NO_ROWS);
12314
new_table.no_rows=1;
12317
#ifdef TO_BE_DONE_LATER_IN_4_1
12319
To use start_bulk_insert() (which is new in 4.1) we need to find
12320
all places where a corresponding end_bulk_insert() should be put.
12322
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12323
new_table.file->ha_start_bulk_insert(table->file->stats.records);
12325
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12326
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12330
copy all old rows from heap table to MyISAM table
12331
This is the only code that uses record[1] to read/write but this
12332
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12334
while (!table->file->rnd_next(new_table.record[1]))
12336
write_err= new_table.file->ha_write_row(new_table.record[1]);
12340
/* copy row that filled HEAP table */
12341
if ((write_err=new_table.file->ha_write_row(table->record[0])))
12343
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12344
!ignore_last_dupp_key_error)
12348
/* remove heap table and change to use myisam table */
12349
(void) table->file->ha_rnd_end();
12350
(void) table->file->close(); // This deletes the table !
12351
delete table->file;
12353
plugin_unlock(0, table->s->db_plugin);
12354
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12355
new_table.s= table->s; // Keep old share
12359
table->file->change_table_ptr(table, table->s);
12360
table->use_all_columns();
12361
if (save_proc_info)
12363
const char *new_proc_info=
12364
(!strcmp(save_proc_info,"Copying to tmp table") ?
12365
"Copying to tmp table on disk" : save_proc_info);
12366
thd_proc_info(thd, new_proc_info);
12371
table->file->print_error(write_err, MYF(0));
12372
(void) table->file->ha_rnd_end();
12373
(void) new_table.file->close();
12375
new_table.file->ha_delete_table(new_table.s->table_name.str);
12377
delete new_table.file;
12378
thd_proc_info(thd, save_proc_info);
12379
table->mem_root= new_table.mem_root;
3350
int safe_index_read(JoinTable *tab)
3353
Table *table= tab->table;
3354
if ((error=table->cursor->index_read_map(table->getInsertRecord(),
12787
SemiJoinDuplicateElimination: Weed out duplicate row combinations
12790
do_sj_dups_weedout()
12794
1 The row combination is a duplicate (discard it)
12795
0 The row combination is not a duplicate (continue)
12798
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl)
12801
SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
12802
SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
12803
uchar *ptr= sjtbl->tmp_table->record[0] + 1;
12804
uchar *nulls_ptr= ptr;
12806
/* Put the the rowids tuple into table->record[0]: */
12808
// 1. Store the length
12809
if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
12811
*ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
12816
int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
12820
// 2. Zero the null bytes
12821
if (sjtbl->null_bytes)
12823
bzero(ptr, sjtbl->null_bytes);
12824
ptr += sjtbl->null_bytes;
12827
// 3. Put the rowids
12828
for (uint i=0; tab != tab_end; tab++, i++)
12830
handler *h= tab->join_tab->table->file;
12831
if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
12833
/* It's a NULL-complemented row */
12834
*(nulls_ptr + tab->null_byte) |= tab->null_bit;
12835
bzero(ptr + tab->rowid_offset, h->ref_length);
12839
/* Copy the rowid value */
12840
if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
12841
h->position(tab->join_tab->table->record[0]);
12842
memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
12846
error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
12849
/* create_myisam_from_heap will generate error if needed */
12850
if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
12851
create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo,
12852
&sjtbl->recinfo, error, 1))
12854
//return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
12862
SemiJoinDuplicateElimination: Reset the temporary table
12865
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
12867
if (sj_tbl->tmp_table)
12868
return sj_tbl->tmp_table->file->ha_delete_all_rows();
12873
Process one record of the nested loop join.
12875
This function will evaluate parts of WHERE/ON clauses that are
12876
applicable to the partial record on hand and in case of success
12877
submit this record to the next level of the nested loop.
12880
static enum_nested_loop_state
12881
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
12884
bool not_used_in_distinct=join_tab->not_used_in_distinct;
12885
ha_rows found_records=join->found_records;
12886
COND *select_cond= join_tab->select_cond;
12888
if (error > 0 || (join->thd->is_error())) // Fatal error
12889
return NESTED_LOOP_ERROR;
12891
return NESTED_LOOP_NO_MORE_ROWS;
12892
if (join->thd->killed) // Aborted by user
12894
join->thd->send_kill_message();
12895
return NESTED_LOOP_KILLED; /* purecov: inspected */
12897
if (!select_cond || select_cond->val_int())
12900
There is no select condition or the attached pushed down
12901
condition is true => a match is found.
12904
while (join_tab->first_unmatched && found)
12907
The while condition is always false if join_tab is not
12908
the last inner join table of an outer join operation.
12910
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
12912
Mark that a match for current outer table is found.
12913
This activates push down conditional predicates attached
12914
to the all inner tables of the outer join.
12916
first_unmatched->found= 1;
12917
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
12919
if (tab->table->reginfo.not_exists_optimize)
12920
return NESTED_LOOP_NO_MORE_ROWS;
12921
/* Check all predicates that has just been activated. */
12923
Actually all predicates non-guarded by first_unmatched->found
12924
will be re-evaluated again. It could be fixed, but, probably,
12925
it's not worth doing now.
12927
if (tab->select_cond && !tab->select_cond->val_int())
12929
/* The condition attached to table tab is false */
12930
if (tab == join_tab)
12935
Set a return point if rejected predicate is attached
12936
not to the last table of the current nest level.
12938
join->return_tab= tab;
12939
return NESTED_LOOP_OK;
12944
Check whether join_tab is not the last inner table
12945
for another embedding outer join.
12947
if ((first_unmatched= first_unmatched->first_upper) &&
12948
first_unmatched->last_inner != join_tab)
12949
first_unmatched= 0;
12950
join_tab->first_unmatched= first_unmatched;
12953
JOIN_TAB *return_tab= join->return_tab;
12954
join_tab->found_match= true;
12955
if (join_tab->check_weed_out_table)
12957
int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
12959
return NESTED_LOOP_ERROR;
12961
return NESTED_LOOP_OK;
12963
else if (join_tab->do_firstmatch)
12966
We should return to the join_tab->do_firstmatch after we have
12967
enumerated all the suffixes for current prefix row combination
12969
return_tab= join_tab->do_firstmatch;
12973
It was not just a return to lower loop level when one
12974
of the newly activated predicates is evaluated as false
12975
(See above join->return_tab= tab).
12977
join->examined_rows++;
12978
join->thd->row_count++;
12982
enum enum_nested_loop_state rc;
12983
/* A match from join_tab is found for the current partial join. */
12984
rc= (*join_tab->next_select)(join, join_tab+1, 0);
12985
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
12987
if (return_tab < join->return_tab)
12988
join->return_tab= return_tab;
12990
if (join->return_tab < join_tab)
12991
return NESTED_LOOP_OK;
12993
Test if this was a SELECT DISTINCT query on a table that
12994
was not in the field list; In this case we can abort if
12995
we found a row, as no new rows can be added to the result.
12997
if (not_used_in_distinct && found_records != join->found_records)
12998
return NESTED_LOOP_NO_MORE_ROWS;
13001
join_tab->read_record.file->unlock_row();
13006
The condition pushed down to the table join_tab rejects all rows
13007
with the beginning coinciding with the current partial join.
13009
join->examined_rows++;
13010
join->thd->row_count++;
13011
join_tab->read_record.file->unlock_row();
13013
return NESTED_LOOP_OK;
13020
Construct a NULL complimented partial join record and feed it to the next
13021
level of the nested loop. This function is used in case we have
13022
an OUTER join and no matching record was found.
13025
static enum_nested_loop_state
13026
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
13029
The table join_tab is the first inner table of a outer join operation
13030
and no matches has been found for the current outer row.
13032
JOIN_TAB *last_inner_tab= join_tab->last_inner;
13033
/* Cache variables for faster loop */
13035
for ( ; join_tab <= last_inner_tab ; join_tab++)
13037
/* Change the the values of guard predicate variables. */
13038
join_tab->found= 1;
13039
join_tab->not_null_compl= 0;
13040
/* The outer row is complemented by nulls for each inner tables */
13041
restore_record(join_tab->table,s->default_values); // Make empty record
13042
mark_as_null_row(join_tab->table); // For group by without error
13043
select_cond= join_tab->select_cond;
13044
/* Check all attached conditions for inner table rows. */
13045
if (select_cond && !select_cond->val_int())
13046
return NESTED_LOOP_OK;
13050
The row complemented by nulls might be the first row
13051
of embedding outer joins.
13052
If so, perform the same actions as in the code
13053
for the first regular outer join row above.
13057
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13058
if ((first_unmatched= first_unmatched->first_upper) &&
13059
first_unmatched->last_inner != join_tab)
13060
first_unmatched= 0;
13061
join_tab->first_unmatched= first_unmatched;
13062
if (!first_unmatched)
13064
first_unmatched->found= 1;
13065
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13067
if (tab->select_cond && !tab->select_cond->val_int())
13069
join->return_tab= tab;
13070
return NESTED_LOOP_OK;
13075
The row complemented by nulls satisfies all conditions
13076
attached to inner tables.
13077
Send the row complemented by nulls to be joined with the
13080
return (*join_tab->next_select)(join, join_tab+1, 0);
13084
static enum_nested_loop_state
13085
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13087
enum_nested_loop_state rc= NESTED_LOOP_OK;
13091
join_tab->table->null_row= 0;
13092
if (!join_tab->cache.records)
13093
return NESTED_LOOP_OK; /* Nothing to do */
13095
(void) store_record_in_cache(&join_tab->cache); // Must save this for later
13096
if (join_tab->use_quick == 2)
13098
if (join_tab->select->quick)
13099
{ /* Used quick select last. reset it */
13100
delete join_tab->select->quick;
13101
join_tab->select->quick=0;
13104
/* read through all records */
13105
if ((error=join_init_read_record(join_tab)))
13107
reset_cache_write(&join_tab->cache);
13108
return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13111
for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13113
tmp->status=tmp->table->status;
13114
tmp->table->status=0;
13117
info= &join_tab->read_record;
13120
if (join->thd->killed)
13122
join->thd->send_kill_message();
13123
return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13125
SQL_SELECT *select=join_tab->select;
13126
if (rc == NESTED_LOOP_OK &&
13127
(!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13130
reset_cache_read(&join_tab->cache);
13131
for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13133
read_cached_record(join_tab);
13134
if (!select || !select->skip_record())
13137
if (!join_tab->check_weed_out_table ||
13138
!(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13140
rc= (join_tab->next_select)(join,join_tab+1,0);
13141
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13143
reset_cache_write(&join_tab->cache);
13148
return NESTED_LOOP_ERROR;
13152
} while (!(error=info->read_record(info)));
13155
read_cached_record(join_tab); // Restore current record
13156
reset_cache_write(&join_tab->cache);
13157
if (error > 0) // Fatal error
13158
return NESTED_LOOP_ERROR; /* purecov: inspected */
13159
for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13160
tmp2->table->status=tmp2->status;
13161
return NESTED_LOOP_OK;
13165
/*****************************************************************************
13166
The different ways to read a record
13167
Returns -1 if row was not found, 0 if row was found and 1 on errors
13168
*****************************************************************************/
13170
/** Help function when we get some an error from the table handler. */
13172
int report_error(TABLE *table, int error)
13174
if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13176
table->status= STATUS_GARBAGE;
13177
return -1; // key not found; ok
13180
Locking reads can legally return also these errors, do not
13181
print them to the .err log
13183
if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13184
sql_print_error("Got error %d when reading table '%s'",
13185
error, table->s->path.str);
13186
table->file->print_error(error,MYF(0));
13191
int safe_index_read(JOIN_TAB *tab)
13194
TABLE *table= tab->table;
13195
if ((error=table->file->index_read_map(table->record[0],
3355
13196
tab->ref.key_buff,
3356
13197
make_prev_keypart_map(tab->ref.key_parts),
3357
13198
HA_READ_KEY_EXACT)))
3358
return table->report_error(error);
13199
return report_error(table, error);
3362
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
13205
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
3365
Table *table=tab->table;
13208
TABLE *table=tab->table;
3366
13209
table->const_table=1;
3367
13210
table->null_row=0;
3368
13211
table->status=STATUS_NO_RECORD;
3370
if (tab->type == AM_SYSTEM)
13213
if (tab->type == JT_SYSTEM)
3372
13215
if ((error=join_read_system(tab)))
3373
13216
{ // Info for DESCRIBE
3374
13217
tab->info="const row not found";
3375
13218
/* Mark for EXPLAIN that the row was not found */
3376
pos->setFanout(0.0);
3377
pos->clearRefDependMap();
3378
if (! table->maybe_null || error > 0)
13219
pos->records_read=0.0;
13220
pos->ref_depend_map= 0;
13221
if (!table->maybe_null || error > 0)
3384
if (! table->key_read &&
3385
table->covering_keys.test(tab->ref.key) &&
3386
! table->no_keyread &&
3387
(int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
13227
if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13228
!table->no_keyread &&
13229
(int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
3389
13231
table->key_read=1;
3390
table->cursor->extra(HA_EXTRA_KEYREAD);
13232
table->file->extra(HA_EXTRA_KEYREAD);
3391
13233
tab->index= tab->ref.key;
3393
13235
error=join_read_const(tab);
3394
13236
if (table->key_read)
3396
13238
table->key_read=0;
3397
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
13239
table->file->extra(HA_EXTRA_NO_KEYREAD);
3401
13243
tab->info="unique row not found";
3402
13244
/* Mark for EXPLAIN that the row was not found */
3403
pos->setFanout(0.0);
3404
pos->clearRefDependMap();
13245
pos->records_read=0.0;
13246
pos->ref_depend_map= 0;
3405
13247
if (!table->maybe_null || error > 0)
3409
13251
if (*tab->on_expr_ref && !table->null_row)
3411
13253
if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
3412
table->mark_as_null_row();
13254
mark_as_null_row(table);
3414
13256
if (!table->null_row)
3415
13257
table->maybe_null=0;
3417
13259
/* Check appearance of new constant items in Item_equal objects */
3418
Join *join= tab->join;
13260
JOIN *join= tab->join;
3419
13261
if (join->conds)
3420
13262
update_const_equal_items(join->conds, tab);
3422
13264
for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
3424
TableList *embedded;
3425
TableList *embedding= tbl;
13266
TABLE_LIST *embedded;
13267
TABLE_LIST *embedding= tbl;
3428
13270
embedded= embedding;
3429
13271
if (embedded->on_expr)
3430
13272
update_const_equal_items(embedded->on_expr, tab);
3431
embedding= embedded->getEmbedding();
13273
embedding= embedded->embedding;
3433
13275
while (embedding &&
3434
embedding->getNestedJoin()->join_list.head() == embedded);
13276
embedding->nested_join->join_list.head() == embedded);
3440
int join_read_system(JoinTable *tab)
13284
join_read_system(JOIN_TAB *tab)
3442
Table *table= tab->table;
13286
TABLE *table= tab->table;
3444
13288
if (table->status & STATUS_GARBAGE) // If first read
3446
if ((error=table->cursor->read_first_row(table->getInsertRecord(),
3447
table->getShare()->getPrimaryKey())))
13290
if ((error=table->file->read_first_row(table->record[0],
13291
table->s->primary_key)))
3449
13293
if (error != HA_ERR_END_OF_FILE)
3450
return table->report_error(error);
3451
tab->table->mark_as_null_row();
3452
table->emptyRecord(); // Make empty record
13294
return report_error(table, error);
13295
mark_as_null_row(tab->table);
13296
empty_record(table); // Make empty record
3455
table->storeRecord();
13299
store_record(table,record[1]);
3457
13301
else if (!table->status) // Only happens with left join
3458
table->restoreRecord(); // restore old record
13302
restore_record(table,record[1]); // restore old record
3459
13303
table->null_row=0;
3460
13304
return table->status ? -1 : 0;
3464
13309
Read a (constant) table when there is at most one matching row.
6320
static void print_table_array(Session *session, String *str, TableList **table,
6323
(*table)->print(session, str, QT_ORDINARY);
6325
for (TableList **tbl= table + 1; tbl < end; tbl++)
6327
TableList *curr= *tbl;
17299
/** Allocate memory needed for other rollup functions. */
17301
bool JOIN::rollup_init()
17306
tmp_table_param.quick_group= 0; // Can't create groups in tmp table
17307
rollup.state= ROLLUP::STATE_INITED;
17310
Create pointers to the different sum function groups
17311
These are updated by rollup_make_fields()
17313
tmp_table_param.group_parts= send_group_parts;
17315
if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17317
sizeof(List<Item>) +
17318
ref_pointer_array_size)
17319
* send_group_parts )))
17322
rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17323
rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17324
ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17327
Prepare space for field list for the different levels
17328
These will be filled up in rollup_make_fields()
17330
for (i= 0 ; i < send_group_parts ; i++)
17332
rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17333
List<Item> *rollup_fields= &rollup.fields[i];
17334
rollup_fields->empty();
17335
rollup.ref_pointer_arrays[i]= ref_array;
17336
ref_array+= all_fields.elements;
17338
for (i= 0 ; i < send_group_parts; i++)
17340
for (j=0 ; j < fields_list.elements ; j++)
17341
rollup.fields[i].push_back(rollup.null_items[i]);
17343
List_iterator<Item> it(all_fields);
17345
while ((item= it++))
17348
bool found_in_group= 0;
17350
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17352
if (*group_tmp->item == item)
17354
item->maybe_null= 1;
17356
if (item->const_item())
17359
For ROLLUP queries each constant item referenced in GROUP BY list
17360
is wrapped up into an Item_func object yielding the same value
17361
as the constant item. The objects of the wrapper class are never
17362
considered as constant items and besides they inherit all
17363
properties of the Item_result_field class.
17364
This wrapping allows us to ensure writing constant items
17365
into temporary tables whenever the result of the ROLLUP
17366
operation has to be written into a temporary table, e.g. when
17367
ROLLUP is used together with DISTINCT in the SELECT list.
17368
Usually when creating temporary tables for a intermidiate
17369
result we do not include fields for constant expressions.
17371
Item* new_item= new Item_func_rollup_const(item);
17374
new_item->fix_fields(thd, (Item **) 0);
17375
thd->change_item_tree(it.ref(), new_item);
17376
for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17378
if (*tmp->item == item)
17379
thd->change_item_tree(tmp->item, new_item);
17384
if (item->type() == Item::FUNC_ITEM && !found_in_group)
17386
bool changed= false;
17387
if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17390
We have to prevent creation of a field in a temporary table for
17391
an expression that contains GROUP BY attributes.
17392
Marking the expression item as 'with_sum_func' will ensure this.
17395
item->with_sum_func= 1;
17403
Fill up rollup structures with pointers to fields to use.
17405
Creates copies of item_sum items for each sum level.
17407
@param fields_arg List of all fields (hidden and real ones)
17408
@param sel_fields Pointer to selected fields
17409
@param func Store here a pointer to all fields
17413
In this case func is pointing to next not used element.
17418
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17421
List_iterator_fast<Item> it(fields_arg);
17422
Item *first_field= sel_fields.head();
17426
Create field lists for the different levels
17428
The idea here is to have a separate field list for each rollup level to
17429
avoid all runtime checks of which columns should be NULL.
17431
The list is stored in reverse order to get sum function in such an order
17432
in func that it makes it easy to reset them with init_sum_functions()
17434
Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17436
rollup.fields[0] will contain list where a,b,c is NULL
17437
rollup.fields[1] will contain list where b,c is NULL
17439
rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17441
sum_funcs_end[0] points to all sum functions
17442
sum_funcs_end[1] points to all sum functions, except grand totals
17446
for (level=0 ; level < send_group_parts ; level++)
17449
uint pos= send_group_parts - level -1;
17450
bool real_fields= 0;
17452
List_iterator<Item> new_it(rollup.fields[pos]);
17453
Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17454
ORDER *start_group;
17456
/* Point to first hidden field */
17457
Item **ref_array= ref_array_start + fields_arg.elements-1;
17459
/* Remember where the sum functions ends for the previous level */
17460
sum_funcs_end[pos+1]= *func;
17462
/* Find the start of the group for this level */
17463
for (i= 0, start_group= group_list ;
17465
start_group= start_group->next)
17469
while ((item= it++))
17471
if (item == first_field)
17473
real_fields= 1; // End of hidden fields
17474
ref_array= ref_array_start;
17477
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17478
(!((Item_sum*) item)->depended_from() ||
17479
((Item_sum *)item)->depended_from() == select_lex))
17483
This is a top level summary function that must be replaced with
17484
a sum function that is reset for this level.
17486
NOTE: This code creates an object which is not that nice in a
17487
sub select. Fortunately it's not common to have rollup in
17490
item= item->copy_or_same(thd);
17491
((Item_sum*) item)->make_unique();
17492
*(*func)= (Item_sum*) item;
17497
/* Check if this is something that is part of this group by */
17499
for (group_tmp= start_group, i= pos ;
17500
group_tmp ; group_tmp= group_tmp->next, i++)
17502
if (*group_tmp->item == item)
17505
This is an element that is used by the GROUP BY and should be
17506
set to NULL in this level
17508
Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17511
item->maybe_null= 1; // Value will be null sometimes
17512
null_item->result_field= item->get_tmp_table_field();
17521
(void) new_it++; // Point to next item
17522
new_it.replace(item); // Replace previous
17529
sum_funcs_end[0]= *func; // Point to last function
17534
Send all rollup levels higher than the current one to the client.
17538
SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17541
@param idx Level we are on:
17542
- 0 = Total sum level
17543
- 1 = First group changed (a)
17544
- 2 = Second group changed (a,b)
17549
1 If send_data_failed()
17552
int JOIN::rollup_send_data(uint idx)
17555
for (i= send_group_parts ; i-- > idx ; )
17557
/* Get reference pointers to sum functions in place */
17558
memcpy((char*) ref_pointer_array,
17559
(char*) rollup.ref_pointer_arrays[i],
17560
ref_pointer_array_size);
17561
if ((!having || having->val_int()))
17563
if (send_records < unit->select_limit_cnt && do_send_rows &&
17564
result->send_data(rollup.fields[i]))
17569
/* Restore ref_pointer_array */
17570
set_items_ref_array(current_ref_pointer_array);
17575
Write all rollup levels higher than the current one to a temp table.
17579
SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17582
@param idx Level we are on:
17583
- 0 = Total sum level
17584
- 1 = First group changed (a)
17585
- 2 = Second group changed (a,b)
17586
@param table reference to temp table
17591
1 if write_data_failed()
17594
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17597
for (i= send_group_parts ; i-- > idx ; )
17599
/* Get reference pointers to sum functions in place */
17600
memcpy((char*) ref_pointer_array,
17601
(char*) rollup.ref_pointer_arrays[i],
17602
ref_pointer_array_size);
17603
if ((!having || having->val_int()))
17607
List_iterator_fast<Item> it(rollup.fields[i]);
17608
while ((item= it++))
17610
if (item->type() == Item::NULL_ITEM && item->is_result_field())
17611
item->save_in_result_field(1);
17613
copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17614
if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17616
if (create_myisam_from_heap(thd, table_arg,
17617
tmp_table_param.start_recinfo,
17618
&tmp_table_param.recinfo,
17624
/* Restore ref_pointer_array */
17625
set_items_ref_array(current_ref_pointer_array);
17630
clear results if there are not rows found for group
17631
(end_send_group/end_write_group)
17636
clear_tables(this);
17637
copy_fields(&tmp_table_param);
17641
Item_sum *func, **func_ptr= sum_funcs;
17642
while ((func= *(func_ptr++)))
17650
Send a description about what how the select will be done to stdout.
17653
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17654
bool distinct,const char *message)
17656
List<Item> field_list;
17657
List<Item> item_list;
17658
THD *thd=join->thd;
17659
select_result *result=join->result;
17660
Item *item_null= new Item_null();
17661
CHARSET_INFO *cs= system_charset_info;
17663
/* Don't log this into the slow query log */
17664
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17665
join->unit->offset_limit_cnt= 0;
17668
NOTE: the number/types of items pushed into item_list must be in sync with
17669
EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17673
item_list.push_back(new Item_int((int32)
17674
join->select_lex->select_number));
17675
item_list.push_back(new Item_string(join->select_lex->type,
17676
strlen(join->select_lex->type), cs));
17677
for (uint i=0 ; i < 7; i++)
17678
item_list.push_back(item_null);
17679
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17680
item_list.push_back(item_null);
17682
item_list.push_back(new Item_string(message,strlen(message),cs));
17683
if (result->send_data(item_list))
17686
else if (join->select_lex == join->unit->fake_select_lex)
17689
here we assume that the query will return at least two rows, so we
17690
show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17691
and no filesort will be actually done, but executing all selects in
17692
the UNION to provide precise EXPLAIN information will hardly be
17695
char table_name_buffer[NAME_LEN];
17698
item_list.push_back(new Item_null);
17700
item_list.push_back(new Item_string(join->select_lex->type,
17701
strlen(join->select_lex->type),
17705
SELECT_LEX *sl= join->unit->first_select();
17706
uint len= 6, lastop= 0;
17707
memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
17708
for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
17711
lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
17712
"%u,", sl->select_number);
17714
if (sl || len + lastop >= NAME_LEN)
17716
memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
17722
table_name_buffer[len - 1]= '>'; // change ',' to '>'
17724
item_list.push_back(new Item_string(table_name_buffer, len, cs));
17727
item_list.push_back(new Item_string(join_type_str[JT_ALL],
17728
strlen(join_type_str[JT_ALL]),
17730
/* possible_keys */
17731
item_list.push_back(item_null);
17733
item_list.push_back(item_null);
17735
item_list.push_back(item_null);
17737
item_list.push_back(item_null);
17739
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17740
item_list.push_back(item_null);
17742
item_list.push_back(item_null);
17744
if (join->unit->global_parameters->order_list.first)
17745
item_list.push_back(new Item_string("Using filesort",
17748
item_list.push_back(new Item_string("", 0, cs));
17750
if (result->send_data(item_list))
17755
table_map used_tables=0;
17756
for (uint i=0 ; i < join->tables ; i++)
17758
JOIN_TAB *tab=join->join_tab+i;
17759
TABLE *table=tab->table;
17760
TABLE_LIST *table_list= tab->table->pos_in_table_list;
17762
char buff1[512], buff2[512], buff3[512];
17763
char keylen_str_buf[64];
17764
String extra(buff, sizeof(buff),cs);
17765
char table_name_buffer[NAME_LEN];
17766
String tmp1(buff1,sizeof(buff1),cs);
17767
String tmp2(buff2,sizeof(buff2),cs);
17768
String tmp3(buff3,sizeof(buff3),cs);
17777
item_list.push_back(new Item_uint((uint32)
17778
join->select_lex->select_number));
17780
item_list.push_back(new Item_string(join->select_lex->type,
17781
strlen(join->select_lex->type),
17783
if (tab->type == JT_ALL && tab->select && tab->select->quick)
17785
quick_type= tab->select->quick->get_type();
17786
if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
17787
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
17788
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
17789
tab->type = JT_INDEX_MERGE;
17791
tab->type = JT_RANGE;
17794
if (table->derived_select_number)
17796
/* Derived table name generation */
17797
int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
17799
table->derived_select_number);
17800
item_list.push_back(new Item_string(table_name_buffer, len, cs));
17804
TABLE_LIST *real_table= table->pos_in_table_list;
17805
item_list.push_back(new Item_string(real_table->alias,
17806
strlen(real_table->alias),
17809
/* "type" column */
17810
item_list.push_back(new Item_string(join_type_str[tab->type],
17811
strlen(join_type_str[tab->type]),
17813
/* Build "possible_keys" value and add it to item_list */
17814
if (!tab->keys.is_clear_all())
17817
for (j=0 ; j < table->s->keys ; j++)
17819
if (tab->keys.is_set(j))
17823
tmp1.append(table->key_info[j].name,
17824
strlen(table->key_info[j].name),
17825
system_charset_info);
17830
item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
17832
item_list.push_back(item_null);
17834
/* Build "key", "key_len", and "ref" values and add them to item_list */
17835
if (tab->ref.key_parts)
17837
KEY *key_info=table->key_info+ tab->ref.key;
17838
register uint length;
17839
item_list.push_back(new Item_string(key_info->name,
17840
strlen(key_info->name),
17841
system_charset_info));
17842
length= longlong2str(tab->ref.key_length, keylen_str_buf, 10) -
17844
item_list.push_back(new Item_string(keylen_str_buf, length,
17845
system_charset_info));
17846
for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
17850
tmp2.append((*ref)->name(), strlen((*ref)->name()),
17851
system_charset_info);
17853
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17855
else if (tab->type == JT_NEXT)
17857
KEY *key_info=table->key_info+ tab->index;
17858
register uint length;
17859
item_list.push_back(new Item_string(key_info->name,
17860
strlen(key_info->name),cs));
17861
length= longlong2str(key_info->key_length, keylen_str_buf, 10) -
17863
item_list.push_back(new Item_string(keylen_str_buf,
17865
system_charset_info));
17866
item_list.push_back(item_null);
17868
else if (tab->select && tab->select->quick)
17870
tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
17871
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17872
item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
17873
item_list.push_back(item_null);
17877
if (table_list->schema_table &&
17878
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
17880
const char *tmp_buff;
17882
if (table_list->has_db_lookup_value)
17884
f_idx= table_list->schema_table->idx_field1;
17885
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17886
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17888
if (table_list->has_table_lookup_value)
17890
if (table_list->has_db_lookup_value)
17892
f_idx= table_list->schema_table->idx_field2;
17893
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17894
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17897
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17899
item_list.push_back(item_null);
17902
item_list.push_back(item_null);
17903
item_list.push_back(item_null);
17904
item_list.push_back(item_null);
17907
/* Add "rows" field to item_list. */
17908
if (table_list->schema_table)
17911
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17912
item_list.push_back(item_null);
17914
item_list.push_back(item_null);
17918
double examined_rows;
17919
if (tab->select && tab->select->quick)
17920
examined_rows= rows2double(tab->select->quick->records);
17921
else if (tab->type == JT_NEXT || tab->type == JT_ALL)
17922
examined_rows= rows2double(tab->limit ? tab->limit :
17923
tab->table->file->records());
17925
examined_rows= join->best_positions[i].records_read;
17927
item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows,
17928
MY_INT64_NUM_DECIMAL_DIGITS));
17930
/* Add "filtered" field to item_list. */
17931
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17935
f= (float) (100.0 * join->best_positions[i].records_read /
17937
item_list.push_back(new Item_float(f, 2));
17941
/* Build "Extra" field and add it to item_list. */
17942
my_bool key_read=table->key_read;
17943
if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
17944
table->covering_keys.is_set(tab->index))
17946
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
17947
!((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
17951
item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
17952
else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
17954
if (tab->packed_info & TAB_INFO_USING_INDEX)
17955
extra.append(STRING_WITH_LEN("; Using index"));
17956
if (tab->packed_info & TAB_INFO_USING_WHERE)
17957
extra.append(STRING_WITH_LEN("; Using where"));
17958
if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
17959
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
17960
/* Skip initial "; "*/
17961
const char *str= extra.ptr();
17962
uint32 len= extra.length();
17968
item_list.push_back(new Item_string(str, len, cs));
17972
uint keyno= MAX_KEY;
17973
if (tab->ref.key_parts)
17974
keyno= tab->ref.key;
17975
else if (tab->select && tab->select->quick)
17976
keyno = tab->select->quick->index;
17978
if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
17979
table->file->pushed_idx_cond)
17980
extra.append(STRING_WITH_LEN("; Using index condition"));
17982
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
17983
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
17984
quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
17986
extra.append(STRING_WITH_LEN("; Using "));
17987
tab->select->quick->add_info_string(&extra);
17991
if (tab->use_quick == 2)
17993
/* 4 bits per 1 hex digit + terminating '\0' */
17994
char buf[MAX_KEY / 4 + 1];
17995
extra.append(STRING_WITH_LEN("; Range checked for each "
17996
"record (index map: 0x"));
17997
extra.append(tab->keys.print(buf));
18000
else if (tab->select->cond)
18002
const COND *pushed_cond= tab->table->file->pushed_cond;
18004
if (thd->variables.engine_condition_pushdown && pushed_cond)
18006
extra.append(STRING_WITH_LEN("; Using where with pushed "
18008
if (thd->lex->describe & DESCRIBE_EXTENDED)
18010
extra.append(STRING_WITH_LEN(": "));
18011
((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
18015
extra.append(STRING_WITH_LEN("; Using where"));
18020
if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
18021
extra.append(STRING_WITH_LEN("; Using index for group-by"));
18023
extra.append(STRING_WITH_LEN("; Using index"));
18025
if (table->reginfo.not_exists_optimize)
18026
extra.append(STRING_WITH_LEN("; Not exists"));
18028
if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
18029
!(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
18030
HA_MRR_USE_DEFAULT_IMPL))
18032
extra.append(STRING_WITH_LEN("; Using MRR"));
18035
if (table_list->schema_table &&
18036
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18038
if (!table_list->table_open_method)
18039
extra.append(STRING_WITH_LEN("; Skip_open_table"));
18040
else if (table_list->table_open_method == OPEN_FRM_ONLY)
18041
extra.append(STRING_WITH_LEN("; Open_frm_only"));
18043
extra.append(STRING_WITH_LEN("; Open_full_table"));
18044
if (table_list->has_db_lookup_value &&
18045
table_list->has_table_lookup_value)
18046
extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18047
else if (table_list->has_db_lookup_value ||
18048
table_list->has_table_lookup_value)
18049
extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18051
extra.append(STRING_WITH_LEN("; Scanned all databases"));
18053
if (need_tmp_table)
18056
extra.append(STRING_WITH_LEN("; Using temporary"));
18061
extra.append(STRING_WITH_LEN("; Using filesort"));
18063
if (distinct & test_all_bits(used_tables,thd->used_tables))
18064
extra.append(STRING_WITH_LEN("; Distinct"));
18066
if (tab->insideout_match_tab)
18068
extra.append(STRING_WITH_LEN("; LooseScan"));
18071
if (tab->flush_weedout_table)
18072
extra.append(STRING_WITH_LEN("; Start temporary"));
18073
else if (tab->check_weed_out_table)
18074
extra.append(STRING_WITH_LEN("; End temporary"));
18075
else if (tab->do_firstmatch)
18077
extra.append(STRING_WITH_LEN("; FirstMatch("));
18078
TABLE *prev_table=tab->do_firstmatch->table;
18079
if (prev_table->derived_select_number)
18081
char namebuf[NAME_LEN];
18082
/* Derived table name generation */
18083
int len= snprintf(namebuf, sizeof(namebuf)-1,
18085
prev_table->derived_select_number);
18086
extra.append(namebuf, len);
18089
extra.append(prev_table->pos_in_table_list->alias);
18090
extra.append(STRING_WITH_LEN(")"));
18093
for (uint part= 0; part < tab->ref.key_parts; part++)
18095
if (tab->ref.cond_guards[part])
18097
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18102
if (i > 0 && tab[-1].next_select == sub_select_cache)
18103
extra.append(STRING_WITH_LEN("; Using join buffer"));
18105
/* Skip initial "; "*/
18106
const char *str= extra.ptr();
18107
uint32 len= extra.length();
18113
item_list.push_back(new Item_string(str, len, cs));
18115
// For next iteration
18116
used_tables|=table->map;
18117
if (result->send_data(item_list))
18121
for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18123
unit= unit->next_unit())
18125
if (mysql_explain_union(thd, unit, result))
18132
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18135
SELECT_LEX *first= unit->first_select();
18137
for (SELECT_LEX *sl= first;
18139
sl= sl->next_select())
18141
// drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18142
uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18143
sl->type= (((&thd->lex->select_lex)==sl)?
18144
(sl->first_inner_unit() || sl->next_select() ?
18145
"PRIMARY" : "SIMPLE"):
18147
((sl->linkage == DERIVED_TABLE_TYPE) ?
18149
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18150
"DEPENDENT SUBQUERY":
18151
(uncacheable?"UNCACHEABLE SUBQUERY":
18153
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18155
uncacheable?"UNCACHEABLE UNION":
18157
sl->options|= SELECT_DESCRIBE;
18159
if (unit->is_union())
18161
unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18162
unit->fake_select_lex->type= "UNION RESULT";
18163
unit->fake_select_lex->options|= SELECT_DESCRIBE;
18164
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18166
res|= unit->cleanup();
18170
thd->lex->current_select= first;
18171
unit->set_limit(unit->global_parameters);
18172
res= mysql_select(thd, &first->ref_pointer_array,
18173
(TABLE_LIST*) first->table_list.first,
18174
first->with_wild, first->item_list,
18176
first->order_list.elements +
18177
first->group_list.elements,
18178
(ORDER*) first->order_list.first,
18179
(ORDER*) first->group_list.first,
18181
(ORDER*) thd->lex->proc_list.first,
18182
first->options | thd->options | SELECT_DESCRIBE,
18183
result, unit, first);
18185
return(res || thd->is_error());
18189
static void print_table_array(THD *thd, String *str, TABLE_LIST **table,
18192
(*table)->print(thd, str, QT_ORDINARY);
18194
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18196
TABLE_LIST *curr= *tbl;
6328
18197
if (curr->outer_join)
6330
18199
/* MySQL converts right to left joins */