40
40
struct st_sargable_param;
42
42
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
43
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
43
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
44
44
DYNAMIC_ARRAY *keyuse);
45
45
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
46
46
JOIN_TAB *join_tab,
85
85
static void make_outerjoin_info(JOIN *join);
86
86
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
87
87
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
88
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
88
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
89
89
static void update_depend_map(JOIN *join);
90
static void update_depend_map(JOIN *join, ORDER *order);
91
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
90
static void update_depend_map(JOIN *join, order_st *order);
91
static order_st *remove_const(JOIN *join,order_st *first_order,COND *cond,
92
92
bool change_list, bool *simple_order);
93
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
93
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
94
94
List<Item> &fields, bool send_row,
95
95
uint64_t select_options, const char *info,
97
97
static COND *build_equal_items(THD *thd, COND *cond,
98
98
COND_EQUAL *inherited,
99
List<TABLE_LIST> *join_list,
99
List<TableList> *join_list,
100
100
COND_EQUAL **cond_equal_ref);
101
101
static COND* substitute_for_best_equal_field(COND *cond,
102
102
COND_EQUAL *cond_equal,
103
103
void *table_join_idx);
104
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
104
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
105
105
COND *conds, bool top, bool in_sj);
106
106
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
107
107
static void restore_prev_nj_state(JOIN_TAB *last);
108
static void reset_nj_counters(List<TABLE_LIST> *join_list);
109
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
108
static void reset_nj_counters(List<TableList> *join_list);
109
static uint build_bitmap_for_nested_joins(List<TableList> *join_list,
110
110
uint first_unused);
115
115
const JOIN_TAB *tab);
117
117
static COND *optimize_cond(JOIN *join, COND *conds,
118
List<TABLE_LIST> *join_list,
118
List<TableList> *join_list,
119
119
Item::cond_result *cond_value);
120
120
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
121
121
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
160
160
table_map used_table,
161
161
bool exclude_expensive_cond);
162
162
static Item* part_of_refkey(Table *form,Field *field);
163
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
163
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
164
164
ha_rows select_limit, bool no_changes,
165
165
const key_map *map);
166
166
static bool list_contains_unique_index(Table *table,
167
167
bool (*find_func) (Field *, void *), void *data);
168
168
static bool find_field_in_item_list (Field *field, void *data);
169
169
static bool find_field_in_order_list (Field *field, void *data);
170
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
170
static int create_sort_index(THD *thd, JOIN *join, order_st *order,
171
171
ha_rows filesort_limit, ha_rows select_limit,
172
172
bool is_order_by);
173
173
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
185
185
static void reset_cache_write(JOIN_CACHE *cache);
186
186
static void read_cached_record(JOIN_TAB *tab);
187
187
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
188
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
189
ORDER *order, List<Item> &fields,
188
static order_st *create_distinct_group(THD *thd, Item **ref_pointer_array,
189
order_st *order, List<Item> &fields,
190
190
List<Item> &all_fields,
191
191
bool *all_order_by_fields_used);
192
static bool test_if_subpart(ORDER *a,ORDER *b);
193
static Table *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
194
static void calc_group_buffer(JOIN *join,ORDER *group);
192
static bool test_if_subpart(order_st *a,order_st *b);
193
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
194
static void calc_group_buffer(JOIN *join,order_st *group);
195
195
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
196
static bool alloc_group_fields(JOIN *join,ORDER *group);
196
static bool alloc_group_fields(JOIN *join,order_st *group);
197
197
// Create list for using with tempory table
198
198
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
199
199
List<Item> &new_list1,
257
257
setup_tables_done_option changed for next rexecution
259
259
res= mysql_select(thd, &select_lex->ref_pointer_array,
260
(TABLE_LIST*) select_lex->table_list.first,
260
(TableList*) select_lex->table_list.first,
261
261
select_lex->with_wild, select_lex->item_list,
262
262
select_lex->where,
263
263
select_lex->order_list.elements +
264
264
select_lex->group_list.elements,
265
(ORDER*) select_lex->order_list.first,
266
(ORDER*) select_lex->group_list.first,
265
(order_st*) select_lex->order_list.first,
266
(order_st*) select_lex->group_list.first,
267
267
select_lex->having,
268
(ORDER*) lex->proc_list.first,
268
(order_st*) lex->proc_list.first,
269
269
select_lex->options | thd->options |
270
270
setup_tables_done_option,
271
271
result, unit, select_lex);
394
394
Function to setup clauses without sum functions.
396
396
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
399
399
List<Item> &fields,
400
400
List<Item> &all_fields,
403
ORDER *group, bool *hidden_group_fields)
403
order_st *group, bool *hidden_group_fields)
406
406
nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
439
439
JOIN::prepare(Item ***rref_pointer_array,
440
TABLE_LIST *tables_init,
440
TableList *tables_init,
441
441
uint wild_num, COND *conds_init, uint og_num,
442
ORDER *order_init, ORDER *group_init,
442
order_st *order_init, order_st *group_init,
443
443
Item *having_init,
444
ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
444
order_st *proc_param_init, SELECT_LEX *select_lex_arg,
445
445
SELECT_LEX_UNIT *unit_arg)
447
447
// to prevent double initialization on EXPLAIN
528
528
requirements are:
529
529
1. Subquery predicate is an IN/=ANY subq predicate
530
530
2. Subquery is a single SELECT (not a UNION)
531
3. Subquery does not have GROUP BY or ORDER BY
531
3. Subquery does not have GROUP BY or order_st BY
532
532
4. Subquery does not use aggregate functions or HAVING
533
533
5. Subquery predicate is at the AND-top-level of ON/WHERE clause
534
534
6. No execution method was already chosen (by a prepared statement).
594
594
(Subquery is non-correlated ||
595
595
Subquery is correlated to any query outer to IN predicate ||
596
596
(Subquery is correlated to the immediate outer query &&
597
Subquery !contains {GROUP BY, ORDER BY [LIMIT],
597
Subquery !contains {GROUP BY, order_st BY [LIMIT],
598
598
aggregate functions) && subquery predicate is not under "NOT IN"))
599
599
6. No execution method was already chosen (by a prepared statement).
825
825
/* Check if this table is functionally dependent on the tables that
826
826
are within the same outer join nest
828
TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
828
TableList *embedding= join_tab->table->pos_in_table_list->embedding;
829
829
if (join_tab->type == JT_EQ_REF)
831
831
Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
961
961
table_map outer_tables;
962
962
} dups_ranges [MAX_TABLES];
964
TABLE_LIST *emb_insideout_nest= NULL;
964
TableList *emb_insideout_nest= NULL;
965
965
table_map emb_sj_map= 0; /* A bitmap of sj-nests (that is, their sj-inner
966
966
tables) whose ranges we're in */
967
967
table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
1492
1492
We have found that grouping can be removed since groups correspond to
1493
1493
only one row anyway, but we still have to guarantee correct result
1494
1494
order. The line below effectively rewrites the query from GROUP BY
1495
<fields> to ORDER BY <fields>. There are two exceptions:
1495
<fields> to order_st BY <fields>. There are two exceptions:
1496
1496
- if skip_sort_order is set (see above), then we can simply skip
1498
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1498
- we can only rewrite order_st BY if the order_st BY fields are 'compatible'
1499
1499
with the GROUP BY ones, i.e. either one is a prefix of another.
1500
We only check if the ORDER BY is a prefix of GROUP BY. In this case
1500
We only check if the order_st BY is a prefix of GROUP BY. In this case
1501
1501
test_if_subpart() copies the ASC/DESC attributes from the original
1503
If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1503
If GROUP BY is a prefix of order_st BY, then it is safe to leave
1506
1506
if (!order || test_if_subpart(group_list, order))
1507
1507
order= skip_sort_order ? 0 : group_list;
1509
1509
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1510
rewritten to IGNORE INDEX FOR ORDER BY(fields).
1510
rewritten to IGNORE INDEX FOR order_st BY(fields).
1512
1512
join_tab->table->keys_in_use_for_order_by=
1513
1513
join_tab->table->keys_in_use_for_group_by;
1533
1533
We are only using one table. In this case we change DISTINCT to a
1534
1534
GROUP BY query if:
1535
- The GROUP BY can be done through indexes (no sort) and the ORDER
1535
- The GROUP BY can be done through indexes (no sort) and the order_st
1536
1536
BY only uses selected fields.
1537
(In this case we can later optimize away GROUP BY and ORDER BY)
1537
(In this case we can later optimize away GROUP BY and order_st BY)
1538
1538
- We are scanning the whole table without LIMIT
1539
1539
This can happen if:
1540
1540
- We are using CALC_FOUND_ROWS
1541
- We are using an ORDER BY that can't be optimized away.
1541
- We are using an order_st BY that can't be optimized away.
1543
1543
We don't want to use this optimization when we are using LIMIT
1544
1544
because in this case we can just create a temporary table that
1627
1627
This has to be done if all tables are not already read (const tables)
1628
1628
and one of the following conditions holds:
1629
1629
- We are using DISTINCT (simple distinct's are already optimized away)
1630
- We are using an ORDER BY or GROUP BY on fields not in the first table
1631
- We are using different ORDER BY and GROUP BY orders
1630
- We are using an order_st BY or GROUP BY on fields not in the first table
1631
- We are using different order_st BY and GROUP BY orders
1632
1632
- The user wants us to buffer the result.
1634
1634
need_tmp= (const_tables != tables &&
1760
1760
Force using of tmp table if sorting by a SP or UDF function due to
1761
1761
their expensive and probably non-deterministic nature.
1763
for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1763
for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1765
1765
Item *item= *tmp_order->item;
1766
1766
if (item->is_expensive())
1805
1805
tmp_table_param.hidden_field_count= (all_fields.elements -
1806
1806
fields_list.elements);
1807
ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1807
order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1810
1810
Pushing LIMIT to the temporary table creation is not applicable
1811
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1811
when there is order_st BY or GROUP BY or there is no GROUP BY, but
1812
1812
there are aggregate functions, because in all these cases we need
1813
1813
all result rows.
2091
2091
if (select_options & SELECT_DESCRIBE)
2094
Check if we managed to optimize ORDER BY away and don't use temporary
2095
table to resolve ORDER BY: in that case, we only may need to do
2094
Check if we managed to optimize order_st BY away and don't use temporary
2095
table to resolve order_st BY: in that case, we only may need to do
2096
2096
filesort for GROUP BY.
2098
2098
if (!order && !no_order && (!skip_sort_order || !need_tmp))
2584
2584
for a, b and c in this list.
2585
2585
@param conds top level item of an expression representing
2586
2586
WHERE clause of the top level select
2587
@param og_num total number of ORDER BY and GROUP BY clauses
2587
@param og_num total number of order_st BY and GROUP BY clauses
2589
@param order linked list of ORDER BY agruments
2589
@param order linked list of order_st BY agruments
2590
2590
@param group linked list of GROUP BY arguments
2591
2591
@param having top level item of HAVING expression
2592
2592
@param proc_param list of PROCEDUREs
2613
2613
mysql_select(THD *thd, Item ***rref_pointer_array,
2614
TABLE_LIST *tables, uint wild_num, List<Item> &fields,
2615
COND *conds, uint og_num, ORDER *order, ORDER *group,
2616
Item *having, ORDER *proc_param, uint64_t select_options,
2614
TableList *tables, uint wild_num, List<Item> &fields,
2615
COND *conds, uint og_num, order_st *order, order_st *group,
2616
Item *having, order_st *proc_param, uint64_t select_options,
2617
2617
select_result *result, SELECT_LEX_UNIT *unit,
2618
2618
SELECT_LEX *select_lex)
2670
/* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2670
/* dump_TableList_graph(select_lex, select_lex->leaf_tables); */
2671
2671
if (join->flatten_subqueries())
2676
/* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2676
/* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
2678
2678
if ((err= join->optimize()))
2725
static TABLE_LIST *alloc_join_nest(THD *thd)
2725
static TableList *alloc_join_nest(THD *thd)
2728
if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2729
sizeof(NESTED_JOIN))))
2728
if (!(tbl= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
2729
sizeof(nested_join_st))))
2731
tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl +
2732
ALIGN_SIZE(sizeof(TABLE_LIST)));
2731
tbl->nested_join= (nested_join_st*) ((uchar*)tbl +
2732
ALIGN_SIZE(sizeof(TableList)));
2737
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2737
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TableList> *tlist)
2739
List_iterator<TABLE_LIST> it(*tlist);
2739
List_iterator<TableList> it(*tlist);
2741
2741
while ((table= it++))
2743
2743
if (table->on_expr)
2758
2758
subq_pred Subquery predicate to be converted
2761
Convert a subquery predicate into a TABLE_LIST semi-join nest. All the
2761
Convert a subquery predicate into a TableList semi-join nest. All the
2762
2762
prerequisites are already checked, so the conversion is always successfull.
2764
2764
Prepared Statements: the transformation is permanent:
2765
- Changes in TABLE_LIST structures are naturally permanent
2765
- Changes in TableList structures are naturally permanent
2766
2766
- Item tree changes are performed on statement MEM_ROOT:
2767
2767
= we activate statement MEM_ROOT
2768
2768
= this function is called before the first fix_prepare_information
2779
2779
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2781
2781
SELECT_LEX *parent_lex= parent_join->select_lex;
2782
TABLE_LIST *emb_tbl_nest= NULL;
2783
List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2782
TableList *emb_tbl_nest= NULL;
2783
List<TableList> *emb_join_list= &parent_lex->top_join_list;
2784
2784
THD *thd= parent_join->thd;
2833
2833
Q: other subqueries may be pointing to this element. What to do?
2834
2834
A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2835
2835
But we'll need to fix other pointers.
2836
A2: Another way: have TABLE_LIST::next_ptr so the following
2836
A2: Another way: have TableList::next_ptr so the following
2837
2837
subqueries know the table has been nested.
2838
A3: changes in the TABLE_LIST::outer_join will make everything work
2838
A3: changes in the TableList::outer_join will make everything work
2841
2841
if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2908
2908
st_select_lex *subq_lex= subq_pred->unit->first_select();
2909
2909
nested_join->join_list.empty();
2910
List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
2911
TABLE_LIST *tl, *last_leaf;
2910
List_iterator_fast<TableList> li(subq_lex->top_join_list);
2911
TableList *tl, *last_leaf;
2912
2912
while ((tl= li++))
2914
2914
tl->embedding= sj_nest;
2952
2952
tl->table->map= ((table_map)1) << table_no;
2953
2953
SELECT_LEX *old_sl= tl->select_lex;
2954
2954
tl->select_lex= parent_join->select_lex;
2955
for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2955
for(TableList *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2956
2956
emb->select_lex= parent_join->select_lex;
2958
2958
parent_join->tables += subq_lex->join->tables;
3271
3271
* Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3273
3273
* Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3274
* Semi-join nests TABLE_LIST::sj_inner_tables
3274
* Semi-join nests TableList::sj_inner_tables
3276
3276
This operation is (and should be) performed at each PS execution since
3277
3277
tables may become/cease to be constant across PS reexecutions.
4515
4515
Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4518
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4518
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
4519
4519
KEY_FIELD **end, uint *and_level,
4520
4520
SARGABLE_PARAM **sargables)
4522
List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4523
List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4522
List_iterator<TableList> li(nested_join_table->nested_join->join_list);
4523
List_iterator<TableList> li2(nested_join_table->nested_join->join_list);
4524
4524
bool have_another = false;
4525
4525
table_map tables= 0;
4527
4527
assert(nested_join_table->nested_join);
4529
4529
while ((table= li++) || (have_another && (li=li2, have_another=false,
7607
Determine if the set is already ordered for ORDER BY, so it can
7604
Determine if the set is already ordered for order_st BY, so it can
7608
7605
disable join cache because it will change the ordering of the results.
7609
7606
Code handles sort table that is at any location (not only first after
7610
7607
the const tables) despite the fact that it's currently prohibited.
8095
Remove the following expressions from ORDER BY and GROUP BY:
8092
Remove the following expressions from order_st BY and GROUP BY:
8096
8093
Constant expressions @n
8097
8094
Expression that only uses tables that are of type EQ_REF and the reference
8098
is in the ORDER list or if all refereed tables are of the above type.
8095
is in the order_st list or if all refereed tables are of the above type.
8100
8097
In the following, the X field can be removed:
8102
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
8103
SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
8099
SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
8100
SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
8106
8103
These can't be optimized:
8108
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
8109
SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
8110
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
8105
SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
8106
SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
8107
SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
8115
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
8112
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
8117
8114
if (tab->cached_eq_ref_table) // If cached
8118
8115
return tab->eq_ref_table;
8248
8245
Returns new sort order
8252
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8249
remove_const(JOIN *join,order_st *first_order, COND *cond,
8253
8250
bool change_list, bool *simple_order)
8255
8252
if (join->tables == join->const_tables)
8256
8253
return change_list ? 0 : first_order; // No need to sort
8258
ORDER *order,**prev_ptr;
8255
order_st *order,**prev_ptr;
8259
8256
table_map first_table= join->join_tab[join->const_tables].table->map;
8260
8257
table_map not_const_tables= ~join->const_table_map;
8315
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
8312
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
8316
8313
List<Item> &fields, bool send_row, uint64_t select_options,
8317
8314
const char *info, Item *having)
9090
9087
static COND *build_equal_items(THD *thd, COND *cond,
9091
9088
COND_EQUAL *inherited,
9092
List<TABLE_LIST> *join_list,
9089
List<TableList> *join_list,
9093
9090
COND_EQUAL **cond_equal_ref)
9095
9092
COND_EQUAL *cond_equal= 0;
9763
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9760
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
9767
NESTED_JOIN *nested_join;
9768
TABLE_LIST *prev_table= 0;
9769
List_iterator<TABLE_LIST> li(*join_list);
9764
nested_join_st *nested_join;
9765
TableList *prev_table= 0;
9766
List_iterator<TableList> li(*join_list);
9772
9769
Try to simplify join operations from join_list.
9950
9947
First unused bit in nested_join_map after the call.
9953
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
9950
static uint build_bitmap_for_nested_joins(List<TableList> *join_list,
9954
9951
uint first_unused)
9956
List_iterator<TABLE_LIST> li(*join_list);
9953
List_iterator<TableList> li(*join_list);
9958
9955
while ((table= li++))
9960
NESTED_JOIN *nested_join;
9957
nested_join_st *nested_join;
9961
9958
if ((nested_join= table->nested_join))
9989
Set NESTED_JOIN::counter=0 in all nested joins in passed list.
9986
Set nested_join_st::counter=0 in all nested joins in passed list.
9991
Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
9988
Recursively set nested_join_st::counter=0 for all nested joins contained in
9992
9989
the passed join_list.
9994
9991
@param join_list List of nested joins to process. It may also contain base
9995
9992
tables which will be ignored.
9998
static void reset_nj_counters(List<TABLE_LIST> *join_list)
9995
static void reset_nj_counters(List<TableList> *join_list)
10000
List_iterator<TABLE_LIST> li(*join_list);
9997
List_iterator<TableList> li(*join_list);
10002
9999
while ((table= li++))
10004
NESTED_JOIN *nested_join;
10001
nested_join_st *nested_join;
10005
10002
if ((nested_join= table->nested_join))
10007
10004
nested_join->counter_= 0;
12615
12612
-1 Reverse key can be used
12618
static int test_if_order_by_key(ORDER *order, Table *table, uint idx,
12615
static int test_if_order_by_key(order_st *order, Table *table, uint idx,
12619
12616
uint *used_key_parts)
12621
12618
KEY_PART_INFO *key_part,*key_part_end;
12749
test_if_subkey(ORDER *order, Table *table, uint ref, uint ref_key_parts,
12746
test_if_subkey(order_st *order, Table *table, uint ref, uint ref_key_parts,
12750
12747
const key_map *usable_keys)
12838
12835
Helper function for list_contains_unique_index.
12839
Find a field reference in a list of ORDER structures.
12836
Find a field reference in a list of order_st structures.
12840
12837
Finds a direct reference of the Field in the list.
12842
12839
@param field The field to search for.
12843
@param data ORDER *.The list to search in
12840
@param data order_st *.The list to search in
12852
12849
find_field_in_order_list (Field *field, void *data)
12854
ORDER *group= (ORDER *) data;
12851
order_st *group= (order_st *) data;
12855
12852
bool part_found= 0;
12856
for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
12853
for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
12858
12855
Item *item= (*tmp_group->item)->real_item();
12859
12856
if (item->type() == Item::FIELD_ITEM &&
13123
13120
bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
13126
Don't use an index scan with ORDER BY without limit.
13123
Don't use an index scan with order_st BY without limit.
13127
13124
For GROUP BY without limit always use index scan
13128
13125
if there is a suitable index.
13129
13126
Why we hold to this asymmetry hardly can be explained
13354
13351
filesort_limit Max number of rows that needs to be sorted
13355
13352
select_limit Max number of rows in final output
13356
13353
Used to decide if we should use index or not
13357
is_order_by true if we are sorting on ORDER BY, false if GROUP BY
13354
is_order_by true if we are sorting on order_st BY, false if GROUP BY
13358
13355
Used to decide if we should use index or not
13376
create_sort_index(THD *thd, JOIN *join, ORDER *order,
13373
create_sort_index(THD *thd, JOIN *join, order_st *order,
13377
13374
ha_rows filesort_limit, ha_rows select_limit,
13378
13375
bool is_order_by)
13762
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
13759
SORT_FIELD *make_unireg_sortorder(order_st *order, uint *length,
13763
13760
SORT_FIELD *sortorder)
13766
13763
SORT_FIELD *sort,*pos;
13769
for (ORDER *tmp = order; tmp; tmp=tmp->next)
13766
for (order_st *tmp = order; tmp; tmp=tmp->next)
13771
13768
if (!sortorder)
13772
13769
sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
14115
cp_buffer_from_ref(THD *thd, Table *table, TABLE_REF *ref)
14112
cp_buffer_from_ref(THD *thd, Table *table __attribute__((unused)), TABLE_REF *ref)
14117
14114
enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
14118
14115
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
14119
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
14120
14116
bool result= 0;
14122
14118
for (store_key **copy=ref->key_copy ; *copy ; copy++)
14138
14133
*****************************************************************************/
14141
Resolve an ORDER BY or GROUP BY column reference.
14136
Resolve an order_st BY or GROUP BY column reference.
14143
Given a column reference (represented by 'order') from a GROUP BY or ORDER
14138
Given a column reference (represented by 'order') from a GROUP BY or order_st
14144
14139
BY clause, find the actual column it represents. If the column being
14145
14140
resolved is from the GROUP BY clause, the procedure searches the SELECT
14146
14141
list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
14147
the ORDER BY clause, only the SELECT list is being searched.
14142
the order_st BY clause, only the SELECT list is being searched.
14149
14144
If 'order' is resolved to an Item, then order->item is set to the found
14150
14145
Item. If there is no item for the found column (that is, it was resolved
14174
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
14175
ORDER *order, List<Item> &fields, List<Item> &all_fields,
14169
find_order_in_list(THD *thd, Item **ref_pointer_array, TableList *tables,
14170
order_st *order, List<Item> &fields, List<Item> &all_fields,
14176
14171
bool is_group_field)
14178
Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
14173
Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
14179
14174
Item::Type order_item_type;
14180
14175
Item **select_item; /* The corresponding item from the SELECT clause. */
14181
14176
Field *from_field; /* The corresponding field from the FROM clause. */
14307
14302
the field list.
14310
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
14311
List<Item> &fields, List<Item> &all_fields, ORDER *order)
14305
int setup_order(THD *thd, Item **ref_pointer_array, TableList *tables,
14306
List<Item> &fields, List<Item> &all_fields, order_st *order)
14313
14308
thd->where="order clause";
14314
14309
for (; order; order=order->next)
14351
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
14352
List<Item> &fields, List<Item> &all_fields, ORDER *order,
14346
setup_group(THD *thd, Item **ref_pointer_array, TableList *tables,
14347
List<Item> &fields, List<Item> &all_fields, order_st *order,
14353
14348
bool *hidden_group_fields)
14355
14350
*hidden_group_fields=0;
14359
14354
return 0; /* Everything is ok */
14444
14439
optimize away 'order by'.
14448
14443
create_distinct_group(THD *thd, Item **ref_pointer_array,
14449
ORDER *order_list, List<Item> &fields,
14444
order_st *order_list, List<Item> &fields,
14450
14445
List<Item> &all_fields __attribute__((unused)),
14451
14446
bool *all_order_by_fields_used)
14453
14448
List_iterator<Item> li(fields);
14455
ORDER *order,*group,**prev;
14450
order_st *order,*group,**prev;
14457
14452
*all_order_by_fields_used= 1;
14458
14453
while ((item=li++))
14920
14915
itr.sublist(res_selected_fields, elements);
14922
Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
14917
Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
14923
14918
reference used in these will resolve to a item that is already calculated
14925
14920
param->copy_funcs.concat(&extra_funcs);
15508
15503
new_item->fix_fields(thd, (Item **) 0);
15509
15504
thd->change_item_tree(it.ref(), new_item);
15510
for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
15505
for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
15512
15507
if (*tmp->item == item)
15513
15508
thd->change_item_tree(tmp->item, new_item);
16302
16297
thd->lex->current_select= first;
16303
16298
unit->set_limit(unit->global_parameters);
16304
16299
res= mysql_select(thd, &first->ref_pointer_array,
16305
(TABLE_LIST*) first->table_list.first,
16300
(TableList*) first->table_list.first,
16306
16301
first->with_wild, first->item_list,
16307
16302
first->where,
16308
16303
first->order_list.elements +
16309
16304
first->group_list.elements,
16310
(ORDER*) first->order_list.first,
16311
(ORDER*) first->group_list.first,
16305
(order_st*) first->order_list.first,
16306
(order_st*) first->group_list.first,
16312
16307
first->having,
16313
(ORDER*) thd->lex->proc_list.first,
16308
(order_st*) thd->lex->proc_list.first,
16314
16309
first->options | thd->options | SELECT_DESCRIBE,
16315
16310
result, unit, first);
16321
static void print_table_array(THD *thd, String *str, TABLE_LIST **table,
16316
static void print_table_array(THD *thd, String *str, TableList **table,
16324
16319
(*table)->print(thd, str, QT_ORDINARY);
16326
for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
16321
for (TableList **tbl= table + 1; tbl < end; tbl++)
16328
TABLE_LIST *curr= *tbl;
16323
TableList *curr= *tbl;
16329
16324
if (curr->outer_join)
16331
16326
/* MySQL converts right to left joins */
16359
16354
static void print_join(THD *thd,
16361
List<TABLE_LIST> *tables,
16356
List<TableList> *tables,
16362
16357
enum_query_type query_type __attribute__((unused)))
16364
16359
/* List is reversed => we should reverse it before using */
16365
List_iterator_fast<TABLE_LIST> ti(*tables);
16366
TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
16360
List_iterator_fast<TableList> ti(*tables);
16361
TableList **table= (TableList **)thd->alloc(sizeof(TableList*) *
16367
16362
tables->elements);
16368
16363
if (table == 0)
16369
16364
return; // out of memory
16371
for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
16366
for (TableList **t= table + (tables->elements - 1); t >= table; t--)