20
20
mysql_select and join optimization
23
22
@defgroup Query_Optimizer Query Optimizer
26
#include <drizzled/server_includes.h>
27
#include <drizzled/sql_select.h>
29
#include <mysys/my_bit.h>
30
#include <drizzled/drizzled_error_messages.h>
32
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
33
"MAYBE_REF","ALL","range","index",
34
"ref_or_null","unique_subquery","index_subquery",
38
struct st_sargable_param;
40
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
41
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
42
DYNAMIC_ARRAY *keyuse);
43
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
45
uint tables, COND *conds,
46
COND_EQUAL *cond_equal,
47
table_map table_map, SELECT_LEX *select_lex,
48
st_sargable_param **sargables);
49
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
50
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
51
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
52
table_map used_tables);
53
static bool choose_plan(JOIN *join,table_map join_tables);
55
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
56
table_map remaining_tables, uint idx,
57
double record_count, double read_time);
58
static void optimize_straight_join(JOIN *join, table_map join_tables);
59
static bool greedy_search(JOIN *join, table_map remaining_tables,
60
uint depth, uint prune_level);
61
static bool best_extension_by_limited_search(JOIN *join,
62
table_map remaining_tables,
63
uint idx, double record_count,
64
double read_time, uint depth,
66
static uint determine_search_depth(JOIN* join);
67
static int join_tab_cmp(const void* ptr1, const void* ptr2);
68
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
70
TODO: 'find_best' is here only temporarily until 'greedy_search' is
73
static bool find_best(JOIN *join,table_map rest_tables,uint index,
74
double record_count,double read_time);
75
static uint cache_record_length(JOIN *join,uint index);
76
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
77
static bool get_best_combination(JOIN *join);
78
static store_key *get_store_key(THD *thd,
79
KEYUSE *keyuse, table_map used_tables,
80
KEY_PART_INFO *key_part, uchar *key_buff,
82
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
83
static void make_outerjoin_info(JOIN *join);
84
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
85
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
86
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
87
static void update_depend_map(JOIN *join);
88
static void update_depend_map(JOIN *join, ORDER *order);
89
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
90
bool change_list, bool *simple_order);
91
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
92
List<Item> &fields, bool send_row,
93
uint64_t select_options, const char *info,
95
static COND *build_equal_items(THD *thd, COND *cond,
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/plugin/info_schema_table.h"
42
#include "drizzled/item/cache.h"
43
#include "drizzled/item/cmpfunc.h"
44
#include "drizzled/item/copy_string.h"
45
#include "drizzled/item/uint.h"
46
#include "drizzled/cached_item.h"
47
#include "drizzled/sql_base.h"
48
#include "drizzled/field/blob.h"
49
#include "drizzled/check_stack_overrun.h"
50
#include "drizzled/lock.h"
51
#include "drizzled/item/outer_ref.h"
52
#include "drizzled/index_hint.h"
53
#include "drizzled/memory/multi_malloc.h"
54
#include "drizzled/records.h"
55
#include "drizzled/internal/iocache.h"
57
#include "drizzled/sql_union.h"
58
#include "drizzled/optimizer/key_field.h"
59
#include "drizzled/optimizer/position.h"
60
#include "drizzled/optimizer/sargable_param.h"
61
#include "drizzled/optimizer/key_use.h"
62
#include "drizzled/optimizer/range.h"
63
#include "drizzled/optimizer/quick_range_select.h"
64
#include "drizzled/optimizer/quick_ror_intersect_select.h"
67
using namespace drizzled;
70
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
71
static COND *build_equal_items(Session *session, COND *cond,
96
72
COND_EQUAL *inherited,
97
List<TABLE_LIST> *join_list,
73
List<TableList> *join_list,
98
74
COND_EQUAL **cond_equal_ref);
99
static COND* substitute_for_best_equal_field(COND *cond,
100
COND_EQUAL *cond_equal,
101
void *table_join_idx);
102
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
103
COND *conds, bool top, bool in_sj);
104
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
105
static void restore_prev_nj_state(JOIN_TAB *last);
106
static void reset_nj_counters(List<TABLE_LIST> *join_list);
107
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
111
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
112
static void restore_prev_sj_state(const table_map remaining_tables,
113
const JOIN_TAB *tab);
115
static COND *optimize_cond(JOIN *join, COND *conds,
116
List<TABLE_LIST> *join_list,
117
Item::cond_result *cond_value);
118
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
119
static bool open_tmp_table(TABLE *table);
120
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
121
MI_COLUMNDEF *start_recinfo,
122
MI_COLUMNDEF **recinfo,
124
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
126
static enum_nested_loop_state
127
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
129
static enum_nested_loop_state
130
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
131
static enum_nested_loop_state
132
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
133
static enum_nested_loop_state
134
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
135
static enum_nested_loop_state
136
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
137
static enum_nested_loop_state
138
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
139
static enum_nested_loop_state
140
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
142
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
143
static int join_read_system(JOIN_TAB *tab);
144
static int join_read_const(JOIN_TAB *tab);
145
static int join_read_key(JOIN_TAB *tab);
146
static int join_read_always_key(JOIN_TAB *tab);
147
static int join_read_last_key(JOIN_TAB *tab);
148
static int join_no_more_records(READ_RECORD *info);
149
static int join_read_next(READ_RECORD *info);
150
static int join_read_next_different(READ_RECORD *info);
151
static int join_init_quick_read_record(JOIN_TAB *tab);
152
static int test_if_quick_select(JOIN_TAB *tab);
153
static int join_init_read_record(JOIN_TAB *tab);
154
static int join_read_first(JOIN_TAB *tab);
155
static int join_read_next_same(READ_RECORD *info);
156
static int join_read_next_same_diff(READ_RECORD *info);
157
static int join_read_last(JOIN_TAB *tab);
158
static int join_read_prev_same(READ_RECORD *info);
159
static int join_read_prev(READ_RECORD *info);
160
int join_read_always_key_or_null(JOIN_TAB *tab);
161
int join_read_next_same_or_null(READ_RECORD *info);
162
static COND *make_cond_for_table(COND *cond,table_map table,
163
table_map used_table,
164
bool exclude_expensive_cond);
165
static Item* part_of_refkey(TABLE *form,Field *field);
166
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
167
ha_rows select_limit, bool no_changes,
169
static bool list_contains_unique_index(TABLE *table,
170
bool (*find_func) (Field *, void *), void *data);
171
static bool find_field_in_item_list (Field *field, void *data);
172
static bool find_field_in_order_list (Field *field, void *data);
173
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
174
ha_rows filesort_limit, ha_rows select_limit,
176
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
178
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
179
ulong offset,Item *having);
180
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
181
uint field_count, Field **first_field,
183
ulong key_length,Item *having);
184
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
185
static ulong used_blob_length(CACHE_FIELD **ptr);
186
static bool store_record_in_cache(JOIN_CACHE *cache);
187
static void reset_cache_read(JOIN_CACHE *cache);
188
static void reset_cache_write(JOIN_CACHE *cache);
189
static void read_cached_record(JOIN_TAB *tab);
190
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
191
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
192
ORDER *order, List<Item> &fields,
193
List<Item> &all_fields,
194
bool *all_order_by_fields_used);
195
static bool test_if_subpart(ORDER *a,ORDER *b);
196
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
197
static void calc_group_buffer(JOIN *join,ORDER *group);
198
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
199
static bool alloc_group_fields(JOIN *join,ORDER *group);
200
// Create list for using with tempory table
201
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
202
List<Item> &new_list1,
203
List<Item> &new_list2,
204
uint elements, List<Item> &items);
205
// Create list for using with tempory table
206
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
207
List<Item> &new_list1,
208
List<Item> &new_list2,
209
uint elements, List<Item> &items);
210
static void init_tmptable_sum_functions(Item_sum **func);
211
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
212
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
213
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
214
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
215
static bool init_sum_functions(Item_sum **func, Item_sum **end);
216
static bool update_sum_func(Item_sum **func);
217
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
218
bool distinct, const char *message=NullS);
219
static Item *remove_additional_cond(Item* conds);
220
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
221
static bool test_if_ref(Item_field *left_item,Item *right_item);
222
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
223
Item *new_cond, bool fix_fields);
76
static Item* part_of_refkey(Table *form,Field *field);
77
static bool cmp_buffer_with_ref(JoinTable *tab);
78
static void change_cond_ref_to_const(Session *session,
79
vector<COND_CMP>& save_list,
84
static bool copy_blobs(Field **ptr);
86
static bool eval_const_cond(COND *cond)
88
return ((Item_func*) cond)->val_int() ? true : false;
226
92
This is used to mark equalities that were made from i-th IN-equality.
385
255
ref->outer_ref= new_ref;
386
256
ref->ref= &ref->outer_ref;
388
if (!ref->fixed && ref->fix_fields(thd, 0))
258
if (!ref->fixed && ref->fix_fields(session, 0))
390
thd->used_tables|= item->used_tables();
260
session->used_tables|= item->used_tables();
395
#define MAGIC_IN_WHERE_TOP_LEVEL 10
397
Function to setup clauses without sum functions.
399
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
403
List<Item> &all_fields,
406
ORDER *group, bool *hidden_group_fields)
409
nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
411
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
412
res= setup_conds(thd, tables, leaves, conds);
414
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
415
res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
417
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
418
res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
419
group, hidden_group_fields);
420
thd->lex->allow_sum_func= save_allow_sum_func;
424
265
/*****************************************************************************
425
266
Check fields, find best join, do the select and output fields.
426
267
mysql_select assumes that all tables are already opened
427
268
*****************************************************************************/
430
Prepare of whole select (including sub queries in future).
433
Add check of calculation of GROUP functions and fields:
434
SELECT COUNT(*)+table.col1 from table1;
442
JOIN::prepare(Item ***rref_pointer_array,
443
TABLE_LIST *tables_init,
444
uint wild_num, COND *conds_init, uint og_num,
445
ORDER *order_init, ORDER *group_init,
447
ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
448
SELECT_LEX_UNIT *unit_arg)
450
// to prevent double initialization on EXPLAIN
456
group_list= group_init;
458
proc_param= proc_param_init;
459
tables_list= tables_init;
460
select_lex= select_lex_arg;
461
select_lex->join= this;
462
join_list= &select_lex->top_join_list;
463
union_part= unit_arg->is_union();
465
thd->lex->current_select->is_item_list_lookup= 1;
467
If we have already executed SELECT, then it have not sense to prevent
468
its table from update (see unique_table())
470
if (thd->derived_tables_processing)
471
select_lex->exclude_from_table_unique_test= true;
473
/* Check that all tables, fields, conds and order are ok */
475
if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
476
setup_tables_and_check_access(thd, &select_lex->context, join_list,
477
tables_list, &select_lex->leaf_tables,
481
TABLE_LIST *table_ptr;
482
for (table_ptr= select_lex->leaf_tables;
484
table_ptr= table_ptr->next_leaf)
487
if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
488
select_lex->setup_ref_array(thd, og_num) ||
489
setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
491
setup_without_group(thd, (*rref_pointer_array), tables_list,
492
select_lex->leaf_tables, fields_list,
493
all_fields, &conds, order, group_list,
494
&hidden_group_fields))
495
return(-1); /* purecov: inspected */
497
ref_pointer_array= *rref_pointer_array;
501
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
502
thd->where="having clause";
503
thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
504
select_lex->having_fix_field= 1;
505
bool having_fix_rc= (!having->fixed &&
506
(having->fix_fields(thd, &having) ||
507
having->check_cols(1)));
508
select_lex->having_fix_field= 0;
509
if (having_fix_rc || thd->is_error())
510
return(-1); /* purecov: inspected */
511
thd->lex->allow_sum_func= save_allow_sum_func;
515
Item_subselect *subselect;
516
Item_in_subselect *in_subs= NULL;
518
Are we in a subquery predicate?
519
TODO: the block below will be executed for every PS execution without need.
521
if ((subselect= select_lex->master_unit()->item))
523
bool do_semijoin= !test(thd->variables.optimizer_switch &
524
OPTIMIZER_SWITCH_NO_SEMIJOIN);
525
if (subselect->substype() == Item_subselect::IN_SUBS)
526
in_subs= (Item_in_subselect*)subselect;
529
Check if we're in subquery that is a candidate for flattening into a
530
semi-join (which is done done in flatten_subqueries()). The
532
1. Subquery predicate is an IN/=ANY subq predicate
533
2. Subquery is a single SELECT (not a UNION)
534
3. Subquery does not have GROUP BY or ORDER BY
535
4. Subquery does not use aggregate functions or HAVING
536
5. Subquery predicate is at the AND-top-level of ON/WHERE clause
537
6. No execution method was already chosen (by a prepared statement).
539
(*). We are not in a subquery of a single table UPDATE/DELETE that
540
doesn't have a JOIN (TODO: We should handle this at some
541
point by switching to multi-table UPDATE/DELETE)
543
(**). We're not in a confluent table-less subquery, like
547
!select_lex->master_unit()->first_select()->next_select() && // 2
548
!select_lex->group_list.elements && !order && // 3
549
!having && !select_lex->with_sum_func && // 4
550
thd->thd_marker && // 5
551
select_lex->outer_select()->join && // (*)
552
select_lex->master_unit()->first_select()->leaf_tables && // (**)
554
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
557
if (!in_subs->left_expr->fixed &&
558
in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
563
Check that the right part of the subselect contains no more than one
564
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
566
if (subselect->substype() == Item_subselect::IN_SUBS &&
567
(select_lex->item_list.elements !=
568
((Item_in_subselect*)subselect)->left_expr->cols()))
570
my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
575
/* Register the subquery for further processing */
576
select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
577
in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
581
bool do_materialize= !test(thd->variables.optimizer_switch &
582
OPTIMIZER_SWITCH_NO_MATERIALIZATION);
584
Check if the subquery predicate can be executed via materialization.
585
The required conditions are:
586
1. Subquery predicate is an IN/=ANY subq predicate
587
2. Subquery is a single SELECT (not a UNION)
588
3. Subquery is not a table-less query. In this case there is no
589
point in materializing.
590
4. Subquery predicate is a top-level predicate
591
(this implies it is not negated)
592
TODO: this is a limitation that should be lifeted once we
593
implement correct NULL semantics (WL#3830)
594
5. Subquery is non-correlated
596
This is an overly restrictive condition. It can be extended to:
597
(Subquery is non-correlated ||
598
Subquery is correlated to any query outer to IN predicate ||
599
(Subquery is correlated to the immediate outer query &&
600
Subquery !contains {GROUP BY, ORDER BY [LIMIT],
601
aggregate functions) && subquery predicate is not under "NOT IN"))
602
6. No execution method was already chosen (by a prepared statement).
604
(*) The subquery must be part of a SELECT statement. The current
605
condition also excludes multi-table update statements.
607
We have to determine whether we will perform subquery materialization
608
before calling the IN=>EXISTS transformation, so that we know whether to
609
perform the whole transformation or only that part of it which wraps
610
Item_in_subselect in an Item_in_optimizer.
612
if (do_materialize &&
614
!select_lex->master_unit()->first_select()->next_select() && // 2
615
select_lex->master_unit()->first_select()->leaf_tables && // 3
616
thd->lex->sql_command == SQLCOM_SELECT) // *
618
if (in_subs->is_top_level_item() && // 4
619
!in_subs->is_correlated && // 5
620
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
621
in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
624
Item_subselect::trans_res trans_res;
625
if ((trans_res= subselect->select_transformer(this)) !=
626
Item_subselect::RES_OK)
628
return((trans_res == Item_subselect::RES_ERROR));
637
for (ord= order; ord; ord= ord->next)
639
Item *item= *ord->item;
640
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
641
item->split_sum_func(thd, ref_pointer_array, all_fields);
645
if (having && having->with_sum_func)
646
having->split_sum_func2(thd, ref_pointer_array, all_fields,
648
if (select_lex->inner_sum_func_list)
650
Item_sum *end=select_lex->inner_sum_func_list;
651
Item_sum *item_sum= end;
654
item_sum= item_sum->next;
655
item_sum->split_sum_func2(thd, ref_pointer_array,
656
all_fields, item_sum->ref_by, false);
657
} while (item_sum != end);
660
if (select_lex->inner_refs_list.elements &&
661
fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
665
Check if there are references to un-aggregated columns when computing
666
aggregate functions with implicit grouping (there is no GROUP BY).
668
MODE_ONLY_FULL_GROUP_BY is enabled here by default
670
if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
672
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
673
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
677
/* Caclulate the number of groups */
679
for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
684
goto err; /* purecov: inspected */
686
if (result && result->prepare(fields_list, unit_arg))
687
goto err; /* purecov: inspected */
689
/* Init join struct */
690
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
691
ref_pointer_array_size= all_fields.elements*sizeof(Item*);
692
this->group= group_list != 0;
695
#ifdef RESTRICTED_GROUP
696
if (sum_func_count && !group_list && (func_count || field_count))
698
my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
702
if (select_lex->olap == ROLLUP_TYPE && rollup_init())
704
if (alloc_func_list())
710
return(-1); /* purecov: inspected */
715
Remove the predicates pushed down into the subquery
718
JOIN::remove_subq_pushed_predicates()
719
where IN Must be NULL
720
OUT The remaining WHERE condition, or NULL
723
Given that this join will be executed using (unique|index)_subquery,
724
without "checking NULL", remove the predicates that were pushed down
727
If the subquery compares scalar values, we can remove the condition that
728
was wrapped into trig_cond (it will be checked when needed by the subquery
731
If the subquery compares row values, we need to keep the wrapped
732
equalities in the WHERE clause: when the left (outer) tuple has both NULL
733
and non-NULL values, we'll do a full table scan and will rely on the
734
equalities corresponding to non-NULL parts of left tuple to filter out
735
non-matching records.
737
TODO: We can remove the equalities that will be guaranteed to be true by the
738
fact that subquery engine will be using index lookup. This must be done only
739
for cases where there are no conversion errors of significance, e.g. 257
740
that is searched in a byte. But this requires homogenization of the return
741
codes of all Field*::store() methods.
744
void JOIN::remove_subq_pushed_predicates(Item **where)
746
if (conds->type() == Item::FUNC_ITEM &&
747
((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
748
((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
749
((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
750
test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
751
((Item_func *)conds)->arguments()[0]))
760
271
Index lookup-based subquery: save some flags for EXPLAIN output
799
Check if the table's rowid is included in the temptable
802
sj_table_is_included()
804
join_tab The table to be checked
807
SemiJoinDuplicateElimination: check the table's rowid should be included
808
in the temptable. This is so if
810
1. The table is not embedded within some semi-join nest
811
2. The has been pulled out of a semi-join nest, or
813
3. The table is functionally dependent on some previous table
815
[4. This is also true for constant tables that can't be
816
NULL-complemented but this function is not called for such tables]
819
true - Include table's rowid
823
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
825
if (join_tab->emb_sj_nest)
828
/* Check if this table is functionally dependent on the tables that
829
are within the same outer join nest
831
TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
832
if (join_tab->type == JT_EQ_REF)
834
Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
836
while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
838
JOIN_TAB *ref_tab= join->join_tab + idx;
839
if (embedding == ref_tab->table->pos_in_table_list->embedding)
842
/* Ok, functionally dependent */
845
/* Not functionally dependent => need to include*/
850
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
851
SJ_TMP_TABLE *sjtbl);
855
Setup the strategies to eliminate semi-join duplicates.
858
setup_semijoin_dups_elimination()
860
options Join options (needed to see if join buffering will be
862
no_jbuf_after Another bit of information re where join buffering will
866
Setup the strategies to eliminate semi-join duplicates. ATM there are 3
869
1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
871
2. FirstMatch (pick only the 1st matching row combination of inner tables)
872
3. InsideOut (scanning the sj-inner table in a way that groups duplicates
873
together and picking the 1st one)
875
The join order has "duplicate-generating ranges", and every range is
876
served by one strategy or a combination of FirstMatch with with some
879
"Duplicate-generating range" is defined as a range within the join order
880
that contains all of the inner tables of a semi-join. All ranges must be
881
disjoint, if tables of several semi-joins are interleaved, then the ranges
882
are joined together, which is equivalent to converting
883
SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
885
SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
888
Applicability conditions are as follows:
890
DuplicateWeedout strategy
891
~~~~~~~~~~~~~~~~~~~~~~~~~
893
(ot|nt)* [ it ((it|ot|nt)* (it|ot))] (nt)*
894
+------+ +=========================+ +---+
897
(1) - Prefix of OuterTables (those that participate in
898
IN-equality and/or are correlated with subquery) and outer
899
Noncorrelated Tables.
900
(2) - The handled range. The range starts with the first sj-inner
901
table, and covers all sj-inner and outer tables
902
Within the range, Inner, Outer, outer Noncorrelated tables
903
may follow in any order.
904
(3) - The suffix of outer Noncorrelated tables.
909
(ot|nt)* [ it ((it|nt)* it) ] (nt)*
910
+------+ +==================+ +---+
913
(1) - Prefix of outer and non-correlated tables
914
(2) - The handled range, which may contain only inner and
915
non-correlated tables.
916
(3) - The suffix of outer Noncorrelated tables.
921
(ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ] (ot|nt)*
922
+--------+ +===========+ +=============+ +------+
925
(1) - Prefix that may contain any outer tables. The prefix must contain
926
all the non-trivially correlated outer tables. (non-trivially means
927
that the correlation is not just through the IN-equality).
929
(2) - Inner table for which the InsideOut scan is performed.
931
(3) - The remainder of the duplicate-generating range. It is served by
932
application of FirstMatch strategy, with the exception that
933
outer IN-correlated tables are considered to be non-correlated.
935
(4) - THe suffix of outer and outer non-correlated tables.
937
If several strategies are applicable, their relative priorities are:
942
This function walks over the join order and sets up the strategies by
943
setting appropriate members in join_tab structures.
947
true Out of memory error
951
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
953
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
956
0 - invalid (EOF marker),
958
2 - Temptable (maybe confluent),
959
3 - Temptable with join buffering
962
uint start_idx; /* Left range bound */
963
uint end_idx; /* Right range bound */
965
For Temptable strategy: Bitmap of all outer and correlated tables from
966
all involved join nests.
968
table_map outer_tables;
969
} dups_ranges [MAX_TABLES];
971
TABLE_LIST *emb_insideout_nest= NULL;
972
table_map emb_sj_map= 0; /* A bitmap of sj-nests (that is, their sj-inner
973
tables) whose ranges we're in */
974
table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
975
table_map range_start_map= 0; /* table_map at current range start */
976
bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
981
First pass: locate the duplicate-generating ranges and pick the strategies.
983
for (i=join->const_tables ; i < join->tables ; i++)
985
JOIN_TAB *tab=join->join_tab+i;
986
TABLE *table=tab->table;
987
cur_map |= table->map;
989
if (tab->emb_sj_nest) // Encountered an sj-inner table
993
dups_ranges[cur_range].start_idx= i;
994
range_start_map= cur_map & ~table->map;
996
Remember if this is a possible start of range that is covered by
997
the InsideOut strategy (the reason that it is not covered could
998
be that it overlaps with anther semi-join's range. we don't
999
support InsideOut for joined ranges)
1001
if (join->best_positions[i].use_insideout_scan)
1002
emb_insideout_nest= tab->emb_sj_nest;
1005
emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1006
emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1008
if (tab->emb_sj_nest != emb_insideout_nest)
1011
Two different semi-joins interleave. This cannot be handled by
1014
emb_insideout_nest= NULL;
1018
if (emb_sj_map) /* We're in duplicate-generating range */
1020
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1021
tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1022
i <= no_jbuf_after && !dealing_with_jbuf)
1025
This table uses join buffering, which makes use of FirstMatch or
1026
InsideOut strategies impossible for the current and (we assume)
1027
preceding duplicate-producing ranges.
1028
That is, for the join order:
1030
x x [ x x] x [x x x] x [x x X* x] x
1032
+-----+ +-----+ | join buffering use
1035
we'll have to remove r1 and r2 and use duplicate-elimination
1036
strategy that spans all the tables, starting from the very 1st
1039
dealing_with_jbuf= true;
1040
emb_insideout_nest= false;
1043
Absorb all preceding duplicate-eliminating ranges. Their strategies
1046
for (int prev_range= 0; prev_range < cur_range; prev_range++)
1048
dups_ranges[cur_range].outer_tables |=
1049
dups_ranges[prev_range].outer_tables;
1051
dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1052
dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1057
Check if we are at the end of duplicate-producing range. We are if
1059
1. It's an InsideOut range (which presumes all correlated tables are
1060
in the prefix), and all inner tables are in the join order prefix,
1062
2. It's a DuplicateElimination range (possibly covering several
1063
SJ-nests), and all inner, outer, and correlated tables of all
1064
sj-nests are in the join order prefix.
1066
bool end_of_range= false;
1067
if (emb_insideout_nest &&
1068
bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1070
/* Save that this range is handled with InsideOut: */
1071
dups_ranges[cur_range].strategy= 1;
1074
else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1077
This is a complete range to be handled with either DuplicateWeedout
1080
dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1082
This will hold tables from within the range that need to be put
1083
into the join buffer before we can use the FirstMatch on its tail.
1085
dups_ranges[cur_range].outer_tables= emb_outer_tables &
1092
dups_ranges[cur_range].end_idx= i+1;
1093
emb_sj_map= emb_outer_tables= 0;
1094
emb_insideout_nest= NULL;
1095
dealing_with_jbuf= false;
1096
dups_ranges[++cur_range].strategy= 0;
1101
THD *thd= join->thd;
1102
SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1104
Second pass: setup the chosen strategies
1106
for (int j= 0; j < cur_range; j++)
1108
JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1110
if (dups_ranges[j].strategy == 1) // InsideOut strategy
1112
tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1115
else // DuplicateWeedout strategy
1117
SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1118
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1119
uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1120
uint jt_null_bits= 0; // # null bits in tuple bytes
1121
SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1122
uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1123
JOIN_TAB *last_outer_tab= tab - 1;
1125
Walk through the range and remember
1126
- tables that need their rowids to be put into temptable
1127
- the last outer table
1129
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1131
if (sj_table_is_included(join, tab))
1133
last_tab->join_tab= tab;
1134
last_tab->rowid_offset= jt_rowid_offset;
1135
jt_rowid_offset += tab->table->file->ref_length;
1136
if (tab->table->maybe_null)
1138
last_tab->null_byte= jt_null_bits / 8;
1139
last_tab->null_bit= jt_null_bits++;
1142
tab->table->prepare_for_position();
1143
tab->rowid_keep_flags= rowid_keep_flags;
1145
cur_map |= tab->table->map;
1146
if (!tab->emb_sj_nest && bitmap_covers(cur_map,
1147
dups_ranges[j].outer_tables))
1148
last_outer_tab= tab;
1151
if (jt_rowid_offset) /* Temptable has at least one rowid */
1153
SJ_TMP_TABLE *sjtbl;
1154
uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1155
if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1156
!(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1158
memcpy(sjtbl->tabs, sjtabs, tabs_size);
1159
sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1160
sjtbl->rowid_len= jt_rowid_offset;
1161
sjtbl->null_bits= jt_null_bits;
1162
sjtbl->null_bytes= (jt_null_bits + 7)/8;
1164
*next_sjtbl_ptr= sjtbl;
1165
next_sjtbl_ptr= &(sjtbl->next);
1169
create_duplicate_weedout_tmp_table(thd,
1174
join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1175
join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1177
tab= last_outer_tab + 1;
1178
jump_to= last_outer_tab;
1181
/* Create the FirstMatch tail */
1182
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1184
if (tab->emb_sj_nest)
1185
tab->do_firstmatch= jump_to;
1194
static void cleanup_sj_tmp_tables(JOIN *join)
1196
for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
1197
sj_tbl= sj_tbl->next)
1199
if (sj_tbl->tmp_table)
1201
free_tmp_table(join->thd, sj_tbl->tmp_table);
1204
join->sj_tmp_tables= NULL;
1207
uint make_join_orderinfo(JOIN *join);
1210
global select optimisation.
1213
error code saved in field 'error'
1224
// to prevent double initialization on EXPLAIN
1229
thd_proc_info(thd, "optimizing");
1230
row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1231
unit->select_limit_cnt);
1232
/* select_limit is used to decide if we are likely to scan the whole table */
1233
select_limit= unit->select_limit_cnt;
1234
if (having || (select_options & OPTION_FOUND_ROWS))
1235
select_limit= HA_POS_ERROR;
1236
do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1237
// Ignore errors of execution if option IGNORE present
1238
if (thd->lex->ignore)
1239
thd->lex->current_select->no_error= 1;
1241
#ifdef HAVE_REF_TO_FIELDS // Not done yet
1242
/* Add HAVING to WHERE if possible */
1243
if (having && !group_list && !sum_func_count)
1250
else if ((conds=new Item_cond_and(conds,having)))
1253
Item_cond_and can't be fixed after creation, so we do not check
1256
conds->fix_fields(thd, &conds);
1257
conds->change_ref_to_fields(thd, tables_list);
1258
conds->top_level_item();
1263
SELECT_LEX *sel= thd->lex->current_select;
1264
if (sel->first_cond_optimization)
1267
The following code will allocate the new items in a permanent
1268
MEMROOT for prepared statements and stored procedures.
1270
sel->first_cond_optimization= 0;
1272
/* Convert all outer joins to inner joins if possible */
1273
conds= simplify_joins(this, join_list, conds, true, false);
1274
build_bitmap_for_nested_joins(join_list, 0);
1277
conds= optimize_cond(this, conds, join_list, &cond_value);
1278
if (thd->is_error())
1285
having= optimize_cond(this, having, join_list, &having_value);
1286
if (thd->is_error())
1291
if (select_lex->where)
1292
select_lex->cond_value= cond_value;
1293
if (select_lex->having)
1294
select_lex->having_value= having_value;
1296
if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
1297
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1298
{ /* Impossible cond */
1299
zero_result_cause= having_value == Item::COND_FALSE ?
1300
"Impossible HAVING" : "Impossible WHERE";
1306
/* Optimize count(*), min() and max() */
1307
if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1311
opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1312
to the WHERE conditions,
1313
or 1 if all items were resolved,
1314
or 0, or an error number HA_ERR_...
1316
if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1318
if (res == HA_ERR_KEY_NOT_FOUND)
1320
zero_result_cause= "No matching min/max row";
1331
zero_result_cause= "No matching min/max row";
1335
zero_result_cause= "Select tables optimized away";
1336
tables_list= 0; // All tables resolved
1338
Extract all table-independent conditions and replace the WHERE
1339
clause with them. All other conditions were computed by opt_sum_query
1340
and the MIN/MAX/COUNT function(s) have been replaced by constants,
1341
so there is no need to compute the whole WHERE clause again.
1342
Notice that make_cond_for_table() will always succeed to remove all
1343
computed conditions, because opt_sum_query() is applicable only to
1345
Preserve conditions for EXPLAIN.
1347
if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1349
COND *table_independent_conds=
1350
make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1351
conds= table_independent_conds;
1360
error= -1; // Error is sent to client
1361
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1363
/* Calculate how to do the join */
1364
thd_proc_info(thd, "statistics");
1365
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1366
thd->is_fatal_error)
1371
/* Remove distinct if only const tables */
1372
select_distinct= select_distinct && (const_tables != tables);
1373
thd_proc_info(thd, "preparing");
1374
if (result->initialize_tables(this))
1376
return(1); // error == -1
1378
if (const_table_map != found_const_table_map &&
1379
!(select_options & SELECT_DESCRIBE) &&
1381
!(conds->used_tables() & RAND_TABLE_BIT) ||
1382
select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1384
zero_result_cause= "no matching row in const table";
1388
if (!(thd->options & OPTION_BIG_SELECTS) &&
1389
best_read > (double) thd->variables.max_join_size &&
1390
!(select_options & SELECT_DESCRIBE))
1391
{ /* purecov: inspected */
1392
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1396
if (const_tables && !thd->locked_tables &&
1397
!(select_options & SELECT_NO_UNLOCK))
1398
mysql_unlock_some_tables(thd, table, const_tables);
1399
if (!conds && outer_join)
1401
/* Handle the case where we have an OUTER JOIN without a WHERE */
1402
conds=new Item_int((int64_t) 1,1); // Always true
1404
select= make_select(*table, const_table_map,
1405
const_table_map, conds, 1, &error);
1407
{ /* purecov: inspected */
1408
error= -1; /* purecov: inspected */
1412
reset_nj_counters(join_list);
1413
make_outerjoin_info(this);
1416
Among the equal fields belonging to the same multiple equality
1417
choose the one that is to be retrieved first and substitute
1418
all references to these in where condition for a reference for
1423
conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1424
conds->update_used_tables();
1428
Permorm the the optimization on fields evaluation mentioned above
1429
for all on expressions.
1431
for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1433
if (*tab->on_expr_ref)
1435
*tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1438
(*tab->on_expr_ref)->update_used_tables();
1442
if (conds &&!outer_join && const_table_map != found_const_table_map &&
1443
(select_options & SELECT_DESCRIBE) &&
1444
select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1446
conds=new Item_int((int64_t) 0,1); // Always false
1448
if (make_join_select(this, select, conds))
1451
"Impossible WHERE noticed after reading const tables";
1452
return(0); // error == 0
1455
error= -1; /* if goto err */
1457
/* Optimize distinct away if possible */
1459
ORDER *org_order= order;
1460
order=remove_const(this, order,conds,1, &simple_order);
1461
if (thd->is_error())
1468
If we are using ORDER BY NULL or ORDER BY const_expression,
1469
return result in any order (even if we are using a GROUP BY)
1471
if (!order && org_order)
1475
Check if we can optimize away GROUP BY/DISTINCT.
1476
We can do that if there are no aggregate functions, the
1477
fields in DISTINCT clause (if present) and/or columns in GROUP BY
1478
(if present) contain direct references to all key parts of
1479
an unique index (in whatever order) and if the key parts of the
1480
unique index cannot contain NULLs.
1481
Note that the unique keys for DISTINCT and GROUP BY should not
1482
be the same (as long as they are unique).
1484
The FROM clause must contain a single non-constant table.
1486
if (tables - const_tables == 1 && (group_list || select_distinct) &&
1487
!tmp_table_param.sum_func_count &&
1488
(!join_tab[const_tables].select ||
1489
!join_tab[const_tables].select->quick ||
1490
join_tab[const_tables].select->quick->get_type() !=
1491
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1494
list_contains_unique_index(join_tab[const_tables].table,
1495
find_field_in_order_list,
1496
(void *) group_list))
1499
We have found that grouping can be removed since groups correspond to
1500
only one row anyway, but we still have to guarantee correct result
1501
order. The line below effectively rewrites the query from GROUP BY
1502
<fields> to ORDER BY <fields>. There are two exceptions:
1503
- if skip_sort_order is set (see above), then we can simply skip
1505
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1506
with the GROUP BY ones, i.e. either one is a prefix of another.
1507
We only check if the ORDER BY is a prefix of GROUP BY. In this case
1508
test_if_subpart() copies the ASC/DESC attributes from the original
1510
If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1513
if (!order || test_if_subpart(group_list, order))
1514
order= skip_sort_order ? 0 : group_list;
1516
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1517
rewritten to IGNORE INDEX FOR ORDER BY(fields).
1519
join_tab->table->keys_in_use_for_order_by=
1520
join_tab->table->keys_in_use_for_group_by;
1524
if (select_distinct &&
1525
list_contains_unique_index(join_tab[const_tables].table,
1526
find_field_in_item_list,
1527
(void *) &fields_list))
1532
if (group_list || tmp_table_param.sum_func_count)
1534
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1537
else if (select_distinct && tables - const_tables == 1)
1540
We are only using one table. In this case we change DISTINCT to a
1542
- The GROUP BY can be done through indexes (no sort) and the ORDER
1543
BY only uses selected fields.
1544
(In this case we can later optimize away GROUP BY and ORDER BY)
1545
- We are scanning the whole table without LIMIT
1547
- We are using CALC_FOUND_ROWS
1548
- We are using an ORDER BY that can't be optimized away.
1550
We don't want to use this optimization when we are using LIMIT
1551
because in this case we can just create a temporary table that
1552
holds LIMIT rows and stop when this table is full.
1554
JOIN_TAB *tab= &join_tab[const_tables];
1555
bool all_order_fields_used;
1557
skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
1558
&tab->table->keys_in_use_for_order_by);
1559
if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1560
order, fields_list, all_fields,
1561
&all_order_fields_used)))
1563
bool skip_group= (skip_sort_order &&
1564
test_if_skip_sort_order(tab, group_list, select_limit, 1,
1565
&tab->table->keys_in_use_for_group_by) != 0);
1566
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1567
if ((skip_group && all_order_fields_used) ||
1568
select_limit == HA_POS_ERROR ||
1569
(order && !skip_sort_order))
1571
/* Change DISTINCT to GROUP BY */
1574
if (all_order_fields_used)
1576
if (order && skip_sort_order)
1579
Force MySQL to read the table in sorted order to get result in
1582
tmp_table_param.quick_group=0;
1586
group=1; // For end_write_group
1591
else if (thd->is_fatal_error) // End of memory
1596
ORDER *old_group_list;
1597
group_list= remove_const(this, (old_group_list= group_list), conds,
1598
rollup.state == ROLLUP::STATE_NONE,
1600
if (thd->is_error())
1605
if (old_group_list && !group_list)
1608
if (!group_list && group)
1610
order=0; // The output has only one row
1612
select_distinct= 0; // No need in distinct for 1 row
1613
group_optimized_away= 1;
1616
calc_group_buffer(this, group_list);
1617
send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1619
if (test_if_subpart(group_list, order) ||
1620
(!group_list && tmp_table_param.sum_func_count))
1623
// Can't use sort on head table if using row cache
1633
Check if we need to create a temporary table.
1634
This has to be done if all tables are not already read (const tables)
1635
and one of the following conditions holds:
1636
- We are using DISTINCT (simple distinct's are already optimized away)
1637
- We are using an ORDER BY or GROUP BY on fields not in the first table
1638
- We are using different ORDER BY and GROUP BY orders
1639
- The user wants us to buffer the result.
1641
need_tmp= (const_tables != tables &&
1642
((select_distinct || !simple_order || !simple_group) ||
1643
(group_list && order) ||
1644
test(select_options & OPTION_BUFFER_RESULT)));
1646
uint no_jbuf_after= make_join_orderinfo(this);
1647
uint64_t select_opts_for_readinfo=
1648
(select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1650
sj_tmp_tables= NULL;
1651
if (!select_lex->sj_nests.is_empty())
1652
setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1655
// No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1656
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1659
/* Create all structures needed for materialized subquery execution. */
1660
if (setup_subquery_materialization())
1664
is this simple IN subquery?
1666
if (!group_list && !order &&
1667
unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1668
tables == 1 && conds &&
1674
if (join_tab[0].type == JT_EQ_REF &&
1675
join_tab[0].ref.items[0]->name == in_left_expr_name)
1677
remove_subq_pushed_predicates(&where);
1678
save_index_subquery_explain_info(join_tab, where);
1679
join_tab[0].type= JT_UNIQUE_SUBQUERY;
1683
subselect_uniquesubquery_engine(thd,
1688
else if (join_tab[0].type == JT_REF &&
1689
join_tab[0].ref.items[0]->name == in_left_expr_name)
1691
remove_subq_pushed_predicates(&where);
1692
save_index_subquery_explain_info(join_tab, where);
1693
join_tab[0].type= JT_INDEX_SUBQUERY;
1697
subselect_indexsubquery_engine(thd,
1704
} else if (join_tab[0].type == JT_REF_OR_NULL &&
1705
join_tab[0].ref.items[0]->name == in_left_expr_name &&
1706
having->name == in_having_cond)
1708
join_tab[0].type= JT_INDEX_SUBQUERY;
1710
conds= remove_additional_cond(conds);
1711
save_index_subquery_explain_info(join_tab, conds);
1713
change_engine(new subselect_indexsubquery_engine(thd,
1723
Need to tell handlers that to play it safe, it should fetch all
1724
columns of the primary key of the tables: this is because MySQL may
1725
build row pointers for the rows, and for all columns of the primary key
1726
the read set has not necessarily been set by the server code.
1728
if (need_tmp || select_distinct || group_list || order)
1730
for (uint i = const_tables; i < tables; i++)
1731
join_tab[i].table->prepare_for_position();
1734
if (const_tables != tables)
1737
Because filesort always does a full table scan or a quick range scan
1738
we must add the removed reference to the select for the table.
1739
We only need to do this when we have a simple_order or simple_group
1740
as in other cases the join is done before the sort.
1742
if ((order || group_list) &&
1743
(join_tab[const_tables].type != JT_ALL) &&
1744
(join_tab[const_tables].type != JT_REF_OR_NULL) &&
1745
((order && simple_order) || (group_list && simple_group)))
1747
if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1752
if (!(select_options & SELECT_BIG_RESULT) &&
1755
!test_if_skip_sort_order(&join_tab[const_tables], group_list,
1756
unit->select_limit_cnt, 0,
1757
&join_tab[const_tables].table->
1758
keys_in_use_for_group_by))) ||
1760
tmp_table_param.quick_group)
1762
need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort
1767
Force using of tmp table if sorting by a SP or UDF function due to
1768
their expensive and probably non-deterministic nature.
1770
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1772
Item *item= *tmp_order->item;
1773
if (item->is_expensive())
1775
/* Force tmp table without sort */
1776
need_tmp=1; simple_order=simple_group=0;
1784
if (select_options & SELECT_DESCRIBE)
1792
The loose index scan access method guarantees that all grouping or
1793
duplicate row elimination (for distinct) is already performed
1794
during data retrieval, and that all MIN/MAX functions are already
1795
computed for each group. Thus all MIN/MAX functions should be
1796
treated as regular functions, and there is no need to perform
1797
grouping in the main execution loop.
1798
Notice that currently loose index scan is applicable only for
1799
single table queries, thus it is sufficient to test only the first
1800
join_tab element of the plan for its access method.
1802
if (join_tab->is_using_loose_index_scan())
1803
tmp_table_param.precomputed_group_by= true;
1805
/* Create a tmp table if distinct or if the sort is too complicated */
1808
thd_proc_info(thd, "Creating tmp table");
1810
init_items_ref_array();
1812
tmp_table_param.hidden_field_count= (all_fields.elements -
1813
fields_list.elements);
1814
ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1817
Pushing LIMIT to the temporary table creation is not applicable
1818
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1819
there are aggregate functions, because in all these cases we need
1822
ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1824
!thd->lex->current_select->with_sum_func) ?
1825
select_limit : HA_POS_ERROR;
1827
if (!(exec_tmp_table1=
1828
create_tmp_table(thd, &tmp_table_param, all_fields,
1830
group_list ? 0 : select_distinct,
1831
group_list && simple_group,
1840
We don't have to store rows in temp table that doesn't match HAVING if:
1841
- we are sorting the table and writing complete group rows to the
1843
- We are using DISTINCT without resolving the distinct as a GROUP BY
1846
If having is not handled here, it will be checked before the row
1847
is sent to the client.
1850
(sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1853
/* if group or order on first table, sort first */
1854
if (group_list && simple_group)
1856
thd_proc_info(thd, "Sorting for group");
1857
if (create_sort_index(thd, this, group_list,
1858
HA_POS_ERROR, HA_POS_ERROR, false) ||
1859
alloc_group_fields(this, group_list) ||
1860
make_sum_func_list(all_fields, fields_list, 1) ||
1861
setup_sum_funcs(thd, sum_funcs))
1869
if (make_sum_func_list(all_fields, fields_list, 0) ||
1870
setup_sum_funcs(thd, sum_funcs))
1875
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1877
thd_proc_info(thd, "Sorting for order");
1878
if (create_sort_index(thd, this, order,
1879
HA_POS_ERROR, HA_POS_ERROR, true))
1888
Optimize distinct when used on some of the tables
1889
SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1890
In this case we can stop scanning t2 when we have found one t1.a
1893
if (exec_tmp_table1->distinct)
1895
table_map used_tables= thd->used_tables;
1896
JOIN_TAB *last_join_tab= join_tab+tables-1;
1899
if (used_tables & last_join_tab->table->map)
1901
last_join_tab->not_used_in_distinct=1;
1902
} while (last_join_tab-- != join_tab);
1903
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1904
if (order && skip_sort_order)
1906
/* Should always succeed */
1907
if (test_if_skip_sort_order(&join_tab[const_tables],
1908
order, unit->select_limit_cnt, 0,
1909
&join_tab[const_tables].table->
1910
keys_in_use_for_order_by))
1916
If this join belongs to an uncacheable subquery save
1919
if (select_lex->uncacheable && !is_top_level_join() &&
1920
init_save_join_tab())
1921
return(-1); /* purecov: inspected */
1930
Restore values in temporary join.
1932
void JOIN::restore_tmp()
1934
memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1941
unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1942
select_lex->offset_limit->val_uint() :
1947
if (exec_tmp_table1)
1949
exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1950
exec_tmp_table1->file->ha_delete_all_rows();
1951
free_io_cache(exec_tmp_table1);
1952
filesort_free_buffers(exec_tmp_table1,0);
1954
if (exec_tmp_table2)
1956
exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1957
exec_tmp_table2->file->ha_delete_all_rows();
1958
free_io_cache(exec_tmp_table2);
1959
filesort_free_buffers(exec_tmp_table2,0);
1962
set_items_ref_array(items0);
1965
memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1970
/* Reset of sum functions */
1973
Item_sum *func, **func_ptr= sum_funcs;
1974
while ((func= *(func_ptr++)))
1982
@brief Save the original join layout
1984
@details Saves the original join layout so it can be reused in
1985
re-execution and for EXPLAIN.
1987
@return Operation status
1989
@retval 1 error occurred.
1993
JOIN::init_save_join_tab()
1995
if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
1996
return 1; /* purecov: inspected */
1997
error= 0; // Ensure that tmp_join.error= 0
2004
JOIN::save_join_tab()
2006
if (!join_tab_save && select_lex->master_unit()->uncacheable)
2008
if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2009
sizeof(JOIN_TAB) * tables)))
2020
Note, that create_sort_index calls test_if_skip_sort_order and may
2021
finally replace sorting with index scan if there is a LIMIT clause in
2022
the query. It's never shown in EXPLAIN!
2025
When can we have here thd->net.report_error not zero?
2030
List<Item> *columns_list= &fields_list;
2033
thd_proc_info(thd, "executing");
2035
(void) result->prepare2(); // Currently, this cannot fail.
2037
if (!tables_list && (tables || !select_lex->with_sum_func))
2038
{ // Only test of functions
2039
if (select_options & SELECT_DESCRIBE)
2040
select_describe(this, false, false, false,
2041
(zero_result_cause?zero_result_cause:"No tables used"));
2044
result->send_fields(*columns_list,
2045
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2047
We have to test for 'conds' here as the WHERE may not be constant
2048
even if we don't have any tables for prepared statements or if
2049
conds uses something like 'rand()'.
2051
if (cond_value != Item::COND_FALSE &&
2052
(!conds || conds->val_int()) &&
2053
(!having || having->val_int()))
2055
if (do_send_rows && result->send_data(fields_list))
2059
error= (int) result->send_eof();
2060
send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2061
thd->sent_row_count);
2066
error=(int) result->send_eof();
2070
/* Single select (without union) always returns 0 or 1 row */
2071
thd->limit_found_rows= send_records;
2072
thd->examined_row_count= 0;
2076
Don't reset the found rows count if there're no tables as
2077
FOUND_ROWS() may be called. Never reset the examined row count here.
2078
It must be accumulated from all join iterations of all join parts.
2081
thd->limit_found_rows= 0;
2083
if (zero_result_cause)
2085
(void) return_zero_rows(this, result, select_lex->leaf_tables,
2087
send_row_on_empty_set(),
2094
if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2095
get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2098
if (select_options & SELECT_DESCRIBE)
2101
Check if we managed to optimize ORDER BY away and don't use temporary
2102
table to resolve ORDER BY: in that case, we only may need to do
2103
filesort for GROUP BY.
2105
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2108
Reset 'order' to 'group_list' and reinit variables describing
2112
simple_order= simple_group;
2116
(order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2117
(const_tables == tables ||
2118
((simple_order || skip_sort_order) &&
2119
test_if_skip_sort_order(&join_tab[const_tables], order,
2121
&join_tab[const_tables].table->
2122
keys_in_use_for_query))))
2125
select_describe(this, need_tmp,
2126
order != 0 && !skip_sort_order,
2128
!tables ? "No tables used" : NullS);
2132
JOIN *curr_join= this;
2133
List<Item> *curr_all_fields= &all_fields;
2134
List<Item> *curr_fields_list= &fields_list;
2135
TABLE *curr_tmp_table= 0;
2137
Initialize examined rows here because the values from all join parts
2138
must be accumulated in examined_row_count. Hence every join
2139
iteration must count from zero.
2141
curr_join->examined_rows= 0;
2143
/* Create a tmp table if distinct or if the sort is too complicated */
2149
We are in a non cacheable sub query. Get the saved join structure
2151
(curr_join may have been modified during last exection and we need
2154
curr_join= tmp_join;
2156
curr_tmp_table= exec_tmp_table1;
2158
/* Copy data to the temporary table */
2159
thd_proc_info(thd, "Copying to tmp table");
2160
if (!curr_join->sort_and_group &&
2161
curr_join->const_tables != curr_join->tables)
2162
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2163
if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2168
curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2170
if (curr_join->having)
2171
curr_join->having= curr_join->tmp_having= 0; // Allready done
2173
/* Change sum_fields reference to calculated fields in tmp_table */
2174
curr_join->all_fields= *curr_all_fields;
2177
items1= items0 + all_fields.elements;
2178
if (sort_and_group || curr_tmp_table->group)
2180
if (change_to_use_tmp_fields(thd, items1,
2181
tmp_fields_list1, tmp_all_fields1,
2182
fields_list.elements, all_fields))
2187
if (change_refs_to_tmp_fields(thd, items1,
2188
tmp_fields_list1, tmp_all_fields1,
2189
fields_list.elements, all_fields))
2192
curr_join->tmp_all_fields1= tmp_all_fields1;
2193
curr_join->tmp_fields_list1= tmp_fields_list1;
2194
curr_join->items1= items1;
2196
curr_all_fields= &tmp_all_fields1;
2197
curr_fields_list= &tmp_fields_list1;
2198
curr_join->set_items_ref_array(items1);
2200
if (sort_and_group || curr_tmp_table->group)
2202
curr_join->tmp_table_param.field_count+=
2203
curr_join->tmp_table_param.sum_func_count+
2204
curr_join->tmp_table_param.func_count;
2205
curr_join->tmp_table_param.sum_func_count=
2206
curr_join->tmp_table_param.func_count= 0;
2210
curr_join->tmp_table_param.field_count+=
2211
curr_join->tmp_table_param.func_count;
2212
curr_join->tmp_table_param.func_count= 0;
2215
if (curr_tmp_table->group)
2216
{ // Already grouped
2217
if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2218
curr_join->order= curr_join->group_list; /* order by group */
2219
curr_join->group_list= 0;
2223
If we have different sort & group then we must sort the data by group
2224
and copy it to another tmp table
2225
This code is also used if we are using distinct something
2226
we haven't been able to store in the temporary table yet
2227
like SEC_TO_TIME(SUM(...)).
2230
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))
2231
{ /* Must copy to another table */
2232
/* Free first data from old join */
2233
curr_join->join_free();
2234
if (make_simple_join(curr_join, curr_tmp_table))
2236
calc_group_buffer(curr_join, group_list);
2237
count_field_types(select_lex, &curr_join->tmp_table_param,
2238
curr_join->tmp_all_fields1,
2239
curr_join->select_distinct && !curr_join->group_list);
2240
curr_join->tmp_table_param.hidden_field_count=
2241
(curr_join->tmp_all_fields1.elements-
2242
curr_join->tmp_fields_list1.elements);
2245
if (exec_tmp_table2)
2246
curr_tmp_table= exec_tmp_table2;
2249
/* group data to new table */
2252
If the access method is loose index scan then all MIN/MAX
2253
functions are precomputed, and should be treated as regular
2254
functions. See extended comment in JOIN::exec.
2256
if (curr_join->join_tab->is_using_loose_index_scan())
2257
curr_join->tmp_table_param.precomputed_group_by= true;
2259
if (!(curr_tmp_table=
2260
exec_tmp_table2= create_tmp_table(thd,
2261
&curr_join->tmp_table_param,
2264
curr_join->select_distinct &&
2265
!curr_join->group_list,
2266
1, curr_join->select_options,
2270
curr_join->exec_tmp_table2= exec_tmp_table2;
2272
if (curr_join->group_list)
2274
thd_proc_info(thd, "Creating sort index");
2275
if (curr_join->join_tab == join_tab && save_join_tab())
2279
if (create_sort_index(thd, curr_join, curr_join->group_list,
2280
HA_POS_ERROR, HA_POS_ERROR, false) ||
2281
make_group_fields(this, curr_join))
2285
sortorder= curr_join->sortorder;
2288
thd_proc_info(thd, "Copying to group table");
2290
if (curr_join != this)
2294
curr_join->sum_funcs= sum_funcs2;
2295
curr_join->sum_funcs_end= sum_funcs_end2;
2299
curr_join->alloc_func_list();
2300
sum_funcs2= curr_join->sum_funcs;
2301
sum_funcs_end2= curr_join->sum_funcs_end;
2304
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2307
curr_join->group_list= 0;
2308
if (!curr_join->sort_and_group &&
2309
curr_join->const_tables != curr_join->tables)
2310
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2311
if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2312
(tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2317
end_read_record(&curr_join->join_tab->read_record);
2318
curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2319
curr_join->join_tab[0].table= 0; // Table is freed
2321
// No sum funcs anymore
2324
items2= items1 + all_fields.elements;
2325
if (change_to_use_tmp_fields(thd, items2,
2326
tmp_fields_list2, tmp_all_fields2,
2327
fields_list.elements, tmp_all_fields1))
2329
curr_join->tmp_fields_list2= tmp_fields_list2;
2330
curr_join->tmp_all_fields2= tmp_all_fields2;
2332
curr_fields_list= &curr_join->tmp_fields_list2;
2333
curr_all_fields= &curr_join->tmp_all_fields2;
2334
curr_join->set_items_ref_array(items2);
2335
curr_join->tmp_table_param.field_count+=
2336
curr_join->tmp_table_param.sum_func_count;
2337
curr_join->tmp_table_param.sum_func_count= 0;
2339
if (curr_tmp_table->distinct)
2340
curr_join->select_distinct=0; /* Each row is unique */
2342
curr_join->join_free(); /* Free quick selects */
2343
if (curr_join->select_distinct && ! curr_join->group_list)
2345
thd_proc_info(thd, "Removing duplicates");
2346
if (curr_join->tmp_having)
2347
curr_join->tmp_having->update_used_tables();
2348
if (remove_duplicates(curr_join, curr_tmp_table,
2349
*curr_fields_list, curr_join->tmp_having))
2351
curr_join->tmp_having=0;
2352
curr_join->select_distinct=0;
2354
curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2355
if (make_simple_join(curr_join, curr_tmp_table))
2357
calc_group_buffer(curr_join, curr_join->group_list);
2358
count_field_types(select_lex, &curr_join->tmp_table_param,
2359
*curr_all_fields, 0);
2363
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2365
if (make_group_fields(this, curr_join))
2372
init_items_ref_array();
2373
items3= ref_pointer_array + (all_fields.elements*4);
2374
setup_copy_fields(thd, &curr_join->tmp_table_param,
2375
items3, tmp_fields_list3, tmp_all_fields3,
2376
curr_fields_list->elements, *curr_all_fields);
2377
tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2378
tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2379
tmp_table_param.save_copy_field_end=
2380
curr_join->tmp_table_param.copy_field_end;
2381
curr_join->tmp_all_fields3= tmp_all_fields3;
2382
curr_join->tmp_fields_list3= tmp_fields_list3;
2386
curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2387
curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2388
curr_join->tmp_table_param.copy_field_end=
2389
tmp_table_param.save_copy_field_end;
2391
curr_fields_list= &tmp_fields_list3;
2392
curr_all_fields= &tmp_all_fields3;
2393
curr_join->set_items_ref_array(items3);
2395
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2397
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2398
thd->is_fatal_error)
2401
if (curr_join->group_list || curr_join->order)
2403
thd_proc_info(thd, "Sorting result");
2404
/* If we have already done the group, add HAVING to sorted table */
2405
if (curr_join->tmp_having && ! curr_join->group_list &&
2406
! curr_join->sort_and_group)
2408
// Some tables may have been const
2409
curr_join->tmp_having->update_used_tables();
2410
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2411
table_map used_tables= (curr_join->const_table_map |
2412
curr_table->table->map);
2414
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2417
if (sort_table_cond)
2419
if (!curr_table->select)
2420
if (!(curr_table->select= new SQL_SELECT))
2422
if (!curr_table->select->cond)
2423
curr_table->select->cond= sort_table_cond;
2424
else // This should never happen
2426
if (!(curr_table->select->cond=
2427
new Item_cond_and(curr_table->select->cond,
2431
Item_cond_and do not need fix_fields for execution, its parameters
2432
are fixed or do not need fix_fields, too
2434
curr_table->select->cond->quick_fix_field();
2436
curr_table->select_cond= curr_table->select->cond;
2437
curr_table->select_cond->top_level_item();
2438
curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2445
curr_join->select_limit= HA_POS_ERROR;
2449
We can abort sorting after thd->select_limit rows if we there is no
2450
WHERE clause for any tables after the sorted one.
2452
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2453
JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2454
for (; curr_table < end_table ; curr_table++)
2457
table->keyuse is set in the case there was an original WHERE clause
2458
on the table that was optimized away.
2460
if (curr_table->select_cond ||
2461
(curr_table->keyuse && !curr_table->first_inner))
2463
/* We have to sort all rows */
2464
curr_join->select_limit= HA_POS_ERROR;
2469
if (curr_join->join_tab == join_tab && save_join_tab())
2474
Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2475
chose FILESORT to be faster than INDEX SCAN or there is no
2476
suitable index present.
2477
Note, that create_sort_index calls test_if_skip_sort_order and may
2478
finally replace sorting with index scan if there is a LIMIT clause in
2479
the query. XXX: it's never shown in EXPLAIN!
2480
OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2482
if (create_sort_index(thd, curr_join,
2483
curr_join->group_list ?
2484
curr_join->group_list : curr_join->order,
2485
curr_join->select_limit,
2486
(select_options & OPTION_FOUND_ROWS ?
2487
HA_POS_ERROR : unit->select_limit_cnt),
2488
curr_join->group_list ? true : false))
2490
sortorder= curr_join->sortorder;
2491
if (curr_join->const_tables != curr_join->tables &&
2492
!curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2495
If no IO cache exists for the first table then we are using an
2496
INDEX SCAN and no filesort. Thus we should not remove the sorted
2497
attribute on the INDEX SCAN.
2503
/* XXX: When can we have here thd->is_error() not zero? */
2504
if (thd->is_error())
2506
error= thd->is_error();
2509
curr_join->having= curr_join->tmp_having;
2510
curr_join->fields= curr_fields_list;
2513
thd_proc_info(thd, "Sending data");
2514
result->send_fields(*curr_fields_list,
2515
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2516
error= do_select(curr_join, curr_fields_list, NULL);
2517
thd->limit_found_rows= curr_join->send_records;
2520
/* Accumulate the counts from all join iterations of all join parts. */
2521
thd->examined_row_count+= curr_join->examined_rows;
2524
With EXPLAIN EXTENDED we have to restore original ref_array
2525
for a derived table which is always materialized.
2526
Otherwise we would not be able to print the query correctly.
2529
(thd->lex->describe & DESCRIBE_EXTENDED) &&
2530
select_lex->linkage == DERIVED_TABLE_TYPE)
2531
set_items_ref_array(items0);
2541
Return error that hold JOIN.
2547
select_lex->join= 0;
2551
if (join_tab != tmp_join->join_tab)
2553
JOIN_TAB *tab, *end;
2554
for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2557
tmp_join->tmp_join= 0;
2558
tmp_table_param.copy_field=0;
2559
return(tmp_join->destroy());
2564
if (exec_tmp_table1)
2565
free_tmp_table(thd, exec_tmp_table1);
2566
if (exec_tmp_table2)
2567
free_tmp_table(thd, exec_tmp_table2);
2569
delete_dynamic(&keyuse);
2576
306
An entry point to single-unit select (a select without UNION).
2578
@param thd thread handler
308
@param session thread Cursor
2579
309
@param rref_pointer_array a reference to ref_pointer_array of
2580
310
the top-level select_lex for this query
2581
311
@param tables list of all tables used in this query.
2582
312
The tables have been pre-opened.
2583
@param wild_num number of wildcards used in the top level
313
@param wild_num number of wildcards used in the top level
2584
314
select of this query.
2585
315
For example statement
2586
316
SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2759
Convert a subquery predicate into a TABLE_LIST semi-join nest
2762
convert_subq_to_sj()
2763
parent_join Parent join, the one that has subq_pred in its WHERE/ON
2765
subq_pred Subquery predicate to be converted
2768
Convert a subquery predicate into a TABLE_LIST semi-join nest. All the
2769
prerequisites are already checked, so the conversion is always successfull.
2771
Prepared Statements: the transformation is permanent:
2772
- Changes in TABLE_LIST structures are naturally permanent
2773
- Item tree changes are performed on statement MEM_ROOT:
2774
= we activate statement MEM_ROOT
2775
= this function is called before the first fix_prepare_information
2778
This is intended because the criteria for subquery-to-sj conversion remain
2779
constant for the lifetime of the Prepared Statement.
2783
true Out of memory error
2786
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2788
SELECT_LEX *parent_lex= parent_join->select_lex;
2789
TABLE_LIST *emb_tbl_nest= NULL;
2790
List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2791
THD *thd= parent_join->thd;
2794
1. Find out where to put the predicate into.
2795
Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2797
if ((void*)subq_pred->expr_join_nest != (void*)1)
2799
if (subq_pred->expr_join_nest->nested_join)
2804
... [LEFT] JOIN ( ... ) ON (subquery AND whatever) ...
2806
The sj-nest will be inserted into the brackets nest.
2808
emb_tbl_nest= subq_pred->expr_join_nest;
2809
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2811
else if (!subq_pred->expr_join_nest->outer_join)
2816
... INNER JOIN tblX ON (subquery AND whatever) ...
2818
The sj-nest will be tblX's "sibling", i.e. another child of its
2819
parent. This is ok because tblX is joined as an inner join.
2821
emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2823
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2825
else if (!subq_pred->expr_join_nest->nested_join)
2827
TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;
2828
TABLE_LIST *wrap_nest;
2832
... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2834
we'll need to convert it into:
2836
... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2838
|<----- wrap_nest ---->|
2840
Q: other subqueries may be pointing to this element. What to do?
2841
A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2842
But we'll need to fix other pointers.
2843
A2: Another way: have TABLE_LIST::next_ptr so the following
2844
subqueries know the table has been nested.
2845
A3: changes in the TABLE_LIST::outer_join will make everything work
2848
if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2852
wrap_nest->embedding= outer_tbl->embedding;
2853
wrap_nest->join_list= outer_tbl->join_list;
2854
wrap_nest->alias= (char*) "(sj-wrap)";
2856
wrap_nest->nested_join->join_list.empty();
2857
wrap_nest->nested_join->join_list.push_back(outer_tbl);
2859
outer_tbl->embedding= wrap_nest;
2860
outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2863
wrap_nest will take place of outer_tbl, so move the outer join flag
2866
wrap_nest->outer_join= outer_tbl->outer_join;
2867
outer_tbl->outer_join= 0;
2869
wrap_nest->on_expr= outer_tbl->on_expr;
2870
outer_tbl->on_expr= NULL;
2872
List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2876
if (tbl == outer_tbl)
2878
li.replace(wrap_nest);
2883
Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
2884
semi-join nest into it
2886
emb_join_list= &wrap_nest->nested_join->join_list;
2887
emb_tbl_nest= wrap_nest;
2891
TABLE_LIST *sj_nest;
2892
NESTED_JOIN *nested_join;
2893
if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2897
nested_join= sj_nest->nested_join;
2899
sj_nest->join_list= emb_join_list;
2900
sj_nest->embedding= emb_tbl_nest;
2901
sj_nest->alias= (char*) "(sj-nest)";
2902
/* Nests do not participate in those 'chains', so: */
2903
/* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2904
emb_join_list->push_back(sj_nest);
2907
nested_join->used_tables and nested_join->not_null_tables are
2908
initialized in simplify_joins().
2912
2. Walk through subquery's top list and set 'embedding' to point to the
2915
st_select_lex *subq_lex= subq_pred->unit->first_select();
2916
nested_join->join_list.empty();
2917
List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
2918
TABLE_LIST *tl, *last_leaf;
2921
tl->embedding= sj_nest;
2922
tl->join_list= &nested_join->join_list;
2923
nested_join->join_list.push_back(tl);
2927
Reconnect the next_leaf chain.
2928
TODO: Do we have to put subquery's tables at the end of the chain?
2929
Inserting them at the beginning would be a bit faster.
2930
NOTE: We actually insert them at the front! That's because the order is
2931
reversed in this list.
2933
for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
2934
tl->next_leaf= subq_lex->leaf_tables;
2938
Same as above for next_local chain
2939
(a theory: a next_local chain always starts with ::leaf_tables
2940
because view's tables are inserted after the view)
2942
for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
2943
tl->next_local= subq_lex->leaf_tables;
2945
/* A theory: no need to re-connect the next_global chain */
2947
/* 3. Remove the original subquery predicate from the WHERE/ON */
2949
// The subqueries were replaced for Item_int(1) earlier
2950
subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
2951
/*TODO: also reset the 'with_subselect' there. */
2953
/* n. Adjust the parent_join->tables counter */
2954
uint table_no= parent_join->tables;
2955
/* n. Walk through child's tables and adjust table->map */
2956
for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2958
tl->table->tablenr= table_no;
2959
tl->table->map= ((table_map)1) << table_no;
2960
SELECT_LEX *old_sl= tl->select_lex;
2961
tl->select_lex= parent_join->select_lex;
2962
for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2963
emb->select_lex= parent_join->select_lex;
2965
parent_join->tables += subq_lex->join->tables;
2968
Put the subquery's WHERE into semi-join's sj_on_expr
2969
Add the subquery-induced equalities too.
2971
SELECT_LEX *save_lex= thd->lex->current_select;
2972
thd->lex->current_select=subq_lex;
2973
if (!subq_pred->left_expr->fixed &&
2974
subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
2976
thd->lex->current_select=save_lex;
2978
sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2979
sj_nest->nested_join->sj_depends_on= subq_pred->used_tables() |
2980
subq_pred->left_expr->used_tables();
2981
sj_nest->sj_on_expr= subq_lex->where;
2984
Create the IN-equalities and inject them into semi-join's ON expression.
2985
Additionally, for InsideOut strategy
2986
- Record the number of IN-equalities.
2987
- Create list of pointers to (oe1, ..., ieN). We'll need the list to
2988
see which of the expressions are bound and which are not (for those
2989
we'll produce a distinct stream of (ie_i1,...ie_ik).
2991
(TODO: can we just create a list of pointers and hope the expressions
2992
will not substitute themselves on fix_fields()? or we need to wrap
2993
them into Item_direct_view_refs and store pointers to those. The
2994
pointers to Item_direct_view_refs are guaranteed to be stable as
2995
Item_direct_view_refs doesn't substitute itself with anything in
2996
Item_direct_view_ref::fix_fields.
2998
sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
2999
sj_nest->nested_join->sj_outer_expr_list.empty();
3001
if (subq_pred->left_expr->cols() == 1)
3003
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3005
Item *item_eq= new Item_func_eq(subq_pred->left_expr,
3006
subq_lex->ref_pointer_array[0]);
3007
item_eq->name= (char*)subq_sj_cond_name;
3008
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3012
for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3014
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3017
new Item_func_eq(subq_pred->left_expr->element_index(i),
3018
subq_lex->ref_pointer_array[i]);
3019
item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3020
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3023
/* Fix the created equality and AND */
3024
sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3027
Walk through sj nest's WHERE and ON expressions and call
3028
item->fix_table_changes() for all items.
3030
sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3031
fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3034
/* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3035
subq_lex->master_unit()->exclude_level();
3037
/* Inject sj_on_expr into the parent's WHERE or ON */
3040
emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
3041
sj_nest->sj_on_expr);
3042
emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3046
/* Inject into the WHERE */
3047
parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3048
parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3049
parent_join->select_lex->where= parent_join->conds;
3057
Convert candidate subquery predicates to semi-joins
3060
JOIN::flatten_subqueries()
3063
Convert candidate subquery predicates to semi-joins.
3070
bool JOIN::flatten_subqueries()
3072
Item_in_subselect **in_subq;
3073
Item_in_subselect **in_subq_end;
3075
if (sj_subselects.elements() == 0)
3078
/* 1. Fix children subqueries */
3079
for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
3080
in_subq != in_subq_end; in_subq++)
3082
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3083
child_join->outer_tables = child_join->tables;
3084
if (child_join->flatten_subqueries())
3086
(*in_subq)->sj_convert_priority=
3087
(*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3090
//dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3092
2. Pick which subqueries to convert:
3093
sort the subquery array
3094
- prefer correlated subqueries over uncorrelated;
3095
- prefer subqueries that have greater number of outer tables;
3097
sj_subselects.sort(subq_sj_candidate_cmp);
3098
// #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3099
/* Replace all subqueries to be flattened with Item_int(1) */
3100
for (in_subq= sj_subselects.front();
3101
in_subq != in_subq_end &&
3102
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3105
if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3109
for (in_subq= sj_subselects.front();
3110
in_subq != in_subq_end &&
3111
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3114
if (convert_subq_to_sj(this, *in_subq))
3118
/* 3. Finalize those we didn't convert */
3119
for (; in_subq!= in_subq_end; in_subq++)
3121
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3122
Item_subselect::trans_res res;
3123
(*in_subq)->changed= 0;
3124
(*in_subq)->fixed= 0;
3125
res= (*in_subq)->select_transformer(child_join);
3126
if (res == Item_subselect::RES_ERROR)
3129
(*in_subq)->changed= 1;
3130
(*in_subq)->fixed= 1;
3132
Item *substitute= (*in_subq)->substitution;
3133
bool do_fix_fields= !(*in_subq)->substitution->fixed;
3134
if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3137
//if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3140
sj_subselects.clear();
3146
Setup for execution all subqueries of a query, for which the optimizer
3147
chose hash semi-join.
3149
@details Iterate over all subqueries of the query, and if they are under an
3150
IN predicate, and the optimizer chose to compute it via hash semi-join:
3151
- try to initialize all data structures needed for the materialized execution
3152
of the IN predicate,
3153
- if this fails, then perform the IN=>EXISTS transformation which was
3154
previously blocked during JOIN::prepare.
3156
This method is part of the "code generation" query processing phase.
3158
This phase must be called after substitute_for_best_equal_field() because
3159
that function may replace items with other items from a multiple equality,
3160
and we need to reference the correct items in the index access method of the
3163
@return Operation status
3164
@retval false success.
3165
@retval true error occurred.
3168
bool JOIN::setup_subquery_materialization()
3170
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3171
un= un->next_unit())
3173
for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3175
Item_subselect *subquery_predicate= sl->master_unit()->item;
3176
if (subquery_predicate &&
3177
subquery_predicate->substype() == Item_subselect::IN_SUBS)
3179
Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3180
if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3181
in_subs->setup_engine())
3191
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3194
find_eq_ref_candidate()
3195
table Table to be checked
3196
sj_inner_tables Bitmap of inner tables. eq_ref(inner_table) doesn't
3200
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3203
Check again if it is feasible to factor common parts with constant table
3207
true - There exists an eq_ref(outer-tables) candidate
3211
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3213
KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3218
while (1) /* For each key */
3221
KEY *keyinfo= table->key_info + key;
3222
key_part_map bound_parts= 0;
3223
if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
3225
do /* For all equalities on all key parts */
3227
/* Check if this is "t.keypart = expr(outer_tables) */
3228
if (!(keyuse->used_tables & sj_inner_tables) &&
3229
!(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3231
bound_parts |= 1 << keyuse->keypart;
3234
} while (keyuse->key == key && keyuse->table == table);
3236
if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3238
if (keyuse->table != table)
3246
if (keyuse->table != table)
3249
while (keyuse->key == key);
3258
Pull tables out of semi-join nests, if possible
3261
pull_out_semijoin_tables()
3262
join The join where to do the semi-join flattening
3265
Try to pull tables out of semi-join nests.
3268
When this function is called, the join may have several semi-join nests
3269
(possibly within different semi-join nests), but it is guaranteed that
3270
one semi-join nest does not contain another.
3273
A table can be pulled out of the semi-join nest if
3274
- It is a constant table
3278
* Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3280
* Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3281
* Semi-join nests TABLE_LIST::sj_inner_tables
3283
This operation is (and should be) performed at each PS execution since
3284
tables may become/cease to be constant across PS reexecutions.
3288
1 - Out of memory error
3291
int pull_out_semijoin_tables(JOIN *join)
3293
TABLE_LIST *sj_nest;
3294
List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3296
/* Try pulling out of the each of the semi-joins */
3297
while ((sj_nest= sj_list_it++))
3299
/* Action #1: Mark the constant tables to be pulled out */
3300
table_map pulled_tables= 0;
3302
List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3304
while ((tbl= child_li++))
3308
tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3309
if (tbl->table->map & join->const_table_map)
3311
pulled_tables |= tbl->table->map;
3317
Action #2: Find which tables we can pull out based on
3318
update_ref_and_keys() data. Note that pulling one table out can allow
3319
us to pull out some other tables too.
3321
bool pulled_a_table;
3324
pulled_a_table= false;
3326
while ((tbl= child_li++))
3328
if (tbl->table && !(pulled_tables & tbl->table->map))
3330
if (find_eq_ref_candidate(tbl->table,
3331
sj_nest->nested_join->used_tables &
3334
pulled_a_table= true;
3335
pulled_tables |= tbl->table->map;
3339
} while (pulled_a_table);
3342
if ((sj_nest)->nested_join->used_tables == pulled_tables)
3344
(sj_nest)->sj_inner_tables= 0;
3345
while ((tbl= child_li++))
3348
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3353
/* Record the bitmap of inner tables, mark the inner tables */
3354
table_map inner_tables=(sj_nest)->nested_join->used_tables &
3356
(sj_nest)->sj_inner_tables= inner_tables;
3357
while ((tbl= child_li++))
3361
if (inner_tables & tbl->table->map)
3362
tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3364
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3372
462
/*****************************************************************************
3373
Create JOIN_TABS, make a guess about the table types,
463
Create JoinTableS, make a guess about the table types,
3374
464
Approximate how many records will be used in each table
3375
465
*****************************************************************************/
3378
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3380
const key_map *keys,ha_rows limit)
466
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
3383
if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
469
if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3384
470
return(0); // Fatal error flag is set
3387
473
select->head=table;
3388
474
table->reginfo.impossible_range=0;
3389
if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
475
if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
3390
476
limit, 0, false)) == 1)
3391
477
return(select->quick->records);
3392
478
if (error == -1)
3398
484
return(HA_POS_ERROR); /* This shouldn't happend */
3402
This structure is used to collect info on potentially sargable
3403
predicates in order to check whether they become sargable after
3404
reading const tables.
3405
We form a bitmap of indexes that can be used for sargable predicates.
3406
Only such indexes are involved in range analysis.
3408
typedef struct st_sargable_param
3410
Field *field; /* field against which to check sargability */
3411
Item **arg_value; /* values of potential keys for lookups */
3412
uint num_values; /* number of values in the above array */
3416
Calculate the best possible join and initialize the join structure.
3425
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3426
DYNAMIC_ARRAY *keyuse_array)
3430
uint i,table_count,const_count,key;
3431
table_map found_const_table_map, all_table_map, found_ref, refs;
3432
key_map const_ref, eq_part;
3433
TABLE **table_vector;
3434
JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3435
KEYUSE *keyuse,*start_keyuse;
3436
table_map outer_join=0;
3437
SARGABLE_PARAM *sargables= 0;
3438
JOIN_TAB *stat_vector[MAX_TABLES+1];
3440
table_count=join->tables;
3441
stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3442
stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3443
table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3444
if (!stat || !stat_ref || !table_vector)
3445
return(1); // Eom /* purecov: inspected */
3447
join->best_ref=stat_vector;
3449
stat_end=stat+table_count;
3450
found_const_table_map= all_table_map=0;
3455
s++, tables= tables->next_leaf, i++)
3457
TABLE_LIST *embedding= tables->embedding;
3460
s->const_keys.init();
3461
s->checked_keys.init();
3462
s->needed_reg.init();
3463
table_vector[i]=s->table=table=tables->table;
3464
table->pos_in_table_list= tables;
3465
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3468
table->file->print_error(error, MYF(0));
3471
table->quick_keys.clear_all();
3472
table->reginfo.join_tab=s;
3473
table->reginfo.not_exists_optimize=0;
3474
memset(table->const_key_parts, 0,
3475
sizeof(key_part_map)*table->s->keys);
3476
all_table_map|= table->map;
3478
s->info=0; // For describe
3480
s->dependent= tables->dep_tables;
3481
s->key_dependent= 0;
3482
if (tables->schema_table)
3483
table->file->stats.records= 2;
3484
table->quick_condition_rows= table->file->stats.records;
3486
s->on_expr_ref= &tables->on_expr;
3487
if (*s->on_expr_ref)
3489
/* s is the only inner table of an outer join */
3490
if (!table->file->stats.records && !embedding)
3492
s->dependent= 0; // Ignore LEFT JOIN depend.
3493
set_position(join,const_count++,s,(KEYUSE*) 0);
3496
outer_join|= table->map;
3497
s->embedding_map= 0;
3498
for (;embedding; embedding= embedding->embedding)
3499
s->embedding_map|= embedding->nested_join->nj_map;
3502
if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3504
/* s belongs to a nested join, maybe to several embedded joins */
3505
s->embedding_map= 0;
3508
NESTED_JOIN *nested_join= embedding->nested_join;
3509
s->embedding_map|=nested_join->nj_map;
3510
s->dependent|= embedding->dep_tables;
3511
embedding= embedding->embedding;
3512
outer_join|= nested_join->used_tables;
3517
if ((table->s->system || table->file->stats.records <= 1) &&
3519
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
3521
set_position(join,const_count++,s,(KEYUSE*) 0);
3525
join->outer_join=outer_join;
3527
if (join->outer_join)
3530
Build transitive closure for relation 'to be dependent on'.
3531
This will speed up the plan search for many cases with outer joins,
3532
as well as allow us to catch illegal cross references/
3533
Warshall's algorithm is used to build the transitive closure.
3534
As we use bitmaps to represent the relation the complexity
3535
of the algorithm is O((number of tables)^2).
3537
for (i= 0, s= stat ; i < table_count ; i++, s++)
3539
for (uint j= 0 ; j < table_count ; j++)
3541
table= stat[j].table;
3542
if (s->dependent & table->map)
3543
s->dependent |= table->reginfo.join_tab->dependent;
3546
s->table->maybe_null= 1;
3548
/* Catch illegal cross references for outer joins */
3549
for (i= 0, s= stat ; i < table_count ; i++, s++)
3551
if (s->dependent & s->table->map)
3553
join->tables=0; // Don't use join->table
3554
my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3557
s->key_dependent= s->dependent;
3561
if (conds || outer_join)
3562
if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3563
conds, join->cond_equal,
3564
~outer_join, join->select_lex, &sargables))
3567
/* Read tables with 0 or 1 rows (system tables) */
3568
join->const_table_map= 0;
3570
for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3577
join->const_table_map|=s->table->map;
3578
if ((tmp=join_read_const_table(s, p_pos)))
3581
return(1); // Fatal error
3584
found_const_table_map|= s->table->map;
3587
/* loop until no more const tables are found */
3591
more_const_tables_found:
3596
We only have to loop from stat_vector + const_count as
3597
set_position() will move all const_tables first in stat_vector
3600
for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3605
If equi-join condition by a key is null rejecting and after a
3606
substitution of a const table the key value happens to be null
3607
then we can state that there are no matches for this equi-join.
3609
if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3612
When performing an outer join operation if there are no matching rows
3613
for the single row of the outer table all the inner tables are to be
3614
null complemented and thus considered as constant tables.
3615
Here we apply this consideration to the case of outer join operations
3616
with a single inner table only because the case with nested tables
3617
would require a more thorough analysis.
3618
TODO. Apply single row substitution to null complemented inner tables
3619
for nested outer join operations.
3621
while (keyuse->table == table)
3623
if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3624
keyuse->val->is_null() && keyuse->null_rejecting)
3627
mark_as_null_row(table);
3628
found_const_table_map|= table->map;
3629
join->const_table_map|= table->map;
3630
set_position(join,const_count++,s,(KEYUSE*) 0);
3631
goto more_const_tables_found;
3637
if (s->dependent) // If dependent on some table
3639
// All dep. must be constants
3640
if (s->dependent & ~(found_const_table_map))
3642
if (table->file->stats.records <= 1L &&
3643
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3644
!table->pos_in_table_list->embedding)
3648
join->const_table_map|=table->map;
3649
set_position(join,const_count++,s,(KEYUSE*) 0);
3650
if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3653
return(1); // Fatal error
3656
found_const_table_map|= table->map;
3660
/* check if table can be read by key or table only uses const refs */
3661
if ((keyuse=s->keyuse))
3664
while (keyuse->table == table)
3666
start_keyuse=keyuse;
3668
s->keys.set_bit(key); // QQ: remove this ?
3671
const_ref.clear_all();
3672
eq_part.clear_all();
3675
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3677
if (!((~found_const_table_map) & keyuse->used_tables))
3678
const_ref.set_bit(keyuse->keypart);
3680
refs|=keyuse->used_tables;
3681
eq_part.set_bit(keyuse->keypart);
3684
} while (keyuse->table == table && keyuse->key == key);
3686
if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3687
!table->pos_in_table_list->embedding)
3689
if ((table->key_info[key].flags & (HA_NOSAME))
3692
if (const_ref == eq_part)
3693
{ // Found everything for ref.
3697
join->const_table_map|=table->map;
3698
set_position(join,const_count++,s,start_keyuse);
3699
if (create_ref_for_key(join, s, start_keyuse,
3700
found_const_table_map))
3702
if ((tmp=join_read_const_table(s,
3703
join->positions+const_count-1)))
3706
return(1); // Fatal error
3709
found_const_table_map|= table->map;
3713
found_ref|= refs; // Table is const if all refs are const
3715
else if (const_ref == eq_part)
3716
s->const_keys.set_bit(key);
3721
} while (join->const_table_map & found_ref && ref_changed);
3724
Update info on indexes that can be used for search lookups as
3725
reading const tables may has added new sargable predicates.
3727
if (const_count && sargables)
3729
for( ; sargables->field ; sargables++)
3731
Field *field= sargables->field;
3732
JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3733
key_map possible_keys= field->key_start;
3734
possible_keys.intersect(field->table->keys_in_use_for_query);
3736
for (uint j=0; j < sargables->num_values; j++)
3737
is_const&= sargables->arg_value[j]->const_item();
3739
join_tab[0].const_keys.merge(possible_keys);
3743
if (pull_out_semijoin_tables(join))
3746
/* Calc how many (possible) matched records in each table */
3748
for (s=stat ; s < stat_end ; s++)
3750
if (s->type == JT_SYSTEM || s->type == JT_CONST)
3752
/* Only one matching row */
3753
s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3756
/* Approximate found rows and time to read them */
3757
s->found_records=s->records=s->table->file->stats.records;
3758
s->read_time=(ha_rows) s->table->file->scan_time();
3761
Set a max range of how many seeks we can expect when using keys
3762
This is can't be to high as otherwise we are likely to use
3765
s->worst_seeks= min((double) s->found_records / 10,
3766
(double) s->read_time*3);
3767
if (s->worst_seeks < 2.0) // Fix for small tables
3771
Add to stat->const_keys those indexes for which all group fields or
3772
all select distinct fields participate in one index.
3774
add_group_and_distinct_keys(join, s);
3776
if (!s->const_keys.is_clear_all() &&
3777
!s->table->pos_in_table_list->embedding)
3781
select= make_select(s->table, found_const_table_map,
3782
found_const_table_map,
3783
*s->on_expr_ref ? *s->on_expr_ref : conds,
3787
records= get_quick_record_count(join->thd, select, s->table,
3788
&s->const_keys, join->row_limit);
3789
s->quick=select->quick;
3790
s->needed_reg=select->needed_reg;
3792
if (records == 0 && s->table->reginfo.impossible_range)
3795
Impossible WHERE or ON expression
3796
In case of ON, we mark that the we match one empty NULL row.
3797
In case of WHERE, don't set found_const_table_map to get the
3798
caller to abort with a zero row result.
3800
join->const_table_map|= s->table->map;
3801
set_position(join,const_count++,s,(KEYUSE*) 0);
3803
if (*s->on_expr_ref)
3805
/* Generate empty row */
3806
s->info= "Impossible ON condition";
3807
found_const_table_map|= s->table->map;
3809
mark_as_null_row(s->table); // All fields are NULL
3812
if (records != HA_POS_ERROR)
3814
s->found_records=records;
3815
s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3821
join->join_tab=stat;
3822
join->map2table=stat_ref;
3823
join->table= join->all_tables=table_vector;
3824
join->const_tables=const_count;
3825
join->found_const_table_map=found_const_table_map;
3827
/* Find an optimal join order of the non-constant tables. */
3828
if (join->const_tables != join->tables)
3830
optimize_keyuse(join, keyuse_array);
3831
if (choose_plan(join, all_table_map & ~join->const_table_map))
3836
memcpy(join->best_positions, join->positions,
3837
sizeof(POSITION)*join->const_tables);
3838
join->best_read=1.0;
3840
/* Generate an execution plan from the found optimal join order. */
3841
return(join->thd->killed || get_best_combination(join));
3845
487
/*****************************************************************************
3846
488
Check with keys are used and with tables references with tables
3847
489
Updates in stat:
3850
492
keyuse Pointer to possible keys
3851
493
*****************************************************************************/
3853
/// Used when finding key fields
3854
typedef struct key_field_t {
3856
Item *val; ///< May be empty if diff constant
3858
uint optimize; // KEY_OPTIMIZE_*
3861
If true, the condition this struct represents will not be satisfied
3864
bool null_rejecting;
3865
bool *cond_guard; /* See KEYUSE::cond_guard */
3866
uint sj_pred_no; /* See KEYUSE::sj_pred_no */
3870
Merge new key definitions to old ones, remove those not used in both.
3872
This is called for OR between different levels.
3874
To be able to do 'ref_or_null' we merge a comparison of a column
3875
and 'column IS NULL' to one test. This is useful for sub select queries
3876
that are internally transformed to something like:.
3879
SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
3882
KEY_FIELD::null_rejecting is processed as follows: @n
3883
result has null_rejecting=true if it is set for both ORed references.
3885
- (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true
3886
- (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3889
The result of this is that we're missing some 'ref' accesses.
3890
OptimizerTeam: Fix this
3894
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3897
if (start == new_fields)
3898
return start; // Impossible or
3899
if (new_fields == end)
3900
return start; // No new fields, skip all
3902
KEY_FIELD *first_free=new_fields;
3904
/* Mark all found fields in old array */
3905
for (; new_fields != end ; new_fields++)
3907
for (KEY_FIELD *old=start ; old != first_free ; old++)
3909
if (old->field == new_fields->field)
3912
NOTE: below const_item() call really works as "!used_tables()", i.e.
3913
it can return false where it is feasible to make it return true.
3915
The cause is as follows: Some of the tables are already known to be
3916
const tables (the detection code is in make_join_statistics(),
3917
above the update_ref_and_keys() call), but we didn't propagate
3918
information about this: TABLE::const_table is not set to true, and
3919
Item::update_used_tables() hasn't been called for each item.
3920
The result of this is that we're missing some 'ref' accesses.
3921
TODO: OptimizerTeam: Fix this
3923
if (!new_fields->val->const_item())
3926
If the value matches, we can use the key reference.
3927
If not, we keep it until we have examined all new values
3929
if (old->val->eq(new_fields->val, old->field->binary()))
3931
old->level= and_level;
3932
old->optimize= ((old->optimize & new_fields->optimize &
3933
KEY_OPTIMIZE_EXISTS) |
3934
((old->optimize | new_fields->optimize) &
3935
KEY_OPTIMIZE_REF_OR_NULL));
3936
old->null_rejecting= (old->null_rejecting &&
3937
new_fields->null_rejecting);
3940
else if (old->eq_func && new_fields->eq_func &&
3941
old->val->eq_by_collation(new_fields->val,
3942
old->field->binary(),
3943
old->field->charset()))
3946
old->level= and_level;
3947
old->optimize= ((old->optimize & new_fields->optimize &
3948
KEY_OPTIMIZE_EXISTS) |
3949
((old->optimize | new_fields->optimize) &
3950
KEY_OPTIMIZE_REF_OR_NULL));
3951
old->null_rejecting= (old->null_rejecting &&
3952
new_fields->null_rejecting);
3954
else if (old->eq_func && new_fields->eq_func &&
3955
((old->val->const_item() && old->val->is_null()) ||
3956
new_fields->val->is_null()))
3958
/* field = expression OR field IS NULL */
3959
old->level= and_level;
3960
old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3962
Remember the NOT NULL value unless the value does not depend
3965
if (!old->val->used_tables() && old->val->is_null())
3966
old->val= new_fields->val;
3967
/* The referred expression can be NULL: */
3968
old->null_rejecting= 0;
3973
We are comparing two different const. In this case we can't
3974
use a key-lookup on this so it's better to remove the value
3975
and let the range optimzier handle it
3977
if (old == --first_free) // If last item
3979
*old= *first_free; // Remove old value
3980
old--; // Retry this value
3985
/* Remove all not used items */
3986
for (KEY_FIELD *old=start ; old != first_free ;)
3988
if (old->level != and_level)
3989
{ // Not used in all levels
3990
if (old == --first_free)
3992
*old= *first_free; // Remove old value
4002
Add a possible key to array of possible keys if it's usable as a key
4004
@param key_fields Pointer to add key, if usable
4005
@param and_level And level, to be stored in KEY_FIELD
4006
@param cond Condition predicate
4007
@param field Field used in comparision
4008
@param eq_func True if we used =, <=> or IS NULL
4009
@param value Value used for comparison with field
4010
@param usable_tables Tables which can be used for key optimization
4011
@param sargables IN/OUT Array of found sargable candidates
4014
If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4015
table, we store this to be able to do not exists optimization later.
4018
*key_fields is incremented if we stored a key in the array
4022
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4023
Field *field, bool eq_func, Item **value, uint num_values,
4024
table_map usable_tables, SARGABLE_PARAM **sargables)
4026
uint exists_optimize= 0;
4027
if (!(field->flags & PART_KEY_FLAG))
4029
// Don't remove column IS NULL on a LEFT JOIN table
4030
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4031
!field->table->maybe_null || field->null_ptr)
4032
return; // Not a key. Skip it
4033
exists_optimize= KEY_OPTIMIZE_EXISTS;
4034
assert(num_values == 1);
4038
table_map used_tables=0;
4040
for (uint i=0; i<num_values; i++)
4042
used_tables|=(value[i])->used_tables();
4043
if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4048
if (!(usable_tables & field->table->map))
4050
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4051
!field->table->maybe_null || field->null_ptr)
4052
return; // Can't use left join optimize
4053
exists_optimize= KEY_OPTIMIZE_EXISTS;
4057
JOIN_TAB *stat=field->table->reginfo.join_tab;
4058
key_map possible_keys=field->key_start;
4059
possible_keys.intersect(field->table->keys_in_use_for_query);
4060
stat[0].keys.merge(possible_keys); // Add possible keys
4063
Save the following cases:
4065
Field LIKE constant where constant doesn't start with a wildcard
4066
Field = field2 where field2 is in a different table
4073
stat[0].key_dependent|=used_tables;
4076
for (uint i=0; i<num_values; i++)
4078
if (!(is_const&= value[i]->const_item()))
4082
stat[0].const_keys.merge(possible_keys);
4086
Save info to be able check whether this predicate can be
4087
considered as sargable for range analisis after reading const tables.
4088
We do not save info about equalities as update_const_equal_items
4089
will take care of updating info on keys from sargable equalities.
4092
(*sargables)->field= field;
4093
(*sargables)->arg_value= value;
4094
(*sargables)->num_values= num_values;
4097
We can't always use indexes when comparing a string index to a
4098
number. cmp_type() is checked to allow compare of dates to numbers.
4099
eq_func is NEVER true when num_values > 1
4104
Additional optimization: if we're processing
4105
"t.key BETWEEN c1 AND c1" then proceed as if we were processing
4107
TODO: This is a very limited fix. A more generic fix is possible.
4108
There are 2 options:
4109
A) Make equality propagation code be able to handle BETWEEN
4110
(including cases like t1.key BETWEEN t2.key AND t3.key)
4111
B) Make range optimizer to infer additional "t.key = c" equalities
4112
and use them in equality propagation process (see details in
4115
if ((cond->functype() != Item_func::BETWEEN) ||
4116
((Item_func_between*) cond)->negated ||
4117
!value[0]->eq(value[1], field->binary()))
4122
if (field->result_type() == STRING_RESULT)
4124
if ((*value)->result_type() != STRING_RESULT)
4126
if (field->cmp_type() != (*value)->result_type())
4132
We can't use indexes if the effective collation
4133
of the operation differ from the field collation.
4135
if (field->cmp_type() == STRING_RESULT &&
4136
((Field_str*)field)->charset() != cond->compare_collation())
4143
For the moment eq_func is always true. This slot is reserved for future
4144
extensions where we want to remembers other things than just eq comparisons
4147
/* Store possible eq field */
4148
(*key_fields)->field= field;
4149
(*key_fields)->eq_func= eq_func;
4150
(*key_fields)->val= *value;
4151
(*key_fields)->level= and_level;
4152
(*key_fields)->optimize= exists_optimize;
4154
If the condition has form "tbl.keypart = othertbl.field" and
4155
othertbl.field can be NULL, there will be no matches if othertbl.field
4157
We use null_rejecting in add_not_null_conds() to add
4158
'othertbl.field IS NOT NULL' to tab->select_cond.
4160
(*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4161
cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4162
((*value)->type() == Item::FIELD_ITEM) &&
4163
((Item_field*)*value)->field->maybe_null());
4164
(*key_fields)->cond_guard= NULL;
4165
(*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
4166
cond->name < subq_sj_cond_name + 64)?
4167
cond->name - subq_sj_cond_name: UINT_MAX;
4172
Add possible keys to array of possible keys originated from a simple
4175
@param key_fields Pointer to add key, if usable
4176
@param and_level And level, to be stored in KEY_FIELD
4177
@param cond Condition predicate
4178
@param field Field used in comparision
4179
@param eq_func True if we used =, <=> or IS NULL
4180
@param value Value used for comparison with field
4181
Is NULL for BETWEEN and IN
4182
@param usable_tables Tables which can be used for key optimization
4183
@param sargables IN/OUT Array of found sargable candidates
4186
If field items f1 and f2 belong to the same multiple equality and
4187
a key is added for f1, the the same key is added for f2.
4190
*key_fields is incremented if we stored a key in the array
4194
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4195
Item_func *cond, Item_field *field_item,
4196
bool eq_func, Item **val,
4197
uint num_values, table_map usable_tables,
4198
SARGABLE_PARAM **sargables)
4200
Field *field= field_item->field;
4201
add_key_field(key_fields, and_level, cond, field,
4202
eq_func, val, num_values, usable_tables, sargables);
4203
Item_equal *item_equal= field_item->item_equal;
4207
Add to the set of possible key values every substitution of
4208
the field for an equal field included into item_equal
4210
Item_equal_iterator it(*item_equal);
4212
while ((item= it++))
4214
if (!field->eq(item->field))
4216
add_key_field(key_fields, and_level, cond, item->field,
4217
eq_func, val, num_values, usable_tables,
4225
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4226
COND *cond, table_map usable_tables,
4227
SARGABLE_PARAM **sargables)
4229
if (cond->type() == Item_func::COND_ITEM)
4231
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4232
KEY_FIELD *org_key_fields= *key_fields;
4234
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4238
add_key_fields(join, key_fields, and_level, item, usable_tables,
4240
for (; org_key_fields != *key_fields ; org_key_fields++)
4241
org_key_fields->level= *and_level;
4246
add_key_fields(join, key_fields, and_level, li++, usable_tables,
4251
KEY_FIELD *start_key_fields= *key_fields;
4253
add_key_fields(join, key_fields, and_level, item, usable_tables,
4255
*key_fields=merge_key_fields(org_key_fields,start_key_fields,
4256
*key_fields,++(*and_level));
4263
Subquery optimization: Conditions that are pushed down into subqueries
4264
are wrapped into Item_func_trig_cond. We process the wrapped condition
4265
but need to set cond_guard for KEYUSE elements generated from it.
4268
if (cond->type() == Item::FUNC_ITEM &&
4269
((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4271
Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4272
if (!join->group_list && !join->order &&
4274
join->unit->item->substype() == Item_subselect::IN_SUBS &&
4275
!join->unit->is_union())
4277
KEY_FIELD *save= *key_fields;
4278
add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4280
// Indicate that this ref access candidate is for subquery lookup:
4281
for (; save != *key_fields; save++)
4282
save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4288
/* If item is of type 'field op field/constant' add it to key_fields */
4289
if (cond->type() != Item::FUNC_ITEM)
4291
Item_func *cond_func= (Item_func*) cond;
4292
switch (cond_func->select_optimize()) {
4293
case Item_func::OPTIMIZE_NONE:
4295
case Item_func::OPTIMIZE_KEY:
4299
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4300
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4302
values= cond_func->arguments()+1;
4303
if (cond_func->functype() == Item_func::NE_FUNC &&
4304
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4305
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4307
assert(cond_func->functype() != Item_func::IN_FUNC ||
4308
cond_func->argument_count() != 2);
4309
add_key_equal_fields(key_fields, *and_level, cond_func,
4310
(Item_field*) (cond_func->key_item()->real_item()),
4312
cond_func->argument_count()-1,
4313
usable_tables, sargables);
4315
if (cond_func->functype() == Item_func::BETWEEN)
4317
values= cond_func->arguments();
4318
for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4320
Item_field *field_item;
4321
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4323
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4325
field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4326
add_key_equal_fields(key_fields, *and_level, cond_func,
4327
field_item, 0, values, 1, usable_tables,
4334
case Item_func::OPTIMIZE_OP:
4336
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4337
cond_func->functype() == Item_func::EQUAL_FUNC);
4339
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4340
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4342
add_key_equal_fields(key_fields, *and_level, cond_func,
4343
(Item_field*) (cond_func->arguments()[0])->real_item(),
4345
cond_func->arguments()+1, 1, usable_tables,
4348
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4349
cond_func->functype() != Item_func::LIKE_FUNC &&
4350
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4352
add_key_equal_fields(key_fields, *and_level, cond_func,
4353
(Item_field*) (cond_func->arguments()[1])->real_item(),
4355
cond_func->arguments(),1,usable_tables,
4360
case Item_func::OPTIMIZE_NULL:
4361
/* column_name IS [NOT] NULL */
4362
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4363
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4365
Item *tmp=new Item_null;
4366
if (unlikely(!tmp)) // Should never be true
4368
add_key_equal_fields(key_fields, *and_level, cond_func,
4369
(Item_field*) (cond_func->arguments()[0])->real_item(),
4370
cond_func->functype() == Item_func::ISNULL_FUNC,
4371
&tmp, 1, usable_tables, sargables);
4374
case Item_func::OPTIMIZE_EQUAL:
4375
Item_equal *item_equal= (Item_equal *) cond;
4376
Item *const_item= item_equal->get_const();
4377
Item_equal_iterator it(*item_equal);
4382
For each field field1 from item_equal consider the equality
4383
field1=const_item as a condition allowing an index access of the table
4384
with field1 by the keys value of field1.
4386
while ((item= it++))
4388
add_key_field(key_fields, *and_level, cond_func, item->field,
4389
true, &const_item, 1, usable_tables, sargables);
4395
Consider all pairs of different fields included into item_equal.
4396
For each of them (field1, field1) consider the equality
4397
field1=field2 as a condition allowing an index access of the table
4398
with field1 by the keys value of field2.
4400
Item_equal_iterator fi(*item_equal);
4401
while ((item= fi++))
4403
Field *field= item->field;
4404
while ((item= it++))
4406
if (!field->eq(item->field))
4408
add_key_field(key_fields, *and_level, cond_func, field,
4409
true, (Item **) &item, 1, usable_tables,
4421
497
Add all keys with uses 'field' for some keypart.
4423
499
If field->and_level != and_level then only mark key_part as const_part.
4427
max_part_bit(key_part_map bits)
501
uint32_t max_part_bit(key_part_map bits)
4430
504
for (found=0; bits & 1 ; found++,bits>>=1) ;
4435
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4437
Field *field=key_field->field;
4438
TABLE *form= field->table;
4441
if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4443
for (uint key=0 ; key < form->s->keys ; key++)
4445
if (!(form->keys_in_use_for_query.is_set(key)))
4448
uint key_parts= (uint) form->key_info[key].key_parts;
4449
for (uint part=0 ; part < key_parts ; part++)
4451
if (field->eq(form->key_info[key].key_part[part].field))
4453
keyuse.table= field->table;
4454
keyuse.val = key_field->val;
4456
keyuse.keypart=part;
4457
keyuse.keypart_map= (key_part_map) 1 << part;
4458
keyuse.used_tables=key_field->val->used_tables();
4459
keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4460
keyuse.null_rejecting= key_field->null_rejecting;
4461
keyuse.cond_guard= key_field->cond_guard;
4462
keyuse.sj_pred_no= key_field->sj_pred_no;
4463
VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4471
sort_keyuse(KEYUSE *a,KEYUSE *b)
508
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
4474
if (a->table->tablenr != b->table->tablenr)
4475
return (int) (a->table->tablenr - b->table->tablenr);
4476
if (a->key != b->key)
4477
return (int) (a->key - b->key);
4478
if (a->keypart != b->keypart)
4479
return (int) (a->keypart - b->keypart);
511
if (a->getTable()->tablenr != b->getTable()->tablenr)
512
return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
513
if (a->getKey() != b->getKey())
514
return static_cast<int>((a->getKey() - b->getKey()));
515
if (a->getKeypart() != b->getKeypart())
516
return static_cast<int>((a->getKeypart() - b->getKeypart()));
4480
517
// Place const values before other ones
4481
if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4482
test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
518
if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
519
test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
4484
521
/* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
4485
return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4486
(b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4491
Add to KEY_FIELD array all 'ref' access candidates within nested join.
4493
This function populates KEY_FIELD array with entries generated from the
4494
ON condition of the given nested join, and does the same for nested joins
4495
contained within this nested join.
4497
@param[in] nested_join_table Nested join pseudo-table to process
4498
@param[in,out] end End of the key field array
4499
@param[in,out] and_level And-level
4500
@param[in,out] sargables Array of found sargable candidates
4504
We can add accesses to the tables that are direct children of this nested
4505
join (1), and are not inner tables w.r.t their neighbours (2).
4507
Example for #1 (outer brackets pair denotes nested join this function is
4510
... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4514
... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4516
In examples 1-2 for condition cond, we can add 'ref' access candidates to
4520
... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4522
Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4525
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4526
KEY_FIELD **end, uint *and_level,
4527
SARGABLE_PARAM **sargables)
4529
List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4530
List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4531
bool have_another = false;
4532
table_map tables= 0;
4534
assert(nested_join_table->nested_join);
4536
while ((table= li++) || (have_another && (li=li2, have_another=false,
4539
if (table->nested_join)
4541
if (!table->on_expr)
4543
/* It's a semi-join nest. Walk into it as if it wasn't a nest */
4546
li= List_iterator<TABLE_LIST>(table->nested_join->join_list);
4549
add_key_fields_for_nj(join, table, end, and_level, sargables);
4552
if (!table->on_expr)
4553
tables |= table->table->map;
4555
if (nested_join_table->on_expr)
4556
add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
522
return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
523
(b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
4562
528
Update keyuse array with all possible keys we can use to fetch rows.
4565
@param[out] keyuse Put here ordered array of KEYUSE structures
531
@param[out] keyuse Put here ordered array of KeyUse structures
4566
532
@param join_tab Array in tablenr_order
4567
533
@param tables Number of tables in join
4568
534
@param cond WHERE condition (note that the function analyzes
4821
779
/* Intersect the keys of all group fields. */
4822
780
cur_item= indexed_fields_it++;
4823
possible_keys.merge(cur_item->field->part_of_key);
781
possible_keys|= cur_item->field->part_of_key;
4824
782
while ((cur_item= indexed_fields_it++))
4826
possible_keys.intersect(cur_item->field->part_of_key);
4829
if (!possible_keys.is_clear_all())
4830
join_tab->const_keys.merge(possible_keys);
4834
/*****************************************************************************
4835
Go through all combinations of not marked tables and find the one
4836
which uses least records
4837
*****************************************************************************/
4839
/** Save const tables first as used tables. */
4842
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4844
join->positions[idx].table= table;
4845
join->positions[idx].key=key;
4846
join->positions[idx].records_read=1.0; /* This is a const table */
4847
join->positions[idx].ref_depend_map= 0;
4849
/* Move the const table as down as possible in best_ref */
4850
JOIN_TAB **pos=join->best_ref+idx+1;
4851
JOIN_TAB *next=join->best_ref[idx];
4852
for (;next != table ; pos++)
4854
JOIN_TAB *tmp=pos[0];
4858
join->best_ref[idx]=table;
4863
Given a semi-join nest, find out which of the IN-equalities are bound
4866
get_bound_sj_equalities()
4867
sj_nest Semi-join nest
4868
remaining_tables Tables that are not yet bound
4871
Given a semi-join nest, find out which of the IN-equalities have their
4872
left part expression bound (i.e. the said expression doesn't refer to
4873
any of remaining_tables and can be evaluated).
4876
Bitmap of bound IN-equalities.
4879
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest,
4880
table_map remaining_tables)
4882
List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4886
while ((item= li++))
4889
Q: should this take into account equality propagation and how?
4890
A: If e->outer_side is an Item_field, walk over the equality
4891
class and see if there is an element that is bound?
4892
(this is an optional feature)
4894
if (!(item->used_tables() & remaining_tables))
4904
Find the best access path for an extension of a partial execution
4905
plan and add this path to the plan.
4907
The function finds the best access path to table 's' from the passed
4908
partial plan where an access path is the general term for any means to
4909
access the data in 's'. An access path may use either an index or a scan,
4910
whichever is cheaper. The input partial plan is passed via the array
4911
'join->positions' of length 'idx'. The chosen access method for 's' and its
4912
cost are stored in 'join->positions[idx]'.
4914
@param join pointer to the structure providing all context info
4916
@param s the table to be joined by the function
4917
@param thd thread for the connection that submitted the query
4918
@param remaining_tables set of tables not included into the partial plan yet
4919
@param idx the length of the partial plan
4920
@param record_count estimate for the number of records returned by the
4922
@param read_time the cost of the partial plan
4929
best_access_path(JOIN *join,
4932
table_map remaining_tables,
4934
double record_count,
4935
double read_time __attribute__((unused)))
4937
KEYUSE *best_key= 0;
4938
uint best_max_key_part= 0;
4939
bool found_constraint= 0;
4940
double best= DBL_MAX;
4941
double best_time= DBL_MAX;
4942
double records= DBL_MAX;
4943
table_map best_ref_depends_map= 0;
4946
uint best_is_sj_inside_out= 0;
4949
{ /* Use key if possible */
4950
TABLE *table= s->table;
4951
KEYUSE *keyuse,*start_key=0;
4952
double best_records= DBL_MAX;
4953
uint max_key_part=0;
4954
uint64_t bound_sj_equalities= 0;
4955
bool try_sj_inside_out= false;
4957
Discover the bound equalites. We need to do this, if
4958
1. The next table is an SJ-inner table, and
4959
2. It is the first table from that semijoin, and
4960
3. We're not within a semi-join range (i.e. all semi-joins either have
4961
all or none of their tables in join_table_map), except
4962
s->emb_sj_nest (which we've just entered).
4963
3. All correlation references from this sj-nest are bound
4965
if (s->emb_sj_nest && // (1)
4966
s->emb_sj_nest->sj_in_exprs < 64 &&
4967
((remaining_tables & s->emb_sj_nest->sj_inner_tables) == // (2)
4968
s->emb_sj_nest->sj_inner_tables) && // (2)
4969
join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables && // (3)
4970
!(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4972
/* This table is an InsideOut scan candidate */
4973
bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
4975
try_sj_inside_out= true;
4978
/* Test how we can use keys */
4979
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key
4980
for (keyuse=s->keyuse ; keyuse->table == table ;)
4982
key_part_map found_part= 0;
4983
table_map found_ref= 0;
4984
uint key= keyuse->key;
4985
KEY *keyinfo= table->key_info+key;
4986
/* Bitmap of keyparts where the ref access is over 'keypart=const': */
4987
key_part_map const_part= 0;
4988
/* The or-null keypart in ref-or-null access: */
4989
key_part_map ref_or_null_part= 0;
4991
/* Calculate how many key segments of the current key we can use */
4993
uint64_t handled_sj_equalities=0;
4994
key_part_map sj_insideout_map= 0;
4996
do /* For each keypart */
4998
uint keypart= keyuse->keypart;
4999
table_map best_part_found_ref= 0;
5000
double best_prev_record_reads= DBL_MAX;
5002
do /* For each way to access the keypart */
5006
if 1. expression doesn't refer to forward tables
5007
2. we won't get two ref-or-null's
5009
if (!(remaining_tables & keyuse->used_tables) &&
5010
!(ref_or_null_part && (keyuse->optimize &
5011
KEY_OPTIMIZE_REF_OR_NULL)))
5013
found_part|= keyuse->keypart_map;
5014
if (!(keyuse->used_tables & ~join->const_table_map))
5015
const_part|= keyuse->keypart_map;
5017
double tmp2= prev_record_reads(join, idx, (found_ref |
5018
keyuse->used_tables));
5019
if (tmp2 < best_prev_record_reads)
5021
best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5022
best_prev_record_reads= tmp2;
5024
if (rec > keyuse->ref_table_rows)
5025
rec= keyuse->ref_table_rows;
5027
If there is one 'key_column IS NULL' expression, we can
5028
use this ref_or_null optimisation of this field
5030
if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5031
ref_or_null_part |= keyuse->keypart_map;
5034
if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5036
if (!(remaining_tables & keyuse->used_tables))
5037
bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5040
handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5041
sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5046
} while (keyuse->table == table && keyuse->key == key &&
5047
keyuse->keypart == keypart);
5048
found_ref|= best_part_found_ref;
5049
} while (keyuse->table == table && keyuse->key == key);
5052
Assume that that each key matches a proportional part of table.
5054
if (!found_part && !handled_sj_equalities)
5055
continue; // Nothing usable found
5057
if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5058
rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables
5060
bool sj_inside_out_scan= false;
5062
found_constraint= 1;
5064
Check if InsideOut scan is applicable:
5065
1. All IN-equalities are either "bound" or "handled"
5066
2. Index keyparts are
5069
if (try_sj_inside_out &&
5070
table->covering_keys.is_set(key) &&
5071
(handled_sj_equalities | bound_sj_equalities) == // (1)
5072
PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5074
uint n_fixed_parts= max_part_bit(found_part);
5075
if (n_fixed_parts != keyinfo->key_parts &&
5076
(PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5077
PREV_BITS(uint, keyinfo->key_parts))
5080
Not all parts are fixed. Produce bitmap of remaining bits and
5081
check if all of them are covered.
5083
sj_inside_out_scan= true;
5087
It's a confluent ref scan.
5089
That is, all found KEYUSE elements refer to IN-equalities,
5090
and there is really no ref access because there is no
5091
t.keypart0 = {bound expression}
5093
Calculate the cost of complete loose index scan.
5095
records= (double)s->table->file->stats.records;
5097
/* The cost is entire index scan cost (divided by 2) */
5098
best_time= s->table->file->index_only_read_time(key, records);
5100
/* Now figure how many different keys we will get */
5102
if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5103
records= records / rpc;
5110
Check if we found full key
5112
if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5115
max_key_part= (uint) ~0;
5116
if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5118
tmp = prev_record_reads(join, idx, found_ref);
5124
{ /* We found a const key */
5126
ReuseRangeEstimateForRef-1:
5127
We get here if we've found a ref(const) (c_i are constants):
5128
"(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond]
5130
If range optimizer was able to construct a "range"
5131
access on this index, then its condition "quick_cond" was
5132
eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5133
from the range optimizer.
5135
Proof of (*): By properties of range and ref optimizers
5136
quick_cond will be equal or tighther than ref_const_cond.
5137
ref_const_cond already covers "smallest" possible interval -
5138
a singlepoint interval over all keyparts. Therefore,
5139
quick_cond is equivalent to ref_const_cond (if it was an
5140
empty interval we wouldn't have got here).
5142
if (table->quick_keys.is_set(key))
5143
records= (double) table->quick_rows[key];
5146
/* quick_range couldn't use key! */
5147
records= (double) s->records/rec;
5152
if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5153
{ /* Prefer longer keys */
5155
((double) s->records / (double) rec *
5157
((double) (table->s->max_key_length-keyinfo->key_length) /
5158
(double) table->s->max_key_length)));
5160
records=2.0; /* Can't be as good as a unique */
5163
ReuseRangeEstimateForRef-2: We get here if we could not reuse
5164
E(#rows) from range optimizer. Make another try:
5166
If range optimizer produced E(#rows) for a prefix of the ref
5167
access we're considering, and that E(#rows) is lower then our
5168
current estimate, make an adjustment. The criteria of when we
5169
can make an adjustment is a special case of the criteria used
5170
in ReuseRangeEstimateForRef-3.
5172
if (table->quick_keys.is_set(key) &&
5173
const_part & (1 << table->quick_key_parts[key]) &&
5174
table->quick_n_ranges[key] == 1 &&
5175
records > (double) table->quick_rows[key])
5177
records= (double) table->quick_rows[key];
5180
/* Limit the number of matched rows */
5182
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5183
if (table->covering_keys.is_set(key))
5185
/* we can use only index tree */
5186
tmp= record_count * table->file->index_only_read_time(key, tmp);
5189
tmp= record_count*min(tmp,s->worst_seeks);
5195
Use as much key-parts as possible and a uniq key is better
5196
than a not unique key
5197
Set tmp to (previous record count) * (records / combination)
5199
if ((found_part & 1) &&
5200
(!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5201
found_part == PREV_BITS(uint,keyinfo->key_parts)))
5203
max_key_part= max_part_bit(found_part);
5205
ReuseRangeEstimateForRef-3:
5206
We're now considering a ref[or_null] access via
5207
(t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
5208
(same-as-above but with one cond replaced
5209
with "t.keypart_i IS NULL")] (**)
5211
Try re-using E(#rows) from "range" optimizer:
5212
We can do so if "range" optimizer used the same intervals as
5213
in (**). The intervals used by range optimizer may be not
5214
available at this point (as "range" access might have choosen to
5215
create quick select over another index), so we can't compare
5216
them to (**). We'll make indirect judgements instead.
5217
The sufficient conditions for re-use are:
5218
(C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5219
this is not satisfied we have no way to know which ranges
5220
will be actually scanned by 'ref' until we execute the
5222
(C2) max #key parts in 'range' access == K == max_key_part (this
5223
is apparently a necessary requirement)
5225
We also have a property that "range optimizer produces equal or
5226
tighter set of scan intervals than ref(const) optimizer". Each
5227
of the intervals in (**) are "tightest possible" intervals when
5228
one limits itself to using keyparts 1..K (which we do in #2).
5229
From here it follows that range access used either one, or
5230
both of the (I1) and (I2) intervals:
5232
(t.keypart1=c1 AND ... AND t.keypartK=eK) (I1)
5233
(same-as-above but with one cond replaced
5234
with "t.keypart_i IS NULL") (I2)
5236
The remaining part is to exclude the situation where range
5237
optimizer used one interval while we're considering
5238
ref-or-null and looking for estimate for two intervals. This
5239
is done by last limitation:
5241
(C3) "range optimizer used (have ref_or_null?2:1) intervals"
5243
if (table->quick_keys.is_set(key) && !found_ref && //(C1)
5244
table->quick_key_parts[key] == max_key_part && //(C2)
5245
table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5247
tmp= records= (double) table->quick_rows[key];
5251
/* Check if we have statistic about the distribution */
5252
if ((records= keyinfo->rec_per_key[max_key_part-1]))
5255
Fix for the case where the index statistics is too
5257
(1) We're considering ref(const) and there is quick select
5259
(2) and that quick select uses more keyparts (i.e. it will
5260
scan equal/smaller interval then this ref(const))
5261
(3) and E(#rows) for quick select is higher then our
5264
We'll use E(#rows) from quick select.
5266
Q: Why do we choose to use 'ref'? Won't quick select be
5267
cheaper in some cases ?
5268
TODO: figure this out and adjust the plan choice if needed.
5270
if (!found_ref && table->quick_keys.is_set(key) && // (1)
5271
table->quick_key_parts[key] > max_key_part && // (2)
5272
records < (double)table->quick_rows[key]) // (3)
5273
records= (double)table->quick_rows[key];
5280
Assume that the first key part matches 1% of the file
5281
and that the whole key matches 10 (duplicates) or 1
5283
Assume also that more key matches proportionally more
5285
This gives the formula:
5286
records = (x * (b-a) + a*c-b)/(c-1)
5288
b = records matched by whole key
5289
a = records matched by first key part (1% of all records?)
5290
c = number of key parts in key
5291
x = used key parts (1 <= x <= c)
5294
if (!(rec_per_key=(double)
5295
keyinfo->rec_per_key[keyinfo->key_parts-1]))
5296
rec_per_key=(double) s->records/rec+1;
5300
else if (rec_per_key/(double) s->records >= 0.01)
5304
double a=s->records*0.01;
5305
if (keyinfo->key_parts > 1)
5306
tmp= (max_key_part * (rec_per_key - a) +
5307
a*keyinfo->key_parts - rec_per_key)/
5308
(keyinfo->key_parts-1);
5311
set_if_bigger(tmp,1.0);
5313
records = (ulong) tmp;
5316
if (ref_or_null_part)
5318
/* We need to do two key searches to find key */
5324
ReuseRangeEstimateForRef-4: We get here if we could not reuse
5325
E(#rows) from range optimizer. Make another try:
5327
If range optimizer produced E(#rows) for a prefix of the ref
5328
access we're considering, and that E(#rows) is lower then our
5329
current estimate, make the adjustment.
5331
The decision whether we can re-use the estimate from the range
5332
optimizer is the same as in ReuseRangeEstimateForRef-3,
5333
applied to first table->quick_key_parts[key] key parts.
5335
if (table->quick_keys.is_set(key) &&
5336
table->quick_key_parts[key] <= max_key_part &&
5337
const_part & (1 << table->quick_key_parts[key]) &&
5338
table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5340
records > (double) table->quick_rows[key])
5342
tmp= records= (double) table->quick_rows[key];
5346
/* Limit the number of matched rows */
5347
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5348
if (table->covering_keys.is_set(key))
5350
/* we can use only index tree */
5351
tmp= record_count * table->file->index_only_read_time(key, tmp);
5354
tmp= record_count * min(tmp,s->worst_seeks);
5357
tmp= best_time; // Do nothing
5360
if (sj_inside_out_scan && !start_key)
5368
if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5370
best_time= tmp + records/(double) TIME_FOR_COMPARE;
5372
best_records= records;
5373
best_key= start_key;
5374
best_max_key_part= max_key_part;
5375
best_ref_depends_map= found_ref;
5376
best_is_sj_inside_out= sj_inside_out_scan;
5379
records= best_records;
5383
Don't test table scan if it can't be better.
5384
Prefer key lookup if we would use the same key for scanning.
5386
Don't do a table scan on InnoDB tables, if we can read the used
5387
parts of the row from any of the used index.
5388
This is because table scans uses index and we would not win
5389
anything by using a table scan.
5391
A word for word translation of the below if-statement in sergefp's
5392
understanding: we check if we should use table scan if:
5393
(1) The found 'ref' access produces more records than a table scan
5394
(or index scan, or quick select), or 'ref' is more expensive than
5396
(2) This doesn't hold: the best way to perform table scan is to to perform
5397
'range' access using index IDX, and the best way to perform 'ref'
5398
access is to use the same index IDX, with the same or more key parts.
5399
(note: it is not clear how this rule is/should be extended to
5400
index_merge quick selects)
5401
(3) See above note about InnoDB.
5402
(4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5403
path, but there is no quick select)
5404
If the condition in the above brackets holds, then the only possible
5405
"table scan" access method is ALL/index (there is no quick select).
5406
Since we have a 'ref' access path, and FORCE INDEX instructs us to
5407
choose it over ALL/index, there is no need to consider a full table
5410
if ((records >= s->found_records || best > s->read_time) && // (1)
5411
!(s->quick && best_key && s->quick->index == best_key->key && // (2)
5412
best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5413
!((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3)
5414
! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5415
!(s->table->force_index && best_key && !s->quick)) // (4)
5416
{ // Check full join
5417
ha_rows rnd_records= s->found_records;
5419
If there is a filtering condition on the table (i.e. ref analyzer found
5420
at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5421
preceding this table in the join order we're now considering), then
5422
assume that 25% of the rows will be filtered out by this condition.
5424
This heuristic is supposed to force tables used in exprZ to be before
5425
this table in join order.
5427
if (found_constraint)
5428
rnd_records-= rnd_records/4;
5431
If applicable, get a more accurate estimate. Don't use the two
5434
if (s->table->quick_condition_rows != s->found_records)
5435
rnd_records= s->table->quick_condition_rows;
5438
Range optimizer never proposes a RANGE if it isn't better
5439
than FULL: so if RANGE is present, it's always preferred to FULL.
5440
Here we estimate its cost.
5446
- read record range through 'quick'
5447
- skip rows which does not satisfy WHERE constraints
5449
We take into account possible use of join cache for ALL/index
5450
access (see first else-branch below), but we don't take it into
5451
account here for range/index_merge access. Find out why this is so.
5454
(s->quick->read_time +
5455
(s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5459
/* Estimate cost of reading table. */
5460
tmp= s->table->file->scan_time();
5461
if (s->table->map & join->outer_join) // Can't use join cache
5464
For each record we have to:
5465
- read the whole table record
5466
- skip rows which does not satisfy join condition
5470
(s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5474
/* We read the table as many times as join buffer becomes full. */
5475
tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5477
(double) thd->variables.join_buff_size));
5479
We don't make full cartesian product between rows in the scanned
5480
table and existing records because we skip all rows from the
5481
scanned table, which does not satisfy join condition when
5482
we read the table (see flush_cached_records for details). Here we
5483
take into account cost to read and skip these records.
5485
tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5490
We estimate the cost of evaluating WHERE clause for found records
5491
as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5492
tmp give us total cost of using TABLE SCAN
5494
if (best == DBL_MAX ||
5495
(tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5496
best + record_count/(double) TIME_FOR_COMPARE*records))
5499
If the table has a range (s->quick is set) make_join_select()
5500
will ensure that this will be used
5503
records= rows2double(rnd_records);
5505
/* range/index_merge/ALL/index access method are "independent", so: */
5506
best_ref_depends_map= 0;
5507
best_is_sj_inside_out= false;
5511
/* Update the cost information for the current partial plan */
5512
join->positions[idx].records_read= records;
5513
join->positions[idx].read_time= best;
5514
join->positions[idx].key= best_key;
5515
join->positions[idx].table= s;
5516
join->positions[idx].ref_depend_map= best_ref_depends_map;
5517
join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5520
idx == join->const_tables &&
5521
s->table == join->sort_by_table &&
5522
join->unit->select_limit_cnt >= records)
5523
join->sort_by_table= (TABLE*) 1; // Must use temporary table
5530
Selects and invokes a search strategy for an optimal query plan.
5532
The function checks user-configurable parameters that control the search
5533
strategy for an optimal plan, selects the search method and then invokes
5534
it. Each specific optimization procedure stores the final optimal plan in
5535
the array 'join->best_positions', and the cost of the plan in
5538
@param join pointer to the structure providing all context info for
5540
@param join_tables set of the tables in the query
5543
'MAX_TABLES+2' denotes the old implementation of find_best before
5544
the greedy version. Will be removed when greedy_search is approved.
5553
choose_plan(JOIN *join, table_map join_tables)
5555
uint search_depth= join->thd->variables.optimizer_search_depth;
5556
uint prune_level= join->thd->variables.optimizer_prune_level;
5557
bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5559
join->cur_embedding_map= 0;
5560
reset_nj_counters(join->join_list);
5562
if (SELECT_STRAIGHT_JOIN option is set)
5563
reorder tables so dependent tables come after tables they depend
5564
on, otherwise keep tables in the order they were specified in the query
5566
Apply heuristic: pre-sort all access plans with respect to the number of
5569
my_qsort(join->best_ref + join->const_tables,
5570
join->tables - join->const_tables, sizeof(JOIN_TAB*),
5571
straight_join ? join_tab_cmp_straight : join_tab_cmp);
5572
join->cur_emb_sj_nests= 0;
5575
optimize_straight_join(join, join_tables);
5579
if (search_depth == MAX_TABLES+2)
5581
TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5582
the greedy version. Will be removed when greedy_search is approved.
5584
join->best_read= DBL_MAX;
5585
if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5590
if (search_depth == 0)
5591
/* Automatically determine a reasonable value for 'search_depth' */
5592
search_depth= determine_search_depth(join);
5593
if (greedy_search(join, join_tables, search_depth, prune_level))
5599
Store the cost of this query into a user variable
5600
Don't update last_query_cost for statements that are not "flat joins" :
5601
i.e. they have subqueries, unions or call stored procedures.
5602
TODO: calculate a correct cost for a query with subqueries and UNIONs.
5604
if (join->thd->lex->is_single_level_stmt())
5605
join->thd->status_var.last_query_cost= join->best_read;
5611
Compare two JOIN_TAB objects based on the number of accessed records.
5613
@param ptr1 pointer to first JOIN_TAB object
5614
@param ptr2 pointer to second JOIN_TAB object
784
possible_keys&= cur_item->field->part_of_key;
787
if (possible_keys.any())
788
join_tab->const_keys|= possible_keys;
792
Compare two JoinTable objects based on the number of accessed records.
794
@param ptr1 pointer to first JoinTable object
795
@param ptr2 pointer to second JoinTable object
5617
798
The order relation implemented by join_tab_cmp() is not transitive,
5671
Heuristic procedure to automatically guess a reasonable degree of
5672
exhaustiveness for the greedy search procedure.
5674
The procedure estimates the optimization time and selects a search depth
5675
big enough to result in a near-optimal QEP, that doesn't take too long to
5676
find. If the number of tables in the query exceeds some constant, then
5677
search_depth is set to this constant.
5679
@param join pointer to the structure providing all context info for
5683
This is an extremely simplistic implementation that serves as a stub for a
5684
more advanced analysis of the join. Ideally the search depth should be
5685
determined by learning from previous query optimizations, because it will
5686
depend on the CPU power (and other factors).
5689
this value should be determined dynamically, based on statistics:
5690
uint max_tables_for_exhaustive_opt= 7;
5693
this value could be determined by some mapping of the form:
5694
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5697
A positive integer that specifies the search depth (and thus the
5698
exhaustiveness) of the depth-first search algorithm used by
5703
determine_search_depth(JOIN *join)
5705
uint table_count= join->tables - join->const_tables;
5707
/* TODO: this value should be determined dynamically, based on statistics: */
5708
uint max_tables_for_exhaustive_opt= 7;
5710
if (table_count <= max_tables_for_exhaustive_opt)
5711
search_depth= table_count+1; // use exhaustive for small number of tables
5714
TODO: this value could be determined by some mapping of the form:
5715
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5717
search_depth= max_tables_for_exhaustive_opt; // use greedy search
5719
return search_depth;
5724
Select the best ways to access the tables in a query without reordering them.
5726
Find the best access paths for each query table and compute their costs
5727
according to their order in the array 'join->best_ref' (thus without
5728
reordering the join tables). The function calls sequentially
5729
'best_access_path' for each table in the query to select the best table
5730
access method. The final optimal plan is stored in the array
5731
'join->best_positions', and the corresponding cost in 'join->best_read'.
5733
@param join pointer to the structure providing all context info for
5735
@param join_tables set of the tables in the query
5738
This function can be applied to:
5739
- queries with STRAIGHT_JOIN
5740
- internally to compute the cost of an arbitrary QEP
5742
Thus 'optimize_straight_join' can be used at any stage of the query
5743
optimization process to finalize a QEP as it is.
5747
optimize_straight_join(JOIN *join, table_map join_tables)
5750
uint idx= join->const_tables;
5751
double record_count= 1.0;
5752
double read_time= 0.0;
5754
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5756
/* Find the best access method from 's' to the current partial plan */
5757
advance_sj_state(join_tables, s);
5758
best_access_path(join, s, join->thd, join_tables, idx,
5759
record_count, read_time);
5760
/* compute the cost of the new plan extended with 's' */
5761
record_count*= join->positions[idx].records_read;
5762
read_time+= join->positions[idx].read_time;
5763
join_tables&= ~(s->table->map);
5767
read_time+= record_count / (double) TIME_FOR_COMPARE;
5768
if (join->sort_by_table &&
5769
join->sort_by_table != join->positions[join->const_tables].table->table)
5770
read_time+= record_count; // We have to make a temp table
5771
memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
5772
join->best_read= read_time;
5777
Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5779
The search procedure uses a hybrid greedy/exhaustive search with controlled
5780
exhaustiveness. The search is performed in N = card(remaining_tables)
5781
steps. Each step evaluates how promising is each of the unoptimized tables,
5782
selects the most promising table, and extends the current partial QEP with
5783
that table. Currenly the most 'promising' table is the one with least
5784
expensive extension.\
5786
There are two extreme cases:
5787
-# When (card(remaining_tables) < search_depth), the estimate finds the
5788
best complete continuation of the partial QEP. This continuation can be
5789
used directly as a result of the search.
5790
-# When (search_depth == 1) the 'best_extension_by_limited_search'
5791
consideres the extension of the current QEP with each of the remaining
5794
All other cases are in-between these two extremes. Thus the parameter
5795
'search_depth' controlls the exhaustiveness of the search. The higher the
5796
value, the longer the optimizaton time and possibly the better the
5797
resulting plan. The lower the value, the fewer alternative plans are
5798
estimated, but the more likely to get a bad QEP.
5800
All intermediate and final results of the procedure are stored in 'join':
5801
- join->positions : modified for every partial QEP that is explored
5802
- join->best_positions: modified for the current best complete QEP
5803
- join->best_read : modified for the current best complete QEP
5804
- join->best_ref : might be partially reordered
5806
The final optimal plan is stored in 'join->best_positions', and its
5807
corresponding cost in 'join->best_read'.
5810
The following pseudocode describes the algorithm of 'greedy_search':
5813
procedure greedy_search
5814
input: remaining_tables
5819
(t, a) = best_extension(pplan, remaining_tables);
5820
pplan = concat(pplan, (t, a));
5821
remaining_tables = remaining_tables - t;
5822
} while (remaining_tables != {})
5827
where 'best_extension' is a placeholder for a procedure that selects the
5828
most "promising" of all tables in 'remaining_tables'.
5829
Currently this estimate is performed by calling
5830
'best_extension_by_limited_search' to evaluate all extensions of the
5831
current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5832
mainly depends on that of 'best_extension_by_limited_search'.
5835
If 'best_extension()' == 'best_extension_by_limited_search()', then the
5836
worst-case complexity of this algorithm is <=
5837
O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5838
complexity of greedy_search is O(N!).
5841
In the future, 'greedy_search' might be extended to support other
5842
implementations of 'best_extension', e.g. some simpler quadratic procedure.
5844
@param join pointer to the structure providing all context info
5846
@param remaining_tables set of tables not included into the partial plan yet
5847
@param search_depth controlls the exhaustiveness of the search
5848
@param prune_level the pruning heuristics that should be applied during
5858
greedy_search(JOIN *join,
5859
table_map remaining_tables,
5863
double record_count= 1.0;
5864
double read_time= 0.0;
5865
uint idx= join->const_tables; // index into 'join->best_ref'
5867
uint size_remain; // cardinality of remaining_tables
5869
JOIN_TAB *best_table; // the next plan node to be added to the curr QEP
5871
/* number of tables that remain to be optimized */
5872
size_remain= my_count_bits(remaining_tables);
5875
/* Find the extension of the current QEP with the lowest cost */
5876
join->best_read= DBL_MAX;
5877
if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5878
read_time, search_depth, prune_level))
5881
if (size_remain <= search_depth)
5884
'join->best_positions' contains a complete optimal extension of the
5885
current partial QEP.
5890
/* select the first table in the optimal extension as most promising */
5891
best_pos= join->best_positions[idx];
5892
best_table= best_pos.table;
5894
Each subsequent loop of 'best_extension_by_limited_search' uses
5895
'join->positions' for cost estimates, therefore we have to update its
5898
join->positions[idx]= best_pos;
5900
/* find the position of 'best_table' in 'join->best_ref' */
5902
JOIN_TAB *pos= join->best_ref[best_idx];
5903
while (pos && best_table != pos)
5904
pos= join->best_ref[++best_idx];
5905
assert((pos != NULL)); // should always find 'best_table'
5906
/* move 'best_table' at the first free position in the array of joins */
5907
swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5909
/* compute the cost of the new plan extended with 'best_table' */
5910
record_count*= join->positions[idx].records_read;
5911
read_time+= join->positions[idx].read_time;
5913
remaining_tables&= ~(best_table->table->map);
5921
Find a good, possibly optimal, query execution plan (QEP) by a possibly
5924
The procedure searches for the optimal ordering of the query tables in set
5925
'remaining_tables' of size N, and the corresponding optimal access paths to
5926
each table. The choice of a table order and an access path for each table
5927
constitutes a query execution plan (QEP) that fully specifies how to
5930
The maximal size of the found plan is controlled by the parameter
5931
'search_depth'. When search_depth == N, the resulting plan is complete and
5932
can be used directly as a QEP. If search_depth < N, the found plan consists
5933
of only some of the query tables. Such "partial" optimal plans are useful
5934
only as input to query optimization procedures, and cannot be used directly
5937
The algorithm begins with an empty partial plan stored in 'join->positions'
5938
and a set of N tables - 'remaining_tables'. Each step of the algorithm
5939
evaluates the cost of the partial plan extended by all access plans for
5940
each of the relations in 'remaining_tables', expands the current partial
5941
plan with the access plan that results in lowest cost of the expanded
5942
partial plan, and removes the corresponding relation from
5943
'remaining_tables'. The algorithm continues until it either constructs a
5944
complete optimal plan, or constructs an optimal plartial plan with size =
5947
The final optimal plan is stored in 'join->best_positions'. The
5948
corresponding cost of the optimal plan is in 'join->best_read'.
5951
The procedure uses a recursive depth-first search where the depth of the
5952
recursion (and thus the exhaustiveness of the search) is controlled by the
5953
parameter 'search_depth'.
5956
The pseudocode below describes the algorithm of
5957
'best_extension_by_limited_search'. The worst-case complexity of this
5958
algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
5959
the complexity of greedy_search is O(N!).
5962
procedure best_extension_by_limited_search(
5963
pplan in, // in, partial plan of tables-joined-so-far
5964
pplan_cost, // in, cost of pplan
5965
remaining_tables, // in, set of tables not referenced in pplan
5966
best_plan_so_far, // in/out, best plan found so far
5967
best_plan_so_far_cost,// in/out, cost of best_plan_so_far
5968
search_depth) // in, maximum size of the plans being considered
5970
for each table T from remaining_tables
5972
// Calculate the cost of using table T as above
5973
cost = complex-series-of-calculations;
5975
// Add the cost to the cost so far.
5978
if (pplan_cost >= best_plan_so_far_cost)
5979
// pplan_cost already too great, stop search
5982
pplan= expand pplan by best_access_method;
5983
remaining_tables= remaining_tables - table T;
5984
if (remaining_tables is not an empty set
5988
best_extension_by_limited_search(pplan, pplan_cost,
5991
best_plan_so_far_cost,
5996
best_plan_so_far_cost= pplan_cost;
5997
best_plan_so_far= pplan;
6004
When 'best_extension_by_limited_search' is called for the first time,
6005
'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6006
The actual implementation provides a way to optionally use pruning
6007
heuristic (controlled by the parameter 'prune_level') to reduce the search
6008
space by skipping some partial plans.
6011
The parameter 'search_depth' provides control over the recursion
6012
depth, and thus the size of the resulting optimal plan.
6014
@param join pointer to the structure providing all context info
6016
@param remaining_tables set of tables not included into the partial plan yet
6017
@param idx length of the partial QEP in 'join->positions';
6018
since a depth-first search is used, also corresponds
6019
to the current depth of the search tree;
6020
also an index in the array 'join->best_ref';
6021
@param record_count estimate for the number of records returned by the
6023
@param read_time the cost of the best partial plan
6024
@param search_depth maximum depth of the recursion and thus size of the
6026
(0 < search_depth <= join->tables+1).
6027
@param prune_level pruning heuristics that should be applied during
6029
(values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6038
best_extension_by_limited_search(JOIN *join,
6039
table_map remaining_tables,
6041
double record_count,
6046
THD *thd= join->thd;
6047
if (thd->killed) // Abort
6051
'join' is a partial plan with lower cost than the best plan so far,
6052
so continue expanding it further with the tables in 'remaining_tables'.
6055
double best_record_count= DBL_MAX;
6056
double best_read_time= DBL_MAX;
6058
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6060
table_map real_table_bit= s->table->map;
6061
if ((remaining_tables & real_table_bit) &&
6062
!(remaining_tables & s->dependent) &&
6063
(!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6065
double current_record_count, current_read_time;
6066
advance_sj_state(remaining_tables, s);
6069
psergey-insideout-todo:
6070
when best_access_path() detects it could do an InsideOut scan or
6071
some other scan, have it return an insideout scan and a flag that
6072
requests to "fork" this loop iteration. (Q: how does that behave
6073
when the depth is insufficient??)
6075
/* Find the best access method from 's' to the current partial plan */
6076
best_access_path(join, s, thd, remaining_tables, idx,
6077
record_count, read_time);
6078
/* Compute the cost of extending the plan with 's' */
6079
current_record_count= record_count * join->positions[idx].records_read;
6080
current_read_time= read_time + join->positions[idx].read_time;
6082
/* Expand only partial plans with lower cost than the best QEP so far */
6083
if ((current_read_time +
6084
current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6086
restore_prev_nj_state(s);
6087
restore_prev_sj_state(remaining_tables, s);
6092
Prune some less promising partial plans. This heuristic may miss
6093
the optimal QEPs, thus it results in a non-exhaustive search.
6095
if (prune_level == 1)
6097
if (best_record_count > current_record_count ||
6098
best_read_time > current_read_time ||
6099
(idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6101
if (best_record_count >= current_record_count &&
6102
best_read_time >= current_read_time &&
6103
/* TODO: What is the reasoning behind this condition? */
6104
(!(s->key_dependent & remaining_tables) ||
6105
join->positions[idx].records_read < 2.0))
6107
best_record_count= current_record_count;
6108
best_read_time= current_read_time;
6113
restore_prev_nj_state(s);
6114
restore_prev_sj_state(remaining_tables, s);
6119
if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6120
{ /* Recursively expand the current partial plan */
6121
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6122
if (best_extension_by_limited_search(join,
6123
remaining_tables & ~real_table_bit,
6125
current_record_count,
6130
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6134
'join' is either the best partial QEP with 'search_depth' relations,
6135
or the best complete QEP so far, whichever is smaller.
6137
current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6138
if (join->sort_by_table &&
6139
join->sort_by_table !=
6140
join->positions[join->const_tables].table->table)
6141
/* We have to make a temp table */
6142
current_read_time+= current_record_count;
6143
if ((search_depth == 1) || (current_read_time < join->best_read))
6145
memcpy(join->best_positions, join->positions,
6146
sizeof(POSITION) * (idx + 1));
6147
join->best_read= current_read_time - 0.001;
6150
restore_prev_nj_state(s);
6151
restore_prev_sj_state(remaining_tables, s);
6160
- TODO: this function is here only temporarily until 'greedy_search' is
6161
tested and accepted.
6168
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6171
THD *thd= join->thd;
6176
read_time+=record_count/(double) TIME_FOR_COMPARE;
6177
if (join->sort_by_table &&
6178
join->sort_by_table !=
6179
join->positions[join->const_tables].table->table)
6180
read_time+=record_count; // We have to make a temp table
6181
if (read_time < join->best_read)
6183
memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
6184
join->best_read= read_time - 0.001;
6188
if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6189
return(false); /* Found better before */
6192
double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6193
for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6195
table_map real_table_bit=s->table->map;
6196
if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6197
(!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6199
double records, best;
6200
advance_sj_state(rest_tables, s);
6201
best_access_path(join, s, thd, rest_tables, idx, record_count,
6203
records= join->positions[idx].records_read;
6204
best= join->positions[idx].read_time;
6206
Go to the next level only if there hasn't been a better key on
6207
this level! This will cut down the search for a lot simple cases!
6209
double current_record_count=record_count*records;
6210
double current_read_time=read_time+best;
6211
if (best_record_count > current_record_count ||
6212
best_read_time > current_read_time ||
6213
(idx == join->const_tables && s->table == join->sort_by_table))
6215
if (best_record_count >= current_record_count &&
6216
best_read_time >= current_read_time &&
6217
(!(s->key_dependent & rest_tables) || records < 2.0))
6219
best_record_count=current_record_count;
6220
best_read_time=current_read_time;
6222
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6223
if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6224
current_record_count,current_read_time))
6226
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6228
restore_prev_nj_state(s);
6229
restore_prev_sj_state(rest_tables, s);
6230
if (join->select_options & SELECT_STRAIGHT_JOIN)
6231
break; // Don't test all combinations
6239
847
Find how much space the prevous read not const tables takes in cache.
6242
static void calc_used_field_length(THD *thd __attribute__((unused)),
849
void calc_used_field_length(Session *, JoinTable *join_tab)
6245
uint null_fields,blobs,fields,rec_length;
851
uint32_t null_fields,blobs,fields,rec_length;
6246
852
Field **f_ptr,*field;
6247
MY_BITMAP *read_set= join_tab->table->read_set;;
6249
854
null_fields= blobs= fields= rec_length=0;
6250
855
for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
6252
if (bitmap_is_set(read_set, field->field_index))
857
if (field->isReadSet())
6254
uint flags=field->flags;
859
uint32_t flags=field->flags;
6256
861
rec_length+=field->pack_length();
6257
862
if (flags & BLOB_FLAG)
6259
864
if (!(flags & NOT_NULL_FLAG))
6263
868
if (null_fields)
6264
rec_length+=(join_tab->table->s->null_fields+7)/8;
869
rec_length+=(join_tab->table->getNullFields() + 7)/8;
6265
870
if (join_tab->table->maybe_null)
6266
871
rec_length+=sizeof(bool);
6269
uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6270
(join_tab->table->s->reclength- rec_length));
6271
rec_length+=(uint) max((uint)4,blob_length);
6273
join_tab->used_fields=fields;
6274
join_tab->used_fieldlength=rec_length;
6275
join_tab->used_blobs=blobs;
6280
cache_record_length(JOIN *join,uint idx)
6283
JOIN_TAB **pos,**end;
6286
for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6290
JOIN_TAB *join_tab= *pos;
6291
if (!join_tab->used_fieldlength) /* Not calced yet */
6292
calc_used_field_length(thd, join_tab);
6293
length+=join_tab->used_fieldlength;
6300
Get the number of different row combinations for subset of partial join
6304
join The join structure
6305
idx Number of tables in the partial join order (i.e. the
6306
partial join order is in join->positions[0..idx-1])
6307
found_ref Bitmap of tables for which we need to find # of distinct
6311
Given a partial join order (in join->positions[0..idx-1]) and a subset of
6312
tables within that join order (specified in found_ref), find out how many
6313
distinct row combinations of subset tables will be in the result of the
6316
This is used as follows: Suppose we have a table accessed with a ref-based
6317
method. The ref access depends on current rows of tables in found_ref.
6318
We want to count # of different ref accesses. We assume two ref accesses
6319
will be different if at least one of access parameters is different.
6320
Example: consider a query
6322
SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6325
t1, ref access on t1.key=c1
6326
t2, ref access on t2.key=c2
6327
t3, ref access on t3.key=t1.field
6329
For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6330
For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6331
For t3: n_ref_scans = records_read(t1)*records_read(t2)
6332
n_distinct_ref_scans = #records_read(t1)
6334
The reason for having this function (at least the latest version of it)
6335
is that we need to account for buffering in join execution.
6337
An edge-case example: if we have a non-first table in join accessed via
6338
ref(const) or ref(param) where there is a small number of different
6339
values of param, then the access will likely hit the disk cache and will
6340
not require any disk seeks.
6342
The proper solution would be to assume an LRU disk cache of some size,
6343
calculate probability of cache hits, etc. For now we just count
6344
identical ref accesses as one.
6347
Expected number of row combinations
6351
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6354
POSITION *pos_end= join->positions - 1;
6355
for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6357
if (pos->table->table->map & found_ref)
6359
found_ref|= pos->ref_depend_map;
6361
For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
6362
with no matching row we will get position[t2].records_read==0.
6363
Actually the size of output is one null-complemented row, therefore
6364
we will use value of 1 whenever we get records_read==0.
6367
- the above case can't occur if inner part of outer join has more
6368
than one table: table with no matches will not be marked as const.
6370
- Ideally we should add 1 to records_read for every possible null-
6371
complemented row. We're not doing it because: 1. it will require
6372
non-trivial code and add overhead. 2. The value of records_read
6373
is an inprecise estimate and adding 1 (or, in the worst case,
6374
#max_nested_outer_joins=64-1) will not make it any more precise.
6376
if (pos->records_read > DBL_EPSILON)
6377
found*= pos->records_read;
874
uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
875
(join_tab->table->getRecordLength()- rec_length));
876
rec_length+= max((uint32_t)4,blob_length);
878
join_tab->used_fields= fields;
879
join_tab->used_fieldlength= rec_length;
880
join_tab->used_blobs= blobs;
883
StoredKey *get_store_key(Session *session,
884
optimizer::KeyUse *keyuse,
885
table_map used_tables,
886
KEY_PART_INFO *key_part,
887
unsigned char *key_buff,
890
Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
891
if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
893
return new store_key_const_item(session,
895
key_buff + maybe_null,
896
maybe_null ? key_buff : 0,
900
else if (key_use_val->type() == Item::FIELD_ITEM ||
901
(key_use_val->type() == Item::REF_ITEM &&
902
key_use_val->ref_type() == Item_ref::OUTER_REF &&
903
(*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
904
key_use_val->real_item()->type() == Item::FIELD_ITEM))
906
return new store_key_field(session,
908
key_buff + maybe_null,
909
maybe_null ? key_buff : 0,
911
((Item_field*) key_use_val->real_item())->field,
912
key_use_val->full_name());
914
return new store_key_item(session,
916
key_buff + maybe_null,
917
maybe_null ? key_buff : 0,
6385
Set up join struct according to best position.
923
This function is only called for const items on fields which are keys.
926
returns 1 if there was some conversion made when the field was stored.
6389
get_best_combination(JOIN *join)
6392
table_map used_tables;
6393
JOIN_TAB *join_tab,*j;
6398
table_count=join->tables;
6399
if (!(join->join_tab=join_tab=
6400
(JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6405
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read
6406
for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
928
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
931
Table *table= field->table;
932
Session *session= table->in_use;
933
ha_rows cuted_fields=session->cuted_fields;
936
we should restore old value of count_cuted_fields because
937
store_val_in_field can be called from mysql_insert
938
with select_insert, which make count_cuted_fields= 1
940
enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
941
session->count_cuted_fields= check_flag;
942
error= item->save_in_field(field, 1);
943
session->count_cuted_fields= old_count_cuted_fields;
944
return error || cuted_fields != session->cuted_fields;
947
inline void add_cond_and_fix(Item **e1, Item *e2)
6409
*j= *join->best_positions[tablenr].table;
6410
form=join->table[tablenr]=j->table;
6411
used_tables|= form->map;
6412
form->reginfo.join_tab=j;
6413
if (!*j->on_expr_ref)
6414
form->reginfo.not_exists_optimize=0; // Only with LEFT JOIN
6415
if (j->type == JT_CONST)
6416
continue; // Handled in make_join_stat..
6421
if (j->type == JT_SYSTEM)
6423
if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
952
if ((res= new Item_cond_and(*e1, e2)))
6426
if (tablenr != join->const_tables)
955
res->quick_fix_field();
6429
else if (create_ref_for_key(join, j, keyuse, used_tables))
6430
return(true); // Something went wrong
6433
for (i=0 ; i < table_count ; i++)
6434
join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6435
update_depend_map(join);
6440
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6441
table_map used_tables)
962
bool create_ref_for_key(JOIN *join,
964
optimizer::KeyUse *org_keyuse,
965
table_map used_tables)
6443
KEYUSE *keyuse=org_keyuse;
6444
THD *thd= join->thd;
6445
uint keyparts,length,key;
967
optimizer::KeyUse *keyuse= org_keyuse;
968
Session *session= join->session;
6449
975
/* Use best key from find_best */
6452
keyinfo=table->key_info+key;
977
key= keyuse->getKey();
978
keyinfo= table->key_info + key;
6456
uint found_part_ref_or_null= 0;
982
uint32_t found_part_ref_or_null= 0;
6458
984
Calculate length for the used key
6459
985
Stop if there is a missing key part or when we find second key_part
6846
Fill in outer join related info for the execution plan structure.
6848
For each outer join operation left after simplification of the
6849
original query the function set up the following pointers in the linear
6850
structure join->join_tab representing the selected execution plan.
6851
The first inner table t0 for the operation is set to refer to the last
6852
inner table tk through the field t0->last_inner.
6853
Any inner table ti for the operation are set to refer to the first
6854
inner table ti->first_inner.
6855
The first inner table t0 for the operation is set to refer to the
6856
first inner table of the embedding outer join operation, if there is any,
6857
through the field t0->first_upper.
6858
The on expression for the outer join operation is attached to the
6859
corresponding first inner table through the field t0->on_expr_ref.
6860
Here ti are structures of the JOIN_TAB type.
6862
EXAMPLE. For the query:
6866
(t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6867
ON (t1.a=t2.a AND t1.b=t3.b)
6871
given the execution plan with the table order t1,t2,t3,t4
6872
is selected, the following references will be set;
6873
t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6874
t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6875
on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
6876
*t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6878
@param join reference to the info fully describing the query
6881
The function assumes that the simplification procedure has been
6882
already applied to the join query (see simplify_joins).
6883
This function can be called only after the execution plan
6888
make_outerjoin_info(JOIN *join)
6890
for (uint i=join->const_tables ; i < join->tables ; i++)
6892
JOIN_TAB *tab=join->join_tab+i;
6893
TABLE *table=tab->table;
6894
TABLE_LIST *tbl= table->pos_in_table_list;
6895
TABLE_LIST *embedding= tbl->embedding;
6897
if (tbl->outer_join)
6900
Table tab is the only one inner table for outer join.
6901
(Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6902
is in the query above.)
6904
tab->last_inner= tab->first_inner= tab;
6905
tab->on_expr_ref= &tbl->on_expr;
6906
tab->cond_equal= tbl->cond_equal;
6908
tab->first_upper= embedding->nested_join->first_nested;
6910
for ( ; embedding ; embedding= embedding->embedding)
6912
/* Ignore sj-nests: */
6913
if (!embedding->on_expr)
6915
NESTED_JOIN *nested_join= embedding->nested_join;
6916
if (!nested_join->counter_)
6919
Table tab is the first inner table for nested_join.
6920
Save reference to it in the nested join structure.
6922
nested_join->first_nested= tab;
6923
tab->on_expr_ref= &embedding->on_expr;
6924
tab->cond_equal= tbl->cond_equal;
6925
if (embedding->embedding)
6926
tab->first_upper= embedding->embedding->nested_join->first_nested;
6928
if (!tab->first_inner)
6929
tab->first_inner= nested_join->first_nested;
6930
if (++nested_join->counter_ < nested_join->join_list.elements)
6932
/* Table tab is the last inner table for nested join. */
6933
nested_join->first_nested->last_inner= tab;
6941
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6943
THD *thd= join->thd;
6946
add_not_null_conds(join);
6947
table_map used_tables;
6948
if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6949
{ /* there may be a select without a cond. */
6950
if (join->tables > 1)
6951
cond->update_used_tables(); // Tablenr may have changed
6952
if (join->const_tables == join->tables &&
6953
thd->lex->current_select->master_unit() ==
6954
&thd->lex->unit) // not upper level SELECT
6955
join->const_table_map|=RAND_TABLE_BIT;
6956
{ // Check const tables
6958
make_cond_for_table(cond,
6959
join->const_table_map,
6961
for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6962
tab < join->join_tab+join->tables ; tab++)
6964
if (*tab->on_expr_ref)
6966
JOIN_TAB *cond_tab= tab->first_inner;
6967
COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6968
join->const_table_map,
6972
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6975
tmp->quick_fix_field();
6976
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6977
new Item_cond_and(cond_tab->select_cond,
6979
if (!cond_tab->select_cond)
6981
cond_tab->select_cond->quick_fix_field();
6984
if (const_cond && !const_cond->val_int())
6986
return(1); // Impossible const condition
6990
used_tables=((select->const_tables=join->const_table_map) |
6991
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6992
for (uint i=join->const_tables ; i < join->tables ; i++)
6994
JOIN_TAB *tab=join->join_tab+i;
6996
first_inner is the X in queries like:
6997
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
6999
JOIN_TAB *first_inner_tab= tab->first_inner;
7000
table_map current_map= tab->table->map;
7001
bool use_quick_range=0;
7005
Following force including random expression in last table condition.
7006
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7008
if (i == join->tables-1)
7009
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7010
used_tables|=current_map;
7012
if (tab->type == JT_REF && tab->quick &&
7013
(uint) tab->ref.key == tab->quick->index &&
7014
tab->ref.key_length < tab->quick->max_used_key_length)
7016
/* Range uses longer key; Use this instead of ref on key */
7021
tab->ref.key_parts=0; // Don't use ref key.
7022
join->best_positions[i].records_read= rows2double(tab->quick->records);
7024
We will use join cache here : prevent sorting of the first
7025
table only and sort at the end.
7027
if (i != join->const_tables && join->tables > join->const_tables + 1)
7033
tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7034
if (cond && !tmp && tab->quick)
7036
if (tab->type != JT_ALL)
7039
Don't use the quick method
7040
We come here in the case where we have 'key=constant' and
7041
the test is removed by make_cond_for_table()
7049
Hack to handle the case where we only refer to a table
7050
in the ON part of an OUTER JOIN. In this case we want the code
7051
below to check if we should use 'quick' instead.
7053
tmp= new Item_int((int64_t) 1,1); // Always true
7057
if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7058
tab->type == JT_EQ_REF)
7060
SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7061
thd->memdup((uchar*) select,
7064
return(1); // End of memory
7066
If tab is an inner table of an outer join operation,
7067
add a match guard to the pushed down predicate.
7068
The guard will turn the predicate on only after
7069
the first match for outer tables is encountered.
7074
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7075
a cond, so neutralize the hack above.
7077
if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7079
tab->select_cond=sel->cond=tmp;
7080
/* Push condition to storage engine if this is enabled
7081
and the condition is not guarded */
7082
tab->table->file->pushed_cond= NULL;
7083
if (thd->variables.engine_condition_pushdown)
7086
make_cond_for_table(tmp, current_map, current_map, 0);
7089
/* Push condition to handler */
7090
if (!tab->table->file->cond_push(push_cond))
7091
tab->table->file->pushed_cond= push_cond;
7096
tab->select_cond= sel->cond= NULL;
7098
sel->head=tab->table;
7101
/* Use quick key read if it's a constant and it's not used
7103
if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7104
&& (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7106
sel->quick=tab->quick; // Use value from get_quick_...
7107
sel->quick_keys.clear_all();
7108
sel->needed_reg.clear_all();
7116
uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7117
if (i == join->const_tables && ref_key)
7119
if (!tab->const_keys.is_clear_all() &&
7120
tab->table->reginfo.impossible_range)
7123
else if (tab->type == JT_ALL && ! use_quick_range)
7125
if (!tab->const_keys.is_clear_all() &&
7126
tab->table->reginfo.impossible_range)
7127
return(1); // Impossible range
7129
We plan to scan all rows.
7130
Check again if we should use an index.
7131
We could have used an column from a previous table in
7132
the index if we are using limit and this is the first table
7135
if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7136
(!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)))
7138
/* Join with outer join condition */
7139
COND *orig_cond=sel->cond;
7140
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7143
We can't call sel->cond->fix_fields,
7144
as it will break tab->on_expr if it's AND condition
7145
(fix_fields currently removes extra AND/OR levels).
7146
Yet attributes of the just built condition are not needed.
7147
Thus we call sel->cond->quick_fix_field for safety.
7149
if (sel->cond && !sel->cond->fixed)
7150
sel->cond->quick_fix_field();
7152
if (sel->test_quick_select(thd, tab->keys,
7153
used_tables & ~ current_map,
7154
(join->select_options &
7157
join->unit->select_limit_cnt), 0,
7161
Before reporting "Impossible WHERE" for the whole query
7162
we have to check isn't it only "impossible ON" instead
7164
sel->cond=orig_cond;
7165
if (!*tab->on_expr_ref ||
7166
sel->test_quick_select(thd, tab->keys,
7167
used_tables & ~ current_map,
7168
(join->select_options &
7171
join->unit->select_limit_cnt),0,
7173
return(1); // Impossible WHERE
7176
sel->cond=orig_cond;
7178
/* Fix for EXPLAIN */
7180
join->best_positions[i].records_read= (double)sel->quick->records;
7184
sel->needed_reg=tab->needed_reg;
7185
sel->quick_keys.clear_all();
7187
if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7188
!sel->needed_reg.is_subset(tab->checked_keys))
7190
tab->keys=sel->quick_keys;
7191
tab->keys.merge(sel->needed_reg);
7192
tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7193
(select->quick_keys.is_clear_all() ||
7195
(select->quick->records >= 100L)))) ?
7197
sel->read_tables= used_tables & ~current_map;
7199
if (i != join->const_tables && tab->use_quick != 2)
7200
{ /* Read with cache */
7202
(tmp=make_cond_for_table(cond,
7203
join->const_table_map |
7207
tab->cache.select=(SQL_SELECT*)
7208
thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7209
tab->cache.select->cond=tmp;
7210
tab->cache.select->read_tables=join->const_table_map;
7217
Push down conditions from all on expressions.
7218
Each of these conditions are guarded by a variable
7219
that turns if off just before null complemented row for
7220
outer joins is formed. Thus, the condition from an
7221
'on expression' are guaranteed not to be checked for
7222
the null complemented row.
7225
/* First push down constant conditions from on expressions */
7226
for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7227
join_tab < join->join_tab+join->tables ; join_tab++)
7229
if (*join_tab->on_expr_ref)
7231
JOIN_TAB *cond_tab= join_tab->first_inner;
7232
COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7233
join->const_table_map,
7237
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7240
tmp->quick_fix_field();
7241
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7242
new Item_cond_and(cond_tab->select_cond,tmp);
7243
if (!cond_tab->select_cond)
7245
cond_tab->select_cond->quick_fix_field();
7249
/* Push down non-constant conditions from on expressions */
7250
JOIN_TAB *last_tab= tab;
7251
while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7254
Table tab is the last inner table of an outer join.
7255
An on expression is always attached to it.
7257
COND *on_expr= *first_inner_tab->on_expr_ref;
7259
table_map used_tables2= (join->const_table_map |
7260
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7261
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7263
current_map= tab->table->map;
7264
used_tables2|= current_map;
7265
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7269
JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7271
First add the guards for match variables of
7272
all embedding outer join operations.
7274
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7279
Now add the guard turning the predicate off for
7280
the null complemented row.
7282
tmp_cond= new Item_func_trig_cond(tmp_cond,
7286
tmp_cond->quick_fix_field();
7287
/* Add the predicate to other pushed down predicates */
7288
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7289
new Item_cond_and(cond_tab->select_cond,
7291
if (!cond_tab->select_cond)
7293
cond_tab->select_cond->quick_fix_field();
7296
first_inner_tab= first_inner_tab->first_upper;
7305
1215
Check if given expression uses only table fields covered by the given index
9625
2744
if (!(left_const && right_const) &&
9626
2745
args[0]->result_type() == args[1]->result_type())
9630
resolve_const_item(thd, &args[1], args[0]);
9631
func->update_used_tables();
9632
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9635
else if (left_const)
9637
resolve_const_item(thd, &args[0], args[1]);
9638
func->update_used_tables();
9639
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9649
Simplify joins replacing outer joins by inner joins whenever it's
9652
The function, during a retrieval of join_list, eliminates those
9653
outer joins that can be converted into inner join, possibly nested.
9654
It also moves the on expressions for the converted outer joins
9655
and from inner joins to conds.
9656
The function also calculates some attributes for nested joins:
9660
- on_expr_dep_tables
9661
The first two attributes are used to test whether an outer join can
9662
be substituted for an inner join. The third attribute represents the
9663
relation 'to be dependent on' for tables. If table t2 is dependent
9664
on table t1, then in any evaluated execution plan table access to
9665
table t2 must precede access to table t2. This relation is used also
9666
to check whether the query contains invalid cross-references.
9667
The forth attribute is an auxiliary one and is used to calculate
9669
As the attribute dep_tables qualifies possibles orders of tables in the
9670
execution plan, the dependencies required by the straight join
9671
modifiers are reflected in this attribute as well.
9672
The function also removes all braces that can be removed from the join
9673
expression without changing its meaning.
9676
An outer join can be replaced by an inner join if the where condition
9677
or the on expression for an embedding nested join contains a conjunctive
9678
predicate rejecting null values for some attribute of the inner tables.
9682
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9684
the predicate t2.b < 5 rejects nulls.
9685
The query is converted first to:
9687
SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9689
then to the equivalent form:
9691
SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9695
Similarly the following query:
9697
SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9702
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
9706
One conversion might trigger another:
9708
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9709
LEFT JOIN t3 ON t3.b=t2.b
9710
WHERE t3 IS NOT NULL =>
9711
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9712
WHERE t3 IS NOT NULL AND t3.b=t2.b =>
9713
SELECT * FROM t1, t2, t3
9714
WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9717
The function removes all unnecessary braces from the expression
9718
produced by the conversions.
9721
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9723
finally is converted to:
9725
SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9730
It also will remove braces from the following queries:
9732
SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9733
SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9736
The benefit of this simplification procedure is that it might return
9737
a query for which the optimizer can evaluate execution plan with more
9738
join orders. With a left join operation the optimizer does not
9739
consider any plan where one of the inner tables is before some of outer
9743
The function is implemented by a recursive procedure. On the recursive
9744
ascent all attributes are calculated, all outer joins that can be
9745
converted are replaced and then all unnecessary braces are removed.
9746
As join list contains join tables in the reverse order sequential
9747
elimination of outer joins does not require extra recursive calls.
9750
Remove all semi-joins that have are within another semi-join (i.e. have
9751
an "ancestor" semi-join nest)
9754
Here is an example of a join query with invalid cross references:
9756
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
9759
@param join reference to the query info
9760
@param join_list list representation of the join to be converted
9761
@param conds conditions to add on expressions for converted joins
9762
@param top true <=> conds is the where condition
9765
- The new condition, if success
9770
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9774
NESTED_JOIN *nested_join;
9775
TABLE_LIST *prev_table= 0;
9776
List_iterator<TABLE_LIST> li(*join_list);
9779
Try to simplify join operations from join_list.
9780
The most outer join operation is checked for conversion first.
9782
while ((table= li++))
9784
table_map used_tables;
9785
table_map not_null_tables= (table_map) 0;
9787
if ((nested_join= table->nested_join))
9790
If the element of join_list is a nested join apply
9791
the procedure to its nested join list first.
9795
Item *expr= table->on_expr;
9797
If an on expression E is attached to the table,
9798
check all null rejected predicates in this expression.
9799
If such a predicate over an attribute belonging to
9800
an inner table of an embedded outer join is found,
9801
the outer join is converted to an inner join and
9802
the corresponding on expression is added to E.
9804
expr= simplify_joins(join, &nested_join->join_list,
9805
expr, false, in_sj || table->sj_on_expr);
9807
if (!table->prep_on_expr || expr != table->on_expr)
9811
table->on_expr= expr;
9812
table->prep_on_expr= expr->copy_andor_structure(join->thd);
9815
nested_join->used_tables= (table_map) 0;
9816
nested_join->not_null_tables=(table_map) 0;
9817
conds= simplify_joins(join, &nested_join->join_list, conds, top,
9818
in_sj || table->sj_on_expr);
9819
used_tables= nested_join->used_tables;
9820
not_null_tables= nested_join->not_null_tables;
9824
if (!table->prep_on_expr)
9825
table->prep_on_expr= table->on_expr;
9826
used_tables= table->table->map;
9828
not_null_tables= conds->not_null_tables();
9831
if (table->embedding)
9833
table->embedding->nested_join->used_tables|= used_tables;
9834
table->embedding->nested_join->not_null_tables|= not_null_tables;
9837
if (!table->outer_join || (used_tables & not_null_tables))
9840
For some of the inner tables there are conjunctive predicates
9841
that reject nulls => the outer join can be replaced by an inner join.
9843
table->outer_join= 0;
9846
/* Add ON expression to the WHERE or upper-level ON condition. */
9849
conds= and_conds(conds, table->on_expr);
9850
conds->top_level_item();
9851
/* conds is always a new item as both cond and on_expr existed */
9852
assert(!conds->fixed);
9853
conds->fix_fields(join->thd, &conds);
9856
conds= table->on_expr;
9857
table->prep_on_expr= table->on_expr= 0;
9865
Only inner tables of non-convertible outer joins
9866
remain with on_expr.
9870
table->dep_tables|= table->on_expr->used_tables();
9871
if (table->embedding)
9873
table->dep_tables&= ~table->embedding->nested_join->used_tables;
9875
Embedding table depends on tables used
9876
in embedded on expressions.
9878
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9881
table->dep_tables&= ~table->table->map;
9886
/* The order of tables is reverse: prev_table follows table */
9887
if (prev_table->straight)
9888
prev_table->dep_tables|= used_tables;
9889
if (prev_table->on_expr)
9891
prev_table->dep_tables|= table->on_expr_dep_tables;
9892
table_map prev_used_tables= prev_table->nested_join ?
9893
prev_table->nested_join->used_tables :
9894
prev_table->table->map;
9896
If on expression contains only references to inner tables
9897
we still make the inner tables dependent on the outer tables.
9898
It would be enough to set dependency only on one outer table
9899
for them. Yet this is really a rare case.
9901
if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9902
prev_table->dep_tables|= used_tables;
9909
Flatten nested joins that can be flattened.
9910
no ON expression and not a semi-join => can be flattened.
9913
while ((table= li++))
9915
nested_join= table->nested_join;
9916
if (table->sj_on_expr && !in_sj)
9919
If this is a semi-join that is not contained within another semi-join,
9920
leave it intact (otherwise it is flattened)
9922
join->select_lex->sj_nests.push_back(table);
9924
else if (nested_join && !table->on_expr)
9927
List_iterator<TABLE_LIST> it(nested_join->join_list);
9930
tbl->embedding= table->embedding;
9931
tbl->join_list= table->join_list;
9933
li.replace(nested_join->join_list);
9941
Assign each nested join structure a bit in nested_join_map.
9943
Assign each nested join structure (except "confluent" ones - those that
9944
embed only one element) a bit in nested_join_map.
9946
@param join Join being processed
9947
@param join_list List of tables
9948
@param first_unused Number of first unused bit in nested_join_map before the
9952
This function is called after simplify_joins(), when there are no
9953
redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9954
we will not run out of bits in nested_join_map.
9957
First unused bit in nested_join_map after the call.
9960
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
9963
List_iterator<TABLE_LIST> li(*join_list);
9965
while ((table= li++))
9967
NESTED_JOIN *nested_join;
9968
if ((nested_join= table->nested_join))
9971
It is guaranteed by simplify_joins() function that a nested join
9972
that has only one child is either
9973
- a single-table view (the child is the underlying table), or
9974
- a single-table semi-join nest
9976
We don't assign bits to such sj-nests because
9977
1. it is redundant (a "sequence" of one table cannot be interleaved
9979
2. we could run out bits in nested_join_map otherwise.
9981
if (nested_join->join_list.elements != 1)
9983
/* Don't assign bits to sj-nests */
9985
nested_join->nj_map= (nested_join_map) 1 << first_unused++;
9986
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
9991
return(first_unused);
9996
Set NESTED_JOIN::counter=0 in all nested joins in passed list.
9998
Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
9999
the passed join_list.
10001
@param join_list List of nested joins to process. It may also contain base
10002
tables which will be ignored.
10005
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10007
List_iterator<TABLE_LIST> li(*join_list);
10009
while ((table= li++))
10011
NESTED_JOIN *nested_join;
10012
if ((nested_join= table->nested_join))
10014
nested_join->counter_= 0;
10015
reset_nj_counters(&nested_join->join_list);
2749
resolve_const_item(session, &args[1], args[0]);
2750
func->update_used_tables();
2751
change_cond_ref_to_const(session, save_list, and_father, and_father,
2754
else if (left_const)
2756
resolve_const_item(session, &args[0], args[1]);
2757
func->update_used_tables();
2758
change_cond_ref_to_const(session, save_list, and_father, and_father,
10023
2767
Check interleaving with an inner tables of an outer join for
10024
2768
extension table.
10026
Check if table next_tab can be added to current partial join order, and
2770
Check if table next_tab can be added to current partial join order, and
10027
2771
if yes, record that it has been added.
10029
2773
The function assumes that both current partial join order and its
10030
2774
extension with next_tab are valid wrt table dependencies.
10034
LIMITATIONS ON JOIN ORDER
2778
LIMITATIONS ON JOIN order_st
10035
2779
The nested [outer] joins executioner algorithm imposes these limitations
10036
2780
on join order:
10037
1. "Outer tables first" - any "outer" table must be before any
2781
1. "Outer tables first" - any "outer" table must be before any
10038
2782
corresponding "inner" table.
10039
2783
2. "No interleaving" - tables inside a nested join must form a continuous
10040
sequence in join order (i.e. the sequence must not be interrupted by
2784
sequence in join order (i.e. the sequence must not be interrupted by
10041
2785
tables that are outside of this nested join).
10043
2787
#1 is checked elsewhere, this function checks #2 provided that #1 has
10044
2788
been already checked.
10046
2790
WHY NEED NON-INTERLEAVING
10047
Consider an example:
2791
Consider an example:
10049
2793
select * from t0 join t1 left join (t2 join t3) on cond1
10503
3178
if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10506
return right_item->eq(*const_item, 1);
10507
*const_item=right_item;
3181
return right_item->eq(*const_item, 1);
3182
*const_item=right_item;
10511
3186
else if (right_item->eq(comp_item,1))
10513
3188
if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10516
return left_item->eq(*const_item, 1);
10517
*const_item=left_item;
10525
/****************************************************************************
10526
Create internal temporary table
10527
****************************************************************************/
10530
Create field for temporary table from given field.
10532
@param thd Thread handler
10533
@param org_field field from which new field will be created
10534
@param name New field name
10535
@param table Temporary table
10536
@param item !=NULL if item->result_field should point to new field.
10537
This is relevant for how fill_record() is going to work:
10538
If item != NULL then fill_record() will update
10539
the record in the original table.
10540
If item == NULL then fill_record() will update
10541
the temporary table
10542
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10543
field instead of blob.
10551
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10552
const char *name, TABLE *table,
10553
Item_field *item, uint convert_blob_length)
10558
Make sure that the blob fits into a Field_varstring which has
10561
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10562
(org_field->flags & BLOB_FLAG))
10563
new_field= new Field_varstring(convert_blob_length,
10564
org_field->maybe_null(),
10565
org_field->field_name, table->s,
10566
org_field->charset());
10568
new_field= org_field->new_field(thd->mem_root, table,
10569
table == org_field->table);
10572
new_field->init(table);
10573
new_field->orig_table= org_field->orig_table;
10575
item->result_field= new_field;
10577
new_field->field_name= name;
10578
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10579
if (org_field->maybe_null() || (item && item->maybe_null))
10580
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
10581
if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
10582
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10583
else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
10584
((Field_double *) new_field)->not_fixed= true;
10590
Create field for temporary table using type of given item.
10592
@param thd Thread handler
10593
@param item Item to create a field for
10594
@param table Temporary table
10595
@param copy_func If set and item is a function, store copy of
10597
@param modify_item 1 if item->result_field should point to new
10598
item. This is relevent for how fill_record()
10600
If modify_item is 1 then fill_record() will
10601
update the record in the original table.
10602
If modify_item is 0 then fill_record() will
10603
update the temporary table
10604
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10605
field instead of blob.
10613
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
10614
Item *item, TABLE *table,
10615
Item ***copy_func, bool modify_item,
10616
uint convert_blob_length)
10618
bool maybe_null= item->maybe_null;
10621
switch (item->result_type()) {
10623
new_field= new Field_double(item->max_length, maybe_null,
10624
item->name, item->decimals, true);
10628
Select an integer type with the minimal fit precision.
10629
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10630
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
10631
Field_long : make them Field_int64_t.
10633
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10634
new_field=new Field_int64_t(item->max_length, maybe_null,
10635
item->name, item->unsigned_flag);
10637
new_field=new Field_long(item->max_length, maybe_null,
10638
item->name, item->unsigned_flag);
10640
case STRING_RESULT:
10641
assert(item->collation.collation);
10643
enum enum_field_types type;
10645
DATE/TIME fields have STRING_RESULT result type.
10646
To preserve type they needed to be handled separately.
10648
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
10649
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
10650
type == DRIZZLE_TYPE_TIMESTAMP)
10651
new_field= item->tmp_table_field_from_field_type(table, 1);
10653
Make sure that the blob fits into a Field_varstring which has
10656
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10657
convert_blob_length <= Field_varstring::MAX_SIZE &&
10658
convert_blob_length)
10659
new_field= new Field_varstring(convert_blob_length, maybe_null,
10660
item->name, table->s,
10661
item->collation.collation);
10663
new_field= item->make_string_field(table);
10664
new_field->set_derivation(item->collation.derivation);
10666
case DECIMAL_RESULT:
10668
uint8_t dec= item->decimals;
10669
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
10670
uint32_t len= item->max_length;
10673
Trying to put too many digits overall in a DECIMAL(prec,dec)
10674
will always throw a warning. We must limit dec to
10675
DECIMAL_MAX_SCALE however to prevent an assert() later.
10680
signed int overflow;
10682
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
10685
If the value still overflows the field with the corrected dec,
10686
we'll throw out decimals rather than integers. This is still
10687
bad and of course throws a truncation warning.
10688
+1: for decimal point
10691
overflow= my_decimal_precision_to_length(intg + dec, dec,
10692
item->unsigned_flag) - len;
10695
dec= max(0, dec - overflow); // too long, discard fract
10697
len -= item->decimals - dec; // corrected value fits
10700
new_field= new Field_new_decimal(len, maybe_null, item->name,
10701
dec, item->unsigned_flag);
10706
// This case should never be choosen
10712
new_field->init(table);
10714
if (copy_func && item->is_result_field())
10715
*((*copy_func)++) = item; // Save for copy_funcs
10717
item->set_result_field(new_field);
10718
if (item->type() == Item::NULL_ITEM)
10719
new_field->is_created_from_null_item= true;
10725
Create field for information schema table.
10727
@param thd Thread handler
10728
@param table Temporary table
10729
@param item Item to create a field for
10737
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
10738
Item *item, TABLE *table)
10740
if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
10743
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10744
field= new Field_blob(item->max_length, item->maybe_null,
10745
item->name, item->collation.collation);
10747
field= new Field_varstring(item->max_length, item->maybe_null,
10749
table->s, item->collation.collation);
10751
field->init(table);
10754
return item->tmp_table_field_from_field_type(table, 0);
10759
Create field for temporary table.
10761
@param thd Thread handler
10762
@param table Temporary table
10763
@param item Item to create a field for
10764
@param type Type of item (normally item->type)
10765
@param copy_func If set and item is a function, store copy of item
10767
@param from_field if field will be created using other field as example,
10768
pointer example field will be written here
10769
@param default_field If field has a default value field, store it here
10770
@param group 1 if we are going to do a relative group by on result
10771
@param modify_item 1 if item->result_field should point to new item.
10772
This is relevent for how fill_record() is going to
10774
If modify_item is 1 then fill_record() will update
10775
the record in the original table.
10776
If modify_item is 0 then fill_record() will update
10777
the temporary table
10778
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10779
field instead of blob.
10787
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10788
Item ***copy_func, Field **from_field,
10789
Field **default_field,
10790
bool group, bool modify_item,
10791
bool table_cant_handle_bit_fields __attribute__((unused)),
10792
bool make_copy_field,
10793
uint convert_blob_length)
10796
Item::Type orig_type= type;
10797
Item *orig_item= 0;
10799
if (type != Item::FIELD_ITEM &&
10800
item->real_item()->type() == Item::FIELD_ITEM)
10803
item= item->real_item();
10804
type= Item::FIELD_ITEM;
10808
case Item::SUM_FUNC_ITEM:
10810
Item_sum *item_sum=(Item_sum*) item;
10811
result= item_sum->create_tmp_field(group, table, convert_blob_length);
10813
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10816
case Item::FIELD_ITEM:
10817
case Item::DEFAULT_VALUE_ITEM:
10819
Item_field *field= (Item_field*) item;
10820
bool orig_modify= modify_item;
10821
if (orig_type == Item::REF_ITEM)
10824
If item have to be able to store NULLs but underlaid field can't do it,
10825
create_tmp_field_from_field() can't be used for tmp field creation.
10827
if (field->maybe_null && !field->field->maybe_null())
10829
result= create_tmp_field_from_item(thd, item, table, NULL,
10830
modify_item, convert_blob_length);
10831
*from_field= field->field;
10832
if (result && modify_item)
10833
field->result_field= result;
10836
result= create_tmp_field_from_field(thd, (*from_field= field->field),
10837
orig_item ? orig_item->name :
10840
modify_item ? field :
10842
convert_blob_length);
10843
if (orig_type == Item::REF_ITEM && orig_modify)
10844
((Item_ref*)orig_item)->set_result_field(result);
10845
if (field->field->eq_def(result))
10846
*default_field= field->field;
10850
case Item::FUNC_ITEM:
10852
case Item::COND_ITEM:
10853
case Item::FIELD_AVG_ITEM:
10854
case Item::FIELD_STD_ITEM:
10855
case Item::SUBSELECT_ITEM:
10856
/* The following can only happen with 'CREATE TABLE ... SELECT' */
10857
case Item::PROC_ITEM:
10858
case Item::INT_ITEM:
10859
case Item::REAL_ITEM:
10860
case Item::DECIMAL_ITEM:
10861
case Item::STRING_ITEM:
10862
case Item::REF_ITEM:
10863
case Item::NULL_ITEM:
10864
case Item::VARBIN_ITEM:
10865
if (make_copy_field)
10867
assert(((Item_result_field*)item)->result_field);
10868
*from_field= ((Item_result_field*)item)->result_field;
10870
return create_tmp_field_from_item(thd, item, table,
10871
(make_copy_field ? 0 : copy_func),
10872
modify_item, convert_blob_length);
10873
case Item::TYPE_HOLDER:
10874
result= ((Item_type_holder *)item)->make_field_by_type(table);
10875
result->set_derivation(item->collation.derivation);
10877
default: // Dosen't have to be stored
10883
Set up column usage bitmaps for a temporary table
10886
For temporary tables, we need one bitmap with all columns set and
10887
a tmp_set bitmap to be used by things like filesort.
10890
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10892
uint field_count= table->s->fields;
10893
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10895
bitmap_init(&table->tmp_set,
10896
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10897
field_count, false);
10898
/* write_set and all_set are copies of read_set */
10899
table->def_write_set= table->def_read_set;
10900
table->s->all_set= table->def_read_set;
10901
bitmap_set_all(&table->s->all_set);
10902
table->default_column_bitmaps();
10907
Create a temp table according to a field list.
10909
Given field pointers are changed to point at tmp_table for
10910
send_fields. The table object is self contained: it's
10911
allocated in its own memory root, as well as Field objects
10912
created for table columns.
10913
This function will replace Item_sum items in 'fields' list with
10914
corresponding Item_field items, pointing at the fields in the
10915
temporary table, unless this was prohibited by true
10916
value of argument save_sum_fields. The Item_field objects
10917
are created in THD memory root.
10919
@param thd thread handle
10920
@param param a description used as input to create the table
10921
@param fields list of items that will be used to define
10922
column types of the table (also see NOTES)
10923
@param group TODO document
10924
@param distinct should table rows be distinct
10925
@param save_sum_fields see NOTES
10926
@param select_options
10928
@param table_alias possible name of the temporary table that can
10929
be used for name resolving; can be "".
10932
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10933
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
10934
#define RATIO_TO_PACK_ROWS 2
10935
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
10938
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10939
ORDER *group, bool distinct, bool save_sum_fields,
10940
uint64_t select_options, ha_rows rows_limit,
10943
MEM_ROOT *mem_root_save, own_root;
10945
TABLE_SHARE *share;
10946
uint i,field_count,null_count,null_pack_length;
10947
uint copy_func_count= param->func_count;
10948
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
10949
uint blob_count,group_null_items, string_count;
10950
uint temp_pool_slot=MY_BIT_NONE;
10952
ulong reclength, string_total_length;
10953
bool using_unique_constraint= 0;
10954
bool use_packed_rows= 0;
10955
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10956
char *tmpname,path[FN_REFLEN];
10957
uchar *pos, *group_buff, *bitmaps;
10959
Field **reg_field, **from_field, **default_field;
10961
Copy_field *copy=0;
10963
KEY_PART_INFO *key_part_info;
10965
MI_COLUMNDEF *recinfo;
10966
uint total_uneven_bit_length= 0;
10967
bool force_copy_fields= param->force_copy_fields;
10969
status_var_increment(thd->status_var.created_tmp_tables);
10971
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10972
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10974
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10975
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10976
current_pid, temp_pool_slot);
10979
/* if we run out of slots or we are not using tempool */
10980
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10981
thd->thread_id, thd->tmp_table++);
10985
No need to change table name to lower case as we are only creating
10986
MyISAM or HEAP tables here
10988
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
10993
if (!param->quick_group)
10994
group=0; // Can't use group key
10995
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
10998
marker == 4 means two things:
10999
- store NULLs in the key, and
11000
- convert BIT fields to 64-bit long, needed because MEMORY tables
11001
can't index BIT fields.
11003
(*tmp->item)->marker= 4;
11004
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11005
using_unique_constraint=1;
11007
if (param->group_length >= MAX_BLOB_WIDTH)
11008
using_unique_constraint=1;
11010
distinct=0; // Can't use distinct
11013
field_count=param->field_count+param->func_count+param->sum_func_count;
11014
hidden_field_count=param->hidden_field_count;
11017
When loose index scan is employed as access method, it already
11018
computes all groups and the result of all aggregate functions. We
11019
make space for the items of the aggregate function in the list of
11020
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11021
these items are stored in the temporary table.
11023
if (param->precomputed_group_by)
11024
copy_func_count+= param->sum_func_count;
11026
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11028
if (!multi_alloc_root(&own_root,
11029
&table, sizeof(*table),
11030
&share, sizeof(*share),
11031
®_field, sizeof(Field*) * (field_count+1),
11032
&default_field, sizeof(Field*) * (field_count),
11033
&blob_field, sizeof(uint)*(field_count+1),
11034
&from_field, sizeof(Field*)*field_count,
11035
©_func, sizeof(*copy_func)*(copy_func_count+1),
11036
¶m->keyinfo, sizeof(*param->keyinfo),
11038
sizeof(*key_part_info)*(param->group_parts+1),
11039
¶m->start_recinfo,
11040
sizeof(*param->recinfo)*(field_count*2+4),
11041
&tmpname, (uint) strlen(path)+1,
11042
&group_buff, (group && ! using_unique_constraint ?
11043
param->group_length : 0),
11044
&bitmaps, bitmap_buffer_size(field_count)*2,
11047
if (temp_pool_slot != MY_BIT_NONE)
11048
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11049
return(NULL); /* purecov: inspected */
11051
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11052
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11054
if (temp_pool_slot != MY_BIT_NONE)
11055
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11056
free_root(&own_root, MYF(0)); /* purecov: inspected */
11057
return(NULL); /* purecov: inspected */
11059
param->items_to_copy= copy_func;
11060
stpcpy(tmpname,path);
11061
/* make table according to fields */
11063
memset(table, 0, sizeof(*table));
11064
memset(reg_field, 0, sizeof(Field*)*(field_count+1));
11065
memset(default_field, 0, sizeof(Field*) * (field_count));
11066
memset(from_field, 0, sizeof(Field*)*field_count);
11068
table->mem_root= own_root;
11069
mem_root_save= thd->mem_root;
11070
thd->mem_root= &table->mem_root;
11072
table->field=reg_field;
11073
table->alias= table_alias;
11074
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11075
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11077
table->temp_pool_slot = temp_pool_slot;
11078
table->copy_blobs= 1;
11079
table->in_use= thd;
11080
table->quick_keys.init();
11081
table->covering_keys.init();
11082
table->keys_in_use_for_query.init();
11085
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11086
share->blob_field= blob_field;
11087
share->blob_ptr_size= portable_sizeof_char_ptr;
11088
share->db_low_byte_first=1; // True for HEAP and MyISAM
11089
share->table_charset= param->table_charset;
11090
share->primary_key= MAX_KEY; // Indicate no primary key
11091
share->keys_for_keyread.init();
11092
share->keys_in_use.init();
11094
/* Calculate which type of fields we will store in the temporary table */
11096
reclength= string_total_length= 0;
11097
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11098
param->using_indirect_summary_function=0;
11100
List_iterator_fast<Item> li(fields);
11102
Field **tmp_from_field=from_field;
11103
while ((item=li++))
11105
Item::Type type=item->type();
11106
if (not_all_columns)
11108
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11110
if (item->used_tables() & OUTER_REF_TABLE_BIT)
11111
item->update_used_tables();
11112
if (type == Item::SUBSELECT_ITEM ||
11113
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
11116
Mark that the we have ignored an item that refers to a summary
11117
function. We need to know this if someone is going to use
11118
DISTINCT on the result.
11120
param->using_indirect_summary_function=1;
11124
if (item->const_item() && (int) hidden_field_count <= 0)
11125
continue; // We don't have to store this
11127
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11128
{ /* Can't calc group yet */
11129
((Item_sum*) item)->result_field=0;
11130
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11132
Item **argp= ((Item_sum*) item)->args + i;
11134
if (!arg->const_item())
11137
create_tmp_field(thd, table, arg, arg->type(), ©_func,
11138
tmp_from_field, &default_field[fieldnr],
11139
group != 0,not_all_columns,
11141
param->convert_blob_length);
11143
goto err; // Should be OOM
11145
reclength+=new_field->pack_length();
11146
if (new_field->flags & BLOB_FLAG)
11148
*blob_field++= fieldnr;
11151
*(reg_field++)= new_field;
11152
if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
11155
string_total_length+= new_field->pack_length();
11157
thd->mem_root= mem_root_save;
11158
thd->change_item_tree(argp, new Item_field(new_field));
11159
thd->mem_root= &table->mem_root;
11160
if (!(new_field->flags & NOT_NULL_FLAG))
11164
new_field->maybe_null() is still false, it will be
11165
changed below. But we have to setup Item_field correctly
11167
(*argp)->maybe_null=1;
11169
new_field->field_index= fieldnr++;
11176
The last parameter to create_tmp_field() is a bit tricky:
11178
We need to set it to 0 in union, to get fill_record() to modify the
11180
We need to set it to 1 on multi-table-update and in select to
11181
write rows to the temporary table.
11182
We here distinguish between UNION and multi-table-updates by the fact
11183
that in the later case group is set to the row pointer.
11185
Field *new_field= (param->schema_table) ?
11186
create_tmp_field_for_schema(thd, item, table) :
11187
create_tmp_field(thd, table, item, type, ©_func,
11188
tmp_from_field, &default_field[fieldnr],
11190
!force_copy_fields &&
11191
(not_all_columns || group !=0),
11193
If item->marker == 4 then we force create_tmp_field
11194
to create a 64-bit longs for BIT fields because HEAP
11195
tables can't index BIT fields directly. We do the same
11196
for distinct, as we want the distinct index to be
11197
usable in this case too.
11199
item->marker == 4 || param->bit_fields_as_long,
11201
param->convert_blob_length);
11205
if (thd->is_fatal_error)
11206
goto err; // Got OOM
11207
continue; // Some kindf of const item
11209
if (type == Item::SUM_FUNC_ITEM)
11210
((Item_sum *) item)->result_field= new_field;
11212
reclength+=new_field->pack_length();
11213
if (!(new_field->flags & NOT_NULL_FLAG))
11215
if (new_field->flags & BLOB_FLAG)
11217
*blob_field++= fieldnr;
11220
if (item->marker == 4 && item->maybe_null)
11222
group_null_items++;
11223
new_field->flags|= GROUP_FLAG;
11225
new_field->field_index= fieldnr++;
11226
*(reg_field++)= new_field;
11228
if (!--hidden_field_count)
11231
This was the last hidden field; Remember how many hidden fields could
11234
hidden_null_count=null_count;
11236
We need to update hidden_field_count as we may have stored group
11237
functions with constant arguments
11239
param->hidden_field_count= fieldnr;
11243
assert(fieldnr == (uint) (reg_field - table->field));
11244
assert(field_count >= (uint) (reg_field - table->field));
11245
field_count= fieldnr;
11247
*blob_field= 0; // End marker
11248
share->fields= field_count;
11250
/* If result table is small; use a heap */
11251
/* future: storage engine selection can be made dynamic? */
11252
if (blob_count || using_unique_constraint ||
11253
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11254
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11256
share->db_plugin= ha_lock_engine(0, myisam_hton);
11257
table->file= get_new_handler(share, &table->mem_root,
11260
(param->group_parts > table->file->max_key_parts() ||
11261
param->group_length > table->file->max_key_length()))
11262
using_unique_constraint=1;
11266
share->db_plugin= ha_lock_engine(0, heap_hton);
11267
table->file= get_new_handler(share, &table->mem_root,
11274
if (!using_unique_constraint)
11275
reclength+= group_null_items; // null flag is stored separately
11277
share->blob_fields= blob_count;
11278
if (blob_count == 0)
11280
/* We need to ensure that first byte is not 0 for the delete link */
11281
if (param->hidden_field_count)
11282
hidden_null_count++;
11286
hidden_null_pack_length=(hidden_null_count+7)/8;
11287
null_pack_length= (hidden_null_pack_length +
11288
(null_count + total_uneven_bit_length + 7) / 8);
11289
reclength+=null_pack_length;
11291
reclength=1; // Dummy select
11292
/* Use packed rows if there is blobs or a lot of space to gain */
11293
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)))
11294
use_packed_rows= 1;
11296
share->reclength= reclength;
11298
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11299
share->rec_buff_length= alloc_length;
11300
if (!(table->record[0]= (uchar*)
11301
alloc_root(&table->mem_root, alloc_length*3)))
11303
table->record[1]= table->record[0]+alloc_length;
11304
share->default_values= table->record[1]+alloc_length;
11306
copy_func[0]=0; // End marker
11307
param->func_count= copy_func - param->items_to_copy;
11309
setup_tmp_table_column_bitmaps(table, bitmaps);
11311
recinfo=param->start_recinfo;
11312
null_flags=(uchar*) table->record[0];
11313
pos=table->record[0]+ null_pack_length;
11314
if (null_pack_length)
11316
memset(recinfo, 0, sizeof(*recinfo));
11317
recinfo->type=FIELD_NORMAL;
11318
recinfo->length=null_pack_length;
11320
memset(null_flags, 255, null_pack_length); // Set null fields
11322
table->null_flags= (uchar*) table->record[0];
11323
share->null_fields= null_count+ hidden_null_count;
11324
share->null_bytes= null_pack_length;
11326
null_count= (blob_count == 0) ? 1 : 0;
11327
hidden_field_count=param->hidden_field_count;
11328
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11330
Field *field= *reg_field;
11332
memset(recinfo, 0, sizeof(*recinfo));
11334
if (!(field->flags & NOT_NULL_FLAG))
11336
if (field->flags & GROUP_FLAG && !using_unique_constraint)
11339
We have to reserve one byte here for NULL bits,
11340
as this is updated by 'end_update()'
11342
*pos++=0; // Null is stored here
11344
recinfo->type=FIELD_NORMAL;
11346
memset(recinfo, 0, sizeof(*recinfo));
11350
recinfo->null_bit= 1 << (null_count & 7);
11351
recinfo->null_pos= null_count/8;
11353
field->move_field(pos,null_flags+null_count/8,
11354
1 << (null_count & 7));
11358
field->move_field(pos,(uchar*) 0,0);
11362
Test if there is a default field value. The test for ->ptr is to skip
11363
'offset' fields generated by initalize_tables
11365
if (default_field[i] && default_field[i]->ptr)
11368
default_field[i] is set only in the cases when 'field' can
11369
inherit the default value that is defined for the field referred
11370
by the Item_field object from which 'field' has been created.
11373
Field *orig_field= default_field[i];
11374
/* Get the value from default_values */
11375
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11376
orig_field->table->record[0]);
11377
orig_field->move_field_offset(diff); // Points now at default_values
11378
if (orig_field->is_real_null())
11382
field->set_notnull();
11383
memcpy(field->ptr, orig_field->ptr, field->pack_length());
11385
orig_field->move_field_offset(-diff); // Back to record[0]
11389
{ /* Not a table Item */
11390
copy->set(field,from_field[i],save_sum_fields);
11393
length=field->pack_length();
11396
/* Make entry for create table */
11397
recinfo->length=length;
11398
if (field->flags & BLOB_FLAG)
11399
recinfo->type= (int) FIELD_BLOB;
11401
recinfo->type=FIELD_NORMAL;
11402
if (!--hidden_field_count)
11403
null_count=(null_count+7) & ~7; // move to next byte
11405
// fix table name in field entry
11406
field->table_name= &table->alias;
11409
param->copy_field_end=copy;
11410
param->recinfo=recinfo;
11411
store_record(table,s->default_values); // Make empty default record
11413
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11414
share->max_rows= ~(ha_rows) 0;
11416
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11417
min(thd->variables.tmp_table_size,
11418
thd->variables.max_heap_table_size) :
11419
thd->variables.tmp_table_size) /
11421
set_if_bigger(share->max_rows,1); // For dummy start options
11423
Push the LIMIT clause to the temporary table creation, so that we
11424
materialize only up to 'rows_limit' records instead of all result records.
11426
set_if_smaller(share->max_rows, rows_limit);
11427
param->end_write_records= rows_limit;
11429
keyinfo= param->keyinfo;
11433
table->group=group; /* Table is grouped by key */
11434
param->group_buff=group_buff;
11436
share->uniques= test(using_unique_constraint);
11437
table->key_info=keyinfo;
11438
keyinfo->key_part=key_part_info;
11439
keyinfo->flags=HA_NOSAME;
11440
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11441
keyinfo->key_length=0;
11442
keyinfo->rec_per_key=0;
11443
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11444
keyinfo->name= (char*) "group_key";
11445
ORDER *cur_group= group;
11446
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11448
Field *field=(*cur_group->item)->get_tmp_table_field();
11449
bool maybe_null=(*cur_group->item)->maybe_null;
11450
key_part_info->null_bit=0;
11451
key_part_info->field= field;
11452
key_part_info->offset= field->offset(table->record[0]);
11453
key_part_info->length= (uint16_t) field->key_length();
11454
key_part_info->type= (uint8_t) field->key_type();
11455
key_part_info->key_type =
11456
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11457
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11458
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11459
0 : FIELDFLAG_BINARY;
11460
if (!using_unique_constraint)
11462
cur_group->buff=(char*) group_buff;
11463
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11468
goto err; /* purecov: inspected */
11472
To be able to group on NULL, we reserved place in group_buff
11473
for the NULL flag just before the column. (see above).
11474
The field data is after this flag.
11475
The NULL flag is updated in 'end_update()' and 'end_write()'
11477
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
11478
key_part_info->null_bit=field->null_bit;
11479
key_part_info->null_offset= (uint) (field->null_ptr -
11480
(uchar*) table->record[0]);
11481
cur_group->buff++; // Pointer to field data
11482
group_buff++; // Skipp null flag
11484
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11485
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11486
group_buff+= cur_group->field->pack_length();
11488
keyinfo->key_length+= key_part_info->length;
11492
if (distinct && field_count != param->hidden_field_count)
11495
Create an unique key or an unique constraint over all columns
11496
that should be in the result. In the temporary table, there are
11497
'param->hidden_field_count' extra columns, whose null bits are stored
11498
in the first 'hidden_null_pack_length' bytes of the row.
11503
Special mode for index creation in MyISAM used to support unique
11504
indexes on blobs with arbitrary length. Such indexes cannot be
11509
null_pack_length-=hidden_null_pack_length;
11510
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11511
(share->uniques ? test(null_pack_length) : 0));
11512
table->distinct= 1;
11514
if (!(key_part_info= (KEY_PART_INFO*)
11515
alloc_root(&table->mem_root,
11516
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11518
memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11519
table->key_info=keyinfo;
11520
keyinfo->key_part=key_part_info;
11521
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11522
keyinfo->key_length=(uint16_t) reclength;
11523
keyinfo->name= (char*) "distinct_key";
11524
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11525
keyinfo->rec_per_key=0;
11528
Create an extra field to hold NULL bits so that unique indexes on
11529
blobs can distinguish NULL from 0. This extra field is not needed
11530
when we do not use UNIQUE indexes for blobs.
11532
if (null_pack_length && share->uniques)
11534
key_part_info->null_bit=0;
11535
key_part_info->offset=hidden_null_pack_length;
11536
key_part_info->length=null_pack_length;
11537
key_part_info->field= new Field_varstring(table->record[0],
11538
(uint32_t) key_part_info->length,
11546
if (!key_part_info->field)
11548
key_part_info->field->init(table);
11549
key_part_info->key_type=FIELDFLAG_BINARY;
11550
key_part_info->type= HA_KEYTYPE_BINARY;
11553
/* Create a distinct key over the columns we are going to return */
11554
for (i=param->hidden_field_count, reg_field=table->field + i ;
11556
i++, reg_field++, key_part_info++)
11558
key_part_info->null_bit=0;
11559
key_part_info->field= *reg_field;
11560
key_part_info->offset= (*reg_field)->offset(table->record[0]);
11561
key_part_info->length= (uint16_t) (*reg_field)->pack_length();
11563
The below method of computing the key format length of the
11564
key part is a copy/paste from opt_range.cc, and table.cc.
11565
This should be factored out, e.g. as a method of Field.
11566
In addition it is not clear if any of the Field::*_length
11567
methods is supposed to compute the same length. If so, it
11570
key_part_info->store_length= key_part_info->length;
11572
if ((*reg_field)->real_maybe_null())
11573
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11574
if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB ||
11575
(*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
11576
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11578
key_part_info->type= (uint8_t) (*reg_field)->key_type();
11579
key_part_info->key_type =
11580
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11581
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11582
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11583
0 : FIELDFLAG_BINARY;
11587
if (thd->is_fatal_error) // If end of memory
11588
goto err; /* purecov: inspected */
11589
share->db_record_offset= 1;
11590
if (share->db_type() == myisam_hton)
11592
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11593
¶m->recinfo, select_options))
11596
if (open_tmp_table(table))
11599
thd->mem_root= mem_root_save;
11604
thd->mem_root= mem_root_save;
11605
free_tmp_table(thd,table); /* purecov: inspected */
11606
if (temp_pool_slot != MY_BIT_NONE)
11607
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11608
return(NULL); /* purecov: inspected */
11615
Create a temporary table to weed out duplicate rowid combinations
11619
create_duplicate_weedout_tmp_table()
11621
uniq_tuple_length_arg
11625
Create a temporary table to weed out duplicate rowid combinations. The
11626
table has a single column that is a concatenation of all rowids in the
11629
Depending on the needed length, there are two cases:
11631
1. When the length of the column < max_key_length:
11633
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11635
2. Otherwise (not a valid SQL syntax but internally supported):
11637
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11639
The code in this function was produced by extraction of relevant parts
11640
from create_tmp_table().
11647
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
11648
uint uniq_tuple_length_arg,
11649
SJ_TMP_TABLE *sjtbl)
11651
MEM_ROOT *mem_root_save, own_root;
11653
TABLE_SHARE *share;
11654
uint temp_pool_slot=MY_BIT_NONE;
11655
char *tmpname,path[FN_REFLEN];
11657
KEY_PART_INFO *key_part_info;
11662
MI_COLUMNDEF *recinfo, *start_recinfo;
11663
bool using_unique_constraint=false;
11664
Field *field, *key_field;
11665
uint blob_count, null_pack_length, null_count;
11670
STEP 1: Get temporary table name
11672
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11673
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11674
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11676
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11677
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11678
current_pid, temp_pool_slot);
11681
/* if we run out of slots or we are not using tempool */
11682
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11683
thd->thread_id, thd->tmp_table++);
11685
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11687
/* STEP 2: Figure if we'll be using a key or blob+constraint */
11688
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11689
using_unique_constraint= true;
11691
/* STEP 3: Allocate memory for temptable description */
11692
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11693
if (!multi_alloc_root(&own_root,
11694
&table, sizeof(*table),
11695
&share, sizeof(*share),
11696
®_field, sizeof(Field*) * (1+1),
11697
&blob_field, sizeof(uint)*2,
11698
&keyinfo, sizeof(*keyinfo),
11699
&key_part_info, sizeof(*key_part_info) * 2,
11701
sizeof(*recinfo)*(1*2+4),
11702
&tmpname, (uint) strlen(path)+1,
11703
&group_buff, (!using_unique_constraint ?
11704
uniq_tuple_length_arg : 0),
11705
&bitmaps, bitmap_buffer_size(1)*2,
11708
if (temp_pool_slot != MY_BIT_NONE)
11709
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11712
stpcpy(tmpname,path);
11715
/* STEP 4: Create TABLE description */
11716
memset(table, 0, sizeof(*table));
11717
memset(reg_field, 0, sizeof(Field*)*2);
11719
table->mem_root= own_root;
11720
mem_root_save= thd->mem_root;
11721
thd->mem_root= &table->mem_root;
11723
table->field=reg_field;
11724
table->alias= "weedout-tmp";
11725
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11726
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11728
table->temp_pool_slot = temp_pool_slot;
11729
table->copy_blobs= 1;
11730
table->in_use= thd;
11731
table->quick_keys.init();
11732
table->covering_keys.init();
11733
table->keys_in_use_for_query.init();
11736
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11737
share->blob_field= blob_field;
11738
share->blob_ptr_size= portable_sizeof_char_ptr;
11739
share->db_low_byte_first=1; // True for HEAP and MyISAM
11740
share->table_charset= NULL;
11741
share->primary_key= MAX_KEY; // Indicate no primary key
11742
share->keys_for_keyread.init();
11743
share->keys_in_use.init();
11747
/* Create the field */
11750
For the sake of uniformity, always use Field_varstring.
11752
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11756
field->table= table;
11757
field->key_start.init(0);
11758
field->part_of_key.init(0);
11759
field->part_of_sortkey.init(0);
11760
field->unireg_check= Field::NONE;
11761
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11762
field->reset_fields();
11763
field->init(table);
11764
field->orig_table= NULL;
11766
field->field_index= 0;
11768
*(reg_field++)= field;
11773
share->blob_fields= 0;
11776
uint reclength= field->pack_length();
11777
if (using_unique_constraint)
11779
share->db_plugin= ha_lock_engine(0, myisam_hton);
11780
table->file= get_new_handler(share, &table->mem_root,
11782
assert(uniq_tuple_length_arg <= table->file->max_key_length());
11786
share->db_plugin= ha_lock_engine(0, heap_hton);
11787
table->file= get_new_handler(share, &table->mem_root,
11795
null_pack_length= 1;
11796
reclength += null_pack_length;
11798
share->reclength= reclength;
11800
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11801
share->rec_buff_length= alloc_length;
11802
if (!(table->record[0]= (uchar*)
11803
alloc_root(&table->mem_root, alloc_length*3)))
11805
table->record[1]= table->record[0]+alloc_length;
11806
share->default_values= table->record[1]+alloc_length;
11808
setup_tmp_table_column_bitmaps(table, bitmaps);
11810
recinfo= start_recinfo;
11811
null_flags=(uchar*) table->record[0];
11812
pos=table->record[0]+ null_pack_length;
11813
if (null_pack_length)
11815
memset(recinfo, 0, sizeof(*recinfo));
11816
recinfo->type=FIELD_NORMAL;
11817
recinfo->length=null_pack_length;
11819
memset(null_flags, 255, null_pack_length); // Set null fields
11821
table->null_flags= (uchar*) table->record[0];
11822
share->null_fields= null_count;
11823
share->null_bytes= null_pack_length;
11828
//Field *field= *reg_field;
11830
memset(recinfo, 0, sizeof(*recinfo));
11831
field->move_field(pos,(uchar*) 0,0);
11835
Test if there is a default field value. The test for ->ptr is to skip
11836
'offset' fields generated by initalize_tables
11838
// Initialize the table field:
11839
memset(field->ptr, 0, field->pack_length());
11841
length=field->pack_length();
11844
/* Make entry for create table */
11845
recinfo->length=length;
11846
if (field->flags & BLOB_FLAG)
11847
recinfo->type= (int) FIELD_BLOB;
11849
recinfo->type=FIELD_NORMAL;
11851
field->table_name= &table->alias;
11854
//param->recinfo=recinfo;
11855
//store_record(table,s->default_values); // Make empty default record
11857
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11858
share->max_rows= ~(ha_rows) 0;
11860
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11861
min(thd->variables.tmp_table_size,
11862
thd->variables.max_heap_table_size) :
11863
thd->variables.tmp_table_size) /
11865
set_if_bigger(share->max_rows,1); // For dummy start options
11868
//// keyinfo= param->keyinfo;
11872
share->uniques= test(using_unique_constraint);
11873
table->key_info=keyinfo;
11874
keyinfo->key_part=key_part_info;
11875
keyinfo->flags=HA_NOSAME;
11876
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11877
keyinfo->key_length=0;
11878
keyinfo->rec_per_key=0;
11879
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11880
keyinfo->name= (char*) "weedout_key";
11882
key_part_info->null_bit=0;
11883
key_part_info->field= field;
11884
key_part_info->offset= field->offset(table->record[0]);
11885
key_part_info->length= (uint16_t) field->key_length();
11886
key_part_info->type= (uint8_t) field->key_type();
11887
key_part_info->key_type = FIELDFLAG_BINARY;
11888
if (!using_unique_constraint)
11890
if (!(key_field= field->new_key_field(thd->mem_root, table,
11895
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11897
keyinfo->key_length+= key_part_info->length;
11901
if (thd->is_fatal_error) // If end of memory
11903
share->db_record_offset= 1;
11904
if (share->db_type() == myisam_hton)
11907
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11910
sjtbl->start_recinfo= start_recinfo;
11911
sjtbl->recinfo= recinfo;
11912
if (open_tmp_table(table))
11915
thd->mem_root= mem_root_save;
11919
thd->mem_root= mem_root_save;
11920
free_tmp_table(thd,table); /* purecov: inspected */
11921
if (temp_pool_slot != MY_BIT_NONE)
11922
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11923
return(NULL); /* purecov: inspected */
11926
/****************************************************************************/
11929
Create a reduced TABLE object with properly set up Field list from a
11930
list of field definitions.
11932
The created table doesn't have a table handler associated with
11933
it, has no keys, no group/distinct, no copy_funcs array.
11934
The sole purpose of this TABLE object is to use the power of Field
11935
class to read/write data to/from table->record[0]. Then one can store
11936
the record in any container (RB tree, hash, etc).
11937
The table is created in THD mem_root, so are the table's fields.
11938
Consequently, if you don't BLOB fields, you don't need to free it.
11940
@param thd connection handle
11941
@param field_list list of column definitions
11944
0 if out of memory, TABLE object in case of success
11947
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11949
uint field_count= field_list.elements;
11950
uint blob_count= 0;
11952
Create_field *cdef; /* column definition */
11953
uint record_length= 0;
11954
uint null_count= 0; /* number of columns which may be null */
11955
uint null_pack_length; /* NULL representation array length */
11959
TABLE_SHARE *share;
11961
if (!multi_alloc_root(thd->mem_root,
11962
&table, sizeof(*table),
11963
&share, sizeof(*share),
11964
&field, (field_count + 1) * sizeof(Field*),
11965
&blob_field, (field_count+1) *sizeof(uint),
11966
&bitmaps, bitmap_buffer_size(field_count)*2,
11970
memset(table, 0, sizeof(*table));
11971
memset(share, 0, sizeof(*share));
11972
table->field= field;
11974
share->blob_field= blob_field;
11975
share->fields= field_count;
11976
share->blob_ptr_size= portable_sizeof_char_ptr;
11977
setup_tmp_table_column_bitmaps(table, bitmaps);
11979
/* Create all fields and calculate the total length of record */
11980
List_iterator_fast<Create_field> it(field_list);
11981
while ((cdef= it++))
11983
*field= make_field(share, 0, cdef->length,
11984
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
11985
f_maybe_null(cdef->pack_flag) ? 1 : 0,
11986
cdef->pack_flag, cdef->sql_type, cdef->charset,
11987
cdef->unireg_check,
11988
cdef->interval, cdef->field_name);
11991
(*field)->init(table);
11992
record_length+= (*field)->pack_length();
11993
if (! ((*field)->flags & NOT_NULL_FLAG))
11996
if ((*field)->flags & BLOB_FLAG)
11997
share->blob_field[blob_count++]= (uint) (field - table->field);
12001
*field= NULL; /* mark the end of the list */
12002
share->blob_field[blob_count]= 0; /* mark the end of the list */
12003
share->blob_fields= blob_count;
12005
null_pack_length= (null_count + 7)/8;
12006
share->reclength= record_length + null_pack_length;
12007
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12008
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12009
if (!table->record[0])
12012
if (null_pack_length)
12014
table->null_flags= (uchar*) table->record[0];
12015
share->null_fields= null_count;
12016
share->null_bytes= null_pack_length;
12019
table->in_use= thd; /* field->reset() may access table->in_use */
12021
/* Set up field pointers */
12022
uchar *null_pos= table->record[0];
12023
uchar *field_pos= null_pos + share->null_bytes;
12026
for (field= table->field; *field; ++field)
12028
Field *cur_field= *field;
12029
if ((cur_field->flags & NOT_NULL_FLAG))
12030
cur_field->move_field(field_pos);
12033
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12035
if (null_bit == (1 << 8))
12041
cur_field->reset();
12043
field_pos+= cur_field->pack_length();
12048
for (field= table->field; *field; ++field)
12049
delete *field; /* just invokes field destructor */
12054
static bool open_tmp_table(TABLE *table)
12057
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12058
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12060
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12064
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */
12070
Create MyISAM temporary table
12073
create_myisam_tmp_table()
12074
table Table object that descrimes the table to be created
12075
keyinfo Description of the index (there is always one index)
12076
start_recinfo MyISAM's column descriptions
12077
recinfo INOUT End of MyISAM's column descriptions
12078
options Option bits
12081
Create a MyISAM temporary table according to passed description. The is
12082
assumed to have one unique index or constraint.
12084
The passed array or MI_COLUMNDEF structures must have this form:
12086
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12087
when there are many nullable columns)
12089
3. One free MI_COLUMNDEF element (*recinfo points here)
12091
This function may use the free element to create hash column for unique
12099
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
12100
MI_COLUMNDEF *start_recinfo,
12101
MI_COLUMNDEF **recinfo,
12106
MI_UNIQUEDEF uniquedef;
12107
TABLE_SHARE *share= table->s;
12110
{ // Get keys for ni_create
12111
bool using_unique_constraint=0;
12112
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12113
sizeof(*seg) * keyinfo->key_parts);
12117
memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
12118
if (keyinfo->key_length >= table->file->max_key_length() ||
12119
keyinfo->key_parts > table->file->max_key_parts() ||
12122
/* Can't create a key; Make a unique constraint instead of a key */
12125
using_unique_constraint=1;
12126
memset(&uniquedef, 0, sizeof(uniquedef));
12127
uniquedef.keysegs=keyinfo->key_parts;
12129
uniquedef.null_are_equal=1;
12131
/* Create extra column for hash value */
12132
memset(*recinfo, 0, sizeof(**recinfo));
12133
(*recinfo)->type= FIELD_CHECK;
12134
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12136
share->reclength+=MI_UNIQUE_HASH_LENGTH;
12140
/* Create an unique key */
12141
memset(&keydef, 0, sizeof(keydef));
12142
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12143
keydef.keysegs= keyinfo->key_parts;
12146
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12148
Field *field=keyinfo->key_part[i].field;
12150
seg->language= field->charset()->number;
12151
seg->length= keyinfo->key_part[i].length;
12152
seg->start= keyinfo->key_part[i].offset;
12153
if (field->flags & BLOB_FLAG)
12156
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12157
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12158
seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
12159
seg->flag= HA_BLOB_PART;
12160
seg->length=0; // Whole blob in unique constraint
12164
seg->type= keyinfo->key_part[i].type;
12166
if (!(field->flags & NOT_NULL_FLAG))
12168
seg->null_bit= field->null_bit;
12169
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12171
We are using a GROUP BY on something that contains NULL
12172
In this case we have to tell MyISAM that two NULL should
12173
on INSERT be regarded at the same value
12175
if (!using_unique_constraint)
12176
keydef.flag|= HA_NULL_ARE_EQUAL;
12180
MI_CREATE_INFO create_info;
12181
memset(&create_info, 0, sizeof(create_info));
12183
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12185
create_info.data_file_length= ~(uint64_t) 0;
12187
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12188
(uint) (*recinfo-start_recinfo),
12190
share->uniques, &uniquedef,
12192
HA_CREATE_TMP_TABLE)))
12194
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12198
status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12199
share->db_record_offset= 1;
12207
free_tmp_table(THD *thd, TABLE *entry)
12209
MEM_ROOT own_root= entry->mem_root;
12210
const char *save_proc_info;
12212
save_proc_info=thd->proc_info;
12213
thd_proc_info(thd, "removing tmp table");
12217
if (entry->db_stat)
12218
entry->file->ha_drop_table(entry->s->table_name.str);
12220
entry->file->ha_delete_table(entry->s->table_name.str);
12221
delete entry->file;
12225
for (Field **ptr=entry->field ; *ptr ; ptr++)
12227
free_io_cache(entry);
12229
if (entry->temp_pool_slot != MY_BIT_NONE)
12230
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12232
plugin_unlock(0, entry->s->db_plugin);
12234
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12235
thd_proc_info(thd, save_proc_info);
12241
If a HEAP table gets full, create a MyISAM table and copy all rows
12245
bool create_myisam_from_heap(THD *thd, TABLE *table,
12246
MI_COLUMNDEF *start_recinfo,
12247
MI_COLUMNDEF **recinfo,
12248
int error, bool ignore_last_dupp_key_error)
12252
const char *save_proc_info;
12255
if (table->s->db_type() != heap_hton ||
12256
error != HA_ERR_RECORD_FILE_FULL)
12258
table->file->print_error(error,MYF(0));
12263
new_table.s= &share;
12264
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12265
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12266
new_table.s->db_type())))
12267
return(1); // End of memory
12269
save_proc_info=thd->proc_info;
12270
thd_proc_info(thd, "converting HEAP to MyISAM");
12272
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12273
recinfo, thd->lex->select_lex.options |
12276
if (open_tmp_table(&new_table))
12278
if (table->file->indexes_are_disabled())
12279
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12280
table->file->ha_index_or_rnd_end();
12281
table->file->ha_rnd_init(1);
12282
if (table->no_rows)
12284
new_table.file->extra(HA_EXTRA_NO_ROWS);
12285
new_table.no_rows=1;
12288
#ifdef TO_BE_DONE_LATER_IN_4_1
12290
To use start_bulk_insert() (which is new in 4.1) we need to find
12291
all places where a corresponding end_bulk_insert() should be put.
12293
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12294
new_table.file->ha_start_bulk_insert(table->file->stats.records);
12296
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12297
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12301
copy all old rows from heap table to MyISAM table
12302
This is the only code that uses record[1] to read/write but this
12303
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12305
while (!table->file->rnd_next(new_table.record[1]))
12307
write_err= new_table.file->ha_write_row(new_table.record[1]);
12311
/* copy row that filled HEAP table */
12312
if ((write_err=new_table.file->ha_write_row(table->record[0])))
12314
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12315
!ignore_last_dupp_key_error)
12319
/* remove heap table and change to use myisam table */
12320
(void) table->file->ha_rnd_end();
12321
(void) table->file->close(); // This deletes the table !
12322
delete table->file;
12324
plugin_unlock(0, table->s->db_plugin);
12325
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12326
new_table.s= table->s; // Keep old share
12330
table->file->change_table_ptr(table, table->s);
12331
table->use_all_columns();
12332
if (save_proc_info)
12334
const char *new_proc_info=
12335
(!strcmp(save_proc_info,"Copying to tmp table") ?
12336
"Copying to tmp table on disk" : save_proc_info);
12337
thd_proc_info(thd, new_proc_info);
12342
table->file->print_error(write_err, MYF(0));
12343
(void) table->file->ha_rnd_end();
12344
(void) new_table.file->close();
12346
new_table.file->ha_delete_table(new_table.s->table_name.str);
12348
delete new_table.file;
12349
thd_proc_info(thd, save_proc_info);
12350
table->mem_root= new_table.mem_root;
3191
return left_item->eq(*const_item, 1);
3192
*const_item=left_item;
12758
SemiJoinDuplicateElimination: Weed out duplicate row combinations
12761
do_sj_dups_weedout()
12765
1 The row combination is a duplicate (discard it)
12766
0 The row combination is not a duplicate (continue)
12769
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl)
12772
SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
12773
SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
12774
uchar *ptr= sjtbl->tmp_table->record[0] + 1;
12775
uchar *nulls_ptr= ptr;
12777
/* Put the the rowids tuple into table->record[0]: */
12779
// 1. Store the length
12780
if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
12782
*ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
12787
int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
12791
// 2. Zero the null bytes
12792
if (sjtbl->null_bytes)
12794
memset(ptr, 0, sjtbl->null_bytes);
12795
ptr += sjtbl->null_bytes;
12798
// 3. Put the rowids
12799
for (uint i=0; tab != tab_end; tab++, i++)
12801
handler *h= tab->join_tab->table->file;
12802
if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
12804
/* It's a NULL-complemented row */
12805
*(nulls_ptr + tab->null_byte) |= tab->null_bit;
12806
memset(ptr + tab->rowid_offset, 0, h->ref_length);
12810
/* Copy the rowid value */
12811
if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
12812
h->position(tab->join_tab->table->record[0]);
12813
memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
12817
error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
12820
/* create_myisam_from_heap will generate error if needed */
12821
if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
12822
create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo,
12823
&sjtbl->recinfo, error, 1))
12825
//return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
12833
SemiJoinDuplicateElimination: Reset the temporary table
12836
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
12838
if (sj_tbl->tmp_table)
12839
return sj_tbl->tmp_table->file->ha_delete_all_rows();
12844
Process one record of the nested loop join.
12846
This function will evaluate parts of WHERE/ON clauses that are
12847
applicable to the partial record on hand and in case of success
12848
submit this record to the next level of the nested loop.
12851
static enum_nested_loop_state
12852
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
12855
bool not_used_in_distinct=join_tab->not_used_in_distinct;
12856
ha_rows found_records=join->found_records;
12857
COND *select_cond= join_tab->select_cond;
12859
if (error > 0 || (join->thd->is_error())) // Fatal error
12860
return NESTED_LOOP_ERROR;
12862
return NESTED_LOOP_NO_MORE_ROWS;
12863
if (join->thd->killed) // Aborted by user
12865
join->thd->send_kill_message();
12866
return NESTED_LOOP_KILLED; /* purecov: inspected */
12868
if (!select_cond || select_cond->val_int())
12871
There is no select condition or the attached pushed down
12872
condition is true => a match is found.
12875
while (join_tab->first_unmatched && found)
12878
The while condition is always false if join_tab is not
12879
the last inner join table of an outer join operation.
12881
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
12883
Mark that a match for current outer table is found.
12884
This activates push down conditional predicates attached
12885
to the all inner tables of the outer join.
12887
first_unmatched->found= 1;
12888
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
12890
if (tab->table->reginfo.not_exists_optimize)
12891
return NESTED_LOOP_NO_MORE_ROWS;
12892
/* Check all predicates that has just been activated. */
12894
Actually all predicates non-guarded by first_unmatched->found
12895
will be re-evaluated again. It could be fixed, but, probably,
12896
it's not worth doing now.
12898
if (tab->select_cond && !tab->select_cond->val_int())
12900
/* The condition attached to table tab is false */
12901
if (tab == join_tab)
12906
Set a return point if rejected predicate is attached
12907
not to the last table of the current nest level.
12909
join->return_tab= tab;
12910
return NESTED_LOOP_OK;
12915
Check whether join_tab is not the last inner table
12916
for another embedding outer join.
12918
if ((first_unmatched= first_unmatched->first_upper) &&
12919
first_unmatched->last_inner != join_tab)
12920
first_unmatched= 0;
12921
join_tab->first_unmatched= first_unmatched;
12924
JOIN_TAB *return_tab= join->return_tab;
12925
join_tab->found_match= true;
12926
if (join_tab->check_weed_out_table)
12928
int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
12930
return NESTED_LOOP_ERROR;
12932
return NESTED_LOOP_OK;
12934
else if (join_tab->do_firstmatch)
12937
We should return to the join_tab->do_firstmatch after we have
12938
enumerated all the suffixes for current prefix row combination
12940
return_tab= join_tab->do_firstmatch;
12944
It was not just a return to lower loop level when one
12945
of the newly activated predicates is evaluated as false
12946
(See above join->return_tab= tab).
12948
join->examined_rows++;
12949
join->thd->row_count++;
12953
enum enum_nested_loop_state rc;
12954
/* A match from join_tab is found for the current partial join. */
12955
rc= (*join_tab->next_select)(join, join_tab+1, 0);
12956
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
12958
if (return_tab < join->return_tab)
12959
join->return_tab= return_tab;
12961
if (join->return_tab < join_tab)
12962
return NESTED_LOOP_OK;
12964
Test if this was a SELECT DISTINCT query on a table that
12965
was not in the field list; In this case we can abort if
12966
we found a row, as no new rows can be added to the result.
12968
if (not_used_in_distinct && found_records != join->found_records)
12969
return NESTED_LOOP_NO_MORE_ROWS;
12972
join_tab->read_record.file->unlock_row();
12977
The condition pushed down to the table join_tab rejects all rows
12978
with the beginning coinciding with the current partial join.
12980
join->examined_rows++;
12981
join->thd->row_count++;
12982
join_tab->read_record.file->unlock_row();
12984
return NESTED_LOOP_OK;
12991
Construct a NULL complimented partial join record and feed it to the next
12992
level of the nested loop. This function is used in case we have
12993
an OUTER join and no matching record was found.
12996
static enum_nested_loop_state
12997
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
13000
The table join_tab is the first inner table of a outer join operation
13001
and no matches has been found for the current outer row.
13003
JOIN_TAB *last_inner_tab= join_tab->last_inner;
13004
/* Cache variables for faster loop */
13006
for ( ; join_tab <= last_inner_tab ; join_tab++)
13008
/* Change the the values of guard predicate variables. */
13009
join_tab->found= 1;
13010
join_tab->not_null_compl= 0;
13011
/* The outer row is complemented by nulls for each inner tables */
13012
restore_record(join_tab->table,s->default_values); // Make empty record
13013
mark_as_null_row(join_tab->table); // For group by without error
13014
select_cond= join_tab->select_cond;
13015
/* Check all attached conditions for inner table rows. */
13016
if (select_cond && !select_cond->val_int())
13017
return NESTED_LOOP_OK;
13021
The row complemented by nulls might be the first row
13022
of embedding outer joins.
13023
If so, perform the same actions as in the code
13024
for the first regular outer join row above.
13028
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13029
if ((first_unmatched= first_unmatched->first_upper) &&
13030
first_unmatched->last_inner != join_tab)
13031
first_unmatched= 0;
13032
join_tab->first_unmatched= first_unmatched;
13033
if (!first_unmatched)
13035
first_unmatched->found= 1;
13036
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13038
if (tab->select_cond && !tab->select_cond->val_int())
13040
join->return_tab= tab;
13041
return NESTED_LOOP_OK;
13046
The row complemented by nulls satisfies all conditions
13047
attached to inner tables.
13048
Send the row complemented by nulls to be joined with the
13051
return (*join_tab->next_select)(join, join_tab+1, 0);
13055
static enum_nested_loop_state
13056
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13058
enum_nested_loop_state rc= NESTED_LOOP_OK;
13062
join_tab->table->null_row= 0;
13063
if (!join_tab->cache.records)
13064
return NESTED_LOOP_OK; /* Nothing to do */
13066
(void) store_record_in_cache(&join_tab->cache); // Must save this for later
13067
if (join_tab->use_quick == 2)
13069
if (join_tab->select->quick)
13070
{ /* Used quick select last. reset it */
13071
delete join_tab->select->quick;
13072
join_tab->select->quick=0;
13075
/* read through all records */
13076
if ((error=join_init_read_record(join_tab)))
13078
reset_cache_write(&join_tab->cache);
13079
return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13082
for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13084
tmp->status=tmp->table->status;
13085
tmp->table->status=0;
13088
info= &join_tab->read_record;
13091
if (join->thd->killed)
13093
join->thd->send_kill_message();
13094
return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13096
SQL_SELECT *select=join_tab->select;
13097
if (rc == NESTED_LOOP_OK &&
13098
(!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13101
reset_cache_read(&join_tab->cache);
13102
for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13104
read_cached_record(join_tab);
13105
if (!select || !select->skip_record())
13108
if (!join_tab->check_weed_out_table ||
13109
!(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13111
rc= (join_tab->next_select)(join,join_tab+1,0);
13112
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13114
reset_cache_write(&join_tab->cache);
13119
return NESTED_LOOP_ERROR;
13123
} while (!(error=info->read_record(info)));
13126
read_cached_record(join_tab); // Restore current record
13127
reset_cache_write(&join_tab->cache);
13128
if (error > 0) // Fatal error
13129
return NESTED_LOOP_ERROR; /* purecov: inspected */
13130
for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13131
tmp2->table->status=tmp2->status;
13132
return NESTED_LOOP_OK;
13136
/*****************************************************************************
13137
The different ways to read a record
13138
Returns -1 if row was not found, 0 if row was found and 1 on errors
13139
*****************************************************************************/
13141
/** Help function when we get some an error from the table handler. */
13143
int report_error(TABLE *table, int error)
13145
if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13147
table->status= STATUS_GARBAGE;
13148
return -1; // key not found; ok
13151
Locking reads can legally return also these errors, do not
13152
print them to the .err log
13154
if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13155
sql_print_error("Got error %d when reading table '%s'",
13156
error, table->s->path.str);
13157
table->file->print_error(error,MYF(0));
13162
int safe_index_read(JOIN_TAB *tab)
13165
TABLE *table= tab->table;
13166
if ((error=table->file->index_read_map(table->record[0],
3585
int safe_index_read(JoinTable *tab)
3588
Table *table= tab->table;
3589
if ((error=table->cursor->index_read_map(table->record[0],
13167
3590
tab->ref.key_buff,
13168
3591
make_prev_keypart_map(tab->ref.key_parts),
13169
3592
HA_READ_KEY_EXACT)))
13170
return report_error(table, error);
3593
return table->report_error(error);
13176
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
3597
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
13179
TABLE *table=tab->table;
3600
Table *table=tab->table;
13180
3601
table->const_table=1;
13181
3602
table->null_row=0;
13182
3603
table->status=STATUS_NO_RECORD;
13184
if (tab->type == JT_SYSTEM)
3605
if (tab->type == AM_SYSTEM)
13186
3607
if ((error=join_read_system(tab)))
13187
3608
{ // Info for DESCRIBE
13188
3609
tab->info="const row not found";
13189
3610
/* Mark for EXPLAIN that the row was not found */
13190
pos->records_read=0.0;
13191
pos->ref_depend_map= 0;
13192
if (!table->maybe_null || error > 0)
3611
pos->setFanout(0.0);
3612
pos->clearRefDependMap();
3613
if (! table->maybe_null || error > 0)
13198
if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13199
!table->no_keyread &&
13200
(int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
3619
if (! table->key_read &&
3620
table->covering_keys.test(tab->ref.key) &&
3621
! table->no_keyread &&
3622
(int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
13202
3624
table->key_read=1;
13203
table->file->extra(HA_EXTRA_KEYREAD);
3625
table->cursor->extra(HA_EXTRA_KEYREAD);
13204
3626
tab->index= tab->ref.key;
13206
3628
error=join_read_const(tab);
13207
3629
if (table->key_read)
13209
3631
table->key_read=0;
13210
table->file->extra(HA_EXTRA_NO_KEYREAD);
3632
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
13214
3636
tab->info="unique row not found";
13215
3637
/* Mark for EXPLAIN that the row was not found */
13216
pos->records_read=0.0;
13217
pos->ref_depend_map= 0;
3638
pos->setFanout(0.0);
3639
pos->clearRefDependMap();
13218
3640
if (!table->maybe_null || error > 0)
13222
3644
if (*tab->on_expr_ref && !table->null_row)
13224
3646
if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
13225
mark_as_null_row(table);
3647
table->mark_as_null_row();
13227
3649
if (!table->null_row)
13228
3650
table->maybe_null=0;
17269
/** Allocate memory needed for other rollup functions. */
17271
bool JOIN::rollup_init()
17276
tmp_table_param.quick_group= 0; // Can't create groups in tmp table
17277
rollup.state= ROLLUP::STATE_INITED;
17280
Create pointers to the different sum function groups
17281
These are updated by rollup_make_fields()
17283
tmp_table_param.group_parts= send_group_parts;
17285
if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17287
sizeof(List<Item>) +
17288
ref_pointer_array_size)
17289
* send_group_parts )))
17292
rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17293
rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17294
ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17297
Prepare space for field list for the different levels
17298
These will be filled up in rollup_make_fields()
17300
for (i= 0 ; i < send_group_parts ; i++)
17302
rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17303
List<Item> *rollup_fields= &rollup.fields[i];
17304
rollup_fields->empty();
17305
rollup.ref_pointer_arrays[i]= ref_array;
17306
ref_array+= all_fields.elements;
17308
for (i= 0 ; i < send_group_parts; i++)
17310
for (j=0 ; j < fields_list.elements ; j++)
17311
rollup.fields[i].push_back(rollup.null_items[i]);
17313
List_iterator<Item> it(all_fields);
17315
while ((item= it++))
17318
bool found_in_group= 0;
17320
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17322
if (*group_tmp->item == item)
17324
item->maybe_null= 1;
17326
if (item->const_item())
17329
For ROLLUP queries each constant item referenced in GROUP BY list
17330
is wrapped up into an Item_func object yielding the same value
17331
as the constant item. The objects of the wrapper class are never
17332
considered as constant items and besides they inherit all
17333
properties of the Item_result_field class.
17334
This wrapping allows us to ensure writing constant items
17335
into temporary tables whenever the result of the ROLLUP
17336
operation has to be written into a temporary table, e.g. when
17337
ROLLUP is used together with DISTINCT in the SELECT list.
17338
Usually when creating temporary tables for a intermidiate
17339
result we do not include fields for constant expressions.
17341
Item* new_item= new Item_func_rollup_const(item);
17344
new_item->fix_fields(thd, (Item **) 0);
17345
thd->change_item_tree(it.ref(), new_item);
17346
for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17348
if (*tmp->item == item)
17349
thd->change_item_tree(tmp->item, new_item);
17354
if (item->type() == Item::FUNC_ITEM && !found_in_group)
17356
bool changed= false;
17357
if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17360
We have to prevent creation of a field in a temporary table for
17361
an expression that contains GROUP BY attributes.
17362
Marking the expression item as 'with_sum_func' will ensure this.
17365
item->with_sum_func= 1;
17373
Fill up rollup structures with pointers to fields to use.
17375
Creates copies of item_sum items for each sum level.
17377
@param fields_arg List of all fields (hidden and real ones)
17378
@param sel_fields Pointer to selected fields
17379
@param func Store here a pointer to all fields
17383
In this case func is pointing to next not used element.
17388
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17391
List_iterator_fast<Item> it(fields_arg);
17392
Item *first_field= sel_fields.head();
17396
Create field lists for the different levels
17398
The idea here is to have a separate field list for each rollup level to
17399
avoid all runtime checks of which columns should be NULL.
17401
The list is stored in reverse order to get sum function in such an order
17402
in func that it makes it easy to reset them with init_sum_functions()
17404
Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17406
rollup.fields[0] will contain list where a,b,c is NULL
17407
rollup.fields[1] will contain list where b,c is NULL
17409
rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17411
sum_funcs_end[0] points to all sum functions
17412
sum_funcs_end[1] points to all sum functions, except grand totals
17416
for (level=0 ; level < send_group_parts ; level++)
17419
uint pos= send_group_parts - level -1;
17420
bool real_fields= 0;
17422
List_iterator<Item> new_it(rollup.fields[pos]);
17423
Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17424
ORDER *start_group;
17426
/* Point to first hidden field */
17427
Item **ref_array= ref_array_start + fields_arg.elements-1;
17429
/* Remember where the sum functions ends for the previous level */
17430
sum_funcs_end[pos+1]= *func;
17432
/* Find the start of the group for this level */
17433
for (i= 0, start_group= group_list ;
17435
start_group= start_group->next)
17439
while ((item= it++))
17441
if (item == first_field)
17443
real_fields= 1; // End of hidden fields
17444
ref_array= ref_array_start;
17447
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17448
(!((Item_sum*) item)->depended_from() ||
17449
((Item_sum *)item)->depended_from() == select_lex))
17453
This is a top level summary function that must be replaced with
17454
a sum function that is reset for this level.
17456
NOTE: This code creates an object which is not that nice in a
17457
sub select. Fortunately it's not common to have rollup in
17460
item= item->copy_or_same(thd);
17461
((Item_sum*) item)->make_unique();
17462
*(*func)= (Item_sum*) item;
17467
/* Check if this is something that is part of this group by */
17469
for (group_tmp= start_group, i= pos ;
17470
group_tmp ; group_tmp= group_tmp->next, i++)
17472
if (*group_tmp->item == item)
17475
This is an element that is used by the GROUP BY and should be
17476
set to NULL in this level
17478
Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17481
item->maybe_null= 1; // Value will be null sometimes
17482
null_item->result_field= item->get_tmp_table_field();
17491
(void) new_it++; // Point to next item
17492
new_it.replace(item); // Replace previous
17499
sum_funcs_end[0]= *func; // Point to last function
17504
Send all rollup levels higher than the current one to the client.
17508
SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17511
@param idx Level we are on:
17512
- 0 = Total sum level
17513
- 1 = First group changed (a)
17514
- 2 = Second group changed (a,b)
17519
1 If send_data_failed()
17522
int JOIN::rollup_send_data(uint idx)
17525
for (i= send_group_parts ; i-- > idx ; )
17527
/* Get reference pointers to sum functions in place */
17528
memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
17529
ref_pointer_array_size);
17530
if ((!having || having->val_int()))
17532
if (send_records < unit->select_limit_cnt && do_send_rows &&
17533
result->send_data(rollup.fields[i]))
17538
/* Restore ref_pointer_array */
17539
set_items_ref_array(current_ref_pointer_array);
17544
Write all rollup levels higher than the current one to a temp table.
17548
SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17551
@param idx Level we are on:
17552
- 0 = Total sum level
17553
- 1 = First group changed (a)
17554
- 2 = Second group changed (a,b)
17555
@param table reference to temp table
17560
1 if write_data_failed()
17563
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17566
for (i= send_group_parts ; i-- > idx ; )
17568
/* Get reference pointers to sum functions in place */
17569
memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
17570
ref_pointer_array_size);
17571
if ((!having || having->val_int()))
17575
List_iterator_fast<Item> it(rollup.fields[i]);
17576
while ((item= it++))
17578
if (item->type() == Item::NULL_ITEM && item->is_result_field())
17579
item->save_in_result_field(1);
17581
copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17582
if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17584
if (create_myisam_from_heap(thd, table_arg,
17585
tmp_table_param.start_recinfo,
17586
&tmp_table_param.recinfo,
17592
/* Restore ref_pointer_array */
17593
set_items_ref_array(current_ref_pointer_array);
17598
clear results if there are not rows found for group
17599
(end_send_group/end_write_group)
17604
clear_tables(this);
17605
copy_fields(&tmp_table_param);
17609
Item_sum *func, **func_ptr= sum_funcs;
17610
while ((func= *(func_ptr++)))
17618
Send a description about what how the select will be done to stdout.
17621
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17622
bool distinct,const char *message)
17624
List<Item> field_list;
17625
List<Item> item_list;
17626
THD *thd=join->thd;
17627
select_result *result=join->result;
17628
Item *item_null= new Item_null();
17629
const CHARSET_INFO * const cs= system_charset_info;
17631
/* Don't log this into the slow query log */
17632
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17633
join->unit->offset_limit_cnt= 0;
17636
NOTE: the number/types of items pushed into item_list must be in sync with
17637
EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17641
item_list.push_back(new Item_int((int32_t)
17642
join->select_lex->select_number));
17643
item_list.push_back(new Item_string(join->select_lex->type,
17644
strlen(join->select_lex->type), cs));
17645
for (uint i=0 ; i < 7; i++)
17646
item_list.push_back(item_null);
17647
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17648
item_list.push_back(item_null);
17650
item_list.push_back(new Item_string(message,strlen(message),cs));
17651
if (result->send_data(item_list))
17654
else if (join->select_lex == join->unit->fake_select_lex)
17657
here we assume that the query will return at least two rows, so we
17658
show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17659
and no filesort will be actually done, but executing all selects in
17660
the UNION to provide precise EXPLAIN information will hardly be
17663
char table_name_buffer[NAME_LEN];
17666
item_list.push_back(new Item_null);
17668
item_list.push_back(new Item_string(join->select_lex->type,
17669
strlen(join->select_lex->type),
17673
SELECT_LEX *sl= join->unit->first_select();
17674
uint len= 6, lastop= 0;
17675
memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
17676
for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
17679
lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
17680
"%u,", sl->select_number);
17682
if (sl || len + lastop >= NAME_LEN)
17684
memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
17690
table_name_buffer[len - 1]= '>'; // change ',' to '>'
17692
item_list.push_back(new Item_string(table_name_buffer, len, cs));
17695
item_list.push_back(new Item_string(join_type_str[JT_ALL],
17696
strlen(join_type_str[JT_ALL]),
17698
/* possible_keys */
17699
item_list.push_back(item_null);
17701
item_list.push_back(item_null);
17703
item_list.push_back(item_null);
17705
item_list.push_back(item_null);
17707
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17708
item_list.push_back(item_null);
17710
item_list.push_back(item_null);
17712
if (join->unit->global_parameters->order_list.first)
17713
item_list.push_back(new Item_string("Using filesort",
17716
item_list.push_back(new Item_string("", 0, cs));
17718
if (result->send_data(item_list))
17723
table_map used_tables=0;
17724
for (uint i=0 ; i < join->tables ; i++)
17726
JOIN_TAB *tab=join->join_tab+i;
17727
TABLE *table=tab->table;
17728
TABLE_LIST *table_list= tab->table->pos_in_table_list;
17730
char buff1[512], buff2[512], buff3[512];
17731
char keylen_str_buf[64];
17732
String extra(buff, sizeof(buff),cs);
17733
char table_name_buffer[NAME_LEN];
17734
String tmp1(buff1,sizeof(buff1),cs);
17735
String tmp2(buff2,sizeof(buff2),cs);
17736
String tmp3(buff3,sizeof(buff3),cs);
17745
item_list.push_back(new Item_uint((uint32_t)
17746
join->select_lex->select_number));
17748
item_list.push_back(new Item_string(join->select_lex->type,
17749
strlen(join->select_lex->type),
17751
if (tab->type == JT_ALL && tab->select && tab->select->quick)
17753
quick_type= tab->select->quick->get_type();
17754
if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
17755
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
17756
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
17757
tab->type = JT_INDEX_MERGE;
17759
tab->type = JT_RANGE;
17762
if (table->derived_select_number)
17764
/* Derived table name generation */
17765
int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
17767
table->derived_select_number);
17768
item_list.push_back(new Item_string(table_name_buffer, len, cs));
17772
TABLE_LIST *real_table= table->pos_in_table_list;
17773
item_list.push_back(new Item_string(real_table->alias,
17774
strlen(real_table->alias),
17777
/* "type" column */
17778
item_list.push_back(new Item_string(join_type_str[tab->type],
17779
strlen(join_type_str[tab->type]),
17781
/* Build "possible_keys" value and add it to item_list */
17782
if (!tab->keys.is_clear_all())
17785
for (j=0 ; j < table->s->keys ; j++)
17787
if (tab->keys.is_set(j))
17791
tmp1.append(table->key_info[j].name,
17792
strlen(table->key_info[j].name),
17793
system_charset_info);
17798
item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
17800
item_list.push_back(item_null);
17802
/* Build "key", "key_len", and "ref" values and add them to item_list */
17803
if (tab->ref.key_parts)
17805
KEY *key_info=table->key_info+ tab->ref.key;
17806
register uint length;
17807
item_list.push_back(new Item_string(key_info->name,
17808
strlen(key_info->name),
17809
system_charset_info));
17810
length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
17812
item_list.push_back(new Item_string(keylen_str_buf, length,
17813
system_charset_info));
17814
for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
17818
tmp2.append((*ref)->name(), strlen((*ref)->name()),
17819
system_charset_info);
17821
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17823
else if (tab->type == JT_NEXT)
17825
KEY *key_info=table->key_info+ tab->index;
17826
register uint length;
17827
item_list.push_back(new Item_string(key_info->name,
17828
strlen(key_info->name),cs));
17829
length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
17831
item_list.push_back(new Item_string(keylen_str_buf,
17833
system_charset_info));
17834
item_list.push_back(item_null);
17836
else if (tab->select && tab->select->quick)
17838
tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
17839
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17840
item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
17841
item_list.push_back(item_null);
17845
if (table_list->schema_table &&
17846
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
17848
const char *tmp_buff;
17850
if (table_list->has_db_lookup_value)
17852
f_idx= table_list->schema_table->idx_field1;
17853
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17854
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17856
if (table_list->has_table_lookup_value)
17858
if (table_list->has_db_lookup_value)
17860
f_idx= table_list->schema_table->idx_field2;
17861
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17862
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17865
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17867
item_list.push_back(item_null);
17870
item_list.push_back(item_null);
17871
item_list.push_back(item_null);
17872
item_list.push_back(item_null);
17875
/* Add "rows" field to item_list. */
17876
if (table_list->schema_table)
17879
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17880
item_list.push_back(item_null);
17882
item_list.push_back(item_null);
17886
double examined_rows;
17887
if (tab->select && tab->select->quick)
17888
examined_rows= rows2double(tab->select->quick->records);
17889
else if (tab->type == JT_NEXT || tab->type == JT_ALL)
17890
examined_rows= rows2double(tab->limit ? tab->limit :
17891
tab->table->file->records());
17893
examined_rows= join->best_positions[i].records_read;
17895
item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
17896
MY_INT64_NUM_DECIMAL_DIGITS));
17898
/* Add "filtered" field to item_list. */
17899
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17903
f= (float) (100.0 * join->best_positions[i].records_read /
17905
item_list.push_back(new Item_float(f, 2));
17909
/* Build "Extra" field and add it to item_list. */
17910
bool key_read=table->key_read;
17911
if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
17912
table->covering_keys.is_set(tab->index))
17914
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
17915
!((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
17919
item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
17920
else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
17922
if (tab->packed_info & TAB_INFO_USING_INDEX)
17923
extra.append(STRING_WITH_LEN("; Using index"));
17924
if (tab->packed_info & TAB_INFO_USING_WHERE)
17925
extra.append(STRING_WITH_LEN("; Using where"));
17926
if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
17927
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
17928
/* Skip initial "; "*/
17929
const char *str= extra.ptr();
17930
uint32_t len= extra.length();
17936
item_list.push_back(new Item_string(str, len, cs));
17940
uint keyno= MAX_KEY;
17941
if (tab->ref.key_parts)
17942
keyno= tab->ref.key;
17943
else if (tab->select && tab->select->quick)
17944
keyno = tab->select->quick->index;
17946
if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
17947
table->file->pushed_idx_cond)
17948
extra.append(STRING_WITH_LEN("; Using index condition"));
17950
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
17951
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
17952
quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
17954
extra.append(STRING_WITH_LEN("; Using "));
17955
tab->select->quick->add_info_string(&extra);
17959
if (tab->use_quick == 2)
17961
/* 4 bits per 1 hex digit + terminating '\0' */
17962
char buf[MAX_KEY / 4 + 1];
17963
extra.append(STRING_WITH_LEN("; Range checked for each "
17964
"record (index map: 0x"));
17965
extra.append(tab->keys.print(buf));
17968
else if (tab->select->cond)
17970
const COND *pushed_cond= tab->table->file->pushed_cond;
17972
if (thd->variables.engine_condition_pushdown && pushed_cond)
17974
extra.append(STRING_WITH_LEN("; Using where with pushed "
17976
if (thd->lex->describe & DESCRIBE_EXTENDED)
17978
extra.append(STRING_WITH_LEN(": "));
17979
((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
17983
extra.append(STRING_WITH_LEN("; Using where"));
17988
if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
17989
extra.append(STRING_WITH_LEN("; Using index for group-by"));
17991
extra.append(STRING_WITH_LEN("; Using index"));
17993
if (table->reginfo.not_exists_optimize)
17994
extra.append(STRING_WITH_LEN("; Not exists"));
17996
if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
17997
!(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
17998
HA_MRR_USE_DEFAULT_IMPL))
18000
extra.append(STRING_WITH_LEN("; Using MRR"));
18003
if (table_list->schema_table &&
18004
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18006
if (!table_list->table_open_method)
18007
extra.append(STRING_WITH_LEN("; Skip_open_table"));
18008
else if (table_list->table_open_method == OPEN_FRM_ONLY)
18009
extra.append(STRING_WITH_LEN("; Open_frm_only"));
18011
extra.append(STRING_WITH_LEN("; Open_full_table"));
18012
if (table_list->has_db_lookup_value &&
18013
table_list->has_table_lookup_value)
18014
extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18015
else if (table_list->has_db_lookup_value ||
18016
table_list->has_table_lookup_value)
18017
extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18019
extra.append(STRING_WITH_LEN("; Scanned all databases"));
18021
if (need_tmp_table)
18024
extra.append(STRING_WITH_LEN("; Using temporary"));
18029
extra.append(STRING_WITH_LEN("; Using filesort"));
18031
if (distinct & test_all_bits(used_tables,thd->used_tables))
18032
extra.append(STRING_WITH_LEN("; Distinct"));
18034
if (tab->insideout_match_tab)
18036
extra.append(STRING_WITH_LEN("; LooseScan"));
18039
if (tab->flush_weedout_table)
18040
extra.append(STRING_WITH_LEN("; Start temporary"));
18041
else if (tab->check_weed_out_table)
18042
extra.append(STRING_WITH_LEN("; End temporary"));
18043
else if (tab->do_firstmatch)
18045
extra.append(STRING_WITH_LEN("; FirstMatch("));
18046
TABLE *prev_table=tab->do_firstmatch->table;
18047
if (prev_table->derived_select_number)
18049
char namebuf[NAME_LEN];
18050
/* Derived table name generation */
18051
int len= snprintf(namebuf, sizeof(namebuf)-1,
18053
prev_table->derived_select_number);
18054
extra.append(namebuf, len);
18057
extra.append(prev_table->pos_in_table_list->alias);
18058
extra.append(STRING_WITH_LEN(")"));
18061
for (uint part= 0; part < tab->ref.key_parts; part++)
18063
if (tab->ref.cond_guards[part])
18065
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18070
if (i > 0 && tab[-1].next_select == sub_select_cache)
18071
extra.append(STRING_WITH_LEN("; Using join buffer"));
18073
/* Skip initial "; "*/
18074
const char *str= extra.ptr();
18075
uint32_t len= extra.length();
18081
item_list.push_back(new Item_string(str, len, cs));
18083
// For next iteration
18084
used_tables|=table->map;
18085
if (result->send_data(item_list))
18089
for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18091
unit= unit->next_unit())
18093
if (mysql_explain_union(thd, unit, result))
18100
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18103
SELECT_LEX *first= unit->first_select();
18105
for (SELECT_LEX *sl= first;
18107
sl= sl->next_select())
18109
// drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18110
uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18111
sl->type= (((&thd->lex->select_lex)==sl)?
18112
(sl->first_inner_unit() || sl->next_select() ?
18113
"PRIMARY" : "SIMPLE"):
18115
((sl->linkage == DERIVED_TABLE_TYPE) ?
18117
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18118
"DEPENDENT SUBQUERY":
18119
(uncacheable?"UNCACHEABLE SUBQUERY":
18121
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18123
uncacheable?"UNCACHEABLE UNION":
18125
sl->options|= SELECT_DESCRIBE;
18127
if (unit->is_union())
18129
unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18130
unit->fake_select_lex->type= "UNION RESULT";
18131
unit->fake_select_lex->options|= SELECT_DESCRIBE;
18132
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18134
res|= unit->cleanup();
18138
thd->lex->current_select= first;
18139
unit->set_limit(unit->global_parameters);
18140
res= mysql_select(thd, &first->ref_pointer_array,
18141
(TABLE_LIST*) first->table_list.first,
18142
first->with_wild, first->item_list,
18144
first->order_list.elements +
18145
first->group_list.elements,
18146
(ORDER*) first->order_list.first,
18147
(ORDER*) first->group_list.first,
18149
(ORDER*) thd->lex->proc_list.first,
18150
first->options | thd->options | SELECT_DESCRIBE,
18151
result, unit, first);
18153
return(res || thd->is_error());
18157
static void print_table_array(THD *thd, String *str, TABLE_LIST **table,
18160
(*table)->print(thd, str, QT_ORDINARY);
18162
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18164
TABLE_LIST *curr= *tbl;
6541
static void print_table_array(Session *session, String *str, TableList **table,
6544
(*table)->print(session, str, QT_ORDINARY);
6546
for (TableList **tbl= table + 1; tbl < end; tbl++)
6548
TableList *curr= *tbl;
18165
6549
if (curr->outer_join)
18167
6551
/* MySQL converts right to left joins */