12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20
select_query and join optimization
20
mysql_select and join optimization
22
23
@defgroup Query_Optimizer Query Optimizer
32
#include <drizzled/sql_select.h> /* include join.h */
34
#include <drizzled/error.h>
35
#include <drizzled/gettext.h>
36
#include <drizzled/util/test.h>
37
#include <drizzled/name_resolution_context_state.h>
38
#include <drizzled/nested_join.h>
39
#include <drizzled/probes.h>
40
#include <drizzled/show.h>
41
#include <drizzled/item/cache.h>
42
#include <drizzled/item/cmpfunc.h>
43
#include <drizzled/item/copy_string.h>
44
#include <drizzled/item/uint.h>
45
#include <drizzled/cached_item.h>
46
#include <drizzled/sql_base.h>
47
#include <drizzled/field/blob.h>
48
#include <drizzled/check_stack_overrun.h>
49
#include <drizzled/lock.h>
50
#include <drizzled/item/outer_ref.h>
51
#include <drizzled/index_hint.h>
52
#include <drizzled/records.h>
53
#include <drizzled/internal/iocache.h>
54
#include <drizzled/drizzled.h>
55
#include <drizzled/plugin/storage_engine.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>
66
#include <drizzled/filesort.h>
67
#include <drizzled/sql_lex.h>
68
#include <drizzled/session.h>
69
#include <drizzled/sort_field.h>
70
#include <drizzled/select_result.h>
77
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
78
static COND *build_equal_items(Session *session, COND *cond,
27
#ifdef USE_PRAGMA_IMPLEMENTATION
28
#pragma implementation // gcc: Class implementation
31
#include "mysql_priv.h"
32
#include "sql_select.h"
34
#include <mysys/my_bit.h>
35
#include <mysys/hash.h>
36
#include <drizzled/drizzled_error_messages.h>
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
39
"MAYBE_REF","ALL","range","index",
40
"ref_or_null","unique_subquery","index_subquery",
44
struct st_sargable_param;
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
48
DYNAMIC_ARRAY *keyuse);
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
51
uint tables, COND *conds,
52
COND_EQUAL *cond_equal,
53
table_map table_map, SELECT_LEX *select_lex,
54
st_sargable_param **sargables);
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
58
table_map used_tables);
59
static bool choose_plan(JOIN *join,table_map join_tables);
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
62
table_map remaining_tables, uint idx,
63
double record_count, double read_time);
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
66
uint depth, uint prune_level);
67
static bool best_extension_by_limited_search(JOIN *join,
68
table_map remaining_tables,
69
uint idx, double record_count,
70
double read_time, uint depth,
72
static uint determine_search_depth(JOIN* join);
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
76
TODO: 'find_best' is here only temporarily until 'greedy_search' is
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
80
double record_count,double read_time);
81
static uint cache_record_length(JOIN *join,uint index);
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
83
static bool get_best_combination(JOIN *join);
84
static store_key *get_store_key(THD *thd,
85
KEYUSE *keyuse, table_map used_tables,
86
KEY_PART_INFO *key_part, uchar *key_buff,
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
89
static void make_outerjoin_info(JOIN *join);
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
91
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
93
static void update_depend_map(JOIN *join);
94
static void update_depend_map(JOIN *join, ORDER *order);
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
96
bool change_list, bool *simple_order);
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
98
List<Item> &fields, bool send_row,
99
uint64_t select_options, const char *info,
101
static COND *build_equal_items(THD *thd, COND *cond,
79
102
COND_EQUAL *inherited,
80
List<TableList> *join_list,
103
List<TABLE_LIST> *join_list,
81
104
COND_EQUAL **cond_equal_ref);
83
static Item* part_of_refkey(Table *form,Field *field);
84
static bool cmp_buffer_with_ref(JoinTable *tab);
85
static void change_cond_ref_to_const(Session *session,
86
list<COND_CMP>& save_list,
91
static bool copy_blobs(Field **ptr);
93
static bool eval_const_cond(COND *cond)
95
return ((Item_func*) cond)->val_int() ? true : false;
105
static COND* substitute_for_best_equal_field(COND *cond,
106
COND_EQUAL *cond_equal,
107
void *table_join_idx);
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
109
COND *conds, bool top, bool in_sj);
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
111
static void restore_prev_nj_state(JOIN_TAB *last);
112
static void reset_nj_counters(List<TABLE_LIST> *join_list);
113
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
118
static void restore_prev_sj_state(const table_map remaining_tables,
119
const JOIN_TAB *tab);
121
static COND *optimize_cond(JOIN *join, COND *conds,
122
List<TABLE_LIST> *join_list,
123
Item::cond_result *cond_value);
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
125
static bool open_tmp_table(TABLE *table);
126
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
127
MI_COLUMNDEF *start_recinfo,
128
MI_COLUMNDEF **recinfo,
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
132
static enum_nested_loop_state
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
135
static enum_nested_loop_state
136
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
137
static enum_nested_loop_state
138
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
139
static enum_nested_loop_state
140
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
141
static enum_nested_loop_state
142
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
143
static enum_nested_loop_state
144
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
145
static enum_nested_loop_state
146
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
148
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
149
static int join_read_system(JOIN_TAB *tab);
150
static int join_read_const(JOIN_TAB *tab);
151
static int join_read_key(JOIN_TAB *tab);
152
static int join_read_always_key(JOIN_TAB *tab);
153
static int join_read_last_key(JOIN_TAB *tab);
154
static int join_no_more_records(READ_RECORD *info);
155
static int join_read_next(READ_RECORD *info);
156
static int join_read_next_different(READ_RECORD *info);
157
static int join_init_quick_read_record(JOIN_TAB *tab);
158
static int test_if_quick_select(JOIN_TAB *tab);
159
static int join_init_read_record(JOIN_TAB *tab);
160
static int join_read_first(JOIN_TAB *tab);
161
static int join_read_next_same(READ_RECORD *info);
162
static int join_read_next_same_diff(READ_RECORD *info);
163
static int join_read_last(JOIN_TAB *tab);
164
static int join_read_prev_same(READ_RECORD *info);
165
static int join_read_prev(READ_RECORD *info);
166
int join_read_always_key_or_null(JOIN_TAB *tab);
167
int join_read_next_same_or_null(READ_RECORD *info);
168
static COND *make_cond_for_table(COND *cond,table_map table,
169
table_map used_table,
170
bool exclude_expensive_cond);
171
static Item* part_of_refkey(TABLE *form,Field *field);
172
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
173
ha_rows select_limit, bool no_changes,
175
static bool list_contains_unique_index(TABLE *table,
176
bool (*find_func) (Field *, void *), void *data);
177
static bool find_field_in_item_list (Field *field, void *data);
178
static bool find_field_in_order_list (Field *field, void *data);
179
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
180
ha_rows filesort_limit, ha_rows select_limit,
182
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
184
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
185
ulong offset,Item *having);
186
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
187
uint field_count, Field **first_field,
189
ulong key_length,Item *having);
190
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
191
static ulong used_blob_length(CACHE_FIELD **ptr);
192
static bool store_record_in_cache(JOIN_CACHE *cache);
193
static void reset_cache_read(JOIN_CACHE *cache);
194
static void reset_cache_write(JOIN_CACHE *cache);
195
static void read_cached_record(JOIN_TAB *tab);
196
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
197
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
198
ORDER *order, List<Item> &fields,
199
List<Item> &all_fields,
200
bool *all_order_by_fields_used);
201
static bool test_if_subpart(ORDER *a,ORDER *b);
202
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
203
static void calc_group_buffer(JOIN *join,ORDER *group);
204
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
205
static bool alloc_group_fields(JOIN *join,ORDER *group);
206
// Create list for using with tempory table
207
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
208
List<Item> &new_list1,
209
List<Item> &new_list2,
210
uint elements, List<Item> &items);
211
// Create list for using with tempory table
212
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
213
List<Item> &new_list1,
214
List<Item> &new_list2,
215
uint elements, List<Item> &items);
216
static void init_tmptable_sum_functions(Item_sum **func);
217
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
218
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
219
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
220
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
221
static bool init_sum_functions(Item_sum **func, Item_sum **end);
222
static bool update_sum_func(Item_sum **func);
223
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
224
bool distinct, const char *message=NullS);
225
static Item *remove_additional_cond(Item* conds);
226
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
227
static bool test_if_ref(Item_field *left_item,Item *right_item);
228
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
229
Item *new_cond, bool fix_fields);
99
232
This is used to mark equalities that were made from i-th IN-equality.
266
391
ref->outer_ref= new_ref;
267
392
ref->ref= &ref->outer_ref;
269
if (!ref->fixed && ref->fix_fields(session, 0))
394
if (!ref->fixed && ref->fix_fields(thd, 0))
271
session->used_tables|= item->used_tables();
396
thd->used_tables|= item->used_tables();
401
#define MAGIC_IN_WHERE_TOP_LEVEL 10
403
Function to setup clauses without sum functions.
405
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
409
List<Item> &all_fields,
412
ORDER *group, bool *hidden_group_fields)
415
nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
417
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
418
res= setup_conds(thd, tables, leaves, conds);
420
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
421
res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
423
thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
424
res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
425
group, hidden_group_fields);
426
thd->lex->allow_sum_func= save_allow_sum_func;
276
430
/*****************************************************************************
277
431
Check fields, find best join, do the select and output fields.
278
select_query assumes that all tables are already opened
432
mysql_select assumes that all tables are already opened
279
433
*****************************************************************************/
436
Prepare of whole select (including sub queries in future).
439
Add check of calculation of GROUP functions and fields:
440
SELECT COUNT(*)+table.col1 from table1;
448
JOIN::prepare(Item ***rref_pointer_array,
449
TABLE_LIST *tables_init,
450
uint wild_num, COND *conds_init, uint og_num,
451
ORDER *order_init, ORDER *group_init,
453
ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
454
SELECT_LEX_UNIT *unit_arg)
456
// to prevent double initialization on EXPLAIN
462
group_list= group_init;
464
proc_param= proc_param_init;
465
tables_list= tables_init;
466
select_lex= select_lex_arg;
467
select_lex->join= this;
468
join_list= &select_lex->top_join_list;
469
union_part= unit_arg->is_union();
471
thd->lex->current_select->is_item_list_lookup= 1;
473
If we have already executed SELECT, then it have not sense to prevent
474
its table from update (see unique_table())
476
if (thd->derived_tables_processing)
477
select_lex->exclude_from_table_unique_test= true;
479
/* Check that all tables, fields, conds and order are ok */
481
if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
482
setup_tables_and_check_access(thd, &select_lex->context, join_list,
483
tables_list, &select_lex->leaf_tables,
487
TABLE_LIST *table_ptr;
488
for (table_ptr= select_lex->leaf_tables;
490
table_ptr= table_ptr->next_leaf)
493
if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
494
select_lex->setup_ref_array(thd, og_num) ||
495
setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
497
setup_without_group(thd, (*rref_pointer_array), tables_list,
498
select_lex->leaf_tables, fields_list,
499
all_fields, &conds, order, group_list,
500
&hidden_group_fields))
501
return(-1); /* purecov: inspected */
503
ref_pointer_array= *rref_pointer_array;
507
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
508
thd->where="having clause";
509
thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
510
select_lex->having_fix_field= 1;
511
bool having_fix_rc= (!having->fixed &&
512
(having->fix_fields(thd, &having) ||
513
having->check_cols(1)));
514
select_lex->having_fix_field= 0;
515
if (having_fix_rc || thd->is_error())
516
return(-1); /* purecov: inspected */
517
thd->lex->allow_sum_func= save_allow_sum_func;
521
Item_subselect *subselect;
522
Item_in_subselect *in_subs= NULL;
524
Are we in a subquery predicate?
525
TODO: the block below will be executed for every PS execution without need.
527
if ((subselect= select_lex->master_unit()->item))
529
bool do_semijoin= !test(thd->variables.optimizer_switch &
530
OPTIMIZER_SWITCH_NO_SEMIJOIN);
531
if (subselect->substype() == Item_subselect::IN_SUBS)
532
in_subs= (Item_in_subselect*)subselect;
535
Check if we're in subquery that is a candidate for flattening into a
536
semi-join (which is done done in flatten_subqueries()). The
538
1. Subquery predicate is an IN/=ANY subq predicate
539
2. Subquery is a single SELECT (not a UNION)
540
3. Subquery does not have GROUP BY or ORDER BY
541
4. Subquery does not use aggregate functions or HAVING
542
5. Subquery predicate is at the AND-top-level of ON/WHERE clause
543
6. No execution method was already chosen (by a prepared statement).
545
(*). We are not in a subquery of a single table UPDATE/DELETE that
546
doesn't have a JOIN (TODO: We should handle this at some
547
point by switching to multi-table UPDATE/DELETE)
549
(**). We're not in a confluent table-less subquery, like
553
!select_lex->master_unit()->first_select()->next_select() && // 2
554
!select_lex->group_list.elements && !order && // 3
555
!having && !select_lex->with_sum_func && // 4
556
thd->thd_marker && // 5
557
select_lex->outer_select()->join && // (*)
558
select_lex->master_unit()->first_select()->leaf_tables && // (**)
560
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
563
if (!in_subs->left_expr->fixed &&
564
in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
569
Check that the right part of the subselect contains no more than one
570
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
572
if (subselect->substype() == Item_subselect::IN_SUBS &&
573
(select_lex->item_list.elements !=
574
((Item_in_subselect*)subselect)->left_expr->cols()))
576
my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
581
/* Register the subquery for further processing */
582
select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
583
in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
587
bool do_materialize= !test(thd->variables.optimizer_switch &
588
OPTIMIZER_SWITCH_NO_MATERIALIZATION);
590
Check if the subquery predicate can be executed via materialization.
591
The required conditions are:
592
1. Subquery predicate is an IN/=ANY subq predicate
593
2. Subquery is a single SELECT (not a UNION)
594
3. Subquery is not a table-less query. In this case there is no
595
point in materializing.
596
4. Subquery predicate is a top-level predicate
597
(this implies it is not negated)
598
TODO: this is a limitation that should be lifeted once we
599
implement correct NULL semantics (WL#3830)
600
5. Subquery is non-correlated
602
This is an overly restrictive condition. It can be extended to:
603
(Subquery is non-correlated ||
604
Subquery is correlated to any query outer to IN predicate ||
605
(Subquery is correlated to the immediate outer query &&
606
Subquery !contains {GROUP BY, ORDER BY [LIMIT],
607
aggregate functions) && subquery predicate is not under "NOT IN"))
608
6. No execution method was already chosen (by a prepared statement).
610
(*) The subquery must be part of a SELECT statement. The current
611
condition also excludes multi-table update statements.
613
We have to determine whether we will perform subquery materialization
614
before calling the IN=>EXISTS transformation, so that we know whether to
615
perform the whole transformation or only that part of it which wraps
616
Item_in_subselect in an Item_in_optimizer.
618
if (do_materialize &&
620
!select_lex->master_unit()->first_select()->next_select() && // 2
621
select_lex->master_unit()->first_select()->leaf_tables && // 3
622
thd->lex->sql_command == SQLCOM_SELECT) // *
624
if (in_subs->is_top_level_item() && // 4
625
!in_subs->is_correlated && // 5
626
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
627
in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
630
Item_subselect::trans_res trans_res;
631
if ((trans_res= subselect->select_transformer(this)) !=
632
Item_subselect::RES_OK)
634
return((trans_res == Item_subselect::RES_ERROR));
643
for (ord= order; ord; ord= ord->next)
645
Item *item= *ord->item;
646
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
647
item->split_sum_func(thd, ref_pointer_array, all_fields);
651
if (having && having->with_sum_func)
652
having->split_sum_func2(thd, ref_pointer_array, all_fields,
654
if (select_lex->inner_sum_func_list)
656
Item_sum *end=select_lex->inner_sum_func_list;
657
Item_sum *item_sum= end;
660
item_sum= item_sum->next;
661
item_sum->split_sum_func2(thd, ref_pointer_array,
662
all_fields, item_sum->ref_by, false);
663
} while (item_sum != end);
666
if (select_lex->inner_refs_list.elements &&
667
fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
671
Check if there are references to un-aggregated columns when computing
672
aggregate functions with implicit grouping (there is no GROUP BY).
674
MODE_ONLY_FULL_GROUP_BY is enabled here by default
676
if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
678
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
679
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
683
/* Caclulate the number of groups */
685
for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
690
goto err; /* purecov: inspected */
692
if (result && result->prepare(fields_list, unit_arg))
693
goto err; /* purecov: inspected */
695
/* Init join struct */
696
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
697
ref_pointer_array_size= all_fields.elements*sizeof(Item*);
698
this->group= group_list != 0;
701
#ifdef RESTRICTED_GROUP
702
if (sum_func_count && !group_list && (func_count || field_count))
704
my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
708
if (select_lex->olap == ROLLUP_TYPE && rollup_init())
710
if (alloc_func_list())
716
return(-1); /* purecov: inspected */
721
Remove the predicates pushed down into the subquery
724
JOIN::remove_subq_pushed_predicates()
725
where IN Must be NULL
726
OUT The remaining WHERE condition, or NULL
729
Given that this join will be executed using (unique|index)_subquery,
730
without "checking NULL", remove the predicates that were pushed down
733
If the subquery compares scalar values, we can remove the condition that
734
was wrapped into trig_cond (it will be checked when needed by the subquery
737
If the subquery compares row values, we need to keep the wrapped
738
equalities in the WHERE clause: when the left (outer) tuple has both NULL
739
and non-NULL values, we'll do a full table scan and will rely on the
740
equalities corresponding to non-NULL parts of left tuple to filter out
741
non-matching records.
743
TODO: We can remove the equalities that will be guaranteed to be true by the
744
fact that subquery engine will be using index lookup. This must be done only
745
for cases where there are no conversion errors of significance, e.g. 257
746
that is searched in a byte. But this requires homogenization of the return
747
codes of all Field*::store() methods.
750
void JOIN::remove_subq_pushed_predicates(Item **where)
752
if (conds->type() == Item::FUNC_ITEM &&
753
((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
754
((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
755
((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
756
test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
757
((Item_func *)conds)->arguments()[0]))
282
766
Index lookup-based subquery: save some flags for EXPLAIN output
805
Check if the table's rowid is included in the temptable
808
sj_table_is_included()
810
join_tab The table to be checked
813
SemiJoinDuplicateElimination: check the table's rowid should be included
814
in the temptable. This is so if
816
1. The table is not embedded within some semi-join nest
817
2. The has been pulled out of a semi-join nest, or
819
3. The table is functionally dependent on some previous table
821
[4. This is also true for constant tables that can't be
822
NULL-complemented but this function is not called for such tables]
825
true - Include table's rowid
829
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
831
if (join_tab->emb_sj_nest)
834
/* Check if this table is functionally dependent on the tables that
835
are within the same outer join nest
837
TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
838
if (join_tab->type == JT_EQ_REF)
840
Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
842
while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
844
JOIN_TAB *ref_tab= join->join_tab + idx;
845
if (embedding == ref_tab->table->pos_in_table_list->embedding)
848
/* Ok, functionally dependent */
851
/* Not functionally dependent => need to include*/
856
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
857
SJ_TMP_TABLE *sjtbl);
861
Setup the strategies to eliminate semi-join duplicates.
864
setup_semijoin_dups_elimination()
866
options Join options (needed to see if join buffering will be
868
no_jbuf_after Another bit of information re where join buffering will
872
Setup the strategies to eliminate semi-join duplicates. ATM there are 3
875
1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
877
2. FirstMatch (pick only the 1st matching row combination of inner tables)
878
3. InsideOut (scanning the sj-inner table in a way that groups duplicates
879
together and picking the 1st one)
881
The join order has "duplicate-generating ranges", and every range is
882
served by one strategy or a combination of FirstMatch with with some
885
"Duplicate-generating range" is defined as a range within the join order
886
that contains all of the inner tables of a semi-join. All ranges must be
887
disjoint, if tables of several semi-joins are interleaved, then the ranges
888
are joined together, which is equivalent to converting
889
SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
891
SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
894
Applicability conditions are as follows:
896
DuplicateWeedout strategy
897
~~~~~~~~~~~~~~~~~~~~~~~~~
899
(ot|nt)* [ it ((it|ot|nt)* (it|ot))] (nt)*
900
+------+ +=========================+ +---+
903
(1) - Prefix of OuterTables (those that participate in
904
IN-equality and/or are correlated with subquery) and outer
905
Noncorrelated Tables.
906
(2) - The handled range. The range starts with the first sj-inner
907
table, and covers all sj-inner and outer tables
908
Within the range, Inner, Outer, outer Noncorrelated tables
909
may follow in any order.
910
(3) - The suffix of outer Noncorrelated tables.
915
(ot|nt)* [ it ((it|nt)* it) ] (nt)*
916
+------+ +==================+ +---+
919
(1) - Prefix of outer and non-correlated tables
920
(2) - The handled range, which may contain only inner and
921
non-correlated tables.
922
(3) - The suffix of outer Noncorrelated tables.
927
(ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ] (ot|nt)*
928
+--------+ +===========+ +=============+ +------+
931
(1) - Prefix that may contain any outer tables. The prefix must contain
932
all the non-trivially correlated outer tables. (non-trivially means
933
that the correlation is not just through the IN-equality).
935
(2) - Inner table for which the InsideOut scan is performed.
937
(3) - The remainder of the duplicate-generating range. It is served by
938
application of FirstMatch strategy, with the exception that
939
outer IN-correlated tables are considered to be non-correlated.
941
(4) - THe suffix of outer and outer non-correlated tables.
943
If several strategies are applicable, their relative priorities are:
948
This function walks over the join order and sets up the strategies by
949
setting appropriate members in join_tab structures.
953
true Out of memory error
957
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
959
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
962
0 - invalid (EOF marker),
964
2 - Temptable (maybe confluent),
965
3 - Temptable with join buffering
968
uint start_idx; /* Left range bound */
969
uint end_idx; /* Right range bound */
971
For Temptable strategy: Bitmap of all outer and correlated tables from
972
all involved join nests.
974
table_map outer_tables;
975
} dups_ranges [MAX_TABLES];
977
TABLE_LIST *emb_insideout_nest= NULL;
978
table_map emb_sj_map= 0; /* A bitmap of sj-nests (that is, their sj-inner
979
tables) whose ranges we're in */
980
table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
981
table_map range_start_map= 0; /* table_map at current range start */
982
bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
987
First pass: locate the duplicate-generating ranges and pick the strategies.
989
for (i=join->const_tables ; i < join->tables ; i++)
991
JOIN_TAB *tab=join->join_tab+i;
992
TABLE *table=tab->table;
993
cur_map |= table->map;
995
if (tab->emb_sj_nest) // Encountered an sj-inner table
999
dups_ranges[cur_range].start_idx= i;
1000
range_start_map= cur_map & ~table->map;
1002
Remember if this is a possible start of range that is covered by
1003
the InsideOut strategy (the reason that it is not covered could
1004
be that it overlaps with anther semi-join's range. we don't
1005
support InsideOut for joined ranges)
1007
if (join->best_positions[i].use_insideout_scan)
1008
emb_insideout_nest= tab->emb_sj_nest;
1011
emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1012
emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1014
if (tab->emb_sj_nest != emb_insideout_nest)
1017
Two different semi-joins interleave. This cannot be handled by
1020
emb_insideout_nest= NULL;
1024
if (emb_sj_map) /* We're in duplicate-generating range */
1026
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1027
tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1028
i <= no_jbuf_after && !dealing_with_jbuf)
1031
This table uses join buffering, which makes use of FirstMatch or
1032
InsideOut strategies impossible for the current and (we assume)
1033
preceding duplicate-producing ranges.
1034
That is, for the join order:
1036
x x [ x x] x [x x x] x [x x X* x] x
1038
+-----+ +-----+ | join buffering use
1041
we'll have to remove r1 and r2 and use duplicate-elimination
1042
strategy that spans all the tables, starting from the very 1st
1045
dealing_with_jbuf= true;
1046
emb_insideout_nest= false;
1049
Absorb all preceding duplicate-eliminating ranges. Their strategies
1052
for (int prev_range= 0; prev_range < cur_range; prev_range++)
1054
dups_ranges[cur_range].outer_tables |=
1055
dups_ranges[prev_range].outer_tables;
1057
dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1058
dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1063
Check if we are at the end of duplicate-producing range. We are if
1065
1. It's an InsideOut range (which presumes all correlated tables are
1066
in the prefix), and all inner tables are in the join order prefix,
1068
2. It's a DuplicateElimination range (possibly covering several
1069
SJ-nests), and all inner, outer, and correlated tables of all
1070
sj-nests are in the join order prefix.
1072
bool end_of_range= false;
1073
if (emb_insideout_nest &&
1074
bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1076
/* Save that this range is handled with InsideOut: */
1077
dups_ranges[cur_range].strategy= 1;
1080
else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1083
This is a complete range to be handled with either DuplicateWeedout
1086
dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1088
This will hold tables from within the range that need to be put
1089
into the join buffer before we can use the FirstMatch on its tail.
1091
dups_ranges[cur_range].outer_tables= emb_outer_tables &
1098
dups_ranges[cur_range].end_idx= i+1;
1099
emb_sj_map= emb_outer_tables= 0;
1100
emb_insideout_nest= NULL;
1101
dealing_with_jbuf= false;
1102
dups_ranges[++cur_range].strategy= 0;
1107
THD *thd= join->thd;
1108
SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1110
Second pass: setup the chosen strategies
1112
for (int j= 0; j < cur_range; j++)
1114
JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1116
if (dups_ranges[j].strategy == 1) // InsideOut strategy
1118
tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1121
else // DuplicateWeedout strategy
1123
SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1124
table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1125
uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1126
uint jt_null_bits= 0; // # null bits in tuple bytes
1127
SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1128
uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1129
JOIN_TAB *last_outer_tab= tab - 1;
1131
Walk through the range and remember
1132
- tables that need their rowids to be put into temptable
1133
- the last outer table
1135
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1137
if (sj_table_is_included(join, tab))
1139
last_tab->join_tab= tab;
1140
last_tab->rowid_offset= jt_rowid_offset;
1141
jt_rowid_offset += tab->table->file->ref_length;
1142
if (tab->table->maybe_null)
1144
last_tab->null_byte= jt_null_bits / 8;
1145
last_tab->null_bit= jt_null_bits++;
1148
tab->table->prepare_for_position();
1149
tab->rowid_keep_flags= rowid_keep_flags;
1151
cur_map |= tab->table->map;
1152
if (!tab->emb_sj_nest && bitmap_covers(cur_map,
1153
dups_ranges[j].outer_tables))
1154
last_outer_tab= tab;
1157
if (jt_rowid_offset) /* Temptable has at least one rowid */
1159
SJ_TMP_TABLE *sjtbl;
1160
uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1161
if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1162
!(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1164
memcpy(sjtbl->tabs, sjtabs, tabs_size);
1165
sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1166
sjtbl->rowid_len= jt_rowid_offset;
1167
sjtbl->null_bits= jt_null_bits;
1168
sjtbl->null_bytes= (jt_null_bits + 7)/8;
1170
*next_sjtbl_ptr= sjtbl;
1171
next_sjtbl_ptr= &(sjtbl->next);
1175
create_duplicate_weedout_tmp_table(thd,
1180
join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1181
join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1183
tab= last_outer_tab + 1;
1184
jump_to= last_outer_tab;
1187
/* Create the FirstMatch tail */
1188
for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1190
if (tab->emb_sj_nest)
1191
tab->do_firstmatch= jump_to;
1200
static void cleanup_sj_tmp_tables(JOIN *join)
1202
for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
1203
sj_tbl= sj_tbl->next)
1205
if (sj_tbl->tmp_table)
1207
free_tmp_table(join->thd, sj_tbl->tmp_table);
1210
join->sj_tmp_tables= NULL;
1213
uint make_join_orderinfo(JOIN *join);
1216
global select optimisation.
1219
error code saved in field 'error'
1230
// to prevent double initialization on EXPLAIN
1235
thd_proc_info(thd, "optimizing");
1236
row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1237
unit->select_limit_cnt);
1238
/* select_limit is used to decide if we are likely to scan the whole table */
1239
select_limit= unit->select_limit_cnt;
1240
if (having || (select_options & OPTION_FOUND_ROWS))
1241
select_limit= HA_POS_ERROR;
1242
do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1243
// Ignore errors of execution if option IGNORE present
1244
if (thd->lex->ignore)
1245
thd->lex->current_select->no_error= 1;
1247
#ifdef HAVE_REF_TO_FIELDS // Not done yet
1248
/* Add HAVING to WHERE if possible */
1249
if (having && !group_list && !sum_func_count)
1256
else if ((conds=new Item_cond_and(conds,having)))
1259
Item_cond_and can't be fixed after creation, so we do not check
1262
conds->fix_fields(thd, &conds);
1263
conds->change_ref_to_fields(thd, tables_list);
1264
conds->top_level_item();
1269
SELECT_LEX *sel= thd->lex->current_select;
1270
if (sel->first_cond_optimization)
1273
The following code will allocate the new items in a permanent
1274
MEMROOT for prepared statements and stored procedures.
1276
sel->first_cond_optimization= 0;
1278
/* Convert all outer joins to inner joins if possible */
1279
conds= simplify_joins(this, join_list, conds, true, false);
1280
build_bitmap_for_nested_joins(join_list, 0);
1283
conds= optimize_cond(this, conds, join_list, &cond_value);
1284
if (thd->is_error())
1291
having= optimize_cond(this, having, join_list, &having_value);
1292
if (thd->is_error())
1297
if (select_lex->where)
1298
select_lex->cond_value= cond_value;
1299
if (select_lex->having)
1300
select_lex->having_value= having_value;
1302
if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
1303
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1304
{ /* Impossible cond */
1305
zero_result_cause= having_value == Item::COND_FALSE ?
1306
"Impossible HAVING" : "Impossible WHERE";
1312
/* Optimize count(*), min() and max() */
1313
if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1317
opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1318
to the WHERE conditions,
1319
or 1 if all items were resolved,
1320
or 0, or an error number HA_ERR_...
1322
if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1324
if (res == HA_ERR_KEY_NOT_FOUND)
1326
zero_result_cause= "No matching min/max row";
1337
zero_result_cause= "No matching min/max row";
1341
zero_result_cause= "Select tables optimized away";
1342
tables_list= 0; // All tables resolved
1344
Extract all table-independent conditions and replace the WHERE
1345
clause with them. All other conditions were computed by opt_sum_query
1346
and the MIN/MAX/COUNT function(s) have been replaced by constants,
1347
so there is no need to compute the whole WHERE clause again.
1348
Notice that make_cond_for_table() will always succeed to remove all
1349
computed conditions, because opt_sum_query() is applicable only to
1351
Preserve conditions for EXPLAIN.
1353
if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1355
COND *table_independent_conds=
1356
make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1357
conds= table_independent_conds;
1366
error= -1; // Error is sent to client
1367
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1369
/* Calculate how to do the join */
1370
thd_proc_info(thd, "statistics");
1371
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1372
thd->is_fatal_error)
1377
/* Remove distinct if only const tables */
1378
select_distinct= select_distinct && (const_tables != tables);
1379
thd_proc_info(thd, "preparing");
1380
if (result->initialize_tables(this))
1382
return(1); // error == -1
1384
if (const_table_map != found_const_table_map &&
1385
!(select_options & SELECT_DESCRIBE) &&
1387
!(conds->used_tables() & RAND_TABLE_BIT) ||
1388
select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1390
zero_result_cause= "no matching row in const table";
1394
if (!(thd->options & OPTION_BIG_SELECTS) &&
1395
best_read > (double) thd->variables.max_join_size &&
1396
!(select_options & SELECT_DESCRIBE))
1397
{ /* purecov: inspected */
1398
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1402
if (const_tables && !thd->locked_tables &&
1403
!(select_options & SELECT_NO_UNLOCK))
1404
mysql_unlock_some_tables(thd, table, const_tables);
1405
if (!conds && outer_join)
1407
/* Handle the case where we have an OUTER JOIN without a WHERE */
1408
conds=new Item_int((int64_t) 1,1); // Always true
1410
select= make_select(*table, const_table_map,
1411
const_table_map, conds, 1, &error);
1413
{ /* purecov: inspected */
1414
error= -1; /* purecov: inspected */
1418
reset_nj_counters(join_list);
1419
make_outerjoin_info(this);
1422
Among the equal fields belonging to the same multiple equality
1423
choose the one that is to be retrieved first and substitute
1424
all references to these in where condition for a reference for
1429
conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1430
conds->update_used_tables();
1434
Permorm the the optimization on fields evaluation mentioned above
1435
for all on expressions.
1437
for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1439
if (*tab->on_expr_ref)
1441
*tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1444
(*tab->on_expr_ref)->update_used_tables();
1448
if (conds &&!outer_join && const_table_map != found_const_table_map &&
1449
(select_options & SELECT_DESCRIBE) &&
1450
select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1452
conds=new Item_int((int64_t) 0,1); // Always false
1454
if (make_join_select(this, select, conds))
1457
"Impossible WHERE noticed after reading const tables";
1458
return(0); // error == 0
1461
error= -1; /* if goto err */
1463
/* Optimize distinct away if possible */
1465
ORDER *org_order= order;
1466
order=remove_const(this, order,conds,1, &simple_order);
1467
if (thd->is_error())
1474
If we are using ORDER BY NULL or ORDER BY const_expression,
1475
return result in any order (even if we are using a GROUP BY)
1477
if (!order && org_order)
1481
Check if we can optimize away GROUP BY/DISTINCT.
1482
We can do that if there are no aggregate functions, the
1483
fields in DISTINCT clause (if present) and/or columns in GROUP BY
1484
(if present) contain direct references to all key parts of
1485
an unique index (in whatever order) and if the key parts of the
1486
unique index cannot contain NULLs.
1487
Note that the unique keys for DISTINCT and GROUP BY should not
1488
be the same (as long as they are unique).
1490
The FROM clause must contain a single non-constant table.
1492
if (tables - const_tables == 1 && (group_list || select_distinct) &&
1493
!tmp_table_param.sum_func_count &&
1494
(!join_tab[const_tables].select ||
1495
!join_tab[const_tables].select->quick ||
1496
join_tab[const_tables].select->quick->get_type() !=
1497
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1500
list_contains_unique_index(join_tab[const_tables].table,
1501
find_field_in_order_list,
1502
(void *) group_list))
1505
We have found that grouping can be removed since groups correspond to
1506
only one row anyway, but we still have to guarantee correct result
1507
order. The line below effectively rewrites the query from GROUP BY
1508
<fields> to ORDER BY <fields>. There are two exceptions:
1509
- if skip_sort_order is set (see above), then we can simply skip
1511
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1512
with the GROUP BY ones, i.e. either one is a prefix of another.
1513
We only check if the ORDER BY is a prefix of GROUP BY. In this case
1514
test_if_subpart() copies the ASC/DESC attributes from the original
1516
If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1519
if (!order || test_if_subpart(group_list, order))
1520
order= skip_sort_order ? 0 : group_list;
1522
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1523
rewritten to IGNORE INDEX FOR ORDER BY(fields).
1525
join_tab->table->keys_in_use_for_order_by=
1526
join_tab->table->keys_in_use_for_group_by;
1530
if (select_distinct &&
1531
list_contains_unique_index(join_tab[const_tables].table,
1532
find_field_in_item_list,
1533
(void *) &fields_list))
1538
if (group_list || tmp_table_param.sum_func_count)
1540
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1543
else if (select_distinct && tables - const_tables == 1)
1546
We are only using one table. In this case we change DISTINCT to a
1548
- The GROUP BY can be done through indexes (no sort) and the ORDER
1549
BY only uses selected fields.
1550
(In this case we can later optimize away GROUP BY and ORDER BY)
1551
- We are scanning the whole table without LIMIT
1553
- We are using CALC_FOUND_ROWS
1554
- We are using an ORDER BY that can't be optimized away.
1556
We don't want to use this optimization when we are using LIMIT
1557
because in this case we can just create a temporary table that
1558
holds LIMIT rows and stop when this table is full.
1560
JOIN_TAB *tab= &join_tab[const_tables];
1561
bool all_order_fields_used;
1563
skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
1564
&tab->table->keys_in_use_for_order_by);
1565
if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1566
order, fields_list, all_fields,
1567
&all_order_fields_used)))
1569
bool skip_group= (skip_sort_order &&
1570
test_if_skip_sort_order(tab, group_list, select_limit, 1,
1571
&tab->table->keys_in_use_for_group_by) != 0);
1572
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1573
if ((skip_group && all_order_fields_used) ||
1574
select_limit == HA_POS_ERROR ||
1575
(order && !skip_sort_order))
1577
/* Change DISTINCT to GROUP BY */
1580
if (all_order_fields_used)
1582
if (order && skip_sort_order)
1585
Force MySQL to read the table in sorted order to get result in
1588
tmp_table_param.quick_group=0;
1592
group=1; // For end_write_group
1597
else if (thd->is_fatal_error) // End of memory
1602
ORDER *old_group_list;
1603
group_list= remove_const(this, (old_group_list= group_list), conds,
1604
rollup.state == ROLLUP::STATE_NONE,
1606
if (thd->is_error())
1611
if (old_group_list && !group_list)
1614
if (!group_list && group)
1616
order=0; // The output has only one row
1618
select_distinct= 0; // No need in distinct for 1 row
1619
group_optimized_away= 1;
1622
calc_group_buffer(this, group_list);
1623
send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1625
if (test_if_subpart(group_list, order) ||
1626
(!group_list && tmp_table_param.sum_func_count))
1629
// Can't use sort on head table if using row cache
1639
Check if we need to create a temporary table.
1640
This has to be done if all tables are not already read (const tables)
1641
and one of the following conditions holds:
1642
- We are using DISTINCT (simple distinct's are already optimized away)
1643
- We are using an ORDER BY or GROUP BY on fields not in the first table
1644
- We are using different ORDER BY and GROUP BY orders
1645
- The user wants us to buffer the result.
1647
need_tmp= (const_tables != tables &&
1648
((select_distinct || !simple_order || !simple_group) ||
1649
(group_list && order) ||
1650
test(select_options & OPTION_BUFFER_RESULT)));
1652
uint no_jbuf_after= make_join_orderinfo(this);
1653
uint64_t select_opts_for_readinfo=
1654
(select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1656
sj_tmp_tables= NULL;
1657
if (!select_lex->sj_nests.is_empty())
1658
setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1661
// No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1662
if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1665
/* Create all structures needed for materialized subquery execution. */
1666
if (setup_subquery_materialization())
1670
is this simple IN subquery?
1672
if (!group_list && !order &&
1673
unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1674
tables == 1 && conds &&
1680
if (join_tab[0].type == JT_EQ_REF &&
1681
join_tab[0].ref.items[0]->name == in_left_expr_name)
1683
remove_subq_pushed_predicates(&where);
1684
save_index_subquery_explain_info(join_tab, where);
1685
join_tab[0].type= JT_UNIQUE_SUBQUERY;
1689
subselect_uniquesubquery_engine(thd,
1694
else if (join_tab[0].type == JT_REF &&
1695
join_tab[0].ref.items[0]->name == in_left_expr_name)
1697
remove_subq_pushed_predicates(&where);
1698
save_index_subquery_explain_info(join_tab, where);
1699
join_tab[0].type= JT_INDEX_SUBQUERY;
1703
subselect_indexsubquery_engine(thd,
1710
} else if (join_tab[0].type == JT_REF_OR_NULL &&
1711
join_tab[0].ref.items[0]->name == in_left_expr_name &&
1712
having->name == in_having_cond)
1714
join_tab[0].type= JT_INDEX_SUBQUERY;
1716
conds= remove_additional_cond(conds);
1717
save_index_subquery_explain_info(join_tab, conds);
1719
change_engine(new subselect_indexsubquery_engine(thd,
1729
Need to tell handlers that to play it safe, it should fetch all
1730
columns of the primary key of the tables: this is because MySQL may
1731
build row pointers for the rows, and for all columns of the primary key
1732
the read set has not necessarily been set by the server code.
1734
if (need_tmp || select_distinct || group_list || order)
1736
for (uint i = const_tables; i < tables; i++)
1737
join_tab[i].table->prepare_for_position();
1740
if (const_tables != tables)
1743
Because filesort always does a full table scan or a quick range scan
1744
we must add the removed reference to the select for the table.
1745
We only need to do this when we have a simple_order or simple_group
1746
as in other cases the join is done before the sort.
1748
if ((order || group_list) &&
1749
(join_tab[const_tables].type != JT_ALL) &&
1750
(join_tab[const_tables].type != JT_REF_OR_NULL) &&
1751
((order && simple_order) || (group_list && simple_group)))
1753
if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1758
if (!(select_options & SELECT_BIG_RESULT) &&
1761
!test_if_skip_sort_order(&join_tab[const_tables], group_list,
1762
unit->select_limit_cnt, 0,
1763
&join_tab[const_tables].table->
1764
keys_in_use_for_group_by))) ||
1766
tmp_table_param.quick_group)
1768
need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort
1773
Force using of tmp table if sorting by a SP or UDF function due to
1774
their expensive and probably non-deterministic nature.
1776
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1778
Item *item= *tmp_order->item;
1779
if (item->is_expensive())
1781
/* Force tmp table without sort */
1782
need_tmp=1; simple_order=simple_group=0;
1790
if (select_options & SELECT_DESCRIBE)
1798
The loose index scan access method guarantees that all grouping or
1799
duplicate row elimination (for distinct) is already performed
1800
during data retrieval, and that all MIN/MAX functions are already
1801
computed for each group. Thus all MIN/MAX functions should be
1802
treated as regular functions, and there is no need to perform
1803
grouping in the main execution loop.
1804
Notice that currently loose index scan is applicable only for
1805
single table queries, thus it is sufficient to test only the first
1806
join_tab element of the plan for its access method.
1808
if (join_tab->is_using_loose_index_scan())
1809
tmp_table_param.precomputed_group_by= true;
1811
/* Create a tmp table if distinct or if the sort is too complicated */
1814
thd_proc_info(thd, "Creating tmp table");
1816
init_items_ref_array();
1818
tmp_table_param.hidden_field_count= (all_fields.elements -
1819
fields_list.elements);
1820
ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1823
Pushing LIMIT to the temporary table creation is not applicable
1824
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1825
there are aggregate functions, because in all these cases we need
1828
ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1830
!thd->lex->current_select->with_sum_func) ?
1831
select_limit : HA_POS_ERROR;
1833
if (!(exec_tmp_table1=
1834
create_tmp_table(thd, &tmp_table_param, all_fields,
1836
group_list ? 0 : select_distinct,
1837
group_list && simple_group,
1846
We don't have to store rows in temp table that doesn't match HAVING if:
1847
- we are sorting the table and writing complete group rows to the
1849
- We are using DISTINCT without resolving the distinct as a GROUP BY
1852
If having is not handled here, it will be checked before the row
1853
is sent to the client.
1856
(sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1859
/* if group or order on first table, sort first */
1860
if (group_list && simple_group)
1862
thd_proc_info(thd, "Sorting for group");
1863
if (create_sort_index(thd, this, group_list,
1864
HA_POS_ERROR, HA_POS_ERROR, false) ||
1865
alloc_group_fields(this, group_list) ||
1866
make_sum_func_list(all_fields, fields_list, 1) ||
1867
setup_sum_funcs(thd, sum_funcs))
1875
if (make_sum_func_list(all_fields, fields_list, 0) ||
1876
setup_sum_funcs(thd, sum_funcs))
1881
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1883
thd_proc_info(thd, "Sorting for order");
1884
if (create_sort_index(thd, this, order,
1885
HA_POS_ERROR, HA_POS_ERROR, true))
1894
Optimize distinct when used on some of the tables
1895
SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1896
In this case we can stop scanning t2 when we have found one t1.a
1899
if (exec_tmp_table1->distinct)
1901
table_map used_tables= thd->used_tables;
1902
JOIN_TAB *last_join_tab= join_tab+tables-1;
1905
if (used_tables & last_join_tab->table->map)
1907
last_join_tab->not_used_in_distinct=1;
1908
} while (last_join_tab-- != join_tab);
1909
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1910
if (order && skip_sort_order)
1912
/* Should always succeed */
1913
if (test_if_skip_sort_order(&join_tab[const_tables],
1914
order, unit->select_limit_cnt, 0,
1915
&join_tab[const_tables].table->
1916
keys_in_use_for_order_by))
1922
If this join belongs to an uncacheable subquery save
1925
if (select_lex->uncacheable && !is_top_level_join() &&
1926
init_save_join_tab())
1927
return(-1); /* purecov: inspected */
1936
Restore values in temporary join.
1938
void JOIN::restore_tmp()
1940
memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1947
unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1948
select_lex->offset_limit->val_uint() :
1953
if (exec_tmp_table1)
1955
exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1956
exec_tmp_table1->file->ha_delete_all_rows();
1957
free_io_cache(exec_tmp_table1);
1958
filesort_free_buffers(exec_tmp_table1,0);
1960
if (exec_tmp_table2)
1962
exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1963
exec_tmp_table2->file->ha_delete_all_rows();
1964
free_io_cache(exec_tmp_table2);
1965
filesort_free_buffers(exec_tmp_table2,0);
1968
set_items_ref_array(items0);
1971
memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1976
/* Reset of sum functions */
1979
Item_sum *func, **func_ptr= sum_funcs;
1980
while ((func= *(func_ptr++)))
1988
@brief Save the original join layout
1990
@details Saves the original join layout so it can be reused in
1991
re-execution and for EXPLAIN.
1993
@return Operation status
1995
@retval 1 error occurred.
1999
JOIN::init_save_join_tab()
2001
if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
2002
return 1; /* purecov: inspected */
2003
error= 0; // Ensure that tmp_join.error= 0
2010
JOIN::save_join_tab()
2012
if (!join_tab_save && select_lex->master_unit()->uncacheable)
2014
if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2015
sizeof(JOIN_TAB) * tables)))
2026
Note, that create_sort_index calls test_if_skip_sort_order and may
2027
finally replace sorting with index scan if there is a LIMIT clause in
2028
the query. It's never shown in EXPLAIN!
2031
When can we have here thd->net.report_error not zero?
2036
List<Item> *columns_list= &fields_list;
2039
thd_proc_info(thd, "executing");
2041
(void) result->prepare2(); // Currently, this cannot fail.
2043
if (!tables_list && (tables || !select_lex->with_sum_func))
2044
{ // Only test of functions
2045
if (select_options & SELECT_DESCRIBE)
2046
select_describe(this, false, false, false,
2047
(zero_result_cause?zero_result_cause:"No tables used"));
2050
result->send_fields(*columns_list,
2051
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2053
We have to test for 'conds' here as the WHERE may not be constant
2054
even if we don't have any tables for prepared statements or if
2055
conds uses something like 'rand()'.
2057
if (cond_value != Item::COND_FALSE &&
2058
(!conds || conds->val_int()) &&
2059
(!having || having->val_int()))
2061
if (do_send_rows && result->send_data(fields_list))
2065
error= (int) result->send_eof();
2066
send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2067
thd->sent_row_count);
2072
error=(int) result->send_eof();
2076
/* Single select (without union) always returns 0 or 1 row */
2077
thd->limit_found_rows= send_records;
2078
thd->examined_row_count= 0;
2082
Don't reset the found rows count if there're no tables as
2083
FOUND_ROWS() may be called. Never reset the examined row count here.
2084
It must be accumulated from all join iterations of all join parts.
2087
thd->limit_found_rows= 0;
2089
if (zero_result_cause)
2091
(void) return_zero_rows(this, result, select_lex->leaf_tables,
2093
send_row_on_empty_set(),
2100
if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2101
get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2104
if (select_options & SELECT_DESCRIBE)
2107
Check if we managed to optimize ORDER BY away and don't use temporary
2108
table to resolve ORDER BY: in that case, we only may need to do
2109
filesort for GROUP BY.
2111
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2114
Reset 'order' to 'group_list' and reinit variables describing
2118
simple_order= simple_group;
2122
(order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2123
(const_tables == tables ||
2124
((simple_order || skip_sort_order) &&
2125
test_if_skip_sort_order(&join_tab[const_tables], order,
2127
&join_tab[const_tables].table->
2128
keys_in_use_for_query))))
2131
select_describe(this, need_tmp,
2132
order != 0 && !skip_sort_order,
2134
!tables ? "No tables used" : NullS);
2138
JOIN *curr_join= this;
2139
List<Item> *curr_all_fields= &all_fields;
2140
List<Item> *curr_fields_list= &fields_list;
2141
TABLE *curr_tmp_table= 0;
2143
Initialize examined rows here because the values from all join parts
2144
must be accumulated in examined_row_count. Hence every join
2145
iteration must count from zero.
2147
curr_join->examined_rows= 0;
2149
/* Create a tmp table if distinct or if the sort is too complicated */
2155
We are in a non cacheable sub query. Get the saved join structure
2157
(curr_join may have been modified during last exection and we need
2160
curr_join= tmp_join;
2162
curr_tmp_table= exec_tmp_table1;
2164
/* Copy data to the temporary table */
2165
thd_proc_info(thd, "Copying to tmp table");
2166
if (!curr_join->sort_and_group &&
2167
curr_join->const_tables != curr_join->tables)
2168
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2169
if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2174
curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2176
if (curr_join->having)
2177
curr_join->having= curr_join->tmp_having= 0; // Allready done
2179
/* Change sum_fields reference to calculated fields in tmp_table */
2180
curr_join->all_fields= *curr_all_fields;
2183
items1= items0 + all_fields.elements;
2184
if (sort_and_group || curr_tmp_table->group)
2186
if (change_to_use_tmp_fields(thd, items1,
2187
tmp_fields_list1, tmp_all_fields1,
2188
fields_list.elements, all_fields))
2193
if (change_refs_to_tmp_fields(thd, items1,
2194
tmp_fields_list1, tmp_all_fields1,
2195
fields_list.elements, all_fields))
2198
curr_join->tmp_all_fields1= tmp_all_fields1;
2199
curr_join->tmp_fields_list1= tmp_fields_list1;
2200
curr_join->items1= items1;
2202
curr_all_fields= &tmp_all_fields1;
2203
curr_fields_list= &tmp_fields_list1;
2204
curr_join->set_items_ref_array(items1);
2206
if (sort_and_group || curr_tmp_table->group)
2208
curr_join->tmp_table_param.field_count+=
2209
curr_join->tmp_table_param.sum_func_count+
2210
curr_join->tmp_table_param.func_count;
2211
curr_join->tmp_table_param.sum_func_count=
2212
curr_join->tmp_table_param.func_count= 0;
2216
curr_join->tmp_table_param.field_count+=
2217
curr_join->tmp_table_param.func_count;
2218
curr_join->tmp_table_param.func_count= 0;
2221
if (curr_tmp_table->group)
2222
{ // Already grouped
2223
if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2224
curr_join->order= curr_join->group_list; /* order by group */
2225
curr_join->group_list= 0;
2229
If we have different sort & group then we must sort the data by group
2230
and copy it to another tmp table
2231
This code is also used if we are using distinct something
2232
we haven't been able to store in the temporary table yet
2233
like SEC_TO_TIME(SUM(...)).
2236
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))
2237
{ /* Must copy to another table */
2238
/* Free first data from old join */
2239
curr_join->join_free();
2240
if (make_simple_join(curr_join, curr_tmp_table))
2242
calc_group_buffer(curr_join, group_list);
2243
count_field_types(select_lex, &curr_join->tmp_table_param,
2244
curr_join->tmp_all_fields1,
2245
curr_join->select_distinct && !curr_join->group_list);
2246
curr_join->tmp_table_param.hidden_field_count=
2247
(curr_join->tmp_all_fields1.elements-
2248
curr_join->tmp_fields_list1.elements);
2251
if (exec_tmp_table2)
2252
curr_tmp_table= exec_tmp_table2;
2255
/* group data to new table */
2258
If the access method is loose index scan then all MIN/MAX
2259
functions are precomputed, and should be treated as regular
2260
functions. See extended comment in JOIN::exec.
2262
if (curr_join->join_tab->is_using_loose_index_scan())
2263
curr_join->tmp_table_param.precomputed_group_by= true;
2265
if (!(curr_tmp_table=
2266
exec_tmp_table2= create_tmp_table(thd,
2267
&curr_join->tmp_table_param,
2270
curr_join->select_distinct &&
2271
!curr_join->group_list,
2272
1, curr_join->select_options,
2276
curr_join->exec_tmp_table2= exec_tmp_table2;
2278
if (curr_join->group_list)
2280
thd_proc_info(thd, "Creating sort index");
2281
if (curr_join->join_tab == join_tab && save_join_tab())
2285
if (create_sort_index(thd, curr_join, curr_join->group_list,
2286
HA_POS_ERROR, HA_POS_ERROR, false) ||
2287
make_group_fields(this, curr_join))
2291
sortorder= curr_join->sortorder;
2294
thd_proc_info(thd, "Copying to group table");
2296
if (curr_join != this)
2300
curr_join->sum_funcs= sum_funcs2;
2301
curr_join->sum_funcs_end= sum_funcs_end2;
2305
curr_join->alloc_func_list();
2306
sum_funcs2= curr_join->sum_funcs;
2307
sum_funcs_end2= curr_join->sum_funcs_end;
2310
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2313
curr_join->group_list= 0;
2314
if (!curr_join->sort_and_group &&
2315
curr_join->const_tables != curr_join->tables)
2316
curr_join->join_tab[curr_join->const_tables].sorted= 0;
2317
if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2318
(tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2323
end_read_record(&curr_join->join_tab->read_record);
2324
curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2325
curr_join->join_tab[0].table= 0; // Table is freed
2327
// No sum funcs anymore
2330
items2= items1 + all_fields.elements;
2331
if (change_to_use_tmp_fields(thd, items2,
2332
tmp_fields_list2, tmp_all_fields2,
2333
fields_list.elements, tmp_all_fields1))
2335
curr_join->tmp_fields_list2= tmp_fields_list2;
2336
curr_join->tmp_all_fields2= tmp_all_fields2;
2338
curr_fields_list= &curr_join->tmp_fields_list2;
2339
curr_all_fields= &curr_join->tmp_all_fields2;
2340
curr_join->set_items_ref_array(items2);
2341
curr_join->tmp_table_param.field_count+=
2342
curr_join->tmp_table_param.sum_func_count;
2343
curr_join->tmp_table_param.sum_func_count= 0;
2345
if (curr_tmp_table->distinct)
2346
curr_join->select_distinct=0; /* Each row is unique */
2348
curr_join->join_free(); /* Free quick selects */
2349
if (curr_join->select_distinct && ! curr_join->group_list)
2351
thd_proc_info(thd, "Removing duplicates");
2352
if (curr_join->tmp_having)
2353
curr_join->tmp_having->update_used_tables();
2354
if (remove_duplicates(curr_join, curr_tmp_table,
2355
*curr_fields_list, curr_join->tmp_having))
2357
curr_join->tmp_having=0;
2358
curr_join->select_distinct=0;
2360
curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2361
if (make_simple_join(curr_join, curr_tmp_table))
2363
calc_group_buffer(curr_join, curr_join->group_list);
2364
count_field_types(select_lex, &curr_join->tmp_table_param,
2365
*curr_all_fields, 0);
2369
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2371
if (make_group_fields(this, curr_join))
2378
init_items_ref_array();
2379
items3= ref_pointer_array + (all_fields.elements*4);
2380
setup_copy_fields(thd, &curr_join->tmp_table_param,
2381
items3, tmp_fields_list3, tmp_all_fields3,
2382
curr_fields_list->elements, *curr_all_fields);
2383
tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2384
tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2385
tmp_table_param.save_copy_field_end=
2386
curr_join->tmp_table_param.copy_field_end;
2387
curr_join->tmp_all_fields3= tmp_all_fields3;
2388
curr_join->tmp_fields_list3= tmp_fields_list3;
2392
curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2393
curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2394
curr_join->tmp_table_param.copy_field_end=
2395
tmp_table_param.save_copy_field_end;
2397
curr_fields_list= &tmp_fields_list3;
2398
curr_all_fields= &tmp_all_fields3;
2399
curr_join->set_items_ref_array(items3);
2401
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2403
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2404
thd->is_fatal_error)
2407
if (curr_join->group_list || curr_join->order)
2409
thd_proc_info(thd, "Sorting result");
2410
/* If we have already done the group, add HAVING to sorted table */
2411
if (curr_join->tmp_having && ! curr_join->group_list &&
2412
! curr_join->sort_and_group)
2414
// Some tables may have been const
2415
curr_join->tmp_having->update_used_tables();
2416
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2417
table_map used_tables= (curr_join->const_table_map |
2418
curr_table->table->map);
2420
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2423
if (sort_table_cond)
2425
if (!curr_table->select)
2426
if (!(curr_table->select= new SQL_SELECT))
2428
if (!curr_table->select->cond)
2429
curr_table->select->cond= sort_table_cond;
2430
else // This should never happen
2432
if (!(curr_table->select->cond=
2433
new Item_cond_and(curr_table->select->cond,
2437
Item_cond_and do not need fix_fields for execution, its parameters
2438
are fixed or do not need fix_fields, too
2440
curr_table->select->cond->quick_fix_field();
2442
curr_table->select_cond= curr_table->select->cond;
2443
curr_table->select_cond->top_level_item();
2444
curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2451
curr_join->select_limit= HA_POS_ERROR;
2455
We can abort sorting after thd->select_limit rows if we there is no
2456
WHERE clause for any tables after the sorted one.
2458
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2459
JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2460
for (; curr_table < end_table ; curr_table++)
2463
table->keyuse is set in the case there was an original WHERE clause
2464
on the table that was optimized away.
2466
if (curr_table->select_cond ||
2467
(curr_table->keyuse && !curr_table->first_inner))
2469
/* We have to sort all rows */
2470
curr_join->select_limit= HA_POS_ERROR;
2475
if (curr_join->join_tab == join_tab && save_join_tab())
2480
Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2481
chose FILESORT to be faster than INDEX SCAN or there is no
2482
suitable index present.
2483
Note, that create_sort_index calls test_if_skip_sort_order and may
2484
finally replace sorting with index scan if there is a LIMIT clause in
2485
the query. XXX: it's never shown in EXPLAIN!
2486
OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2488
if (create_sort_index(thd, curr_join,
2489
curr_join->group_list ?
2490
curr_join->group_list : curr_join->order,
2491
curr_join->select_limit,
2492
(select_options & OPTION_FOUND_ROWS ?
2493
HA_POS_ERROR : unit->select_limit_cnt),
2494
curr_join->group_list ? true : false))
2496
sortorder= curr_join->sortorder;
2497
if (curr_join->const_tables != curr_join->tables &&
2498
!curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2501
If no IO cache exists for the first table then we are using an
2502
INDEX SCAN and no filesort. Thus we should not remove the sorted
2503
attribute on the INDEX SCAN.
2509
/* XXX: When can we have here thd->is_error() not zero? */
2510
if (thd->is_error())
2512
error= thd->is_error();
2515
curr_join->having= curr_join->tmp_having;
2516
curr_join->fields= curr_fields_list;
2519
thd_proc_info(thd, "Sending data");
2520
result->send_fields(*curr_fields_list,
2521
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2522
error= do_select(curr_join, curr_fields_list, NULL);
2523
thd->limit_found_rows= curr_join->send_records;
2526
/* Accumulate the counts from all join iterations of all join parts. */
2527
thd->examined_row_count+= curr_join->examined_rows;
2530
With EXPLAIN EXTENDED we have to restore original ref_array
2531
for a derived table which is always materialized.
2532
Otherwise we would not be able to print the query correctly.
2535
(thd->lex->describe & DESCRIBE_EXTENDED) &&
2536
select_lex->linkage == DERIVED_TABLE_TYPE)
2537
set_items_ref_array(items0);
2547
Return error that hold JOIN.
2553
select_lex->join= 0;
2557
if (join_tab != tmp_join->join_tab)
2559
JOIN_TAB *tab, *end;
2560
for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2563
tmp_join->tmp_join= 0;
2564
tmp_table_param.copy_field=0;
2565
return(tmp_join->destroy());
2570
if (exec_tmp_table1)
2571
free_tmp_table(thd, exec_tmp_table1);
2572
if (exec_tmp_table2)
2573
free_tmp_table(thd, exec_tmp_table2);
2575
delete_dynamic(&keyuse);
317
2582
An entry point to single-unit select (a select without UNION).
319
@param session thread Cursor
2584
@param thd thread handler
320
2585
@param rref_pointer_array a reference to ref_pointer_array of
321
2586
the top-level select_lex for this query
322
2587
@param tables list of all tables used in this query.
323
2588
The tables have been pre-opened.
324
@param wild_num number of wildcards used in the top level
2589
@param wild_num number of wildcards used in the top level
325
2590
select of this query.
326
2591
For example statement
327
2592
SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
448
session->set_proc_info("end");
2716
thd_proc_info(thd, "end");
449
2717
err|= select_lex->cleanup();
450
return(err || session->is_error());
2718
return(err || thd->is_error());
452
2720
return(join->error);
455
inline Item *and_items(Item* cond, Item *item)
2724
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
2725
Item_in_subselect* const *el2)
2727
return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 :
2728
( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2732
inline Item * and_items(Item* cond, Item *item)
457
2734
return (cond? (new Item_cond_and(cond, item)) : item);
2738
static TABLE_LIST *alloc_join_nest(THD *thd)
2741
if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2742
sizeof(NESTED_JOIN))))
2744
tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl +
2745
ALIGN_SIZE(sizeof(TABLE_LIST)));
2750
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2752
List_iterator<TABLE_LIST> it(*tlist);
2754
while ((table= it++))
2757
table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
2758
if (table->nested_join)
2759
fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
2765
Convert a subquery predicate into a TABLE_LIST semi-join nest
2768
convert_subq_to_sj()
2769
parent_join Parent join, the one that has subq_pred in its WHERE/ON
2771
subq_pred Subquery predicate to be converted
2774
Convert a subquery predicate into a TABLE_LIST semi-join nest. All the
2775
prerequisites are already checked, so the conversion is always successfull.
2777
Prepared Statements: the transformation is permanent:
2778
- Changes in TABLE_LIST structures are naturally permanent
2779
- Item tree changes are performed on statement MEM_ROOT:
2780
= we activate statement MEM_ROOT
2781
= this function is called before the first fix_prepare_information
2784
This is intended because the criteria for subquery-to-sj conversion remain
2785
constant for the lifetime of the Prepared Statement.
2789
true Out of memory error
2792
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2794
SELECT_LEX *parent_lex= parent_join->select_lex;
2795
TABLE_LIST *emb_tbl_nest= NULL;
2796
List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2797
THD *thd= parent_join->thd;
2800
1. Find out where to put the predicate into.
2801
Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2803
if ((void*)subq_pred->expr_join_nest != (void*)1)
2805
if (subq_pred->expr_join_nest->nested_join)
2810
... [LEFT] JOIN ( ... ) ON (subquery AND whatever) ...
2812
The sj-nest will be inserted into the brackets nest.
2814
emb_tbl_nest= subq_pred->expr_join_nest;
2815
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2817
else if (!subq_pred->expr_join_nest->outer_join)
2822
... INNER JOIN tblX ON (subquery AND whatever) ...
2824
The sj-nest will be tblX's "sibling", i.e. another child of its
2825
parent. This is ok because tblX is joined as an inner join.
2827
emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2829
emb_join_list= &emb_tbl_nest->nested_join->join_list;
2831
else if (!subq_pred->expr_join_nest->nested_join)
2833
TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;
2834
TABLE_LIST *wrap_nest;
2838
... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2840
we'll need to convert it into:
2842
... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2844
|<----- wrap_nest ---->|
2846
Q: other subqueries may be pointing to this element. What to do?
2847
A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2848
But we'll need to fix other pointers.
2849
A2: Another way: have TABLE_LIST::next_ptr so the following
2850
subqueries know the table has been nested.
2851
A3: changes in the TABLE_LIST::outer_join will make everything work
2854
if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2858
wrap_nest->embedding= outer_tbl->embedding;
2859
wrap_nest->join_list= outer_tbl->join_list;
2860
wrap_nest->alias= (char*) "(sj-wrap)";
2862
wrap_nest->nested_join->join_list.empty();
2863
wrap_nest->nested_join->join_list.push_back(outer_tbl);
2865
outer_tbl->embedding= wrap_nest;
2866
outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2869
wrap_nest will take place of outer_tbl, so move the outer join flag
2872
wrap_nest->outer_join= outer_tbl->outer_join;
2873
outer_tbl->outer_join= 0;
2875
wrap_nest->on_expr= outer_tbl->on_expr;
2876
outer_tbl->on_expr= NULL;
2878
List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2882
if (tbl == outer_tbl)
2884
li.replace(wrap_nest);
2889
Ok now wrap_nest 'contains' outer_tbl and we're ready to add the
2890
semi-join nest into it
2892
emb_join_list= &wrap_nest->nested_join->join_list;
2893
emb_tbl_nest= wrap_nest;
2897
TABLE_LIST *sj_nest;
2898
NESTED_JOIN *nested_join;
2899
if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2903
nested_join= sj_nest->nested_join;
2905
sj_nest->join_list= emb_join_list;
2906
sj_nest->embedding= emb_tbl_nest;
2907
sj_nest->alias= (char*) "(sj-nest)";
2908
/* Nests do not participate in those 'chains', so: */
2909
/* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2910
emb_join_list->push_back(sj_nest);
2913
nested_join->used_tables and nested_join->not_null_tables are
2914
initialized in simplify_joins().
2918
2. Walk through subquery's top list and set 'embedding' to point to the
2921
st_select_lex *subq_lex= subq_pred->unit->first_select();
2922
nested_join->join_list.empty();
2923
List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
2924
TABLE_LIST *tl, *last_leaf;
2927
tl->embedding= sj_nest;
2928
tl->join_list= &nested_join->join_list;
2929
nested_join->join_list.push_back(tl);
2933
Reconnect the next_leaf chain.
2934
TODO: Do we have to put subquery's tables at the end of the chain?
2935
Inserting them at the beginning would be a bit faster.
2936
NOTE: We actually insert them at the front! That's because the order is
2937
reversed in this list.
2939
for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
2940
tl->next_leaf= subq_lex->leaf_tables;
2944
Same as above for next_local chain
2945
(a theory: a next_local chain always starts with ::leaf_tables
2946
because view's tables are inserted after the view)
2948
for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
2949
tl->next_local= subq_lex->leaf_tables;
2951
/* A theory: no need to re-connect the next_global chain */
2953
/* 3. Remove the original subquery predicate from the WHERE/ON */
2955
// The subqueries were replaced for Item_int(1) earlier
2956
subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
2957
/*TODO: also reset the 'with_subselect' there. */
2959
/* n. Adjust the parent_join->tables counter */
2960
uint table_no= parent_join->tables;
2961
/* n. Walk through child's tables and adjust table->map */
2962
for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2964
tl->table->tablenr= table_no;
2965
tl->table->map= ((table_map)1) << table_no;
2966
SELECT_LEX *old_sl= tl->select_lex;
2967
tl->select_lex= parent_join->select_lex;
2968
for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2969
emb->select_lex= parent_join->select_lex;
2971
parent_join->tables += subq_lex->join->tables;
2974
Put the subquery's WHERE into semi-join's sj_on_expr
2975
Add the subquery-induced equalities too.
2977
SELECT_LEX *save_lex= thd->lex->current_select;
2978
thd->lex->current_select=subq_lex;
2979
if (!subq_pred->left_expr->fixed &&
2980
subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
2982
thd->lex->current_select=save_lex;
2984
sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2985
sj_nest->nested_join->sj_depends_on= subq_pred->used_tables() |
2986
subq_pred->left_expr->used_tables();
2987
sj_nest->sj_on_expr= subq_lex->where;
2990
Create the IN-equalities and inject them into semi-join's ON expression.
2991
Additionally, for InsideOut strategy
2992
- Record the number of IN-equalities.
2993
- Create list of pointers to (oe1, ..., ieN). We'll need the list to
2994
see which of the expressions are bound and which are not (for those
2995
we'll produce a distinct stream of (ie_i1,...ie_ik).
2997
(TODO: can we just create a list of pointers and hope the expressions
2998
will not substitute themselves on fix_fields()? or we need to wrap
2999
them into Item_direct_view_refs and store pointers to those. The
3000
pointers to Item_direct_view_refs are guaranteed to be stable as
3001
Item_direct_view_refs doesn't substitute itself with anything in
3002
Item_direct_view_ref::fix_fields.
3004
sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
3005
sj_nest->nested_join->sj_outer_expr_list.empty();
3007
if (subq_pred->left_expr->cols() == 1)
3009
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3011
Item *item_eq= new Item_func_eq(subq_pred->left_expr,
3012
subq_lex->ref_pointer_array[0]);
3013
item_eq->name= (char*)subq_sj_cond_name;
3014
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3018
for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3020
nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3023
new Item_func_eq(subq_pred->left_expr->element_index(i),
3024
subq_lex->ref_pointer_array[i]);
3025
item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3026
sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3029
/* Fix the created equality and AND */
3030
sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3033
Walk through sj nest's WHERE and ON expressions and call
3034
item->fix_table_changes() for all items.
3036
sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3037
fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3040
/* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3041
subq_lex->master_unit()->exclude_level();
3043
/* Inject sj_on_expr into the parent's WHERE or ON */
3046
emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr,
3047
sj_nest->sj_on_expr);
3048
emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3052
/* Inject into the WHERE */
3053
parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3054
parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3055
parent_join->select_lex->where= parent_join->conds;
3063
Convert candidate subquery predicates to semi-joins
3066
JOIN::flatten_subqueries()
3069
Convert candidate subquery predicates to semi-joins.
3076
bool JOIN::flatten_subqueries()
3078
Item_in_subselect **in_subq;
3079
Item_in_subselect **in_subq_end;
3081
if (sj_subselects.elements() == 0)
3084
/* 1. Fix children subqueries */
3085
for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
3086
in_subq != in_subq_end; in_subq++)
3088
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3089
child_join->outer_tables = child_join->tables;
3090
if (child_join->flatten_subqueries())
3092
(*in_subq)->sj_convert_priority=
3093
(*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3096
//dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3098
2. Pick which subqueries to convert:
3099
sort the subquery array
3100
- prefer correlated subqueries over uncorrelated;
3101
- prefer subqueries that have greater number of outer tables;
3103
sj_subselects.sort(subq_sj_candidate_cmp);
3104
// #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3105
/* Replace all subqueries to be flattened with Item_int(1) */
3106
for (in_subq= sj_subselects.front();
3107
in_subq != in_subq_end &&
3108
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3111
if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3115
for (in_subq= sj_subselects.front();
3116
in_subq != in_subq_end &&
3117
tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3120
if (convert_subq_to_sj(this, *in_subq))
3124
/* 3. Finalize those we didn't convert */
3125
for (; in_subq!= in_subq_end; in_subq++)
3127
JOIN *child_join= (*in_subq)->unit->first_select()->join;
3128
Item_subselect::trans_res res;
3129
(*in_subq)->changed= 0;
3130
(*in_subq)->fixed= 0;
3131
res= (*in_subq)->select_transformer(child_join);
3132
if (res == Item_subselect::RES_ERROR)
3135
(*in_subq)->changed= 1;
3136
(*in_subq)->fixed= 1;
3138
Item *substitute= (*in_subq)->substitution;
3139
bool do_fix_fields= !(*in_subq)->substitution->fixed;
3140
if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3143
//if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3146
sj_subselects.clear();
3152
Setup for execution all subqueries of a query, for which the optimizer
3153
chose hash semi-join.
3155
@details Iterate over all subqueries of the query, and if they are under an
3156
IN predicate, and the optimizer chose to compute it via hash semi-join:
3157
- try to initialize all data structures needed for the materialized execution
3158
of the IN predicate,
3159
- if this fails, then perform the IN=>EXISTS transformation which was
3160
previously blocked during JOIN::prepare.
3162
This method is part of the "code generation" query processing phase.
3164
This phase must be called after substitute_for_best_equal_field() because
3165
that function may replace items with other items from a multiple equality,
3166
and we need to reference the correct items in the index access method of the
3169
@return Operation status
3170
@retval false success.
3171
@retval true error occurred.
3174
bool JOIN::setup_subquery_materialization()
3176
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3177
un= un->next_unit())
3179
for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3181
Item_subselect *subquery_predicate= sl->master_unit()->item;
3182
if (subquery_predicate &&
3183
subquery_predicate->substype() == Item_subselect::IN_SUBS)
3185
Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3186
if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3187
in_subs->setup_engine())
3197
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3200
find_eq_ref_candidate()
3201
table Table to be checked
3202
sj_inner_tables Bitmap of inner tables. eq_ref(inner_table) doesn't
3206
Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3209
Check again if it is feasible to factor common parts with constant table
3213
true - There exists an eq_ref(outer-tables) candidate
3217
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3219
KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3224
while (1) /* For each key */
3227
KEY *keyinfo= table->key_info + key;
3228
key_part_map bound_parts= 0;
3229
if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
3231
do /* For all equalities on all key parts */
3233
/* Check if this is "t.keypart = expr(outer_tables) */
3234
if (!(keyuse->used_tables & sj_inner_tables) &&
3235
!(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3237
bound_parts |= 1 << keyuse->keypart;
3240
} while (keyuse->key == key && keyuse->table == table);
3242
if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3244
if (keyuse->table != table)
3252
if (keyuse->table != table)
3255
while (keyuse->key == key);
3264
Pull tables out of semi-join nests, if possible
3267
pull_out_semijoin_tables()
3268
join The join where to do the semi-join flattening
3271
Try to pull tables out of semi-join nests.
3274
When this function is called, the join may have several semi-join nests
3275
(possibly within different semi-join nests), but it is guaranteed that
3276
one semi-join nest does not contain another.
3279
A table can be pulled out of the semi-join nest if
3280
- It is a constant table
3284
* Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3286
* Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3287
* Semi-join nests TABLE_LIST::sj_inner_tables
3289
This operation is (and should be) performed at each PS execution since
3290
tables may become/cease to be constant across PS reexecutions.
3294
1 - Out of memory error
3297
int pull_out_semijoin_tables(JOIN *join)
3299
TABLE_LIST *sj_nest;
3300
List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3302
/* Try pulling out of the each of the semi-joins */
3303
while ((sj_nest= sj_list_it++))
3305
/* Action #1: Mark the constant tables to be pulled out */
3306
table_map pulled_tables= 0;
3308
List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3310
while ((tbl= child_li++))
3314
tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3315
if (tbl->table->map & join->const_table_map)
3317
pulled_tables |= tbl->table->map;
3323
Action #2: Find which tables we can pull out based on
3324
update_ref_and_keys() data. Note that pulling one table out can allow
3325
us to pull out some other tables too.
3327
bool pulled_a_table;
3330
pulled_a_table= false;
3332
while ((tbl= child_li++))
3334
if (tbl->table && !(pulled_tables & tbl->table->map))
3336
if (find_eq_ref_candidate(tbl->table,
3337
sj_nest->nested_join->used_tables &
3340
pulled_a_table= true;
3341
pulled_tables |= tbl->table->map;
3345
} while (pulled_a_table);
3348
if ((sj_nest)->nested_join->used_tables == pulled_tables)
3350
(sj_nest)->sj_inner_tables= 0;
3351
while ((tbl= child_li++))
3354
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3359
/* Record the bitmap of inner tables, mark the inner tables */
3360
table_map inner_tables=(sj_nest)->nested_join->used_tables &
3362
(sj_nest)->sj_inner_tables= inner_tables;
3363
while ((tbl= child_li++))
3367
if (inner_tables & tbl->table->map)
3368
tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3370
tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
460
3378
/*****************************************************************************
461
Create JoinTableS, make a guess about the table types,
3379
Create JOIN_TABS, make a guess about the table types,
462
3380
Approximate how many records will be used in each table
463
3381
*****************************************************************************/
464
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
3384
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3386
const key_map *keys,ha_rows limit)
467
if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3389
if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
468
3390
return(0); // Fatal error flag is set
471
3393
select->head=table;
472
3394
table->reginfo.impossible_range=0;
473
if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
3395
if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
474
3396
limit, 0, false)) == 1)
475
3397
return(select->quick->records);
476
3398
if (error == -1)
482
3404
return(HA_POS_ERROR); /* This shouldn't happend */
3408
This structure is used to collect info on potentially sargable
3409
predicates in order to check whether they become sargable after
3410
reading const tables.
3411
We form a bitmap of indexes that can be used for sargable predicates.
3412
Only such indexes are involved in range analysis.
3414
typedef struct st_sargable_param
3416
Field *field; /* field against which to check sargability */
3417
Item **arg_value; /* values of potential keys for lookups */
3418
uint num_values; /* number of values in the above array */
3422
Calculate the best possible join and initialize the join structure.
3431
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3432
DYNAMIC_ARRAY *keyuse_array)
3436
uint i,table_count,const_count,key;
3437
table_map found_const_table_map, all_table_map, found_ref, refs;
3438
key_map const_ref, eq_part;
3439
TABLE **table_vector;
3440
JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3441
KEYUSE *keyuse,*start_keyuse;
3442
table_map outer_join=0;
3443
SARGABLE_PARAM *sargables= 0;
3444
JOIN_TAB *stat_vector[MAX_TABLES+1];
3446
table_count=join->tables;
3447
stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3448
stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3449
table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3450
if (!stat || !stat_ref || !table_vector)
3451
return(1); // Eom /* purecov: inspected */
3453
join->best_ref=stat_vector;
3455
stat_end=stat+table_count;
3456
found_const_table_map= all_table_map=0;
3461
s++, tables= tables->next_leaf, i++)
3463
TABLE_LIST *embedding= tables->embedding;
3466
s->const_keys.init();
3467
s->checked_keys.init();
3468
s->needed_reg.init();
3469
table_vector[i]=s->table=table=tables->table;
3470
table->pos_in_table_list= tables;
3471
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3474
table->file->print_error(error, MYF(0));
3477
table->quick_keys.clear_all();
3478
table->reginfo.join_tab=s;
3479
table->reginfo.not_exists_optimize=0;
3480
memset((char*) table->const_key_parts, 0,
3481
sizeof(key_part_map)*table->s->keys);
3482
all_table_map|= table->map;
3484
s->info=0; // For describe
3486
s->dependent= tables->dep_tables;
3487
s->key_dependent= 0;
3488
if (tables->schema_table)
3489
table->file->stats.records= 2;
3490
table->quick_condition_rows= table->file->stats.records;
3492
s->on_expr_ref= &tables->on_expr;
3493
if (*s->on_expr_ref)
3495
/* s is the only inner table of an outer join */
3496
if (!table->file->stats.records && !embedding)
3498
s->dependent= 0; // Ignore LEFT JOIN depend.
3499
set_position(join,const_count++,s,(KEYUSE*) 0);
3502
outer_join|= table->map;
3503
s->embedding_map= 0;
3504
for (;embedding; embedding= embedding->embedding)
3505
s->embedding_map|= embedding->nested_join->nj_map;
3508
if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3510
/* s belongs to a nested join, maybe to several embedded joins */
3511
s->embedding_map= 0;
3514
NESTED_JOIN *nested_join= embedding->nested_join;
3515
s->embedding_map|=nested_join->nj_map;
3516
s->dependent|= embedding->dep_tables;
3517
embedding= embedding->embedding;
3518
outer_join|= nested_join->used_tables;
3523
if ((table->s->system || table->file->stats.records <= 1) &&
3525
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
3527
set_position(join,const_count++,s,(KEYUSE*) 0);
3531
join->outer_join=outer_join;
3533
if (join->outer_join)
3536
Build transitive closure for relation 'to be dependent on'.
3537
This will speed up the plan search for many cases with outer joins,
3538
as well as allow us to catch illegal cross references/
3539
Warshall's algorithm is used to build the transitive closure.
3540
As we use bitmaps to represent the relation the complexity
3541
of the algorithm is O((number of tables)^2).
3543
for (i= 0, s= stat ; i < table_count ; i++, s++)
3545
for (uint j= 0 ; j < table_count ; j++)
3547
table= stat[j].table;
3548
if (s->dependent & table->map)
3549
s->dependent |= table->reginfo.join_tab->dependent;
3552
s->table->maybe_null= 1;
3554
/* Catch illegal cross references for outer joins */
3555
for (i= 0, s= stat ; i < table_count ; i++, s++)
3557
if (s->dependent & s->table->map)
3559
join->tables=0; // Don't use join->table
3560
my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3563
s->key_dependent= s->dependent;
3567
if (conds || outer_join)
3568
if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3569
conds, join->cond_equal,
3570
~outer_join, join->select_lex, &sargables))
3573
/* Read tables with 0 or 1 rows (system tables) */
3574
join->const_table_map= 0;
3576
for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3583
join->const_table_map|=s->table->map;
3584
if ((tmp=join_read_const_table(s, p_pos)))
3587
return(1); // Fatal error
3590
found_const_table_map|= s->table->map;
3593
/* loop until no more const tables are found */
3597
more_const_tables_found:
3602
We only have to loop from stat_vector + const_count as
3603
set_position() will move all const_tables first in stat_vector
3606
for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3611
If equi-join condition by a key is null rejecting and after a
3612
substitution of a const table the key value happens to be null
3613
then we can state that there are no matches for this equi-join.
3615
if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3618
When performing an outer join operation if there are no matching rows
3619
for the single row of the outer table all the inner tables are to be
3620
null complemented and thus considered as constant tables.
3621
Here we apply this consideration to the case of outer join operations
3622
with a single inner table only because the case with nested tables
3623
would require a more thorough analysis.
3624
TODO. Apply single row substitution to null complemented inner tables
3625
for nested outer join operations.
3627
while (keyuse->table == table)
3629
if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3630
keyuse->val->is_null() && keyuse->null_rejecting)
3633
mark_as_null_row(table);
3634
found_const_table_map|= table->map;
3635
join->const_table_map|= table->map;
3636
set_position(join,const_count++,s,(KEYUSE*) 0);
3637
goto more_const_tables_found;
3643
if (s->dependent) // If dependent on some table
3645
// All dep. must be constants
3646
if (s->dependent & ~(found_const_table_map))
3648
if (table->file->stats.records <= 1L &&
3649
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3650
!table->pos_in_table_list->embedding)
3654
join->const_table_map|=table->map;
3655
set_position(join,const_count++,s,(KEYUSE*) 0);
3656
if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3659
return(1); // Fatal error
3662
found_const_table_map|= table->map;
3666
/* check if table can be read by key or table only uses const refs */
3667
if ((keyuse=s->keyuse))
3670
while (keyuse->table == table)
3672
start_keyuse=keyuse;
3674
s->keys.set_bit(key); // QQ: remove this ?
3677
const_ref.clear_all();
3678
eq_part.clear_all();
3681
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3683
if (!((~found_const_table_map) & keyuse->used_tables))
3684
const_ref.set_bit(keyuse->keypart);
3686
refs|=keyuse->used_tables;
3687
eq_part.set_bit(keyuse->keypart);
3690
} while (keyuse->table == table && keyuse->key == key);
3692
if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3693
!table->pos_in_table_list->embedding)
3695
if ((table->key_info[key].flags & (HA_NOSAME))
3698
if (const_ref == eq_part)
3699
{ // Found everything for ref.
3703
join->const_table_map|=table->map;
3704
set_position(join,const_count++,s,start_keyuse);
3705
if (create_ref_for_key(join, s, start_keyuse,
3706
found_const_table_map))
3708
if ((tmp=join_read_const_table(s,
3709
join->positions+const_count-1)))
3712
return(1); // Fatal error
3715
found_const_table_map|= table->map;
3719
found_ref|= refs; // Table is const if all refs are const
3721
else if (const_ref == eq_part)
3722
s->const_keys.set_bit(key);
3727
} while (join->const_table_map & found_ref && ref_changed);
3730
Update info on indexes that can be used for search lookups as
3731
reading const tables may has added new sargable predicates.
3733
if (const_count && sargables)
3735
for( ; sargables->field ; sargables++)
3737
Field *field= sargables->field;
3738
JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3739
key_map possible_keys= field->key_start;
3740
possible_keys.intersect(field->table->keys_in_use_for_query);
3742
for (uint j=0; j < sargables->num_values; j++)
3743
is_const&= sargables->arg_value[j]->const_item();
3745
join_tab[0].const_keys.merge(possible_keys);
3749
if (pull_out_semijoin_tables(join))
3752
/* Calc how many (possible) matched records in each table */
3754
for (s=stat ; s < stat_end ; s++)
3756
if (s->type == JT_SYSTEM || s->type == JT_CONST)
3758
/* Only one matching row */
3759
s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3762
/* Approximate found rows and time to read them */
3763
s->found_records=s->records=s->table->file->stats.records;
3764
s->read_time=(ha_rows) s->table->file->scan_time();
3767
Set a max range of how many seeks we can expect when using keys
3768
This is can't be to high as otherwise we are likely to use
3771
s->worst_seeks= min((double) s->found_records / 10,
3772
(double) s->read_time*3);
3773
if (s->worst_seeks < 2.0) // Fix for small tables
3777
Add to stat->const_keys those indexes for which all group fields or
3778
all select distinct fields participate in one index.
3780
add_group_and_distinct_keys(join, s);
3782
if (!s->const_keys.is_clear_all() &&
3783
!s->table->pos_in_table_list->embedding)
3787
select= make_select(s->table, found_const_table_map,
3788
found_const_table_map,
3789
*s->on_expr_ref ? *s->on_expr_ref : conds,
3793
records= get_quick_record_count(join->thd, select, s->table,
3794
&s->const_keys, join->row_limit);
3795
s->quick=select->quick;
3796
s->needed_reg=select->needed_reg;
3798
if (records == 0 && s->table->reginfo.impossible_range)
3801
Impossible WHERE or ON expression
3802
In case of ON, we mark that the we match one empty NULL row.
3803
In case of WHERE, don't set found_const_table_map to get the
3804
caller to abort with a zero row result.
3806
join->const_table_map|= s->table->map;
3807
set_position(join,const_count++,s,(KEYUSE*) 0);
3809
if (*s->on_expr_ref)
3811
/* Generate empty row */
3812
s->info= "Impossible ON condition";
3813
found_const_table_map|= s->table->map;
3815
mark_as_null_row(s->table); // All fields are NULL
3818
if (records != HA_POS_ERROR)
3820
s->found_records=records;
3821
s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3827
join->join_tab=stat;
3828
join->map2table=stat_ref;
3829
join->table= join->all_tables=table_vector;
3830
join->const_tables=const_count;
3831
join->found_const_table_map=found_const_table_map;
3833
/* Find an optimal join order of the non-constant tables. */
3834
if (join->const_tables != join->tables)
3836
optimize_keyuse(join, keyuse_array);
3837
if (choose_plan(join, all_table_map & ~join->const_table_map))
3842
memcpy((uchar*) join->best_positions,(uchar*) join->positions,
3843
sizeof(POSITION)*join->const_tables);
3844
join->best_read=1.0;
3846
/* Generate an execution plan from the found optimal join order. */
3847
return(join->thd->killed || get_best_combination(join));
485
3851
/*****************************************************************************
486
3852
Check with keys are used and with tables references with tables
487
3853
Updates in stat:
490
3856
keyuse Pointer to possible keys
491
3857
*****************************************************************************/
3859
/// Used when finding key fields
3860
typedef struct key_field_t {
3862
Item *val; ///< May be empty if diff constant
3864
uint optimize; // KEY_OPTIMIZE_*
3867
If true, the condition this struct represents will not be satisfied
3870
bool null_rejecting;
3871
bool *cond_guard; /* See KEYUSE::cond_guard */
3872
uint sj_pred_no; /* See KEYUSE::sj_pred_no */
3876
Merge new key definitions to old ones, remove those not used in both.
3878
This is called for OR between different levels.
3880
To be able to do 'ref_or_null' we merge a comparison of a column
3881
and 'column IS NULL' to one test. This is useful for sub select queries
3882
that are internally transformed to something like:.
3885
SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
3888
KEY_FIELD::null_rejecting is processed as follows: @n
3889
result has null_rejecting=true if it is set for both ORed references.
3891
- (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true
3892
- (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3895
The result of this is that we're missing some 'ref' accesses.
3896
OptimizerTeam: Fix this
3900
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3903
if (start == new_fields)
3904
return start; // Impossible or
3905
if (new_fields == end)
3906
return start; // No new fields, skip all
3908
KEY_FIELD *first_free=new_fields;
3910
/* Mark all found fields in old array */
3911
for (; new_fields != end ; new_fields++)
3913
for (KEY_FIELD *old=start ; old != first_free ; old++)
3915
if (old->field == new_fields->field)
3918
NOTE: below const_item() call really works as "!used_tables()", i.e.
3919
it can return false where it is feasible to make it return true.
3921
The cause is as follows: Some of the tables are already known to be
3922
const tables (the detection code is in make_join_statistics(),
3923
above the update_ref_and_keys() call), but we didn't propagate
3924
information about this: TABLE::const_table is not set to true, and
3925
Item::update_used_tables() hasn't been called for each item.
3926
The result of this is that we're missing some 'ref' accesses.
3927
TODO: OptimizerTeam: Fix this
3929
if (!new_fields->val->const_item())
3932
If the value matches, we can use the key reference.
3933
If not, we keep it until we have examined all new values
3935
if (old->val->eq(new_fields->val, old->field->binary()))
3937
old->level= and_level;
3938
old->optimize= ((old->optimize & new_fields->optimize &
3939
KEY_OPTIMIZE_EXISTS) |
3940
((old->optimize | new_fields->optimize) &
3941
KEY_OPTIMIZE_REF_OR_NULL));
3942
old->null_rejecting= (old->null_rejecting &&
3943
new_fields->null_rejecting);
3946
else if (old->eq_func && new_fields->eq_func &&
3947
old->val->eq_by_collation(new_fields->val,
3948
old->field->binary(),
3949
old->field->charset()))
3952
old->level= and_level;
3953
old->optimize= ((old->optimize & new_fields->optimize &
3954
KEY_OPTIMIZE_EXISTS) |
3955
((old->optimize | new_fields->optimize) &
3956
KEY_OPTIMIZE_REF_OR_NULL));
3957
old->null_rejecting= (old->null_rejecting &&
3958
new_fields->null_rejecting);
3960
else if (old->eq_func && new_fields->eq_func &&
3961
((old->val->const_item() && old->val->is_null()) ||
3962
new_fields->val->is_null()))
3964
/* field = expression OR field IS NULL */
3965
old->level= and_level;
3966
old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3968
Remember the NOT NULL value unless the value does not depend
3971
if (!old->val->used_tables() && old->val->is_null())
3972
old->val= new_fields->val;
3973
/* The referred expression can be NULL: */
3974
old->null_rejecting= 0;
3979
We are comparing two different const. In this case we can't
3980
use a key-lookup on this so it's better to remove the value
3981
and let the range optimzier handle it
3983
if (old == --first_free) // If last item
3985
*old= *first_free; // Remove old value
3986
old--; // Retry this value
3991
/* Remove all not used items */
3992
for (KEY_FIELD *old=start ; old != first_free ;)
3994
if (old->level != and_level)
3995
{ // Not used in all levels
3996
if (old == --first_free)
3998
*old= *first_free; // Remove old value
4008
Add a possible key to array of possible keys if it's usable as a key
4010
@param key_fields Pointer to add key, if usable
4011
@param and_level And level, to be stored in KEY_FIELD
4012
@param cond Condition predicate
4013
@param field Field used in comparision
4014
@param eq_func True if we used =, <=> or IS NULL
4015
@param value Value used for comparison with field
4016
@param usable_tables Tables which can be used for key optimization
4017
@param sargables IN/OUT Array of found sargable candidates
4020
If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4021
table, we store this to be able to do not exists optimization later.
4024
*key_fields is incremented if we stored a key in the array
4028
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4029
Field *field, bool eq_func, Item **value, uint num_values,
4030
table_map usable_tables, SARGABLE_PARAM **sargables)
4032
uint exists_optimize= 0;
4033
if (!(field->flags & PART_KEY_FLAG))
4035
// Don't remove column IS NULL on a LEFT JOIN table
4036
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4037
!field->table->maybe_null || field->null_ptr)
4038
return; // Not a key. Skip it
4039
exists_optimize= KEY_OPTIMIZE_EXISTS;
4040
assert(num_values == 1);
4044
table_map used_tables=0;
4046
for (uint i=0; i<num_values; i++)
4048
used_tables|=(value[i])->used_tables();
4049
if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4054
if (!(usable_tables & field->table->map))
4056
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4057
!field->table->maybe_null || field->null_ptr)
4058
return; // Can't use left join optimize
4059
exists_optimize= KEY_OPTIMIZE_EXISTS;
4063
JOIN_TAB *stat=field->table->reginfo.join_tab;
4064
key_map possible_keys=field->key_start;
4065
possible_keys.intersect(field->table->keys_in_use_for_query);
4066
stat[0].keys.merge(possible_keys); // Add possible keys
4069
Save the following cases:
4071
Field LIKE constant where constant doesn't start with a wildcard
4072
Field = field2 where field2 is in a different table
4079
stat[0].key_dependent|=used_tables;
4082
for (uint i=0; i<num_values; i++)
4084
if (!(is_const&= value[i]->const_item()))
4088
stat[0].const_keys.merge(possible_keys);
4092
Save info to be able check whether this predicate can be
4093
considered as sargable for range analisis after reading const tables.
4094
We do not save info about equalities as update_const_equal_items
4095
will take care of updating info on keys from sargable equalities.
4098
(*sargables)->field= field;
4099
(*sargables)->arg_value= value;
4100
(*sargables)->num_values= num_values;
4103
We can't always use indexes when comparing a string index to a
4104
number. cmp_type() is checked to allow compare of dates to numbers.
4105
eq_func is NEVER true when num_values > 1
4110
Additional optimization: if we're processing
4111
"t.key BETWEEN c1 AND c1" then proceed as if we were processing
4113
TODO: This is a very limited fix. A more generic fix is possible.
4114
There are 2 options:
4115
A) Make equality propagation code be able to handle BETWEEN
4116
(including cases like t1.key BETWEEN t2.key AND t3.key)
4117
B) Make range optimizer to infer additional "t.key = c" equalities
4118
and use them in equality propagation process (see details in
4121
if ((cond->functype() != Item_func::BETWEEN) ||
4122
((Item_func_between*) cond)->negated ||
4123
!value[0]->eq(value[1], field->binary()))
4128
if (field->result_type() == STRING_RESULT)
4130
if ((*value)->result_type() != STRING_RESULT)
4132
if (field->cmp_type() != (*value)->result_type())
4138
We can't use indexes if the effective collation
4139
of the operation differ from the field collation.
4141
if (field->cmp_type() == STRING_RESULT &&
4142
((Field_str*)field)->charset() != cond->compare_collation())
4149
For the moment eq_func is always true. This slot is reserved for future
4150
extensions where we want to remembers other things than just eq comparisons
4153
/* Store possible eq field */
4154
(*key_fields)->field= field;
4155
(*key_fields)->eq_func= eq_func;
4156
(*key_fields)->val= *value;
4157
(*key_fields)->level= and_level;
4158
(*key_fields)->optimize= exists_optimize;
4160
If the condition has form "tbl.keypart = othertbl.field" and
4161
othertbl.field can be NULL, there will be no matches if othertbl.field
4163
We use null_rejecting in add_not_null_conds() to add
4164
'othertbl.field IS NOT NULL' to tab->select_cond.
4166
(*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4167
cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4168
((*value)->type() == Item::FIELD_ITEM) &&
4169
((Item_field*)*value)->field->maybe_null());
4170
(*key_fields)->cond_guard= NULL;
4171
(*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name &&
4172
cond->name < subq_sj_cond_name + 64)?
4173
cond->name - subq_sj_cond_name: UINT_MAX;
4178
Add possible keys to array of possible keys originated from a simple
4181
@param key_fields Pointer to add key, if usable
4182
@param and_level And level, to be stored in KEY_FIELD
4183
@param cond Condition predicate
4184
@param field Field used in comparision
4185
@param eq_func True if we used =, <=> or IS NULL
4186
@param value Value used for comparison with field
4187
Is NULL for BETWEEN and IN
4188
@param usable_tables Tables which can be used for key optimization
4189
@param sargables IN/OUT Array of found sargable candidates
4192
If field items f1 and f2 belong to the same multiple equality and
4193
a key is added for f1, the the same key is added for f2.
4196
*key_fields is incremented if we stored a key in the array
4200
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4201
Item_func *cond, Item_field *field_item,
4202
bool eq_func, Item **val,
4203
uint num_values, table_map usable_tables,
4204
SARGABLE_PARAM **sargables)
4206
Field *field= field_item->field;
4207
add_key_field(key_fields, and_level, cond, field,
4208
eq_func, val, num_values, usable_tables, sargables);
4209
Item_equal *item_equal= field_item->item_equal;
4213
Add to the set of possible key values every substitution of
4214
the field for an equal field included into item_equal
4216
Item_equal_iterator it(*item_equal);
4218
while ((item= it++))
4220
if (!field->eq(item->field))
4222
add_key_field(key_fields, and_level, cond, item->field,
4223
eq_func, val, num_values, usable_tables,
4231
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4232
COND *cond, table_map usable_tables,
4233
SARGABLE_PARAM **sargables)
4235
if (cond->type() == Item_func::COND_ITEM)
4237
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4238
KEY_FIELD *org_key_fields= *key_fields;
4240
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4244
add_key_fields(join, key_fields, and_level, item, usable_tables,
4246
for (; org_key_fields != *key_fields ; org_key_fields++)
4247
org_key_fields->level= *and_level;
4252
add_key_fields(join, key_fields, and_level, li++, usable_tables,
4257
KEY_FIELD *start_key_fields= *key_fields;
4259
add_key_fields(join, key_fields, and_level, item, usable_tables,
4261
*key_fields=merge_key_fields(org_key_fields,start_key_fields,
4262
*key_fields,++(*and_level));
4269
Subquery optimization: Conditions that are pushed down into subqueries
4270
are wrapped into Item_func_trig_cond. We process the wrapped condition
4271
but need to set cond_guard for KEYUSE elements generated from it.
4274
if (cond->type() == Item::FUNC_ITEM &&
4275
((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4277
Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4278
if (!join->group_list && !join->order &&
4280
join->unit->item->substype() == Item_subselect::IN_SUBS &&
4281
!join->unit->is_union())
4283
KEY_FIELD *save= *key_fields;
4284
add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4286
// Indicate that this ref access candidate is for subquery lookup:
4287
for (; save != *key_fields; save++)
4288
save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4294
/* If item is of type 'field op field/constant' add it to key_fields */
4295
if (cond->type() != Item::FUNC_ITEM)
4297
Item_func *cond_func= (Item_func*) cond;
4298
switch (cond_func->select_optimize()) {
4299
case Item_func::OPTIMIZE_NONE:
4301
case Item_func::OPTIMIZE_KEY:
4305
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4306
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4308
values= cond_func->arguments()+1;
4309
if (cond_func->functype() == Item_func::NE_FUNC &&
4310
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4311
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4313
assert(cond_func->functype() != Item_func::IN_FUNC ||
4314
cond_func->argument_count() != 2);
4315
add_key_equal_fields(key_fields, *and_level, cond_func,
4316
(Item_field*) (cond_func->key_item()->real_item()),
4318
cond_func->argument_count()-1,
4319
usable_tables, sargables);
4321
if (cond_func->functype() == Item_func::BETWEEN)
4323
values= cond_func->arguments();
4324
for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4326
Item_field *field_item;
4327
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4329
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4331
field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4332
add_key_equal_fields(key_fields, *and_level, cond_func,
4333
field_item, 0, values, 1, usable_tables,
4340
case Item_func::OPTIMIZE_OP:
4342
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4343
cond_func->functype() == Item_func::EQUAL_FUNC);
4345
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4346
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4348
add_key_equal_fields(key_fields, *and_level, cond_func,
4349
(Item_field*) (cond_func->arguments()[0])->real_item(),
4351
cond_func->arguments()+1, 1, usable_tables,
4354
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4355
cond_func->functype() != Item_func::LIKE_FUNC &&
4356
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4358
add_key_equal_fields(key_fields, *and_level, cond_func,
4359
(Item_field*) (cond_func->arguments()[1])->real_item(),
4361
cond_func->arguments(),1,usable_tables,
4366
case Item_func::OPTIMIZE_NULL:
4367
/* column_name IS [NOT] NULL */
4368
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4369
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4371
Item *tmp=new Item_null;
4372
if (unlikely(!tmp)) // Should never be true
4374
add_key_equal_fields(key_fields, *and_level, cond_func,
4375
(Item_field*) (cond_func->arguments()[0])->real_item(),
4376
cond_func->functype() == Item_func::ISNULL_FUNC,
4377
&tmp, 1, usable_tables, sargables);
4380
case Item_func::OPTIMIZE_EQUAL:
4381
Item_equal *item_equal= (Item_equal *) cond;
4382
Item *const_item= item_equal->get_const();
4383
Item_equal_iterator it(*item_equal);
4388
For each field field1 from item_equal consider the equality
4389
field1=const_item as a condition allowing an index access of the table
4390
with field1 by the keys value of field1.
4392
while ((item= it++))
4394
add_key_field(key_fields, *and_level, cond_func, item->field,
4395
true, &const_item, 1, usable_tables, sargables);
4401
Consider all pairs of different fields included into item_equal.
4402
For each of them (field1, field1) consider the equality
4403
field1=field2 as a condition allowing an index access of the table
4404
with field1 by the keys value of field2.
4406
Item_equal_iterator fi(*item_equal);
4407
while ((item= fi++))
4409
Field *field= item->field;
4410
while ((item= it++))
4412
if (!field->eq(item->field))
4414
add_key_field(key_fields, *and_level, cond_func, field,
4415
true, (Item **) &item, 1, usable_tables,
495
4427
Add all keys with uses 'field' for some keypart.
497
4429
If field->and_level != and_level then only mark key_part as const_part.
499
uint32_t max_part_bit(key_part_map bits)
4433
max_part_bit(key_part_map bits)
502
4436
for (found=0; bits & 1 ; found++,bits>>=1) ;
506
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
4441
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4443
Field *field=key_field->field;
4444
TABLE *form= field->table;
4447
if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4449
for (uint key=0 ; key < form->s->keys ; key++)
4451
if (!(form->keys_in_use_for_query.is_set(key)))
4454
uint key_parts= (uint) form->key_info[key].key_parts;
4455
for (uint part=0 ; part < key_parts ; part++)
4457
if (field->eq(form->key_info[key].key_part[part].field))
4459
keyuse.table= field->table;
4460
keyuse.val = key_field->val;
4462
keyuse.keypart=part;
4463
keyuse.keypart_map= (key_part_map) 1 << part;
4464
keyuse.used_tables=key_field->val->used_tables();
4465
keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4466
keyuse.null_rejecting= key_field->null_rejecting;
4467
keyuse.cond_guard= key_field->cond_guard;
4468
keyuse.sj_pred_no= key_field->sj_pred_no;
4469
VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4477
sort_keyuse(KEYUSE *a,KEYUSE *b)
509
if (a->getTable()->tablenr != b->getTable()->tablenr)
510
return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
511
if (a->getKey() != b->getKey())
512
return static_cast<int>((a->getKey() - b->getKey()));
513
if (a->getKeypart() != b->getKeypart())
514
return static_cast<int>((a->getKeypart() - b->getKeypart()));
4480
if (a->table->tablenr != b->table->tablenr)
4481
return (int) (a->table->tablenr - b->table->tablenr);
4482
if (a->key != b->key)
4483
return (int) (a->key - b->key);
4484
if (a->keypart != b->keypart)
4485
return (int) (a->keypart - b->keypart);
515
4486
// Place const values before other ones
516
if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
517
test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
4487
if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4488
test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
519
4490
/* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
520
return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
521
(b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
4491
return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4492
(b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4497
Add to KEY_FIELD array all 'ref' access candidates within nested join.
4499
This function populates KEY_FIELD array with entries generated from the
4500
ON condition of the given nested join, and does the same for nested joins
4501
contained within this nested join.
4503
@param[in] nested_join_table Nested join pseudo-table to process
4504
@param[in,out] end End of the key field array
4505
@param[in,out] and_level And-level
4506
@param[in,out] sargables Array of found sargable candidates
4510
We can add accesses to the tables that are direct children of this nested
4511
join (1), and are not inner tables w.r.t their neighbours (2).
4513
Example for #1 (outer brackets pair denotes nested join this function is
4516
... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4520
... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4522
In examples 1-2 for condition cond, we can add 'ref' access candidates to
4526
... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4528
Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4531
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4532
KEY_FIELD **end, uint *and_level,
4533
SARGABLE_PARAM **sargables)
4535
List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4536
List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4537
bool have_another = false;
4538
table_map tables= 0;
4540
assert(nested_join_table->nested_join);
4542
while ((table= li++) || (have_another && (li=li2, have_another=false,
4545
if (table->nested_join)
4547
if (!table->on_expr)
4549
/* It's a semi-join nest. Walk into it as if it wasn't a nest */
4552
li= List_iterator<TABLE_LIST>(table->nested_join->join_list);
4555
add_key_fields_for_nj(join, table, end, and_level, sargables);
4558
if (!table->on_expr)
4559
tables |= table->table->map;
4561
if (nested_join_table->on_expr)
4562
add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
526
4568
Update keyuse array with all possible keys we can use to fetch rows.
529
@param[out] keyuse Put here ordered array of KeyUse structures
4571
@param[out] keyuse Put here ordered array of KEYUSE structures
530
4572
@param join_tab Array in tablenr_order
531
4573
@param tables Number of tables in join
532
4574
@param cond WHERE condition (note that the function analyzes
781
4827
/* Intersect the keys of all group fields. */
782
4828
cur_item= indexed_fields_it++;
783
possible_keys|= cur_item->field->part_of_key;
4829
possible_keys.merge(cur_item->field->part_of_key);
784
4830
while ((cur_item= indexed_fields_it++))
786
possible_keys&= cur_item->field->part_of_key;
789
if (possible_keys.any())
790
join_tab->const_keys|= possible_keys;
794
Compare two JoinTable objects based on the number of accessed records.
796
@param ptr1 pointer to first JoinTable object
797
@param ptr2 pointer to second JoinTable object
4832
possible_keys.intersect(cur_item->field->part_of_key);
4835
if (!possible_keys.is_clear_all())
4836
join_tab->const_keys.merge(possible_keys);
4840
/*****************************************************************************
4841
Go through all combinations of not marked tables and find the one
4842
which uses least records
4843
*****************************************************************************/
4845
/** Save const tables first as used tables. */
4848
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4850
join->positions[idx].table= table;
4851
join->positions[idx].key=key;
4852
join->positions[idx].records_read=1.0; /* This is a const table */
4853
join->positions[idx].ref_depend_map= 0;
4855
/* Move the const table as down as possible in best_ref */
4856
JOIN_TAB **pos=join->best_ref+idx+1;
4857
JOIN_TAB *next=join->best_ref[idx];
4858
for (;next != table ; pos++)
4860
JOIN_TAB *tmp=pos[0];
4864
join->best_ref[idx]=table;
4869
Given a semi-join nest, find out which of the IN-equalities are bound
4872
get_bound_sj_equalities()
4873
sj_nest Semi-join nest
4874
remaining_tables Tables that are not yet bound
4877
Given a semi-join nest, find out which of the IN-equalities have their
4878
left part expression bound (i.e. the said expression doesn't refer to
4879
any of remaining_tables and can be evaluated).
4882
Bitmap of bound IN-equalities.
4885
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest,
4886
table_map remaining_tables)
4888
List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4892
while ((item= li++))
4895
Q: should this take into account equality propagation and how?
4896
A: If e->outer_side is an Item_field, walk over the equality
4897
class and see if there is an element that is bound?
4898
(this is an optional feature)
4900
if (!(item->used_tables() & remaining_tables))
4910
Find the best access path for an extension of a partial execution
4911
plan and add this path to the plan.
4913
The function finds the best access path to table 's' from the passed
4914
partial plan where an access path is the general term for any means to
4915
access the data in 's'. An access path may use either an index or a scan,
4916
whichever is cheaper. The input partial plan is passed via the array
4917
'join->positions' of length 'idx'. The chosen access method for 's' and its
4918
cost are stored in 'join->positions[idx]'.
4920
@param join pointer to the structure providing all context info
4922
@param s the table to be joined by the function
4923
@param thd thread for the connection that submitted the query
4924
@param remaining_tables set of tables not included into the partial plan yet
4925
@param idx the length of the partial plan
4926
@param record_count estimate for the number of records returned by the
4928
@param read_time the cost of the partial plan
4935
best_access_path(JOIN *join,
4938
table_map remaining_tables,
4940
double record_count,
4941
double read_time __attribute__((unused)))
4943
KEYUSE *best_key= 0;
4944
uint best_max_key_part= 0;
4945
bool found_constraint= 0;
4946
double best= DBL_MAX;
4947
double best_time= DBL_MAX;
4948
double records= DBL_MAX;
4949
table_map best_ref_depends_map= 0;
4952
uint best_is_sj_inside_out= 0;
4955
{ /* Use key if possible */
4956
TABLE *table= s->table;
4957
KEYUSE *keyuse,*start_key=0;
4958
double best_records= DBL_MAX;
4959
uint max_key_part=0;
4960
uint64_t bound_sj_equalities= 0;
4961
bool try_sj_inside_out= false;
4963
Discover the bound equalites. We need to do this, if
4964
1. The next table is an SJ-inner table, and
4965
2. It is the first table from that semijoin, and
4966
3. We're not within a semi-join range (i.e. all semi-joins either have
4967
all or none of their tables in join_table_map), except
4968
s->emb_sj_nest (which we've just entered).
4969
3. All correlation references from this sj-nest are bound
4971
if (s->emb_sj_nest && // (1)
4972
s->emb_sj_nest->sj_in_exprs < 64 &&
4973
((remaining_tables & s->emb_sj_nest->sj_inner_tables) == // (2)
4974
s->emb_sj_nest->sj_inner_tables) && // (2)
4975
join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables && // (3)
4976
!(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4978
/* This table is an InsideOut scan candidate */
4979
bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
4981
try_sj_inside_out= true;
4984
/* Test how we can use keys */
4985
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key
4986
for (keyuse=s->keyuse ; keyuse->table == table ;)
4988
key_part_map found_part= 0;
4989
table_map found_ref= 0;
4990
uint key= keyuse->key;
4991
KEY *keyinfo= table->key_info+key;
4992
/* Bitmap of keyparts where the ref access is over 'keypart=const': */
4993
key_part_map const_part= 0;
4994
/* The or-null keypart in ref-or-null access: */
4995
key_part_map ref_or_null_part= 0;
4997
/* Calculate how many key segments of the current key we can use */
4999
uint64_t handled_sj_equalities=0;
5000
key_part_map sj_insideout_map= 0;
5002
do /* For each keypart */
5004
uint keypart= keyuse->keypart;
5005
table_map best_part_found_ref= 0;
5006
double best_prev_record_reads= DBL_MAX;
5008
do /* For each way to access the keypart */
5012
if 1. expression doesn't refer to forward tables
5013
2. we won't get two ref-or-null's
5015
if (!(remaining_tables & keyuse->used_tables) &&
5016
!(ref_or_null_part && (keyuse->optimize &
5017
KEY_OPTIMIZE_REF_OR_NULL)))
5019
found_part|= keyuse->keypart_map;
5020
if (!(keyuse->used_tables & ~join->const_table_map))
5021
const_part|= keyuse->keypart_map;
5023
double tmp2= prev_record_reads(join, idx, (found_ref |
5024
keyuse->used_tables));
5025
if (tmp2 < best_prev_record_reads)
5027
best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5028
best_prev_record_reads= tmp2;
5030
if (rec > keyuse->ref_table_rows)
5031
rec= keyuse->ref_table_rows;
5033
If there is one 'key_column IS NULL' expression, we can
5034
use this ref_or_null optimisation of this field
5036
if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5037
ref_or_null_part |= keyuse->keypart_map;
5040
if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5042
if (!(remaining_tables & keyuse->used_tables))
5043
bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5046
handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5047
sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5052
} while (keyuse->table == table && keyuse->key == key &&
5053
keyuse->keypart == keypart);
5054
found_ref|= best_part_found_ref;
5055
} while (keyuse->table == table && keyuse->key == key);
5058
Assume that that each key matches a proportional part of table.
5060
if (!found_part && !handled_sj_equalities)
5061
continue; // Nothing usable found
5063
if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5064
rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables
5066
bool sj_inside_out_scan= false;
5068
found_constraint= 1;
5070
Check if InsideOut scan is applicable:
5071
1. All IN-equalities are either "bound" or "handled"
5072
2. Index keyparts are
5075
if (try_sj_inside_out &&
5076
table->covering_keys.is_set(key) &&
5077
(handled_sj_equalities | bound_sj_equalities) == // (1)
5078
PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5080
uint n_fixed_parts= max_part_bit(found_part);
5081
if (n_fixed_parts != keyinfo->key_parts &&
5082
(PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5083
PREV_BITS(uint, keyinfo->key_parts))
5086
Not all parts are fixed. Produce bitmap of remaining bits and
5087
check if all of them are covered.
5089
sj_inside_out_scan= true;
5093
It's a confluent ref scan.
5095
That is, all found KEYUSE elements refer to IN-equalities,
5096
and there is really no ref access because there is no
5097
t.keypart0 = {bound expression}
5099
Calculate the cost of complete loose index scan.
5101
records= (double)s->table->file->stats.records;
5103
/* The cost is entire index scan cost (divided by 2) */
5104
best_time= s->table->file->index_only_read_time(key, records);
5106
/* Now figure how many different keys we will get */
5108
if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5109
records= records / rpc;
5116
Check if we found full key
5118
if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5121
max_key_part= (uint) ~0;
5122
if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5124
tmp = prev_record_reads(join, idx, found_ref);
5130
{ /* We found a const key */
5132
ReuseRangeEstimateForRef-1:
5133
We get here if we've found a ref(const) (c_i are constants):
5134
"(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond]
5136
If range optimizer was able to construct a "range"
5137
access on this index, then its condition "quick_cond" was
5138
eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5139
from the range optimizer.
5141
Proof of (*): By properties of range and ref optimizers
5142
quick_cond will be equal or tighther than ref_const_cond.
5143
ref_const_cond already covers "smallest" possible interval -
5144
a singlepoint interval over all keyparts. Therefore,
5145
quick_cond is equivalent to ref_const_cond (if it was an
5146
empty interval we wouldn't have got here).
5148
if (table->quick_keys.is_set(key))
5149
records= (double) table->quick_rows[key];
5152
/* quick_range couldn't use key! */
5153
records= (double) s->records/rec;
5158
if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5159
{ /* Prefer longer keys */
5161
((double) s->records / (double) rec *
5163
((double) (table->s->max_key_length-keyinfo->key_length) /
5164
(double) table->s->max_key_length)));
5166
records=2.0; /* Can't be as good as a unique */
5169
ReuseRangeEstimateForRef-2: We get here if we could not reuse
5170
E(#rows) from range optimizer. Make another try:
5172
If range optimizer produced E(#rows) for a prefix of the ref
5173
access we're considering, and that E(#rows) is lower then our
5174
current estimate, make an adjustment. The criteria of when we
5175
can make an adjustment is a special case of the criteria used
5176
in ReuseRangeEstimateForRef-3.
5178
if (table->quick_keys.is_set(key) &&
5179
const_part & (1 << table->quick_key_parts[key]) &&
5180
table->quick_n_ranges[key] == 1 &&
5181
records > (double) table->quick_rows[key])
5183
records= (double) table->quick_rows[key];
5186
/* Limit the number of matched rows */
5188
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5189
if (table->covering_keys.is_set(key))
5191
/* we can use only index tree */
5192
tmp= record_count * table->file->index_only_read_time(key, tmp);
5195
tmp= record_count*min(tmp,s->worst_seeks);
5201
Use as much key-parts as possible and a uniq key is better
5202
than a not unique key
5203
Set tmp to (previous record count) * (records / combination)
5205
if ((found_part & 1) &&
5206
(!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5207
found_part == PREV_BITS(uint,keyinfo->key_parts)))
5209
max_key_part= max_part_bit(found_part);
5211
ReuseRangeEstimateForRef-3:
5212
We're now considering a ref[or_null] access via
5213
(t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
5214
(same-as-above but with one cond replaced
5215
with "t.keypart_i IS NULL")] (**)
5217
Try re-using E(#rows) from "range" optimizer:
5218
We can do so if "range" optimizer used the same intervals as
5219
in (**). The intervals used by range optimizer may be not
5220
available at this point (as "range" access might have choosen to
5221
create quick select over another index), so we can't compare
5222
them to (**). We'll make indirect judgements instead.
5223
The sufficient conditions for re-use are:
5224
(C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5225
this is not satisfied we have no way to know which ranges
5226
will be actually scanned by 'ref' until we execute the
5228
(C2) max #key parts in 'range' access == K == max_key_part (this
5229
is apparently a necessary requirement)
5231
We also have a property that "range optimizer produces equal or
5232
tighter set of scan intervals than ref(const) optimizer". Each
5233
of the intervals in (**) are "tightest possible" intervals when
5234
one limits itself to using keyparts 1..K (which we do in #2).
5235
From here it follows that range access used either one, or
5236
both of the (I1) and (I2) intervals:
5238
(t.keypart1=c1 AND ... AND t.keypartK=eK) (I1)
5239
(same-as-above but with one cond replaced
5240
with "t.keypart_i IS NULL") (I2)
5242
The remaining part is to exclude the situation where range
5243
optimizer used one interval while we're considering
5244
ref-or-null and looking for estimate for two intervals. This
5245
is done by last limitation:
5247
(C3) "range optimizer used (have ref_or_null?2:1) intervals"
5249
if (table->quick_keys.is_set(key) && !found_ref && //(C1)
5250
table->quick_key_parts[key] == max_key_part && //(C2)
5251
table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5253
tmp= records= (double) table->quick_rows[key];
5257
/* Check if we have statistic about the distribution */
5258
if ((records= keyinfo->rec_per_key[max_key_part-1]))
5261
Fix for the case where the index statistics is too
5263
(1) We're considering ref(const) and there is quick select
5265
(2) and that quick select uses more keyparts (i.e. it will
5266
scan equal/smaller interval then this ref(const))
5267
(3) and E(#rows) for quick select is higher then our
5270
We'll use E(#rows) from quick select.
5272
Q: Why do we choose to use 'ref'? Won't quick select be
5273
cheaper in some cases ?
5274
TODO: figure this out and adjust the plan choice if needed.
5276
if (!found_ref && table->quick_keys.is_set(key) && // (1)
5277
table->quick_key_parts[key] > max_key_part && // (2)
5278
records < (double)table->quick_rows[key]) // (3)
5279
records= (double)table->quick_rows[key];
5286
Assume that the first key part matches 1% of the file
5287
and that the whole key matches 10 (duplicates) or 1
5289
Assume also that more key matches proportionally more
5291
This gives the formula:
5292
records = (x * (b-a) + a*c-b)/(c-1)
5294
b = records matched by whole key
5295
a = records matched by first key part (1% of all records?)
5296
c = number of key parts in key
5297
x = used key parts (1 <= x <= c)
5300
if (!(rec_per_key=(double)
5301
keyinfo->rec_per_key[keyinfo->key_parts-1]))
5302
rec_per_key=(double) s->records/rec+1;
5306
else if (rec_per_key/(double) s->records >= 0.01)
5310
double a=s->records*0.01;
5311
if (keyinfo->key_parts > 1)
5312
tmp= (max_key_part * (rec_per_key - a) +
5313
a*keyinfo->key_parts - rec_per_key)/
5314
(keyinfo->key_parts-1);
5317
set_if_bigger(tmp,1.0);
5319
records = (ulong) tmp;
5322
if (ref_or_null_part)
5324
/* We need to do two key searches to find key */
5330
ReuseRangeEstimateForRef-4: We get here if we could not reuse
5331
E(#rows) from range optimizer. Make another try:
5333
If range optimizer produced E(#rows) for a prefix of the ref
5334
access we're considering, and that E(#rows) is lower then our
5335
current estimate, make the adjustment.
5337
The decision whether we can re-use the estimate from the range
5338
optimizer is the same as in ReuseRangeEstimateForRef-3,
5339
applied to first table->quick_key_parts[key] key parts.
5341
if (table->quick_keys.is_set(key) &&
5342
table->quick_key_parts[key] <= max_key_part &&
5343
const_part & (1 << table->quick_key_parts[key]) &&
5344
table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5346
records > (double) table->quick_rows[key])
5348
tmp= records= (double) table->quick_rows[key];
5352
/* Limit the number of matched rows */
5353
set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5354
if (table->covering_keys.is_set(key))
5356
/* we can use only index tree */
5357
tmp= record_count * table->file->index_only_read_time(key, tmp);
5360
tmp= record_count * min(tmp,s->worst_seeks);
5363
tmp= best_time; // Do nothing
5366
if (sj_inside_out_scan && !start_key)
5374
if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5376
best_time= tmp + records/(double) TIME_FOR_COMPARE;
5378
best_records= records;
5379
best_key= start_key;
5380
best_max_key_part= max_key_part;
5381
best_ref_depends_map= found_ref;
5382
best_is_sj_inside_out= sj_inside_out_scan;
5385
records= best_records;
5389
Don't test table scan if it can't be better.
5390
Prefer key lookup if we would use the same key for scanning.
5392
Don't do a table scan on InnoDB tables, if we can read the used
5393
parts of the row from any of the used index.
5394
This is because table scans uses index and we would not win
5395
anything by using a table scan.
5397
A word for word translation of the below if-statement in sergefp's
5398
understanding: we check if we should use table scan if:
5399
(1) The found 'ref' access produces more records than a table scan
5400
(or index scan, or quick select), or 'ref' is more expensive than
5402
(2) This doesn't hold: the best way to perform table scan is to to perform
5403
'range' access using index IDX, and the best way to perform 'ref'
5404
access is to use the same index IDX, with the same or more key parts.
5405
(note: it is not clear how this rule is/should be extended to
5406
index_merge quick selects)
5407
(3) See above note about InnoDB.
5408
(4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5409
path, but there is no quick select)
5410
If the condition in the above brackets holds, then the only possible
5411
"table scan" access method is ALL/index (there is no quick select).
5412
Since we have a 'ref' access path, and FORCE INDEX instructs us to
5413
choose it over ALL/index, there is no need to consider a full table
5416
if ((records >= s->found_records || best > s->read_time) && // (1)
5417
!(s->quick && best_key && s->quick->index == best_key->key && // (2)
5418
best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5419
!((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3)
5420
! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5421
!(s->table->force_index && best_key && !s->quick)) // (4)
5422
{ // Check full join
5423
ha_rows rnd_records= s->found_records;
5425
If there is a filtering condition on the table (i.e. ref analyzer found
5426
at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5427
preceding this table in the join order we're now considering), then
5428
assume that 25% of the rows will be filtered out by this condition.
5430
This heuristic is supposed to force tables used in exprZ to be before
5431
this table in join order.
5433
if (found_constraint)
5434
rnd_records-= rnd_records/4;
5437
If applicable, get a more accurate estimate. Don't use the two
5440
if (s->table->quick_condition_rows != s->found_records)
5441
rnd_records= s->table->quick_condition_rows;
5444
Range optimizer never proposes a RANGE if it isn't better
5445
than FULL: so if RANGE is present, it's always preferred to FULL.
5446
Here we estimate its cost.
5452
- read record range through 'quick'
5453
- skip rows which does not satisfy WHERE constraints
5455
We take into account possible use of join cache for ALL/index
5456
access (see first else-branch below), but we don't take it into
5457
account here for range/index_merge access. Find out why this is so.
5460
(s->quick->read_time +
5461
(s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5465
/* Estimate cost of reading table. */
5466
tmp= s->table->file->scan_time();
5467
if (s->table->map & join->outer_join) // Can't use join cache
5470
For each record we have to:
5471
- read the whole table record
5472
- skip rows which does not satisfy join condition
5476
(s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5480
/* We read the table as many times as join buffer becomes full. */
5481
tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5483
(double) thd->variables.join_buff_size));
5485
We don't make full cartesian product between rows in the scanned
5486
table and existing records because we skip all rows from the
5487
scanned table, which does not satisfy join condition when
5488
we read the table (see flush_cached_records for details). Here we
5489
take into account cost to read and skip these records.
5491
tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5496
We estimate the cost of evaluating WHERE clause for found records
5497
as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5498
tmp give us total cost of using TABLE SCAN
5500
if (best == DBL_MAX ||
5501
(tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5502
best + record_count/(double) TIME_FOR_COMPARE*records))
5505
If the table has a range (s->quick is set) make_join_select()
5506
will ensure that this will be used
5509
records= rows2double(rnd_records);
5511
/* range/index_merge/ALL/index access method are "independent", so: */
5512
best_ref_depends_map= 0;
5513
best_is_sj_inside_out= false;
5517
/* Update the cost information for the current partial plan */
5518
join->positions[idx].records_read= records;
5519
join->positions[idx].read_time= best;
5520
join->positions[idx].key= best_key;
5521
join->positions[idx].table= s;
5522
join->positions[idx].ref_depend_map= best_ref_depends_map;
5523
join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5526
idx == join->const_tables &&
5527
s->table == join->sort_by_table &&
5528
join->unit->select_limit_cnt >= records)
5529
join->sort_by_table= (TABLE*) 1; // Must use temporary table
5536
Selects and invokes a search strategy for an optimal query plan.
5538
The function checks user-configurable parameters that control the search
5539
strategy for an optimal plan, selects the search method and then invokes
5540
it. Each specific optimization procedure stores the final optimal plan in
5541
the array 'join->best_positions', and the cost of the plan in
5544
@param join pointer to the structure providing all context info for
5546
@param join_tables set of the tables in the query
5549
'MAX_TABLES+2' denotes the old implementation of find_best before
5550
the greedy version. Will be removed when greedy_search is approved.
5559
choose_plan(JOIN *join, table_map join_tables)
5561
uint search_depth= join->thd->variables.optimizer_search_depth;
5562
uint prune_level= join->thd->variables.optimizer_prune_level;
5563
bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5565
join->cur_embedding_map= 0;
5566
reset_nj_counters(join->join_list);
5568
if (SELECT_STRAIGHT_JOIN option is set)
5569
reorder tables so dependent tables come after tables they depend
5570
on, otherwise keep tables in the order they were specified in the query
5572
Apply heuristic: pre-sort all access plans with respect to the number of
5575
my_qsort(join->best_ref + join->const_tables,
5576
join->tables - join->const_tables, sizeof(JOIN_TAB*),
5577
straight_join ? join_tab_cmp_straight : join_tab_cmp);
5578
join->cur_emb_sj_nests= 0;
5581
optimize_straight_join(join, join_tables);
5585
if (search_depth == MAX_TABLES+2)
5587
TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5588
the greedy version. Will be removed when greedy_search is approved.
5590
join->best_read= DBL_MAX;
5591
if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5596
if (search_depth == 0)
5597
/* Automatically determine a reasonable value for 'search_depth' */
5598
search_depth= determine_search_depth(join);
5599
if (greedy_search(join, join_tables, search_depth, prune_level))
5605
Store the cost of this query into a user variable
5606
Don't update last_query_cost for statements that are not "flat joins" :
5607
i.e. they have subqueries, unions or call stored procedures.
5608
TODO: calculate a correct cost for a query with subqueries and UNIONs.
5610
if (join->thd->lex->is_single_level_stmt())
5611
join->thd->status_var.last_query_cost= join->best_read;
5617
Compare two JOIN_TAB objects based on the number of accessed records.
5619
@param ptr1 pointer to first JOIN_TAB object
5620
@param ptr2 pointer to second JOIN_TAB object
800
5623
The order relation implemented by join_tab_cmp() is not transitive,
5677
Heuristic procedure to automatically guess a reasonable degree of
5678
exhaustiveness for the greedy search procedure.
5680
The procedure estimates the optimization time and selects a search depth
5681
big enough to result in a near-optimal QEP, that doesn't take too long to
5682
find. If the number of tables in the query exceeds some constant, then
5683
search_depth is set to this constant.
5685
@param join pointer to the structure providing all context info for
5689
This is an extremely simplistic implementation that serves as a stub for a
5690
more advanced analysis of the join. Ideally the search depth should be
5691
determined by learning from previous query optimizations, because it will
5692
depend on the CPU power (and other factors).
5695
this value should be determined dynamically, based on statistics:
5696
uint max_tables_for_exhaustive_opt= 7;
5699
this value could be determined by some mapping of the form:
5700
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5703
A positive integer that specifies the search depth (and thus the
5704
exhaustiveness) of the depth-first search algorithm used by
5709
determine_search_depth(JOIN *join)
5711
uint table_count= join->tables - join->const_tables;
5713
/* TODO: this value should be determined dynamically, based on statistics: */
5714
uint max_tables_for_exhaustive_opt= 7;
5716
if (table_count <= max_tables_for_exhaustive_opt)
5717
search_depth= table_count+1; // use exhaustive for small number of tables
5720
TODO: this value could be determined by some mapping of the form:
5721
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5723
search_depth= max_tables_for_exhaustive_opt; // use greedy search
5725
return search_depth;
5730
Select the best ways to access the tables in a query without reordering them.
5732
Find the best access paths for each query table and compute their costs
5733
according to their order in the array 'join->best_ref' (thus without
5734
reordering the join tables). The function calls sequentially
5735
'best_access_path' for each table in the query to select the best table
5736
access method. The final optimal plan is stored in the array
5737
'join->best_positions', and the corresponding cost in 'join->best_read'.
5739
@param join pointer to the structure providing all context info for
5741
@param join_tables set of the tables in the query
5744
This function can be applied to:
5745
- queries with STRAIGHT_JOIN
5746
- internally to compute the cost of an arbitrary QEP
5748
Thus 'optimize_straight_join' can be used at any stage of the query
5749
optimization process to finalize a QEP as it is.
5753
optimize_straight_join(JOIN *join, table_map join_tables)
5756
uint idx= join->const_tables;
5757
double record_count= 1.0;
5758
double read_time= 0.0;
5760
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5762
/* Find the best access method from 's' to the current partial plan */
5763
advance_sj_state(join_tables, s);
5764
best_access_path(join, s, join->thd, join_tables, idx,
5765
record_count, read_time);
5766
/* compute the cost of the new plan extended with 's' */
5767
record_count*= join->positions[idx].records_read;
5768
read_time+= join->positions[idx].read_time;
5769
join_tables&= ~(s->table->map);
5773
read_time+= record_count / (double) TIME_FOR_COMPARE;
5774
if (join->sort_by_table &&
5775
join->sort_by_table != join->positions[join->const_tables].table->table)
5776
read_time+= record_count; // We have to make a temp table
5777
memcpy((uchar*) join->best_positions, (uchar*) join->positions,
5778
sizeof(POSITION)*idx);
5779
join->best_read= read_time;
5784
Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5786
The search procedure uses a hybrid greedy/exhaustive search with controlled
5787
exhaustiveness. The search is performed in N = card(remaining_tables)
5788
steps. Each step evaluates how promising is each of the unoptimized tables,
5789
selects the most promising table, and extends the current partial QEP with
5790
that table. Currenly the most 'promising' table is the one with least
5791
expensive extension.\
5793
There are two extreme cases:
5794
-# When (card(remaining_tables) < search_depth), the estimate finds the
5795
best complete continuation of the partial QEP. This continuation can be
5796
used directly as a result of the search.
5797
-# When (search_depth == 1) the 'best_extension_by_limited_search'
5798
consideres the extension of the current QEP with each of the remaining
5801
All other cases are in-between these two extremes. Thus the parameter
5802
'search_depth' controlls the exhaustiveness of the search. The higher the
5803
value, the longer the optimizaton time and possibly the better the
5804
resulting plan. The lower the value, the fewer alternative plans are
5805
estimated, but the more likely to get a bad QEP.
5807
All intermediate and final results of the procedure are stored in 'join':
5808
- join->positions : modified for every partial QEP that is explored
5809
- join->best_positions: modified for the current best complete QEP
5810
- join->best_read : modified for the current best complete QEP
5811
- join->best_ref : might be partially reordered
5813
The final optimal plan is stored in 'join->best_positions', and its
5814
corresponding cost in 'join->best_read'.
5817
The following pseudocode describes the algorithm of 'greedy_search':
5820
procedure greedy_search
5821
input: remaining_tables
5826
(t, a) = best_extension(pplan, remaining_tables);
5827
pplan = concat(pplan, (t, a));
5828
remaining_tables = remaining_tables - t;
5829
} while (remaining_tables != {})
5834
where 'best_extension' is a placeholder for a procedure that selects the
5835
most "promising" of all tables in 'remaining_tables'.
5836
Currently this estimate is performed by calling
5837
'best_extension_by_limited_search' to evaluate all extensions of the
5838
current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5839
mainly depends on that of 'best_extension_by_limited_search'.
5842
If 'best_extension()' == 'best_extension_by_limited_search()', then the
5843
worst-case complexity of this algorithm is <=
5844
O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5845
complexity of greedy_search is O(N!).
5848
In the future, 'greedy_search' might be extended to support other
5849
implementations of 'best_extension', e.g. some simpler quadratic procedure.
5851
@param join pointer to the structure providing all context info
5853
@param remaining_tables set of tables not included into the partial plan yet
5854
@param search_depth controlls the exhaustiveness of the search
5855
@param prune_level the pruning heuristics that should be applied during
5865
greedy_search(JOIN *join,
5866
table_map remaining_tables,
5870
double record_count= 1.0;
5871
double read_time= 0.0;
5872
uint idx= join->const_tables; // index into 'join->best_ref'
5874
uint size_remain; // cardinality of remaining_tables
5876
JOIN_TAB *best_table; // the next plan node to be added to the curr QEP
5878
/* number of tables that remain to be optimized */
5879
size_remain= my_count_bits(remaining_tables);
5882
/* Find the extension of the current QEP with the lowest cost */
5883
join->best_read= DBL_MAX;
5884
if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5885
read_time, search_depth, prune_level))
5888
if (size_remain <= search_depth)
5891
'join->best_positions' contains a complete optimal extension of the
5892
current partial QEP.
5897
/* select the first table in the optimal extension as most promising */
5898
best_pos= join->best_positions[idx];
5899
best_table= best_pos.table;
5901
Each subsequent loop of 'best_extension_by_limited_search' uses
5902
'join->positions' for cost estimates, therefore we have to update its
5905
join->positions[idx]= best_pos;
5907
/* find the position of 'best_table' in 'join->best_ref' */
5909
JOIN_TAB *pos= join->best_ref[best_idx];
5910
while (pos && best_table != pos)
5911
pos= join->best_ref[++best_idx];
5912
assert((pos != NULL)); // should always find 'best_table'
5913
/* move 'best_table' at the first free position in the array of joins */
5914
swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5916
/* compute the cost of the new plan extended with 'best_table' */
5917
record_count*= join->positions[idx].records_read;
5918
read_time+= join->positions[idx].read_time;
5920
remaining_tables&= ~(best_table->table->map);
5928
Find a good, possibly optimal, query execution plan (QEP) by a possibly
5931
The procedure searches for the optimal ordering of the query tables in set
5932
'remaining_tables' of size N, and the corresponding optimal access paths to
5933
each table. The choice of a table order and an access path for each table
5934
constitutes a query execution plan (QEP) that fully specifies how to
5937
The maximal size of the found plan is controlled by the parameter
5938
'search_depth'. When search_depth == N, the resulting plan is complete and
5939
can be used directly as a QEP. If search_depth < N, the found plan consists
5940
of only some of the query tables. Such "partial" optimal plans are useful
5941
only as input to query optimization procedures, and cannot be used directly
5944
The algorithm begins with an empty partial plan stored in 'join->positions'
5945
and a set of N tables - 'remaining_tables'. Each step of the algorithm
5946
evaluates the cost of the partial plan extended by all access plans for
5947
each of the relations in 'remaining_tables', expands the current partial
5948
plan with the access plan that results in lowest cost of the expanded
5949
partial plan, and removes the corresponding relation from
5950
'remaining_tables'. The algorithm continues until it either constructs a
5951
complete optimal plan, or constructs an optimal plartial plan with size =
5954
The final optimal plan is stored in 'join->best_positions'. The
5955
corresponding cost of the optimal plan is in 'join->best_read'.
5958
The procedure uses a recursive depth-first search where the depth of the
5959
recursion (and thus the exhaustiveness of the search) is controlled by the
5960
parameter 'search_depth'.
5963
The pseudocode below describes the algorithm of
5964
'best_extension_by_limited_search'. The worst-case complexity of this
5965
algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
5966
the complexity of greedy_search is O(N!).
5969
procedure best_extension_by_limited_search(
5970
pplan in, // in, partial plan of tables-joined-so-far
5971
pplan_cost, // in, cost of pplan
5972
remaining_tables, // in, set of tables not referenced in pplan
5973
best_plan_so_far, // in/out, best plan found so far
5974
best_plan_so_far_cost,// in/out, cost of best_plan_so_far
5975
search_depth) // in, maximum size of the plans being considered
5977
for each table T from remaining_tables
5979
// Calculate the cost of using table T as above
5980
cost = complex-series-of-calculations;
5982
// Add the cost to the cost so far.
5985
if (pplan_cost >= best_plan_so_far_cost)
5986
// pplan_cost already too great, stop search
5989
pplan= expand pplan by best_access_method;
5990
remaining_tables= remaining_tables - table T;
5991
if (remaining_tables is not an empty set
5995
best_extension_by_limited_search(pplan, pplan_cost,
5998
best_plan_so_far_cost,
6003
best_plan_so_far_cost= pplan_cost;
6004
best_plan_so_far= pplan;
6011
When 'best_extension_by_limited_search' is called for the first time,
6012
'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6013
The actual implementation provides a way to optionally use pruning
6014
heuristic (controlled by the parameter 'prune_level') to reduce the search
6015
space by skipping some partial plans.
6018
The parameter 'search_depth' provides control over the recursion
6019
depth, and thus the size of the resulting optimal plan.
6021
@param join pointer to the structure providing all context info
6023
@param remaining_tables set of tables not included into the partial plan yet
6024
@param idx length of the partial QEP in 'join->positions';
6025
since a depth-first search is used, also corresponds
6026
to the current depth of the search tree;
6027
also an index in the array 'join->best_ref';
6028
@param record_count estimate for the number of records returned by the
6030
@param read_time the cost of the best partial plan
6031
@param search_depth maximum depth of the recursion and thus size of the
6033
(0 < search_depth <= join->tables+1).
6034
@param prune_level pruning heuristics that should be applied during
6036
(values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6045
best_extension_by_limited_search(JOIN *join,
6046
table_map remaining_tables,
6048
double record_count,
6053
THD *thd= join->thd;
6054
if (thd->killed) // Abort
6058
'join' is a partial plan with lower cost than the best plan so far,
6059
so continue expanding it further with the tables in 'remaining_tables'.
6062
double best_record_count= DBL_MAX;
6063
double best_read_time= DBL_MAX;
6065
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6067
table_map real_table_bit= s->table->map;
6068
if ((remaining_tables & real_table_bit) &&
6069
!(remaining_tables & s->dependent) &&
6070
(!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6072
double current_record_count, current_read_time;
6073
advance_sj_state(remaining_tables, s);
6076
psergey-insideout-todo:
6077
when best_access_path() detects it could do an InsideOut scan or
6078
some other scan, have it return an insideout scan and a flag that
6079
requests to "fork" this loop iteration. (Q: how does that behave
6080
when the depth is insufficient??)
6082
/* Find the best access method from 's' to the current partial plan */
6083
best_access_path(join, s, thd, remaining_tables, idx,
6084
record_count, read_time);
6085
/* Compute the cost of extending the plan with 's' */
6086
current_record_count= record_count * join->positions[idx].records_read;
6087
current_read_time= read_time + join->positions[idx].read_time;
6089
/* Expand only partial plans with lower cost than the best QEP so far */
6090
if ((current_read_time +
6091
current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6093
restore_prev_nj_state(s);
6094
restore_prev_sj_state(remaining_tables, s);
6099
Prune some less promising partial plans. This heuristic may miss
6100
the optimal QEPs, thus it results in a non-exhaustive search.
6102
if (prune_level == 1)
6104
if (best_record_count > current_record_count ||
6105
best_read_time > current_read_time ||
6106
(idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6108
if (best_record_count >= current_record_count &&
6109
best_read_time >= current_read_time &&
6110
/* TODO: What is the reasoning behind this condition? */
6111
(!(s->key_dependent & remaining_tables) ||
6112
join->positions[idx].records_read < 2.0))
6114
best_record_count= current_record_count;
6115
best_read_time= current_read_time;
6120
restore_prev_nj_state(s);
6121
restore_prev_sj_state(remaining_tables, s);
6126
if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6127
{ /* Recursively expand the current partial plan */
6128
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6129
if (best_extension_by_limited_search(join,
6130
remaining_tables & ~real_table_bit,
6132
current_record_count,
6137
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6141
'join' is either the best partial QEP with 'search_depth' relations,
6142
or the best complete QEP so far, whichever is smaller.
6144
current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6145
if (join->sort_by_table &&
6146
join->sort_by_table !=
6147
join->positions[join->const_tables].table->table)
6148
/* We have to make a temp table */
6149
current_read_time+= current_record_count;
6150
if ((search_depth == 1) || (current_read_time < join->best_read))
6152
memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6153
sizeof(POSITION) * (idx + 1));
6154
join->best_read= current_read_time - 0.001;
6157
restore_prev_nj_state(s);
6158
restore_prev_sj_state(remaining_tables, s);
6167
- TODO: this function is here only temporarily until 'greedy_search' is
6168
tested and accepted.
6175
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6178
THD *thd= join->thd;
6183
read_time+=record_count/(double) TIME_FOR_COMPARE;
6184
if (join->sort_by_table &&
6185
join->sort_by_table !=
6186
join->positions[join->const_tables].table->table)
6187
read_time+=record_count; // We have to make a temp table
6188
if (read_time < join->best_read)
6190
memcpy((uchar*) join->best_positions,(uchar*) join->positions,
6191
sizeof(POSITION)*idx);
6192
join->best_read= read_time - 0.001;
6196
if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6197
return(false); /* Found better before */
6200
double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6201
for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6203
table_map real_table_bit=s->table->map;
6204
if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6205
(!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6207
double records, best;
6208
advance_sj_state(rest_tables, s);
6209
best_access_path(join, s, thd, rest_tables, idx, record_count,
6211
records= join->positions[idx].records_read;
6212
best= join->positions[idx].read_time;
6214
Go to the next level only if there hasn't been a better key on
6215
this level! This will cut down the search for a lot simple cases!
6217
double current_record_count=record_count*records;
6218
double current_read_time=read_time+best;
6219
if (best_record_count > current_record_count ||
6220
best_read_time > current_read_time ||
6221
(idx == join->const_tables && s->table == join->sort_by_table))
6223
if (best_record_count >= current_record_count &&
6224
best_read_time >= current_read_time &&
6225
(!(s->key_dependent & rest_tables) || records < 2.0))
6227
best_record_count=current_record_count;
6228
best_read_time=current_read_time;
6230
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6231
if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6232
current_record_count,current_read_time))
6234
swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6236
restore_prev_nj_state(s);
6237
restore_prev_sj_state(rest_tables, s);
6238
if (join->select_options & SELECT_STRAIGHT_JOIN)
6239
break; // Don't test all combinations
849
6247
Find how much space the prevous read not const tables takes in cache.
851
void calc_used_field_length(Session *, JoinTable *join_tab)
6250
static void calc_used_field_length(THD *thd __attribute__((unused)),
853
uint32_t null_fields,blobs,fields,rec_length;
6253
uint null_fields,blobs,fields,rec_length;
854
6254
Field **f_ptr,*field;
6255
MY_BITMAP *read_set= join_tab->table->read_set;;
856
6257
null_fields= blobs= fields= rec_length=0;
857
for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
6258
for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
859
if (field->isReadSet())
6260
if (bitmap_is_set(read_set, field->field_index))
861
uint32_t flags=field->flags;
6262
uint flags=field->flags;
863
6264
rec_length+=field->pack_length();
864
6265
if (flags & BLOB_FLAG)
866
6267
if (!(flags & NOT_NULL_FLAG))
870
6271
if (null_fields)
871
rec_length+=(join_tab->table->getNullFields() + 7)/8;
6272
rec_length+=(join_tab->table->s->null_fields+7)/8;
872
6273
if (join_tab->table->maybe_null)
873
6274
rec_length+=sizeof(bool);
876
uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
877
(join_tab->table->getRecordLength()- rec_length));
878
rec_length+= max((uint32_t)4,blob_length);
880
join_tab->used_fields= fields;
881
join_tab->used_fieldlength= rec_length;
882
join_tab->used_blobs= blobs;
885
StoredKey *get_store_key(Session *session,
886
optimizer::KeyUse *keyuse,
887
table_map used_tables,
888
KeyPartInfo *key_part,
889
unsigned char *key_buff,
892
Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
893
if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
895
return new store_key_const_item(session,
897
key_buff + maybe_null,
898
maybe_null ? key_buff : 0,
902
else if (key_use_val->type() == Item::FIELD_ITEM ||
903
(key_use_val->type() == Item::REF_ITEM &&
904
key_use_val->ref_type() == Item_ref::OUTER_REF &&
905
(*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
906
key_use_val->real_item()->type() == Item::FIELD_ITEM))
908
return new store_key_field(session,
910
key_buff + maybe_null,
911
maybe_null ? key_buff : 0,
913
((Item_field*) key_use_val->real_item())->field,
914
key_use_val->full_name());
916
return new store_key_item(session,
918
key_buff + maybe_null,
919
maybe_null ? key_buff : 0,
6277
uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6278
(join_tab->table->s->reclength- rec_length));
6279
rec_length+=(uint) max(4,blob_length);
6281
join_tab->used_fields=fields;
6282
join_tab->used_fieldlength=rec_length;
6283
join_tab->used_blobs=blobs;
6288
cache_record_length(JOIN *join,uint idx)
6291
JOIN_TAB **pos,**end;
6294
for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6298
JOIN_TAB *join_tab= *pos;
6299
if (!join_tab->used_fieldlength) /* Not calced yet */
6300
calc_used_field_length(thd, join_tab);
6301
length+=join_tab->used_fieldlength;
6308
Get the number of different row combinations for subset of partial join
6312
join The join structure
6313
idx Number of tables in the partial join order (i.e. the
6314
partial join order is in join->positions[0..idx-1])
6315
found_ref Bitmap of tables for which we need to find # of distinct
6319
Given a partial join order (in join->positions[0..idx-1]) and a subset of
6320
tables within that join order (specified in found_ref), find out how many
6321
distinct row combinations of subset tables will be in the result of the
6324
This is used as follows: Suppose we have a table accessed with a ref-based
6325
method. The ref access depends on current rows of tables in found_ref.
6326
We want to count # of different ref accesses. We assume two ref accesses
6327
will be different if at least one of access parameters is different.
6328
Example: consider a query
6330
SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6333
t1, ref access on t1.key=c1
6334
t2, ref access on t2.key=c2
6335
t3, ref access on t3.key=t1.field
6337
For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6338
For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6339
For t3: n_ref_scans = records_read(t1)*records_read(t2)
6340
n_distinct_ref_scans = #records_read(t1)
6342
The reason for having this function (at least the latest version of it)
6343
is that we need to account for buffering in join execution.
6345
An edge-case example: if we have a non-first table in join accessed via
6346
ref(const) or ref(param) where there is a small number of different
6347
values of param, then the access will likely hit the disk cache and will
6348
not require any disk seeks.
6350
The proper solution would be to assume an LRU disk cache of some size,
6351
calculate probability of cache hits, etc. For now we just count
6352
identical ref accesses as one.
6355
Expected number of row combinations
6359
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6362
POSITION *pos_end= join->positions - 1;
6363
for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6365
if (pos->table->table->map & found_ref)
6367
found_ref|= pos->ref_depend_map;
6369
For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
6370
with no matching row we will get position[t2].records_read==0.
6371
Actually the size of output is one null-complemented row, therefore
6372
we will use value of 1 whenever we get records_read==0.
6375
- the above case can't occur if inner part of outer join has more
6376
than one table: table with no matches will not be marked as const.
6378
- Ideally we should add 1 to records_read for every possible null-
6379
complemented row. We're not doing it because: 1. it will require
6380
non-trivial code and add overhead. 2. The value of records_read
6381
is an inprecise estimate and adding 1 (or, in the worst case,
6382
#max_nested_outer_joins=64-1) will not make it any more precise.
6384
if (pos->records_read > DBL_EPSILON)
6385
found*= pos->records_read;
925
This function is only called for const items on fields which are keys.
928
returns 1 if there was some conversion made when the field was stored.
6393
Set up join struct according to best position.
930
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
933
Table *table= field->getTable();
934
Session *session= table->in_use;
935
ha_rows cuted_fields=session->cuted_fields;
938
we should restore old value of count_cuted_fields because
939
store_val_in_field can be called from insert_query
940
with select_insert, which make count_cuted_fields= 1
942
enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
943
session->count_cuted_fields= check_flag;
944
error= item->save_in_field(field, 1);
945
session->count_cuted_fields= old_count_cuted_fields;
946
return error || cuted_fields != session->cuted_fields;
949
inline void add_cond_and_fix(Item **e1, Item *e2)
6397
get_best_combination(JOIN *join)
6400
table_map used_tables;
6401
JOIN_TAB *join_tab,*j;
6406
table_count=join->tables;
6407
if (!(join->join_tab=join_tab=
6408
(JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6413
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read
6414
for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
954
if ((res= new Item_cond_and(*e1, e2)))
6417
*j= *join->best_positions[tablenr].table;
6418
form=join->table[tablenr]=j->table;
6419
used_tables|= form->map;
6420
form->reginfo.join_tab=j;
6421
if (!*j->on_expr_ref)
6422
form->reginfo.not_exists_optimize=0; // Only with LEFT JOIN
6423
if (j->type == JT_CONST)
6424
continue; // Handled in make_join_stat..
6429
if (j->type == JT_SYSTEM)
6431
if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
957
res->quick_fix_field();
6434
if (tablenr != join->const_tables)
6437
else if (create_ref_for_key(join, j, keyuse, used_tables))
6438
return(true); // Something went wrong
6441
for (i=0 ; i < table_count ; i++)
6442
join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6443
update_depend_map(join);
964
bool create_ref_for_key(Join *join,
966
optimizer::KeyUse *org_keyuse,
967
table_map used_tables)
6448
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6449
table_map used_tables)
969
optimizer::KeyUse *keyuse= org_keyuse;
970
Session *session= join->session;
975
KeyInfo *keyinfo= NULL;
6451
KEYUSE *keyuse=org_keyuse;
6452
THD *thd= join->thd;
6453
uint keyparts,length,key;
977
6457
/* Use best key from find_best */
979
key= keyuse->getKey();
980
keyinfo= table->key_info + key;
6460
keyinfo=table->key_info+key;
984
uint32_t found_part_ref_or_null= 0;
6464
uint found_part_ref_or_null= 0;
986
6466
Calculate length for the used key
987
6467
Stop if there is a missing key part or when we find second key_part
6854
Fill in outer join related info for the execution plan structure.
6856
For each outer join operation left after simplification of the
6857
original query the function set up the following pointers in the linear
6858
structure join->join_tab representing the selected execution plan.
6859
The first inner table t0 for the operation is set to refer to the last
6860
inner table tk through the field t0->last_inner.
6861
Any inner table ti for the operation are set to refer to the first
6862
inner table ti->first_inner.
6863
The first inner table t0 for the operation is set to refer to the
6864
first inner table of the embedding outer join operation, if there is any,
6865
through the field t0->first_upper.
6866
The on expression for the outer join operation is attached to the
6867
corresponding first inner table through the field t0->on_expr_ref.
6868
Here ti are structures of the JOIN_TAB type.
6870
EXAMPLE. For the query:
6874
(t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6875
ON (t1.a=t2.a AND t1.b=t3.b)
6879
given the execution plan with the table order t1,t2,t3,t4
6880
is selected, the following references will be set;
6881
t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6882
t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6883
on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
6884
*t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6886
@param join reference to the info fully describing the query
6889
The function assumes that the simplification procedure has been
6890
already applied to the join query (see simplify_joins).
6891
This function can be called only after the execution plan
6896
make_outerjoin_info(JOIN *join)
6898
for (uint i=join->const_tables ; i < join->tables ; i++)
6900
JOIN_TAB *tab=join->join_tab+i;
6901
TABLE *table=tab->table;
6902
TABLE_LIST *tbl= table->pos_in_table_list;
6903
TABLE_LIST *embedding= tbl->embedding;
6905
if (tbl->outer_join)
6908
Table tab is the only one inner table for outer join.
6909
(Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6910
is in the query above.)
6912
tab->last_inner= tab->first_inner= tab;
6913
tab->on_expr_ref= &tbl->on_expr;
6914
tab->cond_equal= tbl->cond_equal;
6916
tab->first_upper= embedding->nested_join->first_nested;
6918
for ( ; embedding ; embedding= embedding->embedding)
6920
/* Ignore sj-nests: */
6921
if (!embedding->on_expr)
6923
NESTED_JOIN *nested_join= embedding->nested_join;
6924
if (!nested_join->counter_)
6927
Table tab is the first inner table for nested_join.
6928
Save reference to it in the nested join structure.
6930
nested_join->first_nested= tab;
6931
tab->on_expr_ref= &embedding->on_expr;
6932
tab->cond_equal= tbl->cond_equal;
6933
if (embedding->embedding)
6934
tab->first_upper= embedding->embedding->nested_join->first_nested;
6936
if (!tab->first_inner)
6937
tab->first_inner= nested_join->first_nested;
6938
if (++nested_join->counter_ < nested_join->join_list.elements)
6940
/* Table tab is the last inner table for nested join. */
6941
nested_join->first_nested->last_inner= tab;
6949
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6951
THD *thd= join->thd;
6954
add_not_null_conds(join);
6955
table_map used_tables;
6956
if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6957
{ /* there may be a select without a cond. */
6958
if (join->tables > 1)
6959
cond->update_used_tables(); // Tablenr may have changed
6960
if (join->const_tables == join->tables &&
6961
thd->lex->current_select->master_unit() ==
6962
&thd->lex->unit) // not upper level SELECT
6963
join->const_table_map|=RAND_TABLE_BIT;
6964
{ // Check const tables
6966
make_cond_for_table(cond,
6967
join->const_table_map,
6969
for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6970
tab < join->join_tab+join->tables ; tab++)
6972
if (*tab->on_expr_ref)
6974
JOIN_TAB *cond_tab= tab->first_inner;
6975
COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6976
join->const_table_map,
6980
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6983
tmp->quick_fix_field();
6984
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6985
new Item_cond_and(cond_tab->select_cond,
6987
if (!cond_tab->select_cond)
6989
cond_tab->select_cond->quick_fix_field();
6992
if (const_cond && !const_cond->val_int())
6994
return(1); // Impossible const condition
6998
used_tables=((select->const_tables=join->const_table_map) |
6999
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7000
for (uint i=join->const_tables ; i < join->tables ; i++)
7002
JOIN_TAB *tab=join->join_tab+i;
7004
first_inner is the X in queries like:
7005
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7007
JOIN_TAB *first_inner_tab= tab->first_inner;
7008
table_map current_map= tab->table->map;
7009
bool use_quick_range=0;
7013
Following force including random expression in last table condition.
7014
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7016
if (i == join->tables-1)
7017
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7018
used_tables|=current_map;
7020
if (tab->type == JT_REF && tab->quick &&
7021
(uint) tab->ref.key == tab->quick->index &&
7022
tab->ref.key_length < tab->quick->max_used_key_length)
7024
/* Range uses longer key; Use this instead of ref on key */
7029
tab->ref.key_parts=0; // Don't use ref key.
7030
join->best_positions[i].records_read= rows2double(tab->quick->records);
7032
We will use join cache here : prevent sorting of the first
7033
table only and sort at the end.
7035
if (i != join->const_tables && join->tables > join->const_tables + 1)
7041
tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7042
if (cond && !tmp && tab->quick)
7044
if (tab->type != JT_ALL)
7047
Don't use the quick method
7048
We come here in the case where we have 'key=constant' and
7049
the test is removed by make_cond_for_table()
7057
Hack to handle the case where we only refer to a table
7058
in the ON part of an OUTER JOIN. In this case we want the code
7059
below to check if we should use 'quick' instead.
7061
tmp= new Item_int((int64_t) 1,1); // Always true
7065
if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7066
tab->type == JT_EQ_REF)
7068
SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7069
thd->memdup((uchar*) select,
7072
return(1); // End of memory
7074
If tab is an inner table of an outer join operation,
7075
add a match guard to the pushed down predicate.
7076
The guard will turn the predicate on only after
7077
the first match for outer tables is encountered.
7082
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7083
a cond, so neutralize the hack above.
7085
if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7087
tab->select_cond=sel->cond=tmp;
7088
/* Push condition to storage engine if this is enabled
7089
and the condition is not guarded */
7090
tab->table->file->pushed_cond= NULL;
7091
if (thd->variables.engine_condition_pushdown)
7094
make_cond_for_table(tmp, current_map, current_map, 0);
7097
/* Push condition to handler */
7098
if (!tab->table->file->cond_push(push_cond))
7099
tab->table->file->pushed_cond= push_cond;
7104
tab->select_cond= sel->cond= NULL;
7106
sel->head=tab->table;
7109
/* Use quick key read if it's a constant and it's not used
7111
if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7112
&& (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7114
sel->quick=tab->quick; // Use value from get_quick_...
7115
sel->quick_keys.clear_all();
7116
sel->needed_reg.clear_all();
7124
uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7125
if (i == join->const_tables && ref_key)
7127
if (!tab->const_keys.is_clear_all() &&
7128
tab->table->reginfo.impossible_range)
7131
else if (tab->type == JT_ALL && ! use_quick_range)
7133
if (!tab->const_keys.is_clear_all() &&
7134
tab->table->reginfo.impossible_range)
7135
return(1); // Impossible range
7137
We plan to scan all rows.
7138
Check again if we should use an index.
7139
We could have used an column from a previous table in
7140
the index if we are using limit and this is the first table
7143
if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7144
(!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)))
7146
/* Join with outer join condition */
7147
COND *orig_cond=sel->cond;
7148
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7151
We can't call sel->cond->fix_fields,
7152
as it will break tab->on_expr if it's AND condition
7153
(fix_fields currently removes extra AND/OR levels).
7154
Yet attributes of the just built condition are not needed.
7155
Thus we call sel->cond->quick_fix_field for safety.
7157
if (sel->cond && !sel->cond->fixed)
7158
sel->cond->quick_fix_field();
7160
if (sel->test_quick_select(thd, tab->keys,
7161
used_tables & ~ current_map,
7162
(join->select_options &
7165
join->unit->select_limit_cnt), 0,
7169
Before reporting "Impossible WHERE" for the whole query
7170
we have to check isn't it only "impossible ON" instead
7172
sel->cond=orig_cond;
7173
if (!*tab->on_expr_ref ||
7174
sel->test_quick_select(thd, tab->keys,
7175
used_tables & ~ current_map,
7176
(join->select_options &
7179
join->unit->select_limit_cnt),0,
7181
return(1); // Impossible WHERE
7184
sel->cond=orig_cond;
7186
/* Fix for EXPLAIN */
7188
join->best_positions[i].records_read= (double)sel->quick->records;
7192
sel->needed_reg=tab->needed_reg;
7193
sel->quick_keys.clear_all();
7195
if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7196
!sel->needed_reg.is_subset(tab->checked_keys))
7198
tab->keys=sel->quick_keys;
7199
tab->keys.merge(sel->needed_reg);
7200
tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7201
(select->quick_keys.is_clear_all() ||
7203
(select->quick->records >= 100L)))) ?
7205
sel->read_tables= used_tables & ~current_map;
7207
if (i != join->const_tables && tab->use_quick != 2)
7208
{ /* Read with cache */
7210
(tmp=make_cond_for_table(cond,
7211
join->const_table_map |
7215
tab->cache.select=(SQL_SELECT*)
7216
thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7217
tab->cache.select->cond=tmp;
7218
tab->cache.select->read_tables=join->const_table_map;
7225
Push down conditions from all on expressions.
7226
Each of these conditions are guarded by a variable
7227
that turns if off just before null complemented row for
7228
outer joins is formed. Thus, the condition from an
7229
'on expression' are guaranteed not to be checked for
7230
the null complemented row.
7233
/* First push down constant conditions from on expressions */
7234
for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7235
join_tab < join->join_tab+join->tables ; join_tab++)
7237
if (*join_tab->on_expr_ref)
7239
JOIN_TAB *cond_tab= join_tab->first_inner;
7240
COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7241
join->const_table_map,
7245
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7248
tmp->quick_fix_field();
7249
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7250
new Item_cond_and(cond_tab->select_cond,tmp);
7251
if (!cond_tab->select_cond)
7253
cond_tab->select_cond->quick_fix_field();
7257
/* Push down non-constant conditions from on expressions */
7258
JOIN_TAB *last_tab= tab;
7259
while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7262
Table tab is the last inner table of an outer join.
7263
An on expression is always attached to it.
7265
COND *on_expr= *first_inner_tab->on_expr_ref;
7267
table_map used_tables2= (join->const_table_map |
7268
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7269
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7271
current_map= tab->table->map;
7272
used_tables2|= current_map;
7273
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7277
JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7279
First add the guards for match variables of
7280
all embedding outer join operations.
7282
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7287
Now add the guard turning the predicate off for
7288
the null complemented row.
7290
tmp_cond= new Item_func_trig_cond(tmp_cond,
7294
tmp_cond->quick_fix_field();
7295
/* Add the predicate to other pushed down predicates */
7296
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7297
new Item_cond_and(cond_tab->select_cond,
7299
if (!cond_tab->select_cond)
7301
cond_tab->select_cond->quick_fix_field();
7304
first_inner_tab= first_inner_tab->first_upper;
7313
Check if given expression uses only table fields covered by the given index
7316
uses_index_fields_only()
7317
item Expression to check
7318
tbl The table having the index
7319
keyno The index number
7320
other_tbls_ok true <=> Fields of other non-const tables are allowed
7323
Check if given expression only uses fields covered by index #keyno in the
7324
table tbl. The expression can use any fields in any other tables.
7326
The expression is guaranteed not to be AND or OR - those constructs are
7327
handled outside of this function.
7334
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno,
7337
if (item->const_item())
7341
Don't push down the triggered conditions. Nested outer joins execution
7342
code may need to evaluate a condition several times (both triggered and
7343
untriggered), and there is no way to put thi
7344
TODO: Consider cloning the triggered condition and using the copies for:
7345
1. push the first copy down, to have most restrictive index condition
7347
2. Put the second copy into tab->select_cond.
7349
if (item->type() == Item::FUNC_ITEM &&
7350
((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7353
if (!(item->used_tables() & tbl->map))
7354
return other_tbls_ok;
7356
Item::Type item_type= item->type();
7357
switch (item_type) {
7358
case Item::FUNC_ITEM:
7360
/* This is a function, apply condition recursively to arguments */
7361
Item_func *item_func= (Item_func*)item;
7363
Item **item_end= (item_func->arguments()) + item_func->argument_count();
7364
for (child= item_func->arguments(); child != item_end; child++)
7366
if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
7371
case Item::COND_ITEM:
7373
/* This is a function, apply condition recursively to arguments */
7374
List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7378
if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7383
case Item::FIELD_ITEM:
7385
Item_field *item_field= (Item_field*)item;
7386
if (item_field->field->table != tbl)
7388
return item_field->field->part_of_key.is_set(keyno);
7390
case Item::REF_ITEM:
7391
return uses_index_fields_only(item->real_item(), tbl, keyno,
7394
return false; /* Play it safe, don't push unknown non-const items */
1216
7399
#define ICP_COND_USES_INDEX_ONLY 10
1222
void JoinTable::cleanup()
1224
safe_delete(select);
1229
size_t size= cache.end - cache.buff;
1230
global_join_buffer.sub(size);
7402
Get a part of the condition that can be checked using only index fields
7405
make_cond_for_index()
7406
cond The source condition
7407
table The table that is partially available
7408
keyno The index in the above table. Only fields covered by the index
7410
other_tbls_ok true <=> Fields of other non-const tables are allowed
7413
Get a part of the condition that can be checked when for the given table
7414
we have values only of fields covered by some index. The condition may
7415
refer to other tables, it is assumed that we have values of all of their
7419
make_cond_for_index(
7420
"cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7423
"cond(t1.field) AND cond(t2.key2)"
7426
Index condition, or NULL if no condition could be inferred.
7429
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7434
if (cond->type() == Item::COND_ITEM)
7437
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7439
Item_cond_and *new_cond=new Item_cond_and;
7442
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7446
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7448
new_cond->argument_list()->push_back(fix);
7449
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7451
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7452
cond->marker= ICP_COND_USES_INDEX_ONLY;
7453
switch (new_cond->argument_list()->elements) {
7457
return new_cond->argument_list()->head();
7459
new_cond->quick_fix_field();
7465
Item_cond_or *new_cond=new Item_cond_or;
7468
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7472
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7475
new_cond->argument_list()->push_back(fix);
7476
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7478
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7479
cond->marker= ICP_COND_USES_INDEX_ONLY;
7480
new_cond->quick_fix_field();
7481
new_cond->top_level_item();
7486
if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
7488
cond->marker= ICP_COND_USES_INDEX_ONLY;
7493
Item *make_cond_remainder(Item *cond, bool exclude_index)
7495
if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7496
return 0; /* Already checked */
7498
if (cond->type() == Item::COND_ITEM)
7500
table_map tbl_map= 0;
7501
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7503
/* Create new top level AND item */
7504
Item_cond_and *new_cond=new Item_cond_and;
7507
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7511
Item *fix= make_cond_remainder(item, exclude_index);
7514
new_cond->argument_list()->push_back(fix);
7515
tbl_map |= fix->used_tables();
7518
switch (new_cond->argument_list()->elements) {
7522
return new_cond->argument_list()->head();
7524
new_cond->quick_fix_field();
7525
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7531
Item_cond_or *new_cond=new Item_cond_or;
7534
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7538
Item *fix= make_cond_remainder(item, false);
7541
new_cond->argument_list()->push_back(fix);
7542
tbl_map |= fix->used_tables();
7544
new_cond->quick_fix_field();
7545
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7546
new_cond->top_level_item();
7555
Try to extract and push the index condition
7559
tab A join tab that has tab->table->file and its condition
7561
keyno Index for which extract and push the condition
7562
other_tbls_ok true <=> Fields of other non-const tables are allowed
7565
Try to extract and push the index condition down to table handler
7568
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7571
if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7572
tab->join->thd->variables.engine_condition_pushdown)
7574
idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7579
tab->pre_idx_push_select_cond= tab->select_cond;
7580
Item *idx_remainder_cond=
7581
tab->table->file->idx_cond_push(keyno, idx_cond);
7584
Disable eq_ref's "lookup cache" if we've pushed down an index
7586
TODO: This check happens to work on current ICP implementations, but
7587
there may exist a compliant implementation that will not work
7588
correctly with it. Sort this out when we stabilize the condition
7591
if (idx_remainder_cond != idx_cond)
7592
tab->ref.disable_cache= true;
7594
Item *row_cond= make_cond_remainder(tab->select_cond, true);
7598
if (!idx_remainder_cond)
7599
tab->select_cond= row_cond;
7602
tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7603
tab->select_cond->quick_fix_field();
7604
((Item_cond_and*)tab->select_cond)->used_tables_cache=
7605
row_cond->used_tables() | idx_remainder_cond->used_tables();
7609
tab->select_cond= idx_remainder_cond;
7612
tab->select->cond= tab->select_cond;
7622
Determine if the set is already ordered for ORDER BY, so it can
7623
disable join cache because it will change the ordering of the results.
7624
Code handles sort table that is at any location (not only first after
7625
the const tables) despite the fact that it's currently prohibited.
7626
We must disable join cache if the first non-const table alone is
7627
ordered. If there is a temp table the ordering is done as a last
7628
operation and doesn't prevent join cache usage.
7630
uint make_join_orderinfo(JOIN *join)
7634
return join->tables;
7636
for (i=join->const_tables ; i < join->tables ; i++)
7638
JOIN_TAB *tab=join->join_tab+i;
7639
TABLE *table=tab->table;
7640
if ((table == join->sort_by_table &&
7641
(!join->order || join->skip_sort_order)) ||
7642
(join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7652
Plan refinement stage: do various set ups for the executioner
7655
make_join_readinfo()
7656
join Join being processed
7657
options Join's options (checking for SELECT_DESCRIBE,
7658
SELECT_NO_JOIN_CACHE)
7659
no_jbuf_after Don't use join buffering after table with this number.
7662
Plan refinement stage: do various set ups for the executioner
7663
- set up use of join buffering
7664
- push index conditions
7665
- increment counters
7670
true - Out of memory
7674
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
7677
bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7680
for (i=join->const_tables ; i < join->tables ; i++)
7682
JOIN_TAB *tab=join->join_tab+i;
7683
TABLE *table=tab->table;
7684
bool using_join_cache;
7685
tab->read_record.table= table;
7686
tab->read_record.file=table->file;
7687
tab->next_select=sub_select; /* normal select */
7689
TODO: don't always instruct first table's ref/range access method to
7690
produce sorted output.
7692
tab->sorted= sorted;
7693
sorted= 0; // only first must be sorted
7694
if (tab->insideout_match_tab)
7696
if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7701
switch (tab->type) {
7702
case JT_SYSTEM: // Only happens with left join
7703
table->status=STATUS_NO_RECORD;
7704
tab->read_first_record= join_read_system;
7705
tab->read_record.read_record= join_no_more_records;
7707
case JT_CONST: // Only happens with left join
7708
table->status=STATUS_NO_RECORD;
7709
tab->read_first_record= join_read_const;
7710
tab->read_record.read_record= join_no_more_records;
7711
if (table->covering_keys.is_set(tab->ref.key) &&
7715
table->file->extra(HA_EXTRA_KEYREAD);
7719
table->status=STATUS_NO_RECORD;
7722
delete tab->select->quick;
7723
tab->select->quick=0;
7727
tab->read_first_record= join_read_key;
7728
tab->read_record.read_record= join_no_more_records;
7729
if (table->covering_keys.is_set(tab->ref.key) &&
7733
table->file->extra(HA_EXTRA_KEYREAD);
7736
push_index_cond(tab, tab->ref.key, true);
7738
case JT_REF_OR_NULL:
7740
table->status=STATUS_NO_RECORD;
7743
delete tab->select->quick;
7744
tab->select->quick=0;
7748
if (table->covering_keys.is_set(tab->ref.key) &&
7752
table->file->extra(HA_EXTRA_KEYREAD);
7755
push_index_cond(tab, tab->ref.key, true);
7756
if (tab->type == JT_REF)
7758
tab->read_first_record= join_read_always_key;
7759
tab->read_record.read_record= tab->insideout_match_tab?
7760
join_read_next_same_diff : join_read_next_same;
7764
tab->read_first_record= join_read_always_key_or_null;
7765
tab->read_record.read_record= join_read_next_same_or_null;
7770
If previous table use cache
7771
If the incoming data set is already sorted don't use cache.
7773
table->status=STATUS_NO_RECORD;
7774
using_join_cache= false;
7775
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7776
tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7777
!tab->insideout_match_tab)
7779
if ((options & SELECT_DESCRIBE) ||
7780
!join_init_cache(join->thd,join->join_tab+join->const_tables,
7781
i-join->const_tables))
7783
using_join_cache= true;
7784
tab[-1].next_select=sub_select_cache; /* Patch previous */
7787
/* These init changes read_record */
7788
if (tab->use_quick == 2)
7790
join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7791
tab->read_first_record= join_init_quick_read_record;
7793
status_var_increment(join->thd->status_var.select_range_check_count);
7797
tab->read_first_record= join_init_read_record;
7798
if (i == join->const_tables)
7800
if (tab->select && tab->select->quick)
7803
status_var_increment(join->thd->status_var.select_range_count);
7807
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7809
status_var_increment(join->thd->status_var.select_scan_count);
7814
if (tab->select && tab->select->quick)
7817
status_var_increment(join->thd->status_var.select_full_range_join_count);
7821
join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7823
status_var_increment(join->thd->status_var.select_full_join_count);
7826
if (!table->no_keyread)
7828
if (tab->select && tab->select->quick &&
7829
tab->select->quick->index != MAX_KEY && //not index_merge
7830
table->covering_keys.is_set(tab->select->quick->index))
7833
table->file->extra(HA_EXTRA_KEYREAD);
7835
else if (!table->covering_keys.is_clear_all() &&
7836
!(tab->select && tab->select->quick))
7837
{ // Only read index tree
7838
if (!tab->insideout_match_tab)
7841
See bug #26447: "Using the clustered index for a table scan
7842
is always faster than using a secondary index".
7844
if (table->s->primary_key != MAX_KEY &&
7845
table->file->primary_key_is_clustered())
7846
tab->index= table->s->primary_key;
7848
tab->index=find_shortest_key(table, & table->covering_keys);
7850
tab->read_first_record= join_read_first;
7851
tab->type=JT_NEXT; // Read with index_first / index_next
7854
if (tab->select && tab->select->quick &&
7855
tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
7856
push_index_cond(tab, tab->select->quick->index, !using_join_cache);
7860
break; /* purecov: deadcode */
7863
abort(); /* purecov: deadcode */
7866
join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7872
Give error if we some tables are done with a full join.
7874
This is used by multi_table_update and multi_table_delete when running
7877
@param join Join condition
7882
1 Error (full join used)
7885
bool error_if_full_join(JOIN *join)
7887
for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
7891
if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
7893
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7894
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7906
void JOIN_TAB::cleanup()
2515
9633
if (!(left_const && right_const) &&
2516
9634
args[0]->result_type() == args[1]->result_type())
2520
resolve_const_item(session, &args[1], args[0]);
2521
func->update_used_tables();
2522
change_cond_ref_to_const(session, save_list, and_father, and_father,
2525
else if (left_const)
2527
resolve_const_item(session, &args[0], args[1]);
2528
func->update_used_tables();
2529
change_cond_ref_to_const(session, save_list, and_father, and_father,
9638
resolve_const_item(thd, &args[1], args[0]);
9639
func->update_used_tables();
9640
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9643
else if (left_const)
9645
resolve_const_item(thd, &args[0], args[1]);
9646
func->update_used_tables();
9647
change_cond_ref_to_const(thd, save_list, and_father, and_father,
9657
Simplify joins replacing outer joins by inner joins whenever it's
9660
The function, during a retrieval of join_list, eliminates those
9661
outer joins that can be converted into inner join, possibly nested.
9662
It also moves the on expressions for the converted outer joins
9663
and from inner joins to conds.
9664
The function also calculates some attributes for nested joins:
9668
- on_expr_dep_tables
9669
The first two attributes are used to test whether an outer join can
9670
be substituted for an inner join. The third attribute represents the
9671
relation 'to be dependent on' for tables. If table t2 is dependent
9672
on table t1, then in any evaluated execution plan table access to
9673
table t2 must precede access to table t2. This relation is used also
9674
to check whether the query contains invalid cross-references.
9675
The forth attribute is an auxiliary one and is used to calculate
9677
As the attribute dep_tables qualifies possibles orders of tables in the
9678
execution plan, the dependencies required by the straight join
9679
modifiers are reflected in this attribute as well.
9680
The function also removes all braces that can be removed from the join
9681
expression without changing its meaning.
9684
An outer join can be replaced by an inner join if the where condition
9685
or the on expression for an embedding nested join contains a conjunctive
9686
predicate rejecting null values for some attribute of the inner tables.
9690
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9692
the predicate t2.b < 5 rejects nulls.
9693
The query is converted first to:
9695
SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9697
then to the equivalent form:
9699
SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9703
Similarly the following query:
9705
SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9710
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
9714
One conversion might trigger another:
9716
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9717
LEFT JOIN t3 ON t3.b=t2.b
9718
WHERE t3 IS NOT NULL =>
9719
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9720
WHERE t3 IS NOT NULL AND t3.b=t2.b =>
9721
SELECT * FROM t1, t2, t3
9722
WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9725
The function removes all unnecessary braces from the expression
9726
produced by the conversions.
9729
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9731
finally is converted to:
9733
SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9738
It also will remove braces from the following queries:
9740
SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9741
SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9744
The benefit of this simplification procedure is that it might return
9745
a query for which the optimizer can evaluate execution plan with more
9746
join orders. With a left join operation the optimizer does not
9747
consider any plan where one of the inner tables is before some of outer
9751
The function is implemented by a recursive procedure. On the recursive
9752
ascent all attributes are calculated, all outer joins that can be
9753
converted are replaced and then all unnecessary braces are removed.
9754
As join list contains join tables in the reverse order sequential
9755
elimination of outer joins does not require extra recursive calls.
9758
Remove all semi-joins that have are within another semi-join (i.e. have
9759
an "ancestor" semi-join nest)
9762
Here is an example of a join query with invalid cross references:
9764
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
9767
@param join reference to the query info
9768
@param join_list list representation of the join to be converted
9769
@param conds conditions to add on expressions for converted joins
9770
@param top true <=> conds is the where condition
9773
- The new condition, if success
9778
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9782
NESTED_JOIN *nested_join;
9783
TABLE_LIST *prev_table= 0;
9784
List_iterator<TABLE_LIST> li(*join_list);
9787
Try to simplify join operations from join_list.
9788
The most outer join operation is checked for conversion first.
9790
while ((table= li++))
9792
table_map used_tables;
9793
table_map not_null_tables= (table_map) 0;
9795
if ((nested_join= table->nested_join))
9798
If the element of join_list is a nested join apply
9799
the procedure to its nested join list first.
9803
Item *expr= table->on_expr;
9805
If an on expression E is attached to the table,
9806
check all null rejected predicates in this expression.
9807
If such a predicate over an attribute belonging to
9808
an inner table of an embedded outer join is found,
9809
the outer join is converted to an inner join and
9810
the corresponding on expression is added to E.
9812
expr= simplify_joins(join, &nested_join->join_list,
9813
expr, false, in_sj || table->sj_on_expr);
9815
if (!table->prep_on_expr || expr != table->on_expr)
9819
table->on_expr= expr;
9820
table->prep_on_expr= expr->copy_andor_structure(join->thd);
9823
nested_join->used_tables= (table_map) 0;
9824
nested_join->not_null_tables=(table_map) 0;
9825
conds= simplify_joins(join, &nested_join->join_list, conds, top,
9826
in_sj || table->sj_on_expr);
9827
used_tables= nested_join->used_tables;
9828
not_null_tables= nested_join->not_null_tables;
9832
if (!table->prep_on_expr)
9833
table->prep_on_expr= table->on_expr;
9834
used_tables= table->table->map;
9836
not_null_tables= conds->not_null_tables();
9839
if (table->embedding)
9841
table->embedding->nested_join->used_tables|= used_tables;
9842
table->embedding->nested_join->not_null_tables|= not_null_tables;
9845
if (!table->outer_join || (used_tables & not_null_tables))
9848
For some of the inner tables there are conjunctive predicates
9849
that reject nulls => the outer join can be replaced by an inner join.
9851
table->outer_join= 0;
9854
/* Add ON expression to the WHERE or upper-level ON condition. */
9857
conds= and_conds(conds, table->on_expr);
9858
conds->top_level_item();
9859
/* conds is always a new item as both cond and on_expr existed */
9860
assert(!conds->fixed);
9861
conds->fix_fields(join->thd, &conds);
9864
conds= table->on_expr;
9865
table->prep_on_expr= table->on_expr= 0;
9873
Only inner tables of non-convertible outer joins
9874
remain with on_expr.
9878
table->dep_tables|= table->on_expr->used_tables();
9879
if (table->embedding)
9881
table->dep_tables&= ~table->embedding->nested_join->used_tables;
9883
Embedding table depends on tables used
9884
in embedded on expressions.
9886
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9889
table->dep_tables&= ~table->table->map;
9894
/* The order of tables is reverse: prev_table follows table */
9895
if (prev_table->straight)
9896
prev_table->dep_tables|= used_tables;
9897
if (prev_table->on_expr)
9899
prev_table->dep_tables|= table->on_expr_dep_tables;
9900
table_map prev_used_tables= prev_table->nested_join ?
9901
prev_table->nested_join->used_tables :
9902
prev_table->table->map;
9904
If on expression contains only references to inner tables
9905
we still make the inner tables dependent on the outer tables.
9906
It would be enough to set dependency only on one outer table
9907
for them. Yet this is really a rare case.
9909
if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9910
prev_table->dep_tables|= used_tables;
9917
Flatten nested joins that can be flattened.
9918
no ON expression and not a semi-join => can be flattened.
9921
while ((table= li++))
9923
nested_join= table->nested_join;
9924
if (table->sj_on_expr && !in_sj)
9927
If this is a semi-join that is not contained within another semi-join,
9928
leave it intact (otherwise it is flattened)
9930
join->select_lex->sj_nests.push_back(table);
9932
else if (nested_join && !table->on_expr)
9935
List_iterator<TABLE_LIST> it(nested_join->join_list);
9938
tbl->embedding= table->embedding;
9939
tbl->join_list= table->join_list;
9941
li.replace(nested_join->join_list);
9949
Assign each nested join structure a bit in nested_join_map.
9951
Assign each nested join structure (except "confluent" ones - those that
9952
embed only one element) a bit in nested_join_map.
9954
@param join Join being processed
9955
@param join_list List of tables
9956
@param first_unused Number of first unused bit in nested_join_map before the
9960
This function is called after simplify_joins(), when there are no
9961
redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9962
we will not run out of bits in nested_join_map.
9965
First unused bit in nested_join_map after the call.
9968
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
9971
List_iterator<TABLE_LIST> li(*join_list);
9973
while ((table= li++))
9975
NESTED_JOIN *nested_join;
9976
if ((nested_join= table->nested_join))
9979
It is guaranteed by simplify_joins() function that a nested join
9980
that has only one child is either
9981
- a single-table view (the child is the underlying table), or
9982
- a single-table semi-join nest
9984
We don't assign bits to such sj-nests because
9985
1. it is redundant (a "sequence" of one table cannot be interleaved
9987
2. we could run out bits in nested_join_map otherwise.
9989
if (nested_join->join_list.elements != 1)
9991
/* Don't assign bits to sj-nests */
9993
nested_join->nj_map= (nested_join_map) 1 << first_unused++;
9994
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
9999
return(first_unused);
10004
Set NESTED_JOIN::counter=0 in all nested joins in passed list.
10006
Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
10007
the passed join_list.
10009
@param join_list List of nested joins to process. It may also contain base
10010
tables which will be ignored.
10013
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10015
List_iterator<TABLE_LIST> li(*join_list);
10017
while ((table= li++))
10019
NESTED_JOIN *nested_join;
10020
if ((nested_join= table->nested_join))
10022
nested_join->counter_= 0;
10023
reset_nj_counters(&nested_join->join_list);
2538
10031
Check interleaving with an inner tables of an outer join for
2539
10032
extension table.
2541
Check if table next_tab can be added to current partial join order, and
10034
Check if table next_tab can be added to current partial join order, and
2542
10035
if yes, record that it has been added.
2544
10037
The function assumes that both current partial join order and its
2545
10038
extension with next_tab are valid wrt table dependencies.
2549
LIMITATIONS ON JOIN order_st
10042
LIMITATIONS ON JOIN ORDER
2550
10043
The nested [outer] joins executioner algorithm imposes these limitations
2551
10044
on join order:
2552
1. "Outer tables first" - any "outer" table must be before any
10045
1. "Outer tables first" - any "outer" table must be before any
2553
10046
corresponding "inner" table.
2554
10047
2. "No interleaving" - tables inside a nested join must form a continuous
2555
sequence in join order (i.e. the sequence must not be interrupted by
10048
sequence in join order (i.e. the sequence must not be interrupted by
2556
10049
tables that are outside of this nested join).
2558
10051
#1 is checked elsewhere, this function checks #2 provided that #1 has
2559
10052
been already checked.
2561
10054
WHY NEED NON-INTERLEAVING
2562
Consider an example:
10055
Consider an example:
2564
10057
select * from t0 join t1 left join (t2 join t3) on cond1
2947
10511
if (test_if_equality_guarantees_uniqueness (left_item, right_item))
2950
return right_item->eq(*const_item, 1);
2951
*const_item=right_item;
10514
return right_item->eq(*const_item, 1);
10515
*const_item=right_item;
2955
10519
else if (right_item->eq(comp_item,1))
2957
10521
if (test_if_equality_guarantees_uniqueness (right_item, left_item))
2960
return left_item->eq(*const_item, 1);
2961
*const_item=left_item;
10524
return left_item->eq(*const_item, 1);
10525
*const_item=left_item;
10533
/****************************************************************************
10534
Create internal temporary table
10535
****************************************************************************/
10538
Create field for temporary table from given field.
10540
@param thd Thread handler
10541
@param org_field field from which new field will be created
10542
@param name New field name
10543
@param table Temporary table
10544
@param item !=NULL if item->result_field should point to new field.
10545
This is relevant for how fill_record() is going to work:
10546
If item != NULL then fill_record() will update
10547
the record in the original table.
10548
If item == NULL then fill_record() will update
10549
the temporary table
10550
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10551
field instead of blob.
10559
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10560
const char *name, TABLE *table,
10561
Item_field *item, uint convert_blob_length)
10566
Make sure that the blob fits into a Field_varstring which has
10569
if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10570
(org_field->flags & BLOB_FLAG))
10571
new_field= new Field_varstring(convert_blob_length,
10572
org_field->maybe_null(),
10573
org_field->field_name, table->s,
10574
org_field->charset());
10576
new_field= org_field->new_field(thd->mem_root, table,
10577
table == org_field->table);
10580
new_field->init(table);
10581
new_field->orig_table= org_field->orig_table;
10583
item->result_field= new_field;
10585
new_field->field_name= name;
10586
new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10587
if (org_field->maybe_null() || (item && item->maybe_null))
10588
new_field->flags&= ~NOT_NULL_FLAG; // Because of outer join
10589
if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
10590
table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10591
else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
10592
((Field_double *) new_field)->not_fixed= true;
10598
Create field for temporary table using type of given item.
10600
@param thd Thread handler
10601
@param item Item to create a field for
10602
@param table Temporary table
10603
@param copy_func If set and item is a function, store copy of
10605
@param modify_item 1 if item->result_field should point to new
10606
item. This is relevent for how fill_record()
10608
If modify_item is 1 then fill_record() will
10609
update the record in the original table.
10610
If modify_item is 0 then fill_record() will
10611
update the temporary table
10612
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10613
field instead of blob.
10621
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
10622
Item *item, TABLE *table,
10623
Item ***copy_func, bool modify_item,
10624
uint convert_blob_length)
10626
bool maybe_null= item->maybe_null;
10629
switch (item->result_type()) {
10631
new_field= new Field_double(item->max_length, maybe_null,
10632
item->name, item->decimals, true);
10636
Select an integer type with the minimal fit precision.
10637
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10638
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
10639
Field_long : make them Field_int64_t.
10641
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10642
new_field=new Field_int64_t(item->max_length, maybe_null,
10643
item->name, item->unsigned_flag);
10645
new_field=new Field_long(item->max_length, maybe_null,
10646
item->name, item->unsigned_flag);
10648
case STRING_RESULT:
10649
assert(item->collation.collation);
10651
enum enum_field_types type;
10653
DATE/TIME fields have STRING_RESULT result type.
10654
To preserve type they needed to be handled separately.
10656
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
10657
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
10658
type == DRIZZLE_TYPE_TIMESTAMP)
10659
new_field= item->tmp_table_field_from_field_type(table, 1);
10661
Make sure that the blob fits into a Field_varstring which has
10664
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10665
convert_blob_length <= Field_varstring::MAX_SIZE &&
10666
convert_blob_length)
10667
new_field= new Field_varstring(convert_blob_length, maybe_null,
10668
item->name, table->s,
10669
item->collation.collation);
10671
new_field= item->make_string_field(table);
10672
new_field->set_derivation(item->collation.derivation);
10674
case DECIMAL_RESULT:
10676
uint8_t dec= item->decimals;
10677
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
10678
uint32_t len= item->max_length;
10681
Trying to put too many digits overall in a DECIMAL(prec,dec)
10682
will always throw a warning. We must limit dec to
10683
DECIMAL_MAX_SCALE however to prevent an assert() later.
10688
signed int overflow;
10690
dec= min(dec, DECIMAL_MAX_SCALE);
10693
If the value still overflows the field with the corrected dec,
10694
we'll throw out decimals rather than integers. This is still
10695
bad and of course throws a truncation warning.
10696
+1: for decimal point
10699
overflow= my_decimal_precision_to_length(intg + dec, dec,
10700
item->unsigned_flag) - len;
10703
dec= max(0, dec - overflow); // too long, discard fract
10705
len -= item->decimals - dec; // corrected value fits
10708
new_field= new Field_new_decimal(len, maybe_null, item->name,
10709
dec, item->unsigned_flag);
10714
// This case should never be choosen
10720
new_field->init(table);
10722
if (copy_func && item->is_result_field())
10723
*((*copy_func)++) = item; // Save for copy_funcs
10725
item->set_result_field(new_field);
10726
if (item->type() == Item::NULL_ITEM)
10727
new_field->is_created_from_null_item= true;
10733
Create field for information schema table.
10735
@param thd Thread handler
10736
@param table Temporary table
10737
@param item Item to create a field for
10745
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
10746
Item *item, TABLE *table)
10748
if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
10751
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10752
field= new Field_blob(item->max_length, item->maybe_null,
10753
item->name, item->collation.collation);
10755
field= new Field_varstring(item->max_length, item->maybe_null,
10757
table->s, item->collation.collation);
10759
field->init(table);
10762
return item->tmp_table_field_from_field_type(table, 0);
10767
Create field for temporary table.
10769
@param thd Thread handler
10770
@param table Temporary table
10771
@param item Item to create a field for
10772
@param type Type of item (normally item->type)
10773
@param copy_func If set and item is a function, store copy of item
10775
@param from_field if field will be created using other field as example,
10776
pointer example field will be written here
10777
@param default_field If field has a default value field, store it here
10778
@param group 1 if we are going to do a relative group by on result
10779
@param modify_item 1 if item->result_field should point to new item.
10780
This is relevent for how fill_record() is going to
10782
If modify_item is 1 then fill_record() will update
10783
the record in the original table.
10784
If modify_item is 0 then fill_record() will update
10785
the temporary table
10786
@param convert_blob_length If >0 create a varstring(convert_blob_length)
10787
field instead of blob.
10795
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10796
Item ***copy_func, Field **from_field,
10797
Field **default_field,
10798
bool group, bool modify_item,
10799
bool table_cant_handle_bit_fields __attribute__((unused)),
10800
bool make_copy_field,
10801
uint convert_blob_length)
10804
Item::Type orig_type= type;
10805
Item *orig_item= 0;
10807
if (type != Item::FIELD_ITEM &&
10808
item->real_item()->type() == Item::FIELD_ITEM)
10811
item= item->real_item();
10812
type= Item::FIELD_ITEM;
10816
case Item::SUM_FUNC_ITEM:
10818
Item_sum *item_sum=(Item_sum*) item;
10819
result= item_sum->create_tmp_field(group, table, convert_blob_length);
10821
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10824
case Item::FIELD_ITEM:
10825
case Item::DEFAULT_VALUE_ITEM:
10827
Item_field *field= (Item_field*) item;
10828
bool orig_modify= modify_item;
10829
if (orig_type == Item::REF_ITEM)
10832
If item have to be able to store NULLs but underlaid field can't do it,
10833
create_tmp_field_from_field() can't be used for tmp field creation.
10835
if (field->maybe_null && !field->field->maybe_null())
10837
result= create_tmp_field_from_item(thd, item, table, NULL,
10838
modify_item, convert_blob_length);
10839
*from_field= field->field;
10840
if (result && modify_item)
10841
field->result_field= result;
10844
result= create_tmp_field_from_field(thd, (*from_field= field->field),
10845
orig_item ? orig_item->name :
10848
modify_item ? field :
10850
convert_blob_length);
10851
if (orig_type == Item::REF_ITEM && orig_modify)
10852
((Item_ref*)orig_item)->set_result_field(result);
10853
if (field->field->eq_def(result))
10854
*default_field= field->field;
10858
case Item::FUNC_ITEM:
10860
case Item::COND_ITEM:
10861
case Item::FIELD_AVG_ITEM:
10862
case Item::FIELD_STD_ITEM:
10863
case Item::SUBSELECT_ITEM:
10864
/* The following can only happen with 'CREATE TABLE ... SELECT' */
10865
case Item::PROC_ITEM:
10866
case Item::INT_ITEM:
10867
case Item::REAL_ITEM:
10868
case Item::DECIMAL_ITEM:
10869
case Item::STRING_ITEM:
10870
case Item::REF_ITEM:
10871
case Item::NULL_ITEM:
10872
case Item::VARBIN_ITEM:
10873
if (make_copy_field)
10875
assert(((Item_result_field*)item)->result_field);
10876
*from_field= ((Item_result_field*)item)->result_field;
10878
return create_tmp_field_from_item(thd, item, table,
10879
(make_copy_field ? 0 : copy_func),
10880
modify_item, convert_blob_length);
10881
case Item::TYPE_HOLDER:
10882
result= ((Item_type_holder *)item)->make_field_by_type(table);
10883
result->set_derivation(item->collation.derivation);
10885
default: // Dosen't have to be stored
10891
Set up column usage bitmaps for a temporary table
10894
For temporary tables, we need one bitmap with all columns set and
10895
a tmp_set bitmap to be used by things like filesort.
10898
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10900
uint field_count= table->s->fields;
10901
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10903
bitmap_init(&table->tmp_set,
10904
(my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10905
field_count, false);
10906
/* write_set and all_set are copies of read_set */
10907
table->def_write_set= table->def_read_set;
10908
table->s->all_set= table->def_read_set;
10909
bitmap_set_all(&table->s->all_set);
10910
table->default_column_bitmaps();
10915
Create a temp table according to a field list.
10917
Given field pointers are changed to point at tmp_table for
10918
send_fields. The table object is self contained: it's
10919
allocated in its own memory root, as well as Field objects
10920
created for table columns.
10921
This function will replace Item_sum items in 'fields' list with
10922
corresponding Item_field items, pointing at the fields in the
10923
temporary table, unless this was prohibited by true
10924
value of argument save_sum_fields. The Item_field objects
10925
are created in THD memory root.
10927
@param thd thread handle
10928
@param param a description used as input to create the table
10929
@param fields list of items that will be used to define
10930
column types of the table (also see NOTES)
10931
@param group TODO document
10932
@param distinct should table rows be distinct
10933
@param save_sum_fields see NOTES
10934
@param select_options
10936
@param table_alias possible name of the temporary table that can
10937
be used for name resolving; can be "".
10940
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10941
#define AVG_STRING_LENGTH_TO_PACK_ROWS 64
10942
#define RATIO_TO_PACK_ROWS 2
10943
#define MIN_STRING_LENGTH_TO_PACK_ROWS 10
10946
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10947
ORDER *group, bool distinct, bool save_sum_fields,
10948
uint64_t select_options, ha_rows rows_limit,
10951
MEM_ROOT *mem_root_save, own_root;
10953
TABLE_SHARE *share;
10954
uint i,field_count,null_count,null_pack_length;
10955
uint copy_func_count= param->func_count;
10956
uint hidden_null_count, hidden_null_pack_length, hidden_field_count;
10957
uint blob_count,group_null_items, string_count;
10958
uint temp_pool_slot=MY_BIT_NONE;
10960
ulong reclength, string_total_length;
10961
bool using_unique_constraint= 0;
10962
bool use_packed_rows= 0;
10963
bool not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10964
char *tmpname,path[FN_REFLEN];
10965
uchar *pos, *group_buff, *bitmaps;
10967
Field **reg_field, **from_field, **default_field;
10969
Copy_field *copy=0;
10971
KEY_PART_INFO *key_part_info;
10973
MI_COLUMNDEF *recinfo;
10974
uint total_uneven_bit_length= 0;
10975
bool force_copy_fields= param->force_copy_fields;
10977
status_var_increment(thd->status_var.created_tmp_tables);
10979
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10980
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10982
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10983
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10984
current_pid, temp_pool_slot);
10987
/* if we run out of slots or we are not using tempool */
10988
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10989
thd->thread_id, thd->tmp_table++);
10993
No need to change table name to lower case as we are only creating
10994
MyISAM or HEAP tables here
10996
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11001
if (!param->quick_group)
11002
group=0; // Can't use group key
11003
else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
11006
marker == 4 means two things:
11007
- store NULLs in the key, and
11008
- convert BIT fields to 64-bit long, needed because MEMORY tables
11009
can't index BIT fields.
11011
(*tmp->item)->marker= 4;
11012
if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11013
using_unique_constraint=1;
11015
if (param->group_length >= MAX_BLOB_WIDTH)
11016
using_unique_constraint=1;
11018
distinct=0; // Can't use distinct
11021
field_count=param->field_count+param->func_count+param->sum_func_count;
11022
hidden_field_count=param->hidden_field_count;
11025
When loose index scan is employed as access method, it already
11026
computes all groups and the result of all aggregate functions. We
11027
make space for the items of the aggregate function in the list of
11028
functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11029
these items are stored in the temporary table.
11031
if (param->precomputed_group_by)
11032
copy_func_count+= param->sum_func_count;
11034
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11036
if (!multi_alloc_root(&own_root,
11037
&table, sizeof(*table),
11038
&share, sizeof(*share),
11039
®_field, sizeof(Field*) * (field_count+1),
11040
&default_field, sizeof(Field*) * (field_count),
11041
&blob_field, sizeof(uint)*(field_count+1),
11042
&from_field, sizeof(Field*)*field_count,
11043
©_func, sizeof(*copy_func)*(copy_func_count+1),
11044
¶m->keyinfo, sizeof(*param->keyinfo),
11046
sizeof(*key_part_info)*(param->group_parts+1),
11047
¶m->start_recinfo,
11048
sizeof(*param->recinfo)*(field_count*2+4),
11049
&tmpname, (uint) strlen(path)+1,
11050
&group_buff, (group && ! using_unique_constraint ?
11051
param->group_length : 0),
11052
&bitmaps, bitmap_buffer_size(field_count)*2,
11055
if (temp_pool_slot != MY_BIT_NONE)
11056
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11057
return(NULL); /* purecov: inspected */
11059
/* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11060
if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11062
if (temp_pool_slot != MY_BIT_NONE)
11063
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11064
free_root(&own_root, MYF(0)); /* purecov: inspected */
11065
return(NULL); /* purecov: inspected */
11067
param->items_to_copy= copy_func;
11068
strmov(tmpname,path);
11069
/* make table according to fields */
11071
memset((char*) table, 0, sizeof(*table));
11072
memset((char*) reg_field, 0, sizeof(Field*)*(field_count+1));
11073
memset((char*) default_field, 0, sizeof(Field*) * (field_count));
11074
memset((char*) from_field, 0, sizeof(Field*)*field_count);
11076
table->mem_root= own_root;
11077
mem_root_save= thd->mem_root;
11078
thd->mem_root= &table->mem_root;
11080
table->field=reg_field;
11081
table->alias= table_alias;
11082
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11083
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11085
table->temp_pool_slot = temp_pool_slot;
11086
table->copy_blobs= 1;
11087
table->in_use= thd;
11088
table->quick_keys.init();
11089
table->covering_keys.init();
11090
table->keys_in_use_for_query.init();
11093
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11094
share->blob_field= blob_field;
11095
share->blob_ptr_size= portable_sizeof_char_ptr;
11096
share->db_low_byte_first=1; // True for HEAP and MyISAM
11097
share->table_charset= param->table_charset;
11098
share->primary_key= MAX_KEY; // Indicate no primary key
11099
share->keys_for_keyread.init();
11100
share->keys_in_use.init();
11102
/* Calculate which type of fields we will store in the temporary table */
11104
reclength= string_total_length= 0;
11105
blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11106
param->using_indirect_summary_function=0;
11108
List_iterator_fast<Item> li(fields);
11110
Field **tmp_from_field=from_field;
11111
while ((item=li++))
11113
Item::Type type=item->type();
11114
if (not_all_columns)
11116
if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11118
if (item->used_tables() & OUTER_REF_TABLE_BIT)
11119
item->update_used_tables();
11120
if (type == Item::SUBSELECT_ITEM ||
11121
(item->used_tables() & ~OUTER_REF_TABLE_BIT))
11124
Mark that the we have ignored an item that refers to a summary
11125
function. We need to know this if someone is going to use
11126
DISTINCT on the result.
11128
param->using_indirect_summary_function=1;
11132
if (item->const_item() && (int) hidden_field_count <= 0)
11133
continue; // We don't have to store this
11135
if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11136
{ /* Can't calc group yet */
11137
((Item_sum*) item)->result_field=0;
11138
for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11140
Item **argp= ((Item_sum*) item)->args + i;
11142
if (!arg->const_item())
11145
create_tmp_field(thd, table, arg, arg->type(), ©_func,
11146
tmp_from_field, &default_field[fieldnr],
11147
group != 0,not_all_columns,
11149
param->convert_blob_length);
11151
goto err; // Should be OOM
11153
reclength+=new_field->pack_length();
11154
if (new_field->flags & BLOB_FLAG)
11156
*blob_field++= fieldnr;
11159
*(reg_field++)= new_field;
11160
if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
11163
string_total_length+= new_field->pack_length();
11165
thd->mem_root= mem_root_save;
11166
thd->change_item_tree(argp, new Item_field(new_field));
11167
thd->mem_root= &table->mem_root;
11168
if (!(new_field->flags & NOT_NULL_FLAG))
11172
new_field->maybe_null() is still false, it will be
11173
changed below. But we have to setup Item_field correctly
11175
(*argp)->maybe_null=1;
11177
new_field->field_index= fieldnr++;
11184
The last parameter to create_tmp_field() is a bit tricky:
11186
We need to set it to 0 in union, to get fill_record() to modify the
11188
We need to set it to 1 on multi-table-update and in select to
11189
write rows to the temporary table.
11190
We here distinguish between UNION and multi-table-updates by the fact
11191
that in the later case group is set to the row pointer.
11193
Field *new_field= (param->schema_table) ?
11194
create_tmp_field_for_schema(thd, item, table) :
11195
create_tmp_field(thd, table, item, type, ©_func,
11196
tmp_from_field, &default_field[fieldnr],
11198
!force_copy_fields &&
11199
(not_all_columns || group !=0),
11201
If item->marker == 4 then we force create_tmp_field
11202
to create a 64-bit longs for BIT fields because HEAP
11203
tables can't index BIT fields directly. We do the same
11204
for distinct, as we want the distinct index to be
11205
usable in this case too.
11207
item->marker == 4 || param->bit_fields_as_long,
11209
param->convert_blob_length);
11213
if (thd->is_fatal_error)
11214
goto err; // Got OOM
11215
continue; // Some kindf of const item
11217
if (type == Item::SUM_FUNC_ITEM)
11218
((Item_sum *) item)->result_field= new_field;
11220
reclength+=new_field->pack_length();
11221
if (!(new_field->flags & NOT_NULL_FLAG))
11223
if (new_field->flags & BLOB_FLAG)
11225
*blob_field++= fieldnr;
11228
if (item->marker == 4 && item->maybe_null)
11230
group_null_items++;
11231
new_field->flags|= GROUP_FLAG;
11233
new_field->field_index= fieldnr++;
11234
*(reg_field++)= new_field;
11236
if (!--hidden_field_count)
11239
This was the last hidden field; Remember how many hidden fields could
11242
hidden_null_count=null_count;
11244
We need to update hidden_field_count as we may have stored group
11245
functions with constant arguments
11247
param->hidden_field_count= fieldnr;
11251
assert(fieldnr == (uint) (reg_field - table->field));
11252
assert(field_count >= (uint) (reg_field - table->field));
11253
field_count= fieldnr;
11255
*blob_field= 0; // End marker
11256
share->fields= field_count;
11258
/* If result table is small; use a heap */
11259
/* future: storage engine selection can be made dynamic? */
11260
if (blob_count || using_unique_constraint ||
11261
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11262
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11264
share->db_plugin= ha_lock_engine(0, myisam_hton);
11265
table->file= get_new_handler(share, &table->mem_root,
11268
(param->group_parts > table->file->max_key_parts() ||
11269
param->group_length > table->file->max_key_length()))
11270
using_unique_constraint=1;
11274
share->db_plugin= ha_lock_engine(0, heap_hton);
11275
table->file= get_new_handler(share, &table->mem_root,
11282
if (!using_unique_constraint)
11283
reclength+= group_null_items; // null flag is stored separately
11285
share->blob_fields= blob_count;
11286
if (blob_count == 0)
11288
/* We need to ensure that first byte is not 0 for the delete link */
11289
if (param->hidden_field_count)
11290
hidden_null_count++;
11294
hidden_null_pack_length=(hidden_null_count+7)/8;
11295
null_pack_length= (hidden_null_pack_length +
11296
(null_count + total_uneven_bit_length + 7) / 8);
11297
reclength+=null_pack_length;
11299
reclength=1; // Dummy select
11300
/* Use packed rows if there is blobs or a lot of space to gain */
11301
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)))
11302
use_packed_rows= 1;
11304
share->reclength= reclength;
11306
uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11307
share->rec_buff_length= alloc_length;
11308
if (!(table->record[0]= (uchar*)
11309
alloc_root(&table->mem_root, alloc_length*3)))
11311
table->record[1]= table->record[0]+alloc_length;
11312
share->default_values= table->record[1]+alloc_length;
11314
copy_func[0]=0; // End marker
11315
param->func_count= copy_func - param->items_to_copy;
11317
setup_tmp_table_column_bitmaps(table, bitmaps);
11319
recinfo=param->start_recinfo;
11320
null_flags=(uchar*) table->record[0];
11321
pos=table->record[0]+ null_pack_length;
11322
if (null_pack_length)
11324
memset((uchar*) recinfo, 0, sizeof(*recinfo));
11325
recinfo->type=FIELD_NORMAL;
11326
recinfo->length=null_pack_length;
11328
memset(null_flags, 255, null_pack_length); // Set null fields
11330
table->null_flags= (uchar*) table->record[0];
11331
share->null_fields= null_count+ hidden_null_count;
11332
share->null_bytes= null_pack_length;
11334
null_count= (blob_count == 0) ? 1 : 0;
11335
hidden_field_count=param->hidden_field_count;
11336
for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11338
Field *field= *reg_field;
11340
memset((uchar*) recinfo, 0, sizeof(*recinfo));
11342
if (!(field->flags & NOT_NULL_FLAG))
11344
if (field->flags & GROUP_FLAG && !using_unique_constraint)
11347
We have to reserve one byte here for NULL bits,
11348
as this is updated by 'end_update()'
11350
*pos++=0; // Null is stored here
11352
recinfo->type=FIELD_NORMAL;
11354
memset((uchar*) recinfo, 0, sizeof(*recinfo));
11358
recinfo->null_bit= 1 << (null_count & 7);
11359
recinfo->null_pos= null_count/8;
11361
field->move_field(pos,null_flags+null_count/8,
11362
1 << (null_count & 7));
11366
field->move_field(pos,(uchar*) 0,0);
11370
Test if there is a default field value. The test for ->ptr is to skip
11371
'offset' fields generated by initalize_tables
11373
if (default_field[i] && default_field[i]->ptr)
11376
default_field[i] is set only in the cases when 'field' can
11377
inherit the default value that is defined for the field referred
11378
by the Item_field object from which 'field' has been created.
11381
Field *orig_field= default_field[i];
11382
/* Get the value from default_values */
11383
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11384
orig_field->table->record[0]);
11385
orig_field->move_field_offset(diff); // Points now at default_values
11386
if (orig_field->is_real_null())
11390
field->set_notnull();
11391
memcpy(field->ptr, orig_field->ptr, field->pack_length());
11393
orig_field->move_field_offset(-diff); // Back to record[0]
11397
{ /* Not a table Item */
11398
copy->set(field,from_field[i],save_sum_fields);
11401
length=field->pack_length();
11404
/* Make entry for create table */
11405
recinfo->length=length;
11406
if (field->flags & BLOB_FLAG)
11407
recinfo->type= (int) FIELD_BLOB;
11409
recinfo->type=FIELD_NORMAL;
11410
if (!--hidden_field_count)
11411
null_count=(null_count+7) & ~7; // move to next byte
11413
// fix table name in field entry
11414
field->table_name= &table->alias;
11417
param->copy_field_end=copy;
11418
param->recinfo=recinfo;
11419
store_record(table,s->default_values); // Make empty default record
11421
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11422
share->max_rows= ~(ha_rows) 0;
11424
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11425
min(thd->variables.tmp_table_size,
11426
thd->variables.max_heap_table_size) :
11427
thd->variables.tmp_table_size) /
11429
set_if_bigger(share->max_rows,1); // For dummy start options
11431
Push the LIMIT clause to the temporary table creation, so that we
11432
materialize only up to 'rows_limit' records instead of all result records.
11434
set_if_smaller(share->max_rows, rows_limit);
11435
param->end_write_records= rows_limit;
11437
keyinfo= param->keyinfo;
11441
table->group=group; /* Table is grouped by key */
11442
param->group_buff=group_buff;
11444
share->uniques= test(using_unique_constraint);
11445
table->key_info=keyinfo;
11446
keyinfo->key_part=key_part_info;
11447
keyinfo->flags=HA_NOSAME;
11448
keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11449
keyinfo->key_length=0;
11450
keyinfo->rec_per_key=0;
11451
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11452
keyinfo->name= (char*) "group_key";
11453
ORDER *cur_group= group;
11454
for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11456
Field *field=(*cur_group->item)->get_tmp_table_field();
11457
bool maybe_null=(*cur_group->item)->maybe_null;
11458
key_part_info->null_bit=0;
11459
key_part_info->field= field;
11460
key_part_info->offset= field->offset(table->record[0]);
11461
key_part_info->length= (uint16_t) field->key_length();
11462
key_part_info->type= (uint8_t) field->key_type();
11463
key_part_info->key_type =
11464
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11465
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11466
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11467
0 : FIELDFLAG_BINARY;
11468
if (!using_unique_constraint)
11470
cur_group->buff=(char*) group_buff;
11471
if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11476
goto err; /* purecov: inspected */
11480
To be able to group on NULL, we reserved place in group_buff
11481
for the NULL flag just before the column. (see above).
11482
The field data is after this flag.
11483
The NULL flag is updated in 'end_update()' and 'end_write()'
11485
keyinfo->flags|= HA_NULL_ARE_EQUAL; // def. that NULL == NULL
11486
key_part_info->null_bit=field->null_bit;
11487
key_part_info->null_offset= (uint) (field->null_ptr -
11488
(uchar*) table->record[0]);
11489
cur_group->buff++; // Pointer to field data
11490
group_buff++; // Skipp null flag
11492
/* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11493
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11494
group_buff+= cur_group->field->pack_length();
11496
keyinfo->key_length+= key_part_info->length;
11500
if (distinct && field_count != param->hidden_field_count)
11503
Create an unique key or an unique constraint over all columns
11504
that should be in the result. In the temporary table, there are
11505
'param->hidden_field_count' extra columns, whose null bits are stored
11506
in the first 'hidden_null_pack_length' bytes of the row.
11511
Special mode for index creation in MyISAM used to support unique
11512
indexes on blobs with arbitrary length. Such indexes cannot be
11517
null_pack_length-=hidden_null_pack_length;
11518
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11519
(share->uniques ? test(null_pack_length) : 0));
11520
table->distinct= 1;
11522
if (!(key_part_info= (KEY_PART_INFO*)
11523
alloc_root(&table->mem_root,
11524
keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11526
memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11527
table->key_info=keyinfo;
11528
keyinfo->key_part=key_part_info;
11529
keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11530
keyinfo->key_length=(uint16_t) reclength;
11531
keyinfo->name= (char*) "distinct_key";
11532
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11533
keyinfo->rec_per_key=0;
11536
Create an extra field to hold NULL bits so that unique indexes on
11537
blobs can distinguish NULL from 0. This extra field is not needed
11538
when we do not use UNIQUE indexes for blobs.
11540
if (null_pack_length && share->uniques)
11542
key_part_info->null_bit=0;
11543
key_part_info->offset=hidden_null_pack_length;
11544
key_part_info->length=null_pack_length;
11545
key_part_info->field= new Field_varstring(table->record[0],
11546
(uint32_t) key_part_info->length,
11554
if (!key_part_info->field)
11556
key_part_info->field->init(table);
11557
key_part_info->key_type=FIELDFLAG_BINARY;
11558
key_part_info->type= HA_KEYTYPE_BINARY;
11561
/* Create a distinct key over the columns we are going to return */
11562
for (i=param->hidden_field_count, reg_field=table->field + i ;
11564
i++, reg_field++, key_part_info++)
11566
key_part_info->null_bit=0;
11567
key_part_info->field= *reg_field;
11568
key_part_info->offset= (*reg_field)->offset(table->record[0]);
11569
key_part_info->length= (uint16_t) (*reg_field)->pack_length();
11571
The below method of computing the key format length of the
11572
key part is a copy/paste from opt_range.cc, and table.cc.
11573
This should be factored out, e.g. as a method of Field.
11574
In addition it is not clear if any of the Field::*_length
11575
methods is supposed to compute the same length. If so, it
11578
key_part_info->store_length= key_part_info->length;
11580
if ((*reg_field)->real_maybe_null())
11581
key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11582
if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB ||
11583
(*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
11584
key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11586
key_part_info->type= (uint8_t) (*reg_field)->key_type();
11587
key_part_info->key_type =
11588
((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11589
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11590
(ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11591
0 : FIELDFLAG_BINARY;
11595
if (thd->is_fatal_error) // If end of memory
11596
goto err; /* purecov: inspected */
11597
share->db_record_offset= 1;
11598
if (share->db_type() == myisam_hton)
11600
if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11601
¶m->recinfo, select_options))
11604
if (open_tmp_table(table))
11607
thd->mem_root= mem_root_save;
11612
thd->mem_root= mem_root_save;
11613
free_tmp_table(thd,table); /* purecov: inspected */
11614
if (temp_pool_slot != MY_BIT_NONE)
11615
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11616
return(NULL); /* purecov: inspected */
11623
Create a temporary table to weed out duplicate rowid combinations
11627
create_duplicate_weedout_tmp_table()
11629
uniq_tuple_length_arg
11633
Create a temporary table to weed out duplicate rowid combinations. The
11634
table has a single column that is a concatenation of all rowids in the
11637
Depending on the needed length, there are two cases:
11639
1. When the length of the column < max_key_length:
11641
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11643
2. Otherwise (not a valid SQL syntax but internally supported):
11645
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11647
The code in this function was produced by extraction of relevant parts
11648
from create_tmp_table().
11655
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
11656
uint uniq_tuple_length_arg,
11657
SJ_TMP_TABLE *sjtbl)
11659
MEM_ROOT *mem_root_save, own_root;
11661
TABLE_SHARE *share;
11662
uint temp_pool_slot=MY_BIT_NONE;
11663
char *tmpname,path[FN_REFLEN];
11665
KEY_PART_INFO *key_part_info;
11670
MI_COLUMNDEF *recinfo, *start_recinfo;
11671
bool using_unique_constraint=false;
11672
Field *field, *key_field;
11673
uint blob_count, null_pack_length, null_count;
11678
STEP 1: Get temporary table name
11680
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11681
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11682
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11684
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11685
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11686
current_pid, temp_pool_slot);
11689
/* if we run out of slots or we are not using tempool */
11690
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11691
thd->thread_id, thd->tmp_table++);
11693
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11695
/* STEP 2: Figure if we'll be using a key or blob+constraint */
11696
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11697
using_unique_constraint= true;
11699
/* STEP 3: Allocate memory for temptable description */
11700
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11701
if (!multi_alloc_root(&own_root,
11702
&table, sizeof(*table),
11703
&share, sizeof(*share),
11704
®_field, sizeof(Field*) * (1+1),
11705
&blob_field, sizeof(uint)*2,
11706
&keyinfo, sizeof(*keyinfo),
11707
&key_part_info, sizeof(*key_part_info) * 2,
11709
sizeof(*recinfo)*(1*2+4),
11710
&tmpname, (uint) strlen(path)+1,
11711
&group_buff, (!using_unique_constraint ?
11712
uniq_tuple_length_arg : 0),
11713
&bitmaps, bitmap_buffer_size(1)*2,
11716
if (temp_pool_slot != MY_BIT_NONE)
11717
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11720
strmov(tmpname,path);
11723
/* STEP 4: Create TABLE description */
11724
memset((char*) table, 0, sizeof(*table));
11725
memset((char*) reg_field, 0, sizeof(Field*)*2);
11727
table->mem_root= own_root;
11728
mem_root_save= thd->mem_root;
11729
thd->mem_root= &table->mem_root;
11731
table->field=reg_field;
11732
table->alias= "weedout-tmp";
11733
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
11734
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11736
table->temp_pool_slot = temp_pool_slot;
11737
table->copy_blobs= 1;
11738
table->in_use= thd;
11739
table->quick_keys.init();
11740
table->covering_keys.init();
11741
table->keys_in_use_for_query.init();
11744
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11745
share->blob_field= blob_field;
11746
share->blob_ptr_size= portable_sizeof_char_ptr;
11747
share->db_low_byte_first=1; // True for HEAP and MyISAM
11748
share->table_charset= NULL;
11749
share->primary_key= MAX_KEY; // Indicate no primary key
11750
share->keys_for_keyread.init();
11751
share->keys_in_use.init();
11755
/* Create the field */
11758
For the sake of uniformity, always use Field_varstring.
11760
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11764
field->table= table;
11765
field->key_start.init(0);
11766
field->part_of_key.init(0);
11767
field->part_of_sortkey.init(0);
11768
field->unireg_check= Field::NONE;
11769
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11770
field->reset_fields();
11771
field->init(table);
11772
field->orig_table= NULL;
11774
field->field_index= 0;
11776
*(reg_field++)= field;
11781
share->blob_fields= 0;
11784
uint reclength= field->pack_length();
11785
if (using_unique_constraint)
11787
share->db_plugin= ha_lock_engine(0, myisam_hton);
11788
table->file= get_new_handler(share, &table->mem_root,
11790
assert(uniq_tuple_length_arg <= table->file->max_key_length());
11794
share->db_plugin= ha_lock_engine(0, heap_hton);
11795
table->file= get_new_handler(share, &table->mem_root,
11803
null_pack_length= 1;
11804
reclength += null_pack_length;
11806
share->reclength= reclength;
11808
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11809
share->rec_buff_length= alloc_length;
11810
if (!(table->record[0]= (uchar*)
11811
alloc_root(&table->mem_root, alloc_length*3)))
11813
table->record[1]= table->record[0]+alloc_length;
11814
share->default_values= table->record[1]+alloc_length;
11816
setup_tmp_table_column_bitmaps(table, bitmaps);
11818
recinfo= start_recinfo;
11819
null_flags=(uchar*) table->record[0];
11820
pos=table->record[0]+ null_pack_length;
11821
if (null_pack_length)
11823
memset((uchar*) recinfo, 0, sizeof(*recinfo));
11824
recinfo->type=FIELD_NORMAL;
11825
recinfo->length=null_pack_length;
11827
memset(null_flags, 255, null_pack_length); // Set null fields
11829
table->null_flags= (uchar*) table->record[0];
11830
share->null_fields= null_count;
11831
share->null_bytes= null_pack_length;
11836
//Field *field= *reg_field;
11838
memset((uchar*) recinfo, 0, sizeof(*recinfo));
11839
field->move_field(pos,(uchar*) 0,0);
11843
Test if there is a default field value. The test for ->ptr is to skip
11844
'offset' fields generated by initalize_tables
11846
// Initialize the table field:
11847
memset(field->ptr, 0, field->pack_length());
11849
length=field->pack_length();
11852
/* Make entry for create table */
11853
recinfo->length=length;
11854
if (field->flags & BLOB_FLAG)
11855
recinfo->type= (int) FIELD_BLOB;
11857
recinfo->type=FIELD_NORMAL;
11859
field->table_name= &table->alias;
11862
//param->recinfo=recinfo;
11863
//store_record(table,s->default_values); // Make empty default record
11865
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
11866
share->max_rows= ~(ha_rows) 0;
11868
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11869
min(thd->variables.tmp_table_size,
11870
thd->variables.max_heap_table_size) :
11871
thd->variables.tmp_table_size) /
11873
set_if_bigger(share->max_rows,1); // For dummy start options
11876
//// keyinfo= param->keyinfo;
11880
share->uniques= test(using_unique_constraint);
11881
table->key_info=keyinfo;
11882
keyinfo->key_part=key_part_info;
11883
keyinfo->flags=HA_NOSAME;
11884
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11885
keyinfo->key_length=0;
11886
keyinfo->rec_per_key=0;
11887
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11888
keyinfo->name= (char*) "weedout_key";
11890
key_part_info->null_bit=0;
11891
key_part_info->field= field;
11892
key_part_info->offset= field->offset(table->record[0]);
11893
key_part_info->length= (uint16_t) field->key_length();
11894
key_part_info->type= (uint8_t) field->key_type();
11895
key_part_info->key_type = FIELDFLAG_BINARY;
11896
if (!using_unique_constraint)
11898
if (!(key_field= field->new_key_field(thd->mem_root, table,
11903
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11905
keyinfo->key_length+= key_part_info->length;
11909
if (thd->is_fatal_error) // If end of memory
11911
share->db_record_offset= 1;
11912
if (share->db_type() == myisam_hton)
11915
if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11918
sjtbl->start_recinfo= start_recinfo;
11919
sjtbl->recinfo= recinfo;
11920
if (open_tmp_table(table))
11923
thd->mem_root= mem_root_save;
11927
thd->mem_root= mem_root_save;
11928
free_tmp_table(thd,table); /* purecov: inspected */
11929
if (temp_pool_slot != MY_BIT_NONE)
11930
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11931
return(NULL); /* purecov: inspected */
11934
/****************************************************************************/
11937
Create a reduced TABLE object with properly set up Field list from a
11938
list of field definitions.
11940
The created table doesn't have a table handler associated with
11941
it, has no keys, no group/distinct, no copy_funcs array.
11942
The sole purpose of this TABLE object is to use the power of Field
11943
class to read/write data to/from table->record[0]. Then one can store
11944
the record in any container (RB tree, hash, etc).
11945
The table is created in THD mem_root, so are the table's fields.
11946
Consequently, if you don't BLOB fields, you don't need to free it.
11948
@param thd connection handle
11949
@param field_list list of column definitions
11952
0 if out of memory, TABLE object in case of success
11955
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11957
uint field_count= field_list.elements;
11958
uint blob_count= 0;
11960
Create_field *cdef; /* column definition */
11961
uint record_length= 0;
11962
uint null_count= 0; /* number of columns which may be null */
11963
uint null_pack_length; /* NULL representation array length */
11967
TABLE_SHARE *share;
11969
if (!multi_alloc_root(thd->mem_root,
11970
&table, sizeof(*table),
11971
&share, sizeof(*share),
11972
&field, (field_count + 1) * sizeof(Field*),
11973
&blob_field, (field_count+1) *sizeof(uint),
11974
&bitmaps, bitmap_buffer_size(field_count)*2,
11978
memset(table, 0, sizeof(*table));
11979
memset(share, 0, sizeof(*share));
11980
table->field= field;
11982
share->blob_field= blob_field;
11983
share->fields= field_count;
11984
share->blob_ptr_size= portable_sizeof_char_ptr;
11985
setup_tmp_table_column_bitmaps(table, bitmaps);
11987
/* Create all fields and calculate the total length of record */
11988
List_iterator_fast<Create_field> it(field_list);
11989
while ((cdef= it++))
11991
*field= make_field(share, 0, cdef->length,
11992
(uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
11993
f_maybe_null(cdef->pack_flag) ? 1 : 0,
11994
cdef->pack_flag, cdef->sql_type, cdef->charset,
11995
cdef->unireg_check,
11996
cdef->interval, cdef->field_name);
11999
(*field)->init(table);
12000
record_length+= (*field)->pack_length();
12001
if (! ((*field)->flags & NOT_NULL_FLAG))
12004
if ((*field)->flags & BLOB_FLAG)
12005
share->blob_field[blob_count++]= (uint) (field - table->field);
12009
*field= NULL; /* mark the end of the list */
12010
share->blob_field[blob_count]= 0; /* mark the end of the list */
12011
share->blob_fields= blob_count;
12013
null_pack_length= (null_count + 7)/8;
12014
share->reclength= record_length + null_pack_length;
12015
share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12016
table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12017
if (!table->record[0])
12020
if (null_pack_length)
12022
table->null_flags= (uchar*) table->record[0];
12023
share->null_fields= null_count;
12024
share->null_bytes= null_pack_length;
12027
table->in_use= thd; /* field->reset() may access table->in_use */
12029
/* Set up field pointers */
12030
uchar *null_pos= table->record[0];
12031
uchar *field_pos= null_pos + share->null_bytes;
12034
for (field= table->field; *field; ++field)
12036
Field *cur_field= *field;
12037
if ((cur_field->flags & NOT_NULL_FLAG))
12038
cur_field->move_field(field_pos);
12041
cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12043
if (null_bit == (1 << 8))
12049
cur_field->reset();
12051
field_pos+= cur_field->pack_length();
12056
for (field= table->field; *field; ++field)
12057
delete *field; /* just invokes field destructor */
12062
static bool open_tmp_table(TABLE *table)
12065
if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12066
HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12068
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12072
(void) table->file->extra(HA_EXTRA_QUICK); /* Faster */
12078
Create MyISAM temporary table
12081
create_myisam_tmp_table()
12082
table Table object that descrimes the table to be created
12083
keyinfo Description of the index (there is always one index)
12084
start_recinfo MyISAM's column descriptions
12085
recinfo INOUT End of MyISAM's column descriptions
12086
options Option bits
12089
Create a MyISAM temporary table according to passed description. The is
12090
assumed to have one unique index or constraint.
12092
The passed array or MI_COLUMNDEF structures must have this form:
12094
1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12095
when there are many nullable columns)
12097
3. One free MI_COLUMNDEF element (*recinfo points here)
12099
This function may use the free element to create hash column for unique
12107
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo,
12108
MI_COLUMNDEF *start_recinfo,
12109
MI_COLUMNDEF **recinfo,
12114
MI_UNIQUEDEF uniquedef;
12115
TABLE_SHARE *share= table->s;
12118
{ // Get keys for ni_create
12119
bool using_unique_constraint=0;
12120
HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12121
sizeof(*seg) * keyinfo->key_parts);
12125
memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
12126
if (keyinfo->key_length >= table->file->max_key_length() ||
12127
keyinfo->key_parts > table->file->max_key_parts() ||
12130
/* Can't create a key; Make a unique constraint instead of a key */
12133
using_unique_constraint=1;
12134
memset((char*) &uniquedef, 0, sizeof(uniquedef));
12135
uniquedef.keysegs=keyinfo->key_parts;
12137
uniquedef.null_are_equal=1;
12139
/* Create extra column for hash value */
12140
memset((uchar*) *recinfo, 0, sizeof(**recinfo));
12141
(*recinfo)->type= FIELD_CHECK;
12142
(*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12144
share->reclength+=MI_UNIQUE_HASH_LENGTH;
12148
/* Create an unique key */
12149
memset((char*) &keydef, 0, sizeof(keydef));
12150
keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12151
keydef.keysegs= keyinfo->key_parts;
12154
for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12156
Field *field=keyinfo->key_part[i].field;
12158
seg->language= field->charset()->number;
12159
seg->length= keyinfo->key_part[i].length;
12160
seg->start= keyinfo->key_part[i].offset;
12161
if (field->flags & BLOB_FLAG)
12164
((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12165
HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12166
seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
12167
seg->flag= HA_BLOB_PART;
12168
seg->length=0; // Whole blob in unique constraint
12172
seg->type= keyinfo->key_part[i].type;
12174
if (!(field->flags & NOT_NULL_FLAG))
12176
seg->null_bit= field->null_bit;
12177
seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12179
We are using a GROUP BY on something that contains NULL
12180
In this case we have to tell MyISAM that two NULL should
12181
on INSERT be regarded at the same value
12183
if (!using_unique_constraint)
12184
keydef.flag|= HA_NULL_ARE_EQUAL;
12188
MI_CREATE_INFO create_info;
12189
memset((char*) &create_info, 0, sizeof(create_info));
12191
if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12193
create_info.data_file_length= ~(uint64_t) 0;
12195
if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12196
(uint) (*recinfo-start_recinfo),
12198
share->uniques, &uniquedef,
12200
HA_CREATE_TMP_TABLE)))
12202
table->file->print_error(error,MYF(0)); /* purecov: inspected */
12206
status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12207
share->db_record_offset= 1;
12215
free_tmp_table(THD *thd, TABLE *entry)
12217
MEM_ROOT own_root= entry->mem_root;
12218
const char *save_proc_info;
12220
save_proc_info=thd->proc_info;
12221
thd_proc_info(thd, "removing tmp table");
12225
if (entry->db_stat)
12226
entry->file->ha_drop_table(entry->s->table_name.str);
12228
entry->file->ha_delete_table(entry->s->table_name.str);
12229
delete entry->file;
12233
for (Field **ptr=entry->field ; *ptr ; ptr++)
12235
free_io_cache(entry);
12237
if (entry->temp_pool_slot != MY_BIT_NONE)
12238
bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12240
plugin_unlock(0, entry->s->db_plugin);
12242
free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12243
thd_proc_info(thd, save_proc_info);
12249
If a HEAP table gets full, create a MyISAM table and copy all rows
12253
bool create_myisam_from_heap(THD *thd, TABLE *table,
12254
MI_COLUMNDEF *start_recinfo,
12255
MI_COLUMNDEF **recinfo,
12256
int error, bool ignore_last_dupp_key_error)
12260
const char *save_proc_info;
12263
if (table->s->db_type() != heap_hton ||
12264
error != HA_ERR_RECORD_FILE_FULL)
12266
table->file->print_error(error,MYF(0));
12271
new_table.s= &share;
12272
new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12273
if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12274
new_table.s->db_type())))
12275
return(1); // End of memory
12277
save_proc_info=thd->proc_info;
12278
thd_proc_info(thd, "converting HEAP to MyISAM");
12280
if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12281
recinfo, thd->lex->select_lex.options |
12284
if (open_tmp_table(&new_table))
12286
if (table->file->indexes_are_disabled())
12287
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12288
table->file->ha_index_or_rnd_end();
12289
table->file->ha_rnd_init(1);
12290
if (table->no_rows)
12292
new_table.file->extra(HA_EXTRA_NO_ROWS);
12293
new_table.no_rows=1;
12296
#ifdef TO_BE_DONE_LATER_IN_4_1
12298
To use start_bulk_insert() (which is new in 4.1) we need to find
12299
all places where a corresponding end_bulk_insert() should be put.
12301
table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12302
new_table.file->ha_start_bulk_insert(table->file->stats.records);
12304
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12305
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12309
copy all old rows from heap table to MyISAM table
12310
This is the only code that uses record[1] to read/write but this
12311
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12313
while (!table->file->rnd_next(new_table.record[1]))
12315
write_err= new_table.file->ha_write_row(new_table.record[1]);
12319
/* copy row that filled HEAP table */
12320
if ((write_err=new_table.file->ha_write_row(table->record[0])))
12322
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12323
!ignore_last_dupp_key_error)
12327
/* remove heap table and change to use myisam table */
12328
(void) table->file->ha_rnd_end();
12329
(void) table->file->close(); // This deletes the table !
12330
delete table->file;
12332
plugin_unlock(0, table->s->db_plugin);
12333
share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12334
new_table.s= table->s; // Keep old share
12338
table->file->change_table_ptr(table, table->s);
12339
table->use_all_columns();
12340
if (save_proc_info)
12342
const char *new_proc_info=
12343
(!strcmp(save_proc_info,"Copying to tmp table") ?
12344
"Copying to tmp table on disk" : save_proc_info);
12345
thd_proc_info(thd, new_proc_info);
12350
table->file->print_error(write_err, MYF(0));
12351
(void) table->file->ha_rnd_end();
12352
(void) new_table.file->close();
12354
new_table.file->ha_delete_table(new_table.s->table_name.str);
12356
delete new_table.file;
12357
thd_proc_info(thd, save_proc_info);
12358
table->mem_root= new_table.mem_root;
3362
int safe_index_read(JoinTable *tab)
3365
Table *table= tab->table;
3366
if ((error=table->cursor->index_read_map(table->getInsertRecord(),
12766
SemiJoinDuplicateElimination: Weed out duplicate row combinations
12769
do_sj_dups_weedout()
12773
1 The row combination is a duplicate (discard it)
12774
0 The row combination is not a duplicate (continue)
12777
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl)
12780
SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
12781
SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
12782
uchar *ptr= sjtbl->tmp_table->record[0] + 1;
12783
uchar *nulls_ptr= ptr;
12785
/* Put the the rowids tuple into table->record[0]: */
12787
// 1. Store the length
12788
if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
12790
*ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
12795
int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
12799
// 2. Zero the null bytes
12800
if (sjtbl->null_bytes)
12802
memset(ptr, 0, sjtbl->null_bytes);
12803
ptr += sjtbl->null_bytes;
12806
// 3. Put the rowids
12807
for (uint i=0; tab != tab_end; tab++, i++)
12809
handler *h= tab->join_tab->table->file;
12810
if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
12812
/* It's a NULL-complemented row */
12813
*(nulls_ptr + tab->null_byte) |= tab->null_bit;
12814
memset(ptr + tab->rowid_offset, 0, h->ref_length);
12818
/* Copy the rowid value */
12819
if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
12820
h->position(tab->join_tab->table->record[0]);
12821
memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
12825
error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
12828
/* create_myisam_from_heap will generate error if needed */
12829
if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
12830
create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo,
12831
&sjtbl->recinfo, error, 1))
12833
//return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
12841
SemiJoinDuplicateElimination: Reset the temporary table
12844
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
12846
if (sj_tbl->tmp_table)
12847
return sj_tbl->tmp_table->file->ha_delete_all_rows();
12852
Process one record of the nested loop join.
12854
This function will evaluate parts of WHERE/ON clauses that are
12855
applicable to the partial record on hand and in case of success
12856
submit this record to the next level of the nested loop.
12859
static enum_nested_loop_state
12860
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
12863
bool not_used_in_distinct=join_tab->not_used_in_distinct;
12864
ha_rows found_records=join->found_records;
12865
COND *select_cond= join_tab->select_cond;
12867
if (error > 0 || (join->thd->is_error())) // Fatal error
12868
return NESTED_LOOP_ERROR;
12870
return NESTED_LOOP_NO_MORE_ROWS;
12871
if (join->thd->killed) // Aborted by user
12873
join->thd->send_kill_message();
12874
return NESTED_LOOP_KILLED; /* purecov: inspected */
12876
if (!select_cond || select_cond->val_int())
12879
There is no select condition or the attached pushed down
12880
condition is true => a match is found.
12883
while (join_tab->first_unmatched && found)
12886
The while condition is always false if join_tab is not
12887
the last inner join table of an outer join operation.
12889
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
12891
Mark that a match for current outer table is found.
12892
This activates push down conditional predicates attached
12893
to the all inner tables of the outer join.
12895
first_unmatched->found= 1;
12896
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
12898
if (tab->table->reginfo.not_exists_optimize)
12899
return NESTED_LOOP_NO_MORE_ROWS;
12900
/* Check all predicates that has just been activated. */
12902
Actually all predicates non-guarded by first_unmatched->found
12903
will be re-evaluated again. It could be fixed, but, probably,
12904
it's not worth doing now.
12906
if (tab->select_cond && !tab->select_cond->val_int())
12908
/* The condition attached to table tab is false */
12909
if (tab == join_tab)
12914
Set a return point if rejected predicate is attached
12915
not to the last table of the current nest level.
12917
join->return_tab= tab;
12918
return NESTED_LOOP_OK;
12923
Check whether join_tab is not the last inner table
12924
for another embedding outer join.
12926
if ((first_unmatched= first_unmatched->first_upper) &&
12927
first_unmatched->last_inner != join_tab)
12928
first_unmatched= 0;
12929
join_tab->first_unmatched= first_unmatched;
12932
JOIN_TAB *return_tab= join->return_tab;
12933
join_tab->found_match= true;
12934
if (join_tab->check_weed_out_table)
12936
int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
12938
return NESTED_LOOP_ERROR;
12940
return NESTED_LOOP_OK;
12942
else if (join_tab->do_firstmatch)
12945
We should return to the join_tab->do_firstmatch after we have
12946
enumerated all the suffixes for current prefix row combination
12948
return_tab= join_tab->do_firstmatch;
12952
It was not just a return to lower loop level when one
12953
of the newly activated predicates is evaluated as false
12954
(See above join->return_tab= tab).
12956
join->examined_rows++;
12957
join->thd->row_count++;
12961
enum enum_nested_loop_state rc;
12962
/* A match from join_tab is found for the current partial join. */
12963
rc= (*join_tab->next_select)(join, join_tab+1, 0);
12964
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
12966
if (return_tab < join->return_tab)
12967
join->return_tab= return_tab;
12969
if (join->return_tab < join_tab)
12970
return NESTED_LOOP_OK;
12972
Test if this was a SELECT DISTINCT query on a table that
12973
was not in the field list; In this case we can abort if
12974
we found a row, as no new rows can be added to the result.
12976
if (not_used_in_distinct && found_records != join->found_records)
12977
return NESTED_LOOP_NO_MORE_ROWS;
12980
join_tab->read_record.file->unlock_row();
12985
The condition pushed down to the table join_tab rejects all rows
12986
with the beginning coinciding with the current partial join.
12988
join->examined_rows++;
12989
join->thd->row_count++;
12990
join_tab->read_record.file->unlock_row();
12992
return NESTED_LOOP_OK;
12999
Construct a NULL complimented partial join record and feed it to the next
13000
level of the nested loop. This function is used in case we have
13001
an OUTER join and no matching record was found.
13004
static enum_nested_loop_state
13005
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
13008
The table join_tab is the first inner table of a outer join operation
13009
and no matches has been found for the current outer row.
13011
JOIN_TAB *last_inner_tab= join_tab->last_inner;
13012
/* Cache variables for faster loop */
13014
for ( ; join_tab <= last_inner_tab ; join_tab++)
13016
/* Change the the values of guard predicate variables. */
13017
join_tab->found= 1;
13018
join_tab->not_null_compl= 0;
13019
/* The outer row is complemented by nulls for each inner tables */
13020
restore_record(join_tab->table,s->default_values); // Make empty record
13021
mark_as_null_row(join_tab->table); // For group by without error
13022
select_cond= join_tab->select_cond;
13023
/* Check all attached conditions for inner table rows. */
13024
if (select_cond && !select_cond->val_int())
13025
return NESTED_LOOP_OK;
13029
The row complemented by nulls might be the first row
13030
of embedding outer joins.
13031
If so, perform the same actions as in the code
13032
for the first regular outer join row above.
13036
JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13037
if ((first_unmatched= first_unmatched->first_upper) &&
13038
first_unmatched->last_inner != join_tab)
13039
first_unmatched= 0;
13040
join_tab->first_unmatched= first_unmatched;
13041
if (!first_unmatched)
13043
first_unmatched->found= 1;
13044
for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13046
if (tab->select_cond && !tab->select_cond->val_int())
13048
join->return_tab= tab;
13049
return NESTED_LOOP_OK;
13054
The row complemented by nulls satisfies all conditions
13055
attached to inner tables.
13056
Send the row complemented by nulls to be joined with the
13059
return (*join_tab->next_select)(join, join_tab+1, 0);
13063
static enum_nested_loop_state
13064
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13066
enum_nested_loop_state rc= NESTED_LOOP_OK;
13070
join_tab->table->null_row= 0;
13071
if (!join_tab->cache.records)
13072
return NESTED_LOOP_OK; /* Nothing to do */
13074
(void) store_record_in_cache(&join_tab->cache); // Must save this for later
13075
if (join_tab->use_quick == 2)
13077
if (join_tab->select->quick)
13078
{ /* Used quick select last. reset it */
13079
delete join_tab->select->quick;
13080
join_tab->select->quick=0;
13083
/* read through all records */
13084
if ((error=join_init_read_record(join_tab)))
13086
reset_cache_write(&join_tab->cache);
13087
return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13090
for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13092
tmp->status=tmp->table->status;
13093
tmp->table->status=0;
13096
info= &join_tab->read_record;
13099
if (join->thd->killed)
13101
join->thd->send_kill_message();
13102
return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13104
SQL_SELECT *select=join_tab->select;
13105
if (rc == NESTED_LOOP_OK &&
13106
(!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13109
reset_cache_read(&join_tab->cache);
13110
for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13112
read_cached_record(join_tab);
13113
if (!select || !select->skip_record())
13116
if (!join_tab->check_weed_out_table ||
13117
!(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13119
rc= (join_tab->next_select)(join,join_tab+1,0);
13120
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13122
reset_cache_write(&join_tab->cache);
13127
return NESTED_LOOP_ERROR;
13131
} while (!(error=info->read_record(info)));
13134
read_cached_record(join_tab); // Restore current record
13135
reset_cache_write(&join_tab->cache);
13136
if (error > 0) // Fatal error
13137
return NESTED_LOOP_ERROR; /* purecov: inspected */
13138
for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13139
tmp2->table->status=tmp2->status;
13140
return NESTED_LOOP_OK;
13144
/*****************************************************************************
13145
The different ways to read a record
13146
Returns -1 if row was not found, 0 if row was found and 1 on errors
13147
*****************************************************************************/
13149
/** Help function when we get some an error from the table handler. */
13151
int report_error(TABLE *table, int error)
13153
if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13155
table->status= STATUS_GARBAGE;
13156
return -1; // key not found; ok
13159
Locking reads can legally return also these errors, do not
13160
print them to the .err log
13162
if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13163
sql_print_error("Got error %d when reading table '%s'",
13164
error, table->s->path.str);
13165
table->file->print_error(error,MYF(0));
13170
int safe_index_read(JOIN_TAB *tab)
13173
TABLE *table= tab->table;
13174
if ((error=table->file->index_read_map(table->record[0],
3367
13175
tab->ref.key_buff,
3368
13176
make_prev_keypart_map(tab->ref.key_parts),
3369
13177
HA_READ_KEY_EXACT)))
3370
return table->report_error(error);
13178
return report_error(table, error);
13184
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
13187
TABLE *table=tab->table;
13188
table->const_table=1;
13190
table->status=STATUS_NO_RECORD;
13192
if (tab->type == JT_SYSTEM)
13194
if ((error=join_read_system(tab)))
13195
{ // Info for DESCRIBE
13196
tab->info="const row not found";
13197
/* Mark for EXPLAIN that the row was not found */
13198
pos->records_read=0.0;
13199
pos->ref_depend_map= 0;
13200
if (!table->maybe_null || error > 0)
13206
if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13207
!table->no_keyread &&
13208
(int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
13211
table->file->extra(HA_EXTRA_KEYREAD);
13212
tab->index= tab->ref.key;
13214
error=join_read_const(tab);
13215
if (table->key_read)
13218
table->file->extra(HA_EXTRA_NO_KEYREAD);
13222
tab->info="unique row not found";
13223
/* Mark for EXPLAIN that the row was not found */
13224
pos->records_read=0.0;
13225
pos->ref_depend_map= 0;
13226
if (!table->maybe_null || error > 0)
13230
if (*tab->on_expr_ref && !table->null_row)
13232
if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
13233
mark_as_null_row(table);
13235
if (!table->null_row)
13236
table->maybe_null=0;
13238
/* Check appearance of new constant items in Item_equal objects */
13239
JOIN *join= tab->join;
13241
update_const_equal_items(join->conds, tab);
13243
for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
13245
TABLE_LIST *embedded;
13246
TABLE_LIST *embedding= tbl;
13249
embedded= embedding;
13250
if (embedded->on_expr)
13251
update_const_equal_items(embedded->on_expr, tab);
13252
embedding= embedded->embedding;
13254
while (embedding &&
13255
embedding->nested_join->join_list.head() == embedded);
13263
join_read_system(JOIN_TAB *tab)
13265
TABLE *table= tab->table;
13267
if (table->status & STATUS_GARBAGE) // If first read
13269
if ((error=table->file->read_first_row(table->record[0],
13270
table->s->primary_key)))
13272
if (error != HA_ERR_END_OF_FILE)
13273
return report_error(table, error);
13274
mark_as_null_row(tab->table);
13275
empty_record(table); // Make empty record
13278
store_record(table,record[1]);
13280
else if (!table->status) // Only happens with left join
13281
restore_record(table,record[1]); // restore old record
13283
return table->status ? -1 : 0;
3375
13288
Read a (constant) table when there is at most one matching row.
6257
static void print_table_array(Session *session, String *str, TableList **table,
6260
(*table)->print(session, str, QT_ORDINARY);
6262
for (TableList **tbl= table + 1; tbl < end; tbl++)
6264
TableList *curr= *tbl;
17277
/** Allocate memory needed for other rollup functions. */
17279
bool JOIN::rollup_init()
17284
tmp_table_param.quick_group= 0; // Can't create groups in tmp table
17285
rollup.state= ROLLUP::STATE_INITED;
17288
Create pointers to the different sum function groups
17289
These are updated by rollup_make_fields()
17291
tmp_table_param.group_parts= send_group_parts;
17293
if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17295
sizeof(List<Item>) +
17296
ref_pointer_array_size)
17297
* send_group_parts )))
17300
rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17301
rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17302
ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17305
Prepare space for field list for the different levels
17306
These will be filled up in rollup_make_fields()
17308
for (i= 0 ; i < send_group_parts ; i++)
17310
rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17311
List<Item> *rollup_fields= &rollup.fields[i];
17312
rollup_fields->empty();
17313
rollup.ref_pointer_arrays[i]= ref_array;
17314
ref_array+= all_fields.elements;
17316
for (i= 0 ; i < send_group_parts; i++)
17318
for (j=0 ; j < fields_list.elements ; j++)
17319
rollup.fields[i].push_back(rollup.null_items[i]);
17321
List_iterator<Item> it(all_fields);
17323
while ((item= it++))
17326
bool found_in_group= 0;
17328
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17330
if (*group_tmp->item == item)
17332
item->maybe_null= 1;
17334
if (item->const_item())
17337
For ROLLUP queries each constant item referenced in GROUP BY list
17338
is wrapped up into an Item_func object yielding the same value
17339
as the constant item. The objects of the wrapper class are never
17340
considered as constant items and besides they inherit all
17341
properties of the Item_result_field class.
17342
This wrapping allows us to ensure writing constant items
17343
into temporary tables whenever the result of the ROLLUP
17344
operation has to be written into a temporary table, e.g. when
17345
ROLLUP is used together with DISTINCT in the SELECT list.
17346
Usually when creating temporary tables for a intermidiate
17347
result we do not include fields for constant expressions.
17349
Item* new_item= new Item_func_rollup_const(item);
17352
new_item->fix_fields(thd, (Item **) 0);
17353
thd->change_item_tree(it.ref(), new_item);
17354
for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17356
if (*tmp->item == item)
17357
thd->change_item_tree(tmp->item, new_item);
17362
if (item->type() == Item::FUNC_ITEM && !found_in_group)
17364
bool changed= false;
17365
if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17368
We have to prevent creation of a field in a temporary table for
17369
an expression that contains GROUP BY attributes.
17370
Marking the expression item as 'with_sum_func' will ensure this.
17373
item->with_sum_func= 1;
17381
Fill up rollup structures with pointers to fields to use.
17383
Creates copies of item_sum items for each sum level.
17385
@param fields_arg List of all fields (hidden and real ones)
17386
@param sel_fields Pointer to selected fields
17387
@param func Store here a pointer to all fields
17391
In this case func is pointing to next not used element.
17396
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17399
List_iterator_fast<Item> it(fields_arg);
17400
Item *first_field= sel_fields.head();
17404
Create field lists for the different levels
17406
The idea here is to have a separate field list for each rollup level to
17407
avoid all runtime checks of which columns should be NULL.
17409
The list is stored in reverse order to get sum function in such an order
17410
in func that it makes it easy to reset them with init_sum_functions()
17412
Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17414
rollup.fields[0] will contain list where a,b,c is NULL
17415
rollup.fields[1] will contain list where b,c is NULL
17417
rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17419
sum_funcs_end[0] points to all sum functions
17420
sum_funcs_end[1] points to all sum functions, except grand totals
17424
for (level=0 ; level < send_group_parts ; level++)
17427
uint pos= send_group_parts - level -1;
17428
bool real_fields= 0;
17430
List_iterator<Item> new_it(rollup.fields[pos]);
17431
Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17432
ORDER *start_group;
17434
/* Point to first hidden field */
17435
Item **ref_array= ref_array_start + fields_arg.elements-1;
17437
/* Remember where the sum functions ends for the previous level */
17438
sum_funcs_end[pos+1]= *func;
17440
/* Find the start of the group for this level */
17441
for (i= 0, start_group= group_list ;
17443
start_group= start_group->next)
17447
while ((item= it++))
17449
if (item == first_field)
17451
real_fields= 1; // End of hidden fields
17452
ref_array= ref_array_start;
17455
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17456
(!((Item_sum*) item)->depended_from() ||
17457
((Item_sum *)item)->depended_from() == select_lex))
17461
This is a top level summary function that must be replaced with
17462
a sum function that is reset for this level.
17464
NOTE: This code creates an object which is not that nice in a
17465
sub select. Fortunately it's not common to have rollup in
17468
item= item->copy_or_same(thd);
17469
((Item_sum*) item)->make_unique();
17470
*(*func)= (Item_sum*) item;
17475
/* Check if this is something that is part of this group by */
17477
for (group_tmp= start_group, i= pos ;
17478
group_tmp ; group_tmp= group_tmp->next, i++)
17480
if (*group_tmp->item == item)
17483
This is an element that is used by the GROUP BY and should be
17484
set to NULL in this level
17486
Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17489
item->maybe_null= 1; // Value will be null sometimes
17490
null_item->result_field= item->get_tmp_table_field();
17499
(void) new_it++; // Point to next item
17500
new_it.replace(item); // Replace previous
17507
sum_funcs_end[0]= *func; // Point to last function
17512
Send all rollup levels higher than the current one to the client.
17516
SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17519
@param idx Level we are on:
17520
- 0 = Total sum level
17521
- 1 = First group changed (a)
17522
- 2 = Second group changed (a,b)
17527
1 If send_data_failed()
17530
int JOIN::rollup_send_data(uint idx)
17533
for (i= send_group_parts ; i-- > idx ; )
17535
/* Get reference pointers to sum functions in place */
17536
memcpy((char*) ref_pointer_array,
17537
(char*) rollup.ref_pointer_arrays[i],
17538
ref_pointer_array_size);
17539
if ((!having || having->val_int()))
17541
if (send_records < unit->select_limit_cnt && do_send_rows &&
17542
result->send_data(rollup.fields[i]))
17547
/* Restore ref_pointer_array */
17548
set_items_ref_array(current_ref_pointer_array);
17553
Write all rollup levels higher than the current one to a temp table.
17557
SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17560
@param idx Level we are on:
17561
- 0 = Total sum level
17562
- 1 = First group changed (a)
17563
- 2 = Second group changed (a,b)
17564
@param table reference to temp table
17569
1 if write_data_failed()
17572
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17575
for (i= send_group_parts ; i-- > idx ; )
17577
/* Get reference pointers to sum functions in place */
17578
memcpy((char*) ref_pointer_array,
17579
(char*) rollup.ref_pointer_arrays[i],
17580
ref_pointer_array_size);
17581
if ((!having || having->val_int()))
17585
List_iterator_fast<Item> it(rollup.fields[i]);
17586
while ((item= it++))
17588
if (item->type() == Item::NULL_ITEM && item->is_result_field())
17589
item->save_in_result_field(1);
17591
copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17592
if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17594
if (create_myisam_from_heap(thd, table_arg,
17595
tmp_table_param.start_recinfo,
17596
&tmp_table_param.recinfo,
17602
/* Restore ref_pointer_array */
17603
set_items_ref_array(current_ref_pointer_array);
17608
clear results if there are not rows found for group
17609
(end_send_group/end_write_group)
17614
clear_tables(this);
17615
copy_fields(&tmp_table_param);
17619
Item_sum *func, **func_ptr= sum_funcs;
17620
while ((func= *(func_ptr++)))
17628
Send a description about what how the select will be done to stdout.
17631
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17632
bool distinct,const char *message)
17634
List<Item> field_list;
17635
List<Item> item_list;
17636
THD *thd=join->thd;
17637
select_result *result=join->result;
17638
Item *item_null= new Item_null();
17639
CHARSET_INFO *cs= system_charset_info;
17641
/* Don't log this into the slow query log */
17642
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17643
join->unit->offset_limit_cnt= 0;
17646
NOTE: the number/types of items pushed into item_list must be in sync with
17647
EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17651
item_list.push_back(new Item_int((int32_t)
17652
join->select_lex->select_number));
17653
item_list.push_back(new Item_string(join->select_lex->type,
17654
strlen(join->select_lex->type), cs));
17655
for (uint i=0 ; i < 7; i++)
17656
item_list.push_back(item_null);
17657
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17658
item_list.push_back(item_null);
17660
item_list.push_back(new Item_string(message,strlen(message),cs));
17661
if (result->send_data(item_list))
17664
else if (join->select_lex == join->unit->fake_select_lex)
17667
here we assume that the query will return at least two rows, so we
17668
show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17669
and no filesort will be actually done, but executing all selects in
17670
the UNION to provide precise EXPLAIN information will hardly be
17673
char table_name_buffer[NAME_LEN];
17676
item_list.push_back(new Item_null);
17678
item_list.push_back(new Item_string(join->select_lex->type,
17679
strlen(join->select_lex->type),
17683
SELECT_LEX *sl= join->unit->first_select();
17684
uint len= 6, lastop= 0;
17685
memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
17686
for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
17689
lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
17690
"%u,", sl->select_number);
17692
if (sl || len + lastop >= NAME_LEN)
17694
memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
17700
table_name_buffer[len - 1]= '>'; // change ',' to '>'
17702
item_list.push_back(new Item_string(table_name_buffer, len, cs));
17705
item_list.push_back(new Item_string(join_type_str[JT_ALL],
17706
strlen(join_type_str[JT_ALL]),
17708
/* possible_keys */
17709
item_list.push_back(item_null);
17711
item_list.push_back(item_null);
17713
item_list.push_back(item_null);
17715
item_list.push_back(item_null);
17717
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17718
item_list.push_back(item_null);
17720
item_list.push_back(item_null);
17722
if (join->unit->global_parameters->order_list.first)
17723
item_list.push_back(new Item_string("Using filesort",
17726
item_list.push_back(new Item_string("", 0, cs));
17728
if (result->send_data(item_list))
17733
table_map used_tables=0;
17734
for (uint i=0 ; i < join->tables ; i++)
17736
JOIN_TAB *tab=join->join_tab+i;
17737
TABLE *table=tab->table;
17738
TABLE_LIST *table_list= tab->table->pos_in_table_list;
17740
char buff1[512], buff2[512], buff3[512];
17741
char keylen_str_buf[64];
17742
String extra(buff, sizeof(buff),cs);
17743
char table_name_buffer[NAME_LEN];
17744
String tmp1(buff1,sizeof(buff1),cs);
17745
String tmp2(buff2,sizeof(buff2),cs);
17746
String tmp3(buff3,sizeof(buff3),cs);
17755
item_list.push_back(new Item_uint((uint32_t)
17756
join->select_lex->select_number));
17758
item_list.push_back(new Item_string(join->select_lex->type,
17759
strlen(join->select_lex->type),
17761
if (tab->type == JT_ALL && tab->select && tab->select->quick)
17763
quick_type= tab->select->quick->get_type();
17764
if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
17765
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
17766
(quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
17767
tab->type = JT_INDEX_MERGE;
17769
tab->type = JT_RANGE;
17772
if (table->derived_select_number)
17774
/* Derived table name generation */
17775
int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
17777
table->derived_select_number);
17778
item_list.push_back(new Item_string(table_name_buffer, len, cs));
17782
TABLE_LIST *real_table= table->pos_in_table_list;
17783
item_list.push_back(new Item_string(real_table->alias,
17784
strlen(real_table->alias),
17787
/* "type" column */
17788
item_list.push_back(new Item_string(join_type_str[tab->type],
17789
strlen(join_type_str[tab->type]),
17791
/* Build "possible_keys" value and add it to item_list */
17792
if (!tab->keys.is_clear_all())
17795
for (j=0 ; j < table->s->keys ; j++)
17797
if (tab->keys.is_set(j))
17801
tmp1.append(table->key_info[j].name,
17802
strlen(table->key_info[j].name),
17803
system_charset_info);
17808
item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
17810
item_list.push_back(item_null);
17812
/* Build "key", "key_len", and "ref" values and add them to item_list */
17813
if (tab->ref.key_parts)
17815
KEY *key_info=table->key_info+ tab->ref.key;
17816
register uint length;
17817
item_list.push_back(new Item_string(key_info->name,
17818
strlen(key_info->name),
17819
system_charset_info));
17820
length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) -
17822
item_list.push_back(new Item_string(keylen_str_buf, length,
17823
system_charset_info));
17824
for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
17828
tmp2.append((*ref)->name(), strlen((*ref)->name()),
17829
system_charset_info);
17831
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17833
else if (tab->type == JT_NEXT)
17835
KEY *key_info=table->key_info+ tab->index;
17836
register uint length;
17837
item_list.push_back(new Item_string(key_info->name,
17838
strlen(key_info->name),cs));
17839
length= int64_t2str(key_info->key_length, keylen_str_buf, 10) -
17841
item_list.push_back(new Item_string(keylen_str_buf,
17843
system_charset_info));
17844
item_list.push_back(item_null);
17846
else if (tab->select && tab->select->quick)
17848
tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
17849
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17850
item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
17851
item_list.push_back(item_null);
17855
if (table_list->schema_table &&
17856
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
17858
const char *tmp_buff;
17860
if (table_list->has_db_lookup_value)
17862
f_idx= table_list->schema_table->idx_field1;
17863
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17864
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17866
if (table_list->has_table_lookup_value)
17868
if (table_list->has_db_lookup_value)
17870
f_idx= table_list->schema_table->idx_field2;
17871
tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17872
tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17875
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17877
item_list.push_back(item_null);
17880
item_list.push_back(item_null);
17881
item_list.push_back(item_null);
17882
item_list.push_back(item_null);
17885
/* Add "rows" field to item_list. */
17886
if (table_list->schema_table)
17889
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17890
item_list.push_back(item_null);
17892
item_list.push_back(item_null);
17896
double examined_rows;
17897
if (tab->select && tab->select->quick)
17898
examined_rows= rows2double(tab->select->quick->records);
17899
else if (tab->type == JT_NEXT || tab->type == JT_ALL)
17900
examined_rows= rows2double(tab->limit ? tab->limit :
17901
tab->table->file->records());
17903
examined_rows= join->best_positions[i].records_read;
17905
item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows,
17906
MY_INT64_NUM_DECIMAL_DIGITS));
17908
/* Add "filtered" field to item_list. */
17909
if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17913
f= (float) (100.0 * join->best_positions[i].records_read /
17915
item_list.push_back(new Item_float(f, 2));
17919
/* Build "Extra" field and add it to item_list. */
17920
bool key_read=table->key_read;
17921
if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
17922
table->covering_keys.is_set(tab->index))
17924
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
17925
!((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
17929
item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
17930
else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
17932
if (tab->packed_info & TAB_INFO_USING_INDEX)
17933
extra.append(STRING_WITH_LEN("; Using index"));
17934
if (tab->packed_info & TAB_INFO_USING_WHERE)
17935
extra.append(STRING_WITH_LEN("; Using where"));
17936
if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
17937
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
17938
/* Skip initial "; "*/
17939
const char *str= extra.ptr();
17940
uint32_t len= extra.length();
17946
item_list.push_back(new Item_string(str, len, cs));
17950
uint keyno= MAX_KEY;
17951
if (tab->ref.key_parts)
17952
keyno= tab->ref.key;
17953
else if (tab->select && tab->select->quick)
17954
keyno = tab->select->quick->index;
17956
if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
17957
table->file->pushed_idx_cond)
17958
extra.append(STRING_WITH_LEN("; Using index condition"));
17960
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
17961
quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
17962
quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
17964
extra.append(STRING_WITH_LEN("; Using "));
17965
tab->select->quick->add_info_string(&extra);
17969
if (tab->use_quick == 2)
17971
/* 4 bits per 1 hex digit + terminating '\0' */
17972
char buf[MAX_KEY / 4 + 1];
17973
extra.append(STRING_WITH_LEN("; Range checked for each "
17974
"record (index map: 0x"));
17975
extra.append(tab->keys.print(buf));
17978
else if (tab->select->cond)
17980
const COND *pushed_cond= tab->table->file->pushed_cond;
17982
if (thd->variables.engine_condition_pushdown && pushed_cond)
17984
extra.append(STRING_WITH_LEN("; Using where with pushed "
17986
if (thd->lex->describe & DESCRIBE_EXTENDED)
17988
extra.append(STRING_WITH_LEN(": "));
17989
((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
17993
extra.append(STRING_WITH_LEN("; Using where"));
17998
if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
17999
extra.append(STRING_WITH_LEN("; Using index for group-by"));
18001
extra.append(STRING_WITH_LEN("; Using index"));
18003
if (table->reginfo.not_exists_optimize)
18004
extra.append(STRING_WITH_LEN("; Not exists"));
18006
if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
18007
!(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
18008
HA_MRR_USE_DEFAULT_IMPL))
18010
extra.append(STRING_WITH_LEN("; Using MRR"));
18013
if (table_list->schema_table &&
18014
table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18016
if (!table_list->table_open_method)
18017
extra.append(STRING_WITH_LEN("; Skip_open_table"));
18018
else if (table_list->table_open_method == OPEN_FRM_ONLY)
18019
extra.append(STRING_WITH_LEN("; Open_frm_only"));
18021
extra.append(STRING_WITH_LEN("; Open_full_table"));
18022
if (table_list->has_db_lookup_value &&
18023
table_list->has_table_lookup_value)
18024
extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18025
else if (table_list->has_db_lookup_value ||
18026
table_list->has_table_lookup_value)
18027
extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18029
extra.append(STRING_WITH_LEN("; Scanned all databases"));
18031
if (need_tmp_table)
18034
extra.append(STRING_WITH_LEN("; Using temporary"));
18039
extra.append(STRING_WITH_LEN("; Using filesort"));
18041
if (distinct & test_all_bits(used_tables,thd->used_tables))
18042
extra.append(STRING_WITH_LEN("; Distinct"));
18044
if (tab->insideout_match_tab)
18046
extra.append(STRING_WITH_LEN("; LooseScan"));
18049
if (tab->flush_weedout_table)
18050
extra.append(STRING_WITH_LEN("; Start temporary"));
18051
else if (tab->check_weed_out_table)
18052
extra.append(STRING_WITH_LEN("; End temporary"));
18053
else if (tab->do_firstmatch)
18055
extra.append(STRING_WITH_LEN("; FirstMatch("));
18056
TABLE *prev_table=tab->do_firstmatch->table;
18057
if (prev_table->derived_select_number)
18059
char namebuf[NAME_LEN];
18060
/* Derived table name generation */
18061
int len= snprintf(namebuf, sizeof(namebuf)-1,
18063
prev_table->derived_select_number);
18064
extra.append(namebuf, len);
18067
extra.append(prev_table->pos_in_table_list->alias);
18068
extra.append(STRING_WITH_LEN(")"));
18071
for (uint part= 0; part < tab->ref.key_parts; part++)
18073
if (tab->ref.cond_guards[part])
18075
extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18080
if (i > 0 && tab[-1].next_select == sub_select_cache)
18081
extra.append(STRING_WITH_LEN("; Using join buffer"));
18083
/* Skip initial "; "*/
18084
const char *str= extra.ptr();
18085
uint32_t len= extra.length();
18091
item_list.push_back(new Item_string(str, len, cs));
18093
// For next iteration
18094
used_tables|=table->map;
18095
if (result->send_data(item_list))
18099
for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18101
unit= unit->next_unit())
18103
if (mysql_explain_union(thd, unit, result))
18110
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18113
SELECT_LEX *first= unit->first_select();
18115
for (SELECT_LEX *sl= first;
18117
sl= sl->next_select())
18119
// drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18120
uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18121
sl->type= (((&thd->lex->select_lex)==sl)?
18122
(sl->first_inner_unit() || sl->next_select() ?
18123
"PRIMARY" : "SIMPLE"):
18125
((sl->linkage == DERIVED_TABLE_TYPE) ?
18127
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18128
"DEPENDENT SUBQUERY":
18129
(uncacheable?"UNCACHEABLE SUBQUERY":
18131
((uncacheable & UNCACHEABLE_DEPENDENT) ?
18133
uncacheable?"UNCACHEABLE UNION":
18135
sl->options|= SELECT_DESCRIBE;
18137
if (unit->is_union())
18139
unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18140
unit->fake_select_lex->type= "UNION RESULT";
18141
unit->fake_select_lex->options|= SELECT_DESCRIBE;
18142
if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18144
res|= unit->cleanup();
18148
thd->lex->current_select= first;
18149
unit->set_limit(unit->global_parameters);
18150
res= mysql_select(thd, &first->ref_pointer_array,
18151
(TABLE_LIST*) first->table_list.first,
18152
first->with_wild, first->item_list,
18154
first->order_list.elements +
18155
first->group_list.elements,
18156
(ORDER*) first->order_list.first,
18157
(ORDER*) first->group_list.first,
18159
(ORDER*) thd->lex->proc_list.first,
18160
first->options | thd->options | SELECT_DESCRIBE,
18161
result, unit, first);
18163
return(res || thd->is_error());
18167
static void print_table_array(THD *thd, String *str, TABLE_LIST **table,
18170
(*table)->print(thd, str, QT_ORDINARY);
18172
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18174
TABLE_LIST *curr= *tbl;
6265
18175
if (curr->outer_join)
6267
18177
/* MySQL converts right to left joins */