45
44
DYNAMIC_ARRAY *keyuse);
46
45
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
47
46
JOIN_TAB *join_tab,
48
uint32_t tables, COND *conds,
47
uint tables, COND *conds,
49
48
COND_EQUAL *cond_equal,
50
49
table_map table_map, SELECT_LEX *select_lex,
51
50
st_sargable_param **sargables);
52
51
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
53
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
52
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
54
53
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
55
54
table_map used_tables);
56
55
static bool choose_plan(JOIN *join,table_map join_tables);
58
57
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
59
table_map remaining_tables, uint32_t idx,
58
table_map remaining_tables, uint idx,
60
59
double record_count, double read_time);
61
60
static void optimize_straight_join(JOIN *join, table_map join_tables);
62
61
static bool greedy_search(JOIN *join, table_map remaining_tables,
63
uint32_t depth, uint32_t prune_level);
62
uint depth, uint prune_level);
64
63
static bool best_extension_by_limited_search(JOIN *join,
65
64
table_map remaining_tables,
66
uint32_t idx, double record_count,
67
double read_time, uint32_t depth,
68
uint32_t prune_level);
69
static uint32_t determine_search_depth(JOIN* join);
65
uint idx, double record_count,
66
double read_time, uint depth,
68
static uint determine_search_depth(JOIN* join);
70
69
static int join_tab_cmp(const void* ptr1, const void* ptr2);
71
70
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
73
72
TODO: 'find_best' is here only temporarily until 'greedy_search' is
74
73
tested and approved.
76
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
75
static bool find_best(JOIN *join,table_map rest_tables,uint index,
77
76
double record_count,double read_time);
78
static uint32_t cache_record_length(JOIN *join,uint32_t index);
79
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
77
static uint cache_record_length(JOIN *join,uint index);
78
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
80
79
static bool get_best_combination(JOIN *join);
81
80
static store_key *get_store_key(THD *thd,
82
81
KEYUSE *keyuse, table_map used_tables,
83
KEY_PART_INFO *key_part, unsigned char *key_buff,
82
KEY_PART_INFO *key_part, uchar *key_buff,
85
84
static bool make_simple_join(JOIN *join,Table *tmp_table);
86
85
static void make_outerjoin_info(JOIN *join);
87
86
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
88
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
87
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
89
88
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
90
89
static void update_depend_map(JOIN *join);
91
90
static void update_depend_map(JOIN *join, order_st *order);
176
175
static int remove_dup_with_compare(THD *thd, Table *entry, Field **field,
177
176
ulong offset,Item *having);
178
177
static int remove_dup_with_hash_index(THD *thd,Table *table,
179
uint32_t field_count, Field **first_field,
178
uint field_count, Field **first_field,
181
180
ulong key_length,Item *having);
182
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint32_t table_count);
181
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
183
182
static ulong used_blob_length(CACHE_FIELD **ptr);
184
183
static bool store_record_in_cache(JOIN_CACHE *cache);
185
184
static void reset_cache_read(JOIN_CACHE *cache);
199
198
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
200
199
List<Item> &new_list1,
201
200
List<Item> &new_list2,
202
uint32_t elements, List<Item> &items);
201
uint elements, List<Item> &items);
203
202
// Create list for using with tempory table
204
203
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
205
204
List<Item> &new_list1,
206
205
List<Item> &new_list2,
207
uint32_t elements, List<Item> &items);
206
uint elements, List<Item> &items);
208
207
static void init_tmptable_sum_functions(Item_sum **func);
209
208
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
210
209
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
1256
SELECT_LEX *sel= thd->lex->current_select;
1257
if (sel->first_cond_optimization)
1260
The following code will allocate the new items in a permanent
1261
MEMROOT for prepared statements and stored procedures.
1263
sel->first_cond_optimization= 0;
1258
/* Convert all outer joins to inner joins if possible */
1259
conds= simplify_joins(this, join_list, conds, true, false);
1260
build_bitmap_for_nested_joins(join_list, 0);
1265
/* Convert all outer joins to inner joins if possible */
1266
conds= simplify_joins(this, join_list, conds, true, false);
1267
build_bitmap_for_nested_joins(join_list, 0);
1262
1270
conds= optimize_cond(this, conds, join_list, &cond_value);
1263
1271
if (thd->is_error())
2605
2613
mysql_select(THD *thd, Item ***rref_pointer_array,
2606
TableList *tables, uint32_t wild_num, List<Item> &fields,
2607
COND *conds, uint32_t og_num, order_st *order, order_st *group,
2614
TableList *tables, uint wild_num, List<Item> &fields,
2615
COND *conds, uint og_num, order_st *order, order_st *group,
2608
2616
Item *having, order_st *proc_param, uint64_t select_options,
2609
2617
select_result *result, SELECT_LEX_UNIT *unit,
2610
2618
SELECT_LEX *select_lex)
4007
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
4008
Field *field, bool eq_func, Item **value, uint32_t num_values,
4015
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4016
Field *field, bool eq_func, Item **value, uint num_values,
4009
4017
table_map usable_tables, SARGABLE_PARAM **sargables)
4011
uint32_t exists_optimize= 0;
4019
uint exists_optimize= 0;
4012
4020
if (!(field->flags & PART_KEY_FLAG))
4014
4022
// Don't remove column IS NULL on a LEFT JOIN table
4568
4576
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4569
uint32_t tables, COND *cond,
4577
uint tables, COND *cond,
4570
4578
COND_EQUAL *cond_equal __attribute__((unused)),
4571
4579
table_map normal_tables, SELECT_LEX *select_lex,
4572
4580
SARGABLE_PARAM **sargables)
4574
4582
uint and_level,i,found_eq_constant;
4575
4583
KEY_FIELD *key_fields, *end, *field;
4577
uint32_t m= cmax(select_lex->max_equal_elems,(uint32_t)1);
4585
uint m= max(select_lex->max_equal_elems,(uint32_t)1);
4580
4588
We use the same piece of memory to store both KEY_FIELD
6480
6488
keyuse=org_keyuse;
6482
6490
store_key **ref_key= j->ref.key_copy;
6483
unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
6491
uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
6484
6492
bool keyuse_uses_no_tables= true;
6487
6495
for (i=0 ; i < keyparts ; keyuse++,i++)
6489
6497
while (keyuse->keypart != i ||
6490
6498
((~used_tables) & keyuse->used_tables))
6491
6499
keyuse++; /* Skip other parts */
6493
uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
6501
uint maybe_null= test(keyinfo->key_part[i].null_bit);
6494
6502
j->ref.items[i]=keyuse->val; // Save for cond removal
6495
6503
j->ref.cond_guards[i]= keyuse->cond_guard;
6496
6504
if (keyuse->null_rejecting)