1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
* Implementation of the Join class
26
* @defgroup Query_Optimizer Query Optimizer
35
#include <drizzled/item/cache.h>
36
#include <drizzled/item/cmpfunc.h>
37
#include <drizzled/item/copy_string.h>
38
#include <drizzled/item/uint.h>
39
#include <drizzled/cached_item.h>
40
#include <drizzled/sql_base.h>
41
#include <drizzled/sql_select.h> /* include join.h */
42
#include <drizzled/lock.h>
43
#include <drizzled/nested_join.h>
44
#include <drizzled/join.h>
45
#include <drizzled/join_cache.h>
46
#include <drizzled/show.h>
47
#include <drizzled/field/blob.h>
48
#include <drizzled/open_tables_state.h>
49
#include <drizzled/optimizer/position.h>
50
#include <drizzled/optimizer/sargable_param.h>
51
#include <drizzled/optimizer/key_use.h>
52
#include <drizzled/optimizer/range.h>
53
#include <drizzled/optimizer/sum.h>
54
#include <drizzled/optimizer/explain_plan.h>
55
#include <drizzled/optimizer/access_method_factory.h>
56
#include <drizzled/optimizer/access_method.h>
57
#include <drizzled/records.h>
58
#include <drizzled/probes.h>
59
#include <drizzled/internal/my_bit.h>
60
#include <drizzled/internal/my_sys.h>
61
#include <drizzled/internal/iocache.h>
62
#include <drizzled/plugin/storage_engine.h>
63
#include <drizzled/session.h>
64
#include <drizzled/select_result.h>
65
#include <drizzled/debug.h>
66
#include <drizzled/item/subselect.h>
67
#include <drizzled/my_hash.h>
68
#include <drizzled/sql_lex.h>
69
#include <drizzled/statistics_variables.h>
70
#include <drizzled/system_variables.h>
77
extern plugin::StorageEngine *heap_engine;
79
/** Declarations of static functions used in this source file. */
80
static bool make_group_fields(Join *main_join, Join *curr_join);
81
static void calc_group_buffer(Join *join, Order *group);
82
static bool alloc_group_fields(Join *join, Order *group);
83
static uint32_t cache_record_length(Join *join, uint32_t index);
84
static double prev_record_reads(Join *join, uint32_t idx, table_map found_ref);
85
static bool get_best_combination(Join *join);
86
static void set_position(Join *join,
89
optimizer::KeyUse *key);
90
static bool choose_plan(Join *join,table_map join_tables);
91
static void best_access_path(Join *join, JoinTable *s,
93
table_map remaining_tables,
97
static void optimize_straight_join(Join *join, table_map join_tables);
98
static bool greedy_search(Join *join, table_map remaining_tables, uint32_t depth, uint32_t prune_level);
99
static bool best_extension_by_limited_search(Join *join,
100
table_map remaining_tables,
105
uint32_t prune_level);
106
static uint32_t determine_search_depth(Join* join);
107
static void make_simple_join(Join*, Table*);
108
static void make_outerjoin_info(Join *join);
109
static bool make_join_select(Join *join, optimizer::SqlSelect *select,COND *item);
110
static void make_join_readinfo(Join&);
111
static void update_depend_map(Join *join);
112
static void update_depend_map(Join *join, Order *order);
113
static Order *remove_constants(Join *join,Order *first_order,COND *cond, bool change_list, bool *simple_order);
114
static void return_zero_rows(Join *join,
119
uint64_t select_options,
122
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top);
123
static int remove_duplicates(Join *join,Table *entry,List<Item> &fields, Item *having);
124
static int setup_without_group(Session *session,
125
Item **ref_pointer_array,
129
List<Item> &all_fields,
133
bool *hidden_group_fields);
134
static bool make_join_statistics(Join *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
135
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused);
136
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables);
137
static void reset_nj_counters(List<TableList> *join_list);
138
static bool test_if_subpart(Order *a,Order *b);
139
static void restore_prev_nj_state(JoinTable *last);
140
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
141
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
143
Join::Join(Session *session_arg,
144
List<Item> &fields_arg,
145
uint64_t select_options_arg,
146
select_result *result_arg) :
158
sort_and_group(false),
162
no_field_update(false),
164
resume_nested_loop(false),
165
no_const_tables(false),
166
select_distinct(false),
167
group_optimized_away(false),
171
skip_sort_order(false),
175
hidden_group_fields(false),
177
found_const_table_map(0),
184
fetch_limit(HA_POS_ERROR),
185
session(session_arg),
186
fields_list(fields_arg),
191
exec_tmp_table1(NULL),
192
exec_tmp_table2(NULL),
197
having_history(NULL),
198
select_options(select_options_arg),
200
lock(session_arg->open_tables.lock),
202
all_fields(fields_arg),
206
ref_pointer_array(NULL),
211
ref_pointer_array_size(0),
212
zero_result_cause(NULL),
215
join_tab_reexec(NULL)
217
select_distinct= test(select_options & SELECT_DISTINCT);
218
if (&fields_list != &fields_arg) /* only copy if not same*/
219
fields_list= fields_arg;
220
memset(&keyuse, 0, sizeof(keyuse));
221
tmp_table_param.init();
222
tmp_table_param.end_write_records= HA_POS_ERROR;
223
rollup.setState(Rollup::STATE_NONE);
227
* This method is currently only used when a subselect EXPLAIN is performed.
228
* I pulled out the init() method and have simply reset the values to what
229
* was previously in the init() method. See the note about the hack in
232
void Join::reset(Session *session_arg,
233
List<Item> &fields_arg,
234
uint64_t select_options_arg,
235
select_result *result_arg)
248
sort_and_group= false;
252
no_field_update= false;
254
resume_nested_loop= false;
255
no_const_tables= false;
256
select_distinct= false;
257
group_optimized_away= false;
261
skip_sort_order= false;
265
hidden_group_fields= false;
267
found_const_table_map= 0;
274
fetch_limit= HA_POS_ERROR;
275
session= session_arg;
276
fields_list= fields_arg;
281
exec_tmp_table1= NULL;
282
exec_tmp_table2= NULL;
287
having_history= NULL;
288
select_options= select_options_arg;
290
lock= session_arg->open_tables.lock;
292
all_fields= fields_arg;
296
ref_pointer_array= NULL;
301
ref_pointer_array_size= 0;
302
zero_result_cause= NULL;
305
join_tab_reexec= NULL;
306
select_distinct= test(select_options & SELECT_DISTINCT);
307
if (&fields_list != &fields_arg) /* only copy if not same*/
308
fields_list= fields_arg;
309
memset(&keyuse, 0, sizeof(keyuse));
310
tmp_table_param.init();
311
tmp_table_param.end_write_records= HA_POS_ERROR;
312
rollup.setState(Rollup::STATE_NONE);
315
bool Join::is_top_level_join() const
317
return unit == &session->lex().unit && (unit->fake_select_lex == 0 || select_lex == unit->fake_select_lex);
321
Prepare of whole select (including sub queries in future).
324
Add check of calculation of GROUP functions and fields:
325
SELECT COUNT(*)+table.col1 from table1;
332
int Join::prepare(Item ***rref_pointer_array,
333
TableList *tables_init,
340
Select_Lex *select_lex_arg,
341
Select_Lex_Unit *unit_arg)
343
// to prevent double initialization on EXPLAIN
349
group_list= group_init;
351
tables_list= tables_init;
352
select_lex= select_lex_arg;
353
select_lex->join= this;
354
join_list= &select_lex->top_join_list;
355
union_part= unit_arg->is_union();
357
session->lex().current_select->is_item_list_lookup= 1;
359
If we have already executed SELECT, then it have not sense to prevent
360
its table from update (see unique_table())
362
if (session->derived_tables_processing)
363
select_lex->exclude_from_table_unique_test= true;
365
/* Check that all tables, fields, conds and order are ok */
367
if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
368
setup_tables_and_check_access(session, &select_lex->context, join_list, tables_list, &select_lex->leaf_tables, false))
373
TableList *table_ptr;
374
for (table_ptr= select_lex->leaf_tables; table_ptr; table_ptr= table_ptr->next_leaf)
380
if (setup_wild(session, fields_list, &all_fields, wild_num))
382
select_lex->setup_ref_array(session, og_num);
383
if (setup_fields(session, *rref_pointer_array, fields_list, MARK_COLUMNS_READ, &all_fields, 1) ||
384
setup_without_group(session, *rref_pointer_array, tables_list, select_lex->leaf_tables, fields_list,
385
all_fields, &conds, order, group_list, &hidden_group_fields))
388
ref_pointer_array= *rref_pointer_array;
392
nesting_map save_allow_sum_func= session->lex().allow_sum_func;
393
session->setWhere("having clause");
394
session->lex().allow_sum_func|= 1 << select_lex_arg->nest_level;
395
select_lex->having_fix_field= 1;
396
bool having_fix_rc= (!having->fixed &&
397
(having->fix_fields(session, &having) ||
398
having->check_cols(1)));
399
select_lex->having_fix_field= 0;
400
if (having_fix_rc || session->is_error())
402
session->lex().allow_sum_func= save_allow_sum_func;
406
Item_subselect *subselect;
407
Item_in_subselect *in_subs= NULL;
409
Are we in a subquery predicate?
410
TODO: the block below will be executed for every PS execution without need.
412
if ((subselect= select_lex->master_unit()->item))
414
if (subselect->substype() == Item_subselect::IN_SUBS)
415
in_subs= (Item_in_subselect*)subselect;
418
bool do_materialize= true;
420
Check if the subquery predicate can be executed via materialization.
421
The required conditions are:
422
1. Subquery predicate is an IN/=ANY subq predicate
423
2. Subquery is a single SELECT (not a UNION)
424
3. Subquery is not a table-less query. In this case there is no
425
point in materializing.
426
4. Subquery predicate is a top-level predicate
427
(this implies it is not negated)
428
TODO: this is a limitation that should be lifeted once we
429
implement correct NULL semantics (WL#3830)
430
5. Subquery is non-correlated
432
This is an overly restrictive condition. It can be extended to:
433
(Subquery is non-correlated ||
434
Subquery is correlated to any query outer to IN predicate ||
435
(Subquery is correlated to the immediate outer query &&
436
Subquery !contains {GROUP BY, ORDER BY [LIMIT],
437
aggregate functions) && subquery predicate is not under "NOT IN"))
438
6. No execution method was already chosen (by a prepared statement).
440
(*) The subquery must be part of a SELECT statement. The current
441
condition also excludes multi-table update statements.
443
We have to determine whether we will perform subquery materialization
444
before calling the IN=>EXISTS transformation, so that we know whether to
445
perform the whole transformation or only that part of it which wraps
446
Item_in_subselect in an Item_in_optimizer.
448
if (do_materialize &&
450
!select_lex->master_unit()->first_select()->next_select() && // 2
451
select_lex->master_unit()->first_select()->leaf_tables && // 3
452
session->lex().sql_command == SQLCOM_SELECT) // *
454
if (in_subs->is_top_level_item() && // 4
455
!in_subs->is_correlated && // 5
456
in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
457
in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
460
Item_subselect::trans_res trans_res;
461
if ((trans_res= subselect->select_transformer(this)) !=
462
Item_subselect::RES_OK)
464
return((trans_res == Item_subselect::RES_ERROR));
473
for (ord= order; ord; ord= ord->next)
475
Item *item= *ord->item;
476
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
477
item->split_sum_func(session, ref_pointer_array, all_fields);
481
if (having && having->with_sum_func)
482
having->split_sum_func(session, ref_pointer_array, all_fields,
484
if (select_lex->inner_sum_func_list)
486
Item_sum *end=select_lex->inner_sum_func_list;
487
Item_sum *item_sum= end;
490
item_sum= item_sum->next;
491
item_sum->split_sum_func(session, ref_pointer_array,
492
all_fields, item_sum->ref_by, false);
493
} while (item_sum != end);
496
if (select_lex->inner_refs_list.size() &&
497
fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
501
Check if there are references to un-aggregated columns when computing
502
aggregate functions with implicit grouping (there is no GROUP BY).
504
MODE_ONLY_FULL_GROUP_BY is enabled here by default
507
select_lex->full_group_by_flag.test(NON_AGG_FIELD_USED) &&
508
select_lex->full_group_by_flag.test(SUM_FUNC_USED))
510
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
511
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
515
/* Caclulate the number of groups */
517
for (Order *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
525
* The below will create the new table for
526
* CREATE TABLE ... SELECT
528
* @see create_table_from_items() in drizzled/sql_insert.cc
530
if (result && result->prepare(fields_list, unit_arg))
533
/* Init join struct */
534
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
535
ref_pointer_array_size= all_fields.size() * sizeof(Item*);
536
this->group= group_list != 0;
539
#ifdef RESTRICTED_GROUP
540
if (sum_func_count && !group_list && (func_count || field_count))
542
my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
546
if (select_lex->olap == ROLLUP_TYPE && rollup_init())
549
if (alloc_func_list())
556
Remove the predicates pushed down into the subquery
559
Join::remove_subq_pushed_predicates()
560
where IN Must be NULL
561
OUT The remaining WHERE condition, or NULL
564
Given that this join will be executed using (unique|index)_subquery,
565
without "checking NULL", remove the predicates that were pushed down
568
If the subquery compares scalar values, we can remove the condition that
569
was wrapped into trig_cond (it will be checked when needed by the subquery
572
If the subquery compares row values, we need to keep the wrapped
573
equalities in the WHERE clause: when the left (outer) tuple has both NULL
574
and non-NULL values, we'll do a full table scan and will rely on the
575
equalities corresponding to non-NULL parts of left tuple to filter out
576
non-matching records.
578
TODO: We can remove the equalities that will be guaranteed to be true by the
579
fact that subquery engine will be using index lookup. This must be done only
580
for cases where there are no conversion errors of significance, e.g. 257
581
that is searched in a byte. But this requires homogenization of the return
582
codes of all Field*::store() methods.
584
void Join::remove_subq_pushed_predicates(Item **where)
586
if (conds->type() == Item::FUNC_ITEM &&
587
((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
588
((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
589
((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
590
test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
591
((Item_func *)conds)->arguments()[0]))
599
global select optimisation.
602
error code saved in field 'error'
611
// to prevent double initialization on EXPLAIN
616
session->set_proc_info("optimizing");
617
row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
618
unit->select_limit_cnt);
619
/* select_limit is used to decide if we are likely to scan the whole table */
620
select_limit= unit->select_limit_cnt;
621
if (having || (select_options & OPTION_FOUND_ROWS))
622
select_limit= HA_POS_ERROR;
623
do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
624
// Ignore errors of execution if option IGNORE present
625
if (session->lex().ignore)
626
session->lex().current_select->no_error= 1;
628
#ifdef HAVE_REF_TO_FIELDS // Not done yet
629
/* Add HAVING to WHERE if possible */
630
if (having && !group_list && !sum_func_count)
639
conds= new Item_cond_and(conds,having);
642
Item_cond_and can't be fixed after creation, so we do not check
645
conds->fix_fields(session, &conds);
646
conds->change_ref_to_fields(session, tables_list);
647
conds->top_level_item();
653
/* Convert all outer joins to inner joins if possible */
654
conds= simplify_joins(this, join_list, conds, true);
655
build_bitmap_for_nested_joins(join_list, 0);
657
conds= optimize_cond(this, conds, join_list, &cond_value);
658
if (session->is_error())
665
having= optimize_cond(this, having, join_list, &having_value);
666
if (session->is_error())
671
if (select_lex->where)
672
select_lex->cond_value= cond_value;
673
if (select_lex->having)
674
select_lex->having_value= having_value;
676
if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
677
(!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
678
{ /* Impossible cond */
679
zero_result_cause= having_value == Item::COND_FALSE ?
680
"Impossible HAVING" : "Impossible WHERE";
682
goto setup_subq_exit;
686
/* Optimize count(*), cmin() and cmax() */
687
if (tables_list && tmp_table_param.sum_func_count && ! group_list)
691
optimizer::sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
692
to the WHERE conditions,
693
or 1 if all items were resolved,
694
or 0, or an error number HA_ERR_...
696
if ((res= optimizer::sum_query(select_lex->leaf_tables, all_fields, conds)))
698
if (res == HA_ERR_KEY_NOT_FOUND)
700
zero_result_cause= "No matching min/max row";
702
goto setup_subq_exit;
711
zero_result_cause= "No matching min/max row";
713
goto setup_subq_exit;
715
zero_result_cause= "Select tables optimized away";
716
tables_list= 0; // All tables resolved
717
const_tables= tables;
719
Extract all table-independent conditions and replace the WHERE
720
clause with them. All other conditions were computed by optimizer::sum_query
721
and the MIN/MAX/COUNT function(s) have been replaced by constants,
722
so there is no need to compute the whole WHERE clause again.
723
Notice that make_cond_for_table() will always succeed to remove all
724
computed conditions, because optimizer::sum_query() is applicable only to
726
Preserve conditions for EXPLAIN.
728
if (conds && !(session->lex().describe & DESCRIBE_EXTENDED))
730
COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
731
conds= table_independent_conds;
733
goto setup_subq_exit;
741
error= -1; // Error is sent to client
742
sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
744
/* Calculate how to do the join */
745
session->set_proc_info("statistics");
746
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
747
session->is_fatal_error)
752
/* Remove distinct if only const tables */
753
select_distinct= select_distinct && (const_tables != tables);
754
session->set_proc_info("preparing");
755
if (result->initialize_tables(this))
757
return 1; // error == -1
759
if (const_table_map != found_const_table_map &&
760
!(select_options & SELECT_DESCRIBE) &&
762
!(conds->used_tables() & RAND_TABLE_BIT) ||
763
select_lex->master_unit() == &session->lex().unit)) // upper level SELECT
765
zero_result_cause= "no matching row in const table";
766
goto setup_subq_exit;
768
if (!(session->options & OPTION_BIG_SELECTS) &&
769
best_read > (double) session->variables.max_join_size &&
770
!(select_options & SELECT_DESCRIBE))
772
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
776
if (const_tables && !(select_options & SELECT_NO_UNLOCK))
777
session->unlockSomeTables(table, const_tables);
778
if (!conds && outer_join)
780
/* Handle the case where we have an OUTER JOIN without a WHERE */
781
conds=new Item_int((int64_t) 1,1); // Always true
783
select= optimizer::make_select(*table, const_table_map,
784
const_table_map, conds, 1, &error);
791
reset_nj_counters(join_list);
792
make_outerjoin_info(this);
795
Among the equal fields belonging to the same multiple equality
796
choose the one that is to be retrieved first and substitute
797
all references to these in where condition for a reference for
802
conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
803
conds->update_used_tables();
807
Permorm the the optimization on fields evaluation mentioned above
808
for all on expressions.
810
for (JoinTable *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
812
if (*tab->on_expr_ref)
814
*tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
817
(*tab->on_expr_ref)->update_used_tables();
821
if (conds &&!outer_join && const_table_map != found_const_table_map &&
822
(select_options & SELECT_DESCRIBE) &&
823
select_lex->master_unit() == &session->lex().unit) // upper level SELECT
825
conds=new Item_int(0, 1); // Always false
828
if (make_join_select(this, select, conds))
831
"Impossible WHERE noticed after reading const tables";
832
goto setup_subq_exit;
835
error= -1; /* if goto err */
837
/* Optimize distinct away if possible */
839
Order *org_order= order;
840
order= remove_constants(this, order,conds,1, &simple_order);
841
if (session->is_error())
848
If we are using ORDER BY NULL or ORDER BY const_expression,
849
return result in any order (even if we are using a GROUP BY)
851
if (!order && org_order)
855
Check if we can optimize away GROUP BY/DISTINCT.
856
We can do that if there are no aggregate functions, the
857
fields in DISTINCT clause (if present) and/or columns in GROUP BY
858
(if present) contain direct references to all key parts of
859
an unique index (in whatever order) and if the key parts of the
860
unique index cannot contain NULLs.
861
Note that the unique keys for DISTINCT and GROUP BY should not
862
be the same (as long as they are unique).
864
The FROM clause must contain a single non-constant table.
866
if (tables - const_tables == 1 && (group_list || select_distinct) &&
867
! tmp_table_param.sum_func_count &&
868
(! join_tab[const_tables].select ||
869
! join_tab[const_tables].select->quick ||
870
join_tab[const_tables].select->quick->get_type() !=
871
optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))
873
if (group_list && list_contains_unique_index(join_tab[const_tables].table, find_field_in_order_list, (void *) group_list))
876
We have found that grouping can be removed since groups correspond to
877
only one row anyway, but we still have to guarantee correct result
878
order. The line below effectively rewrites the query from GROUP BY
879
<fields> to ORDER BY <fields>. There are two exceptions:
880
- if skip_sort_order is set (see above), then we can simply skip
882
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
883
with the GROUP BY ones, i.e. either one is a prefix of another.
884
We only check if the ORDER BY is a prefix of GROUP BY. In this case
885
test_if_subpart() copies the ASC/DESC attributes from the original
887
If GROUP BY is a prefix of order_st BY, then it is safe to leave
890
if (! order || test_if_subpart(group_list, order))
891
order= skip_sort_order ? 0 : group_list;
893
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
894
rewritten to IGNORE INDEX FOR order_st BY(fields).
896
join_tab->table->keys_in_use_for_order_by=
897
join_tab->table->keys_in_use_for_group_by;
901
if (select_distinct &&
902
list_contains_unique_index(join_tab[const_tables].table,
903
find_field_in_item_list,
904
(void *) &fields_list))
909
if (group_list || tmp_table_param.sum_func_count)
911
if (! hidden_group_fields && rollup.getState() == Rollup::STATE_NONE)
914
else if (select_distinct && tables - const_tables == 1)
917
We are only using one table. In this case we change DISTINCT to a
919
- The GROUP BY can be done through indexes (no sort) and the order_st
920
BY only uses selected fields.
921
(In this case we can later optimize away GROUP BY and order_st BY)
922
- We are scanning the whole table without LIMIT
924
- We are using CALC_FOUND_ROWS
925
- We are using an ORDER BY that can't be optimized away.
927
We don't want to use this optimization when we are using LIMIT
928
because in this case we can just create a temporary table that
929
holds LIMIT rows and stop when this table is full.
931
JoinTable *tab= &join_tab[const_tables];
932
bool all_order_fields_used;
934
skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
935
&tab->table->keys_in_use_for_order_by);
936
if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
937
order, fields_list, all_fields,
938
&all_order_fields_used)))
940
bool skip_group= (skip_sort_order &&
941
test_if_skip_sort_order(tab, group_list, select_limit, 1,
942
&tab->table->keys_in_use_for_group_by) != 0);
943
count_field_types(select_lex, &tmp_table_param, all_fields, 0);
944
if ((skip_group && all_order_fields_used) ||
945
select_limit == HA_POS_ERROR ||
946
(order && !skip_sort_order))
948
/* Change DISTINCT to GROUP BY */
951
if (all_order_fields_used)
953
if (order && skip_sort_order)
956
Force MySQL to read the table in sorted order to get result in
959
tmp_table_param.quick_group=0;
963
group=1; // For end_write_group
968
else if (session->is_fatal_error) // End of memory
973
Order *old_group_list;
974
group_list= remove_constants(this, (old_group_list= group_list), conds,
975
rollup.getState() == Rollup::STATE_NONE,
977
if (session->is_error())
982
if (old_group_list && !group_list)
985
if (!group_list && group)
987
order=0; // The output has only one row
989
select_distinct= 0; // No need in distinct for 1 row
990
group_optimized_away= 1;
993
calc_group_buffer(this, group_list);
994
send_group_parts= tmp_table_param.group_parts; /* Save org parts */
996
if (test_if_subpart(group_list, order) ||
997
(!group_list && tmp_table_param.sum_func_count))
1000
// Can't use sort on head table if using row cache
1010
Check if we need to create a temporary table.
1011
This has to be done if all tables are not already read (const tables)
1012
and one of the following conditions holds:
1013
- We are using DISTINCT (simple distinct's are already optimized away)
1014
- We are using an ORDER BY or GROUP BY on fields not in the first table
1015
- We are using different ORDER BY and GROUP BY orders
1016
- The user wants us to buffer the result.
1018
need_tmp= (const_tables != tables &&
1019
((select_distinct || !simple_order || !simple_group) ||
1020
(group_list && order) ||
1021
test(select_options & OPTION_BUFFER_RESULT)));
1023
// No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1024
make_join_readinfo(*this);
1026
/* Create all structures needed for materialized subquery execution. */
1027
if (setup_subquery_materialization())
1030
/* Cache constant expressions in WHERE, HAVING, ON clauses. */
1031
cache_const_exprs();
1034
is this simple IN subquery?
1036
if (!group_list && !order &&
1037
unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1038
tables == 1 && conds &&
1044
if (join_tab[0].type == AM_EQ_REF && join_tab[0].ref.items[0]->name == in_left_expr_name)
1046
remove_subq_pushed_predicates(&where);
1047
save_index_subquery_explain_info(join_tab, where);
1048
join_tab[0].type= AM_UNIQUE_SUBQUERY;
1050
return(unit->item->change_engine(new subselect_uniquesubquery_engine(session, join_tab, unit->item, where)));
1052
else if (join_tab[0].type == AM_REF &&
1053
join_tab[0].ref.items[0]->name == in_left_expr_name)
1055
remove_subq_pushed_predicates(&where);
1056
save_index_subquery_explain_info(join_tab, where);
1057
join_tab[0].type= AM_INDEX_SUBQUERY;
1059
return(unit->item->change_engine(new subselect_indexsubquery_engine(session, join_tab, unit->item, where, NULL, 0)));
1062
else if (join_tab[0].type == AM_REF_OR_NULL &&
1063
join_tab[0].ref.items[0]->name == in_left_expr_name &&
1064
having->name == in_having_cond)
1066
join_tab[0].type= AM_INDEX_SUBQUERY;
1068
conds= remove_additional_cond(conds);
1069
save_index_subquery_explain_info(join_tab, conds);
1070
return(unit->item->change_engine(new subselect_indexsubquery_engine(session, join_tab, unit->item, conds, having, 1)));
1075
Need to tell handlers that to play it safe, it should fetch all
1076
columns of the primary key of the tables: this is because MySQL may
1077
build row pointers for the rows, and for all columns of the primary key
1078
the read set has not necessarily been set by the server code.
1080
if (need_tmp || select_distinct || group_list || order)
1082
for (uint32_t i = const_tables; i < tables; i++)
1083
join_tab[i].table->prepare_for_position();
1086
if (const_tables != tables)
1089
Because filesort always does a full table scan or a quick range scan
1090
we must add the removed reference to the select for the table.
1091
We only need to do this when we have a simple_order or simple_group
1092
as in other cases the join is done before the sort.
1094
if ((order || group_list) &&
1095
(join_tab[const_tables].type != AM_ALL) &&
1096
(join_tab[const_tables].type != AM_REF_OR_NULL) &&
1097
((order && simple_order) || (group_list && simple_group)))
1099
if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
1104
if (!(select_options & SELECT_BIG_RESULT) &&
1107
!test_if_skip_sort_order(&join_tab[const_tables], group_list,
1108
unit->select_limit_cnt, 0,
1109
&join_tab[const_tables].table->
1110
keys_in_use_for_group_by))) ||
1112
tmp_table_param.quick_group)
1114
need_tmp=1; simple_order=simple_group=0; // Force tmp table without sort
1119
Force using of tmp table if sorting by a SP or UDF function due to
1120
their expensive and probably non-deterministic nature.
1122
for (Order *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1124
Item *item= *tmp_order->item;
1125
if (item->is_expensive())
1127
/* Force tmp table without sort */
1128
need_tmp=1; simple_order=simple_group=0;
1136
if (select_options & SELECT_DESCRIBE)
1144
The loose index scan access method guarantees that all grouping or
1145
duplicate row elimination (for distinct) is already performed
1146
during data retrieval, and that all MIN/MAX functions are already
1147
computed for each group. Thus all MIN/MAX functions should be
1148
treated as regular functions, and there is no need to perform
1149
grouping in the main execution loop.
1150
Notice that currently loose index scan is applicable only for
1151
single table queries, thus it is sufficient to test only the first
1152
join_tab element of the plan for its access method.
1154
if (join_tab->is_using_loose_index_scan())
1155
tmp_table_param.precomputed_group_by= true;
1157
/* Create a tmp table if distinct or if the sort is too complicated */
1160
session->set_proc_info("Creating tmp table");
1162
init_items_ref_array();
1164
tmp_table_param.hidden_field_count= all_fields.size() - fields_list.size();
1165
Order *tmp_group= (((not simple_group) or not (getDebug().test(debug::NO_KEY_GROUP))) ? group_list : NULL);
1168
Pushing LIMIT to the temporary table creation is not applicable
1169
when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1170
there are aggregate functions, because in all these cases we need
1173
ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1175
!session->lex().current_select->with_sum_func) ?
1176
select_limit : HA_POS_ERROR;
1178
if (!(exec_tmp_table1=
1179
create_tmp_table(session, &tmp_table_param, all_fields,
1181
group_list ? 0 : select_distinct,
1182
group_list && simple_group,
1191
We don't have to store rows in temp table that doesn't match HAVING if:
1192
- we are sorting the table and writing complete group rows to the
1194
- We are using DISTINCT without resolving the distinct as a GROUP BY
1197
If having is not handled here, it will be checked before the row
1198
is sent to the client.
1200
if (tmp_having && (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1203
/* if group or order on first table, sort first */
1204
if (group_list && simple_group)
1206
session->set_proc_info("Sorting for group");
1207
if (create_sort_index(session, this, group_list,
1208
HA_POS_ERROR, HA_POS_ERROR, false) ||
1209
alloc_group_fields(this, group_list) ||
1210
make_sum_func_list(all_fields, fields_list, 1) ||
1211
setup_sum_funcs(session, sum_funcs))
1219
if (make_sum_func_list(all_fields, fields_list, 0) ||
1220
setup_sum_funcs(session, sum_funcs))
1225
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1227
session->set_proc_info("Sorting for order");
1228
if (create_sort_index(session, this, order, HA_POS_ERROR, HA_POS_ERROR, true))
1237
Optimize distinct when used on some of the tables
1238
SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1239
In this case we can stop scanning t2 when we have found one t1.a
1242
if (exec_tmp_table1->distinct)
1244
table_map used_tables= session->used_tables;
1245
JoinTable *last_join_tab= join_tab+tables-1;
1248
if (used_tables & last_join_tab->table->map)
1250
last_join_tab->not_used_in_distinct=1;
1251
} while (last_join_tab-- != join_tab);
1252
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1253
if (order && skip_sort_order)
1255
/* Should always succeed */
1256
if (test_if_skip_sort_order(&join_tab[const_tables],
1257
order, unit->select_limit_cnt, 0,
1258
&join_tab[const_tables].table->
1259
keys_in_use_for_order_by))
1265
If this join belongs to an uncacheable subquery save
1268
if (select_lex->uncacheable.any() && not is_top_level_join())
1269
init_save_join_tab();
1276
/* Even with zero matching rows, subqueries in the HAVING clause
1277
may need to be evaluated if there are aggregate functions in the query.
1279
if (setup_subquery_materialization())
1286
Restore values in temporary join.
1288
void Join::restore_tmp()
1290
memcpy(tmp_join, this, (size_t) sizeof(Join));
1295
unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1296
select_lex->offset_limit->val_uint() :
1301
if (exec_tmp_table1)
1303
exec_tmp_table1->cursor->extra(HA_EXTRA_RESET_STATE);
1304
exec_tmp_table1->cursor->ha_delete_all_rows();
1305
exec_tmp_table1->free_io_cache();
1306
exec_tmp_table1->filesort_free_buffers();
1308
if (exec_tmp_table2)
1310
exec_tmp_table2->cursor->extra(HA_EXTRA_RESET_STATE);
1311
exec_tmp_table2->cursor->ha_delete_all_rows();
1312
exec_tmp_table2->free_io_cache();
1313
exec_tmp_table2->filesort_free_buffers();
1316
set_items_ref_array(items0);
1319
memcpy(join_tab, join_tab_save, sizeof(JoinTable) * tables);
1324
/* Reset of sum functions */
1327
Item_sum *func, **func_ptr= sum_funcs;
1328
while ((func= *(func_ptr++)))
1336
@brief Save the original join layout
1338
@details Saves the original join layout so it can be reused in
1339
re-execution and for EXPLAIN.
1341
@return Operation status
1343
@retval 1 error occurred.
1345
void Join::init_save_join_tab()
1347
tmp_join= (Join*)session->mem.alloc(sizeof(Join));
1349
error= 0; // Ensure that tmp_join.error= 0
1353
void Join::save_join_tab()
1355
if (! join_tab_save && select_lex->master_unit()->uncacheable.any())
1357
join_tab_save= (JoinTable*)session->mem.memdup(join_tab, sizeof(JoinTable) * tables);
1365
Note, that create_sort_index calls test_if_skip_sort_order and may
1366
finally replace sorting with index scan if there is a LIMIT clause in
1367
the query. It's never shown in EXPLAIN!
1370
When can we have here session->net.report_error not zero?
1374
List<Item> *columns_list= &fields_list;
1377
session->set_proc_info("executing");
1380
if (!tables_list && (tables || !select_lex->with_sum_func))
1382
/* Only test of functions */
1383
if (select_options & SELECT_DESCRIBE)
1385
optimizer::ExplainPlan planner(this,
1389
(zero_result_cause ? zero_result_cause : "No tables used"));
1390
planner.printPlan();
1394
result->send_fields(*columns_list);
1396
We have to test for 'conds' here as the WHERE may not be constant
1397
even if we don't have any tables for prepared statements or if
1398
conds uses something like 'rand()'.
1400
if (cond_value != Item::COND_FALSE &&
1401
(!conds || conds->val_int()) &&
1402
(!having || having->val_int()))
1404
if (do_send_rows && result->send_data(fields_list))
1408
error= (int) result->send_eof();
1409
send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : session->sent_row_count);
1414
error= (int) result->send_eof();
1418
/* Single select (without union) always returns 0 or 1 row */
1419
session->limit_found_rows= send_records;
1420
session->examined_row_count= 0;
1424
Don't reset the found rows count if there're no tables as
1425
FOUND_ROWS() may be called. Never reset the examined row count here.
1426
It must be accumulated from all join iterations of all join parts.
1429
session->limit_found_rows= 0;
1431
if (zero_result_cause)
1433
return_zero_rows(this, result, select_lex->leaf_tables, *columns_list, send_row_on_empty_set(), select_options, zero_result_cause, having);
1437
if (select_options & SELECT_DESCRIBE)
1440
Check if we managed to optimize ORDER BY away and don't use temporary
1441
table to resolve order_st BY: in that case, we only may need to do
1442
filesort for GROUP BY.
1444
if (!order && !no_order && (!skip_sort_order || !need_tmp))
1446
/* Reset 'order' to 'group_list' and reinit variables describing 'order' */
1448
simple_order= simple_group;
1451
if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
1453
if (const_tables == tables
1454
|| ((simple_order || skip_sort_order)
1455
&& test_if_skip_sort_order(&join_tab[const_tables], order, select_limit, 0, &join_tab[const_tables].table->keys_in_use_for_query)))
1459
optimizer::ExplainPlan planner(this,
1461
order != 0 && ! skip_sort_order,
1463
! tables ? "No tables used" : NULL);
1464
planner.printPlan();
1468
Join *curr_join= this;
1469
List<Item> *curr_all_fields= &all_fields;
1470
List<Item> *curr_fields_list= &fields_list;
1471
Table *curr_tmp_table= 0;
1473
Initialize examined rows here because the values from all join parts
1474
must be accumulated in examined_row_count. Hence every join
1475
iteration must count from zero.
1477
curr_join->examined_rows= 0;
1479
/* Create a tmp table if distinct or if the sort is too complicated */
1485
We are in a non cacheable sub query. Get the saved join structure
1487
(curr_join may have been modified during last exection and we need
1490
curr_join= tmp_join;
1492
curr_tmp_table= exec_tmp_table1;
1494
/* Copy data to the temporary table */
1495
session->set_proc_info("Copying to tmp table");
1496
if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
1497
curr_join->join_tab[curr_join->const_tables].sorted= 0;
1498
if ((tmp_error= do_select(curr_join, NULL, curr_tmp_table)))
1503
curr_tmp_table->cursor->info(HA_STATUS_VARIABLE);
1505
if (curr_join->having)
1506
curr_join->having= curr_join->tmp_having= 0; // Allready done
1508
/* Change sum_fields reference to calculated fields in tmp_table */
1509
curr_join->all_fields= *curr_all_fields;
1512
items1= items0 + all_fields.size();
1513
if (sort_and_group || curr_tmp_table->group)
1515
if (change_to_use_tmp_fields(session, items1,
1516
tmp_fields_list1, tmp_all_fields1,
1517
fields_list.size(), all_fields))
1522
if (change_refs_to_tmp_fields(session, items1,
1523
tmp_fields_list1, tmp_all_fields1,
1524
fields_list.size(), all_fields))
1527
curr_join->tmp_all_fields1= tmp_all_fields1;
1528
curr_join->tmp_fields_list1= tmp_fields_list1;
1529
curr_join->items1= items1;
1531
curr_all_fields= &tmp_all_fields1;
1532
curr_fields_list= &tmp_fields_list1;
1533
curr_join->set_items_ref_array(items1);
1535
if (sort_and_group || curr_tmp_table->group)
1537
curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count
1538
+ curr_join->tmp_table_param.func_count;
1539
curr_join->tmp_table_param.sum_func_count= 0;
1540
curr_join->tmp_table_param.func_count= 0;
1544
curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.func_count;
1545
curr_join->tmp_table_param.func_count= 0;
1548
if (curr_tmp_table->group)
1549
{ // Already grouped
1550
if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
1551
curr_join->order= curr_join->group_list; /* order by group */
1552
curr_join->group_list= 0;
1556
If we have different sort & group then we must sort the data by group
1557
and copy it to another tmp table
1558
This code is also used if we are using distinct something
1559
we haven't been able to store in the temporary table yet
1560
like SEC_TO_TIME(SUM(...)).
1563
if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct))
1564
|| (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
1565
{ /* Must copy to another table */
1566
/* Free first data from old join */
1567
curr_join->join_free();
1568
make_simple_join(curr_join, curr_tmp_table);
1569
calc_group_buffer(curr_join, group_list);
1570
count_field_types(select_lex, &curr_join->tmp_table_param,
1571
curr_join->tmp_all_fields1,
1572
curr_join->select_distinct && !curr_join->group_list);
1573
curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.size()
1574
- curr_join->tmp_fields_list1.size();
1576
if (exec_tmp_table2)
1578
curr_tmp_table= exec_tmp_table2;
1582
/* group data to new table */
1585
If the access method is loose index scan then all MIN/MAX
1586
functions are precomputed, and should be treated as regular
1587
functions. See extended comment in Join::exec.
1589
if (curr_join->join_tab->is_using_loose_index_scan())
1590
curr_join->tmp_table_param.precomputed_group_by= true;
1592
if (!(curr_tmp_table=
1593
exec_tmp_table2= create_tmp_table(session,
1594
&curr_join->tmp_table_param,
1597
curr_join->select_distinct &&
1598
!curr_join->group_list,
1599
1, curr_join->select_options,
1606
curr_join->exec_tmp_table2= exec_tmp_table2;
1608
if (curr_join->group_list)
1610
session->set_proc_info("Creating sort index");
1611
if (curr_join->join_tab == join_tab)
1613
if (create_sort_index(session, curr_join, curr_join->group_list, HA_POS_ERROR, HA_POS_ERROR, false) ||
1614
make_group_fields(this, curr_join))
1618
sortorder= curr_join->sortorder;
1621
session->set_proc_info("Copying to group table");
1623
if (curr_join != this)
1627
curr_join->sum_funcs= sum_funcs2;
1628
curr_join->sum_funcs_end= sum_funcs_end2;
1632
curr_join->alloc_func_list();
1633
sum_funcs2= curr_join->sum_funcs;
1634
sum_funcs_end2= curr_join->sum_funcs_end;
1637
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
1639
curr_join->group_list= 0;
1641
if (!curr_join->sort_and_group && (curr_join->const_tables != curr_join->tables))
1642
curr_join->join_tab[curr_join->const_tables].sorted= 0;
1644
if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs)
1645
|| (tmp_error= do_select(curr_join, NULL, curr_tmp_table)))
1650
curr_join->join_tab->read_record.end_read_record();
1651
curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
1652
curr_join->join_tab[0].table= 0; // Table is freed
1654
// No sum funcs anymore
1657
items2= items1 + all_fields.size();
1658
if (change_to_use_tmp_fields(session, items2,
1659
tmp_fields_list2, tmp_all_fields2,
1660
fields_list.size(), tmp_all_fields1))
1662
curr_join->tmp_fields_list2= tmp_fields_list2;
1663
curr_join->tmp_all_fields2= tmp_all_fields2;
1665
curr_fields_list= &curr_join->tmp_fields_list2;
1666
curr_all_fields= &curr_join->tmp_all_fields2;
1667
curr_join->set_items_ref_array(items2);
1668
curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count;
1669
curr_join->tmp_table_param.sum_func_count= 0;
1671
if (curr_tmp_table->distinct)
1672
curr_join->select_distinct=0; /* Each row is unique */
1674
curr_join->join_free(); /* Free quick selects */
1675
if (curr_join->select_distinct && ! curr_join->group_list)
1677
session->set_proc_info("Removing duplicates");
1678
if (curr_join->tmp_having)
1679
curr_join->tmp_having->update_used_tables();
1681
if (remove_duplicates(curr_join, curr_tmp_table,
1682
*curr_fields_list, curr_join->tmp_having))
1685
curr_join->tmp_having=0;
1686
curr_join->select_distinct=0;
1688
curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
1689
make_simple_join(curr_join, curr_tmp_table);
1690
calc_group_buffer(curr_join, curr_join->group_list);
1691
count_field_types(select_lex, &curr_join->tmp_table_param, *curr_all_fields, 0);
1695
if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
1697
if (make_group_fields(this, curr_join))
1703
init_items_ref_array();
1704
items3= ref_pointer_array + (all_fields.size() * 4);
1705
setup_copy_fields(session, &curr_join->tmp_table_param,
1706
items3, tmp_fields_list3, tmp_all_fields3,
1707
curr_fields_list->size(), *curr_all_fields);
1708
tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
1709
tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
1710
tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
1711
curr_join->tmp_all_fields3= tmp_all_fields3;
1712
curr_join->tmp_fields_list3= tmp_fields_list3;
1716
curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
1717
curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
1718
curr_join->tmp_table_param.copy_field_end= tmp_table_param.save_copy_field_end;
1720
curr_fields_list= &tmp_fields_list3;
1721
curr_all_fields= &tmp_all_fields3;
1722
curr_join->set_items_ref_array(items3);
1724
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
1726
setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
1727
session->is_fatal_error)
1730
if (curr_join->group_list || curr_join->order)
1732
session->set_proc_info("Sorting result");
1733
/* If we have already done the group, add HAVING to sorted table */
1734
if (curr_join->tmp_having && ! curr_join->group_list && ! curr_join->sort_and_group)
1736
// Some tables may have been const
1737
curr_join->tmp_having->update_used_tables();
1738
JoinTable *curr_table= &curr_join->join_tab[curr_join->const_tables];
1739
table_map used_tables= (curr_join->const_table_map |
1740
curr_table->table->map);
1742
Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
1743
if (sort_table_cond)
1745
if (!curr_table->select)
1746
curr_table->select= new optimizer::SqlSelect;
1747
if (!curr_table->select->cond)
1748
curr_table->select->cond= sort_table_cond;
1749
else // This should never happen
1751
curr_table->select->cond= new Item_cond_and(curr_table->select->cond, sort_table_cond);
1753
Item_cond_and do not need fix_fields for execution, its parameters
1754
are fixed or do not need fix_fields, too
1756
curr_table->select->cond->quick_fix_field();
1758
curr_table->select_cond= curr_table->select->cond;
1759
curr_table->select_cond->top_level_item();
1760
curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having, ~ (table_map) 0, ~used_tables, 0);
1765
curr_join->select_limit= HA_POS_ERROR;
1769
We can abort sorting after session->select_limit rows if we there is no
1770
WHERE clause for any tables after the sorted one.
1772
JoinTable *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
1773
JoinTable *end_table= &curr_join->join_tab[curr_join->tables];
1774
for (; curr_table < end_table ; curr_table++)
1777
table->keyuse is set in the case there was an original WHERE clause
1778
on the table that was optimized away.
1780
if (curr_table->select_cond ||
1781
(curr_table->keyuse && !curr_table->first_inner))
1783
/* We have to sort all rows */
1784
curr_join->select_limit= HA_POS_ERROR;
1789
if (curr_join->join_tab == join_tab)
1792
Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
1793
chose FILESORT to be faster than INDEX SCAN or there is no
1794
suitable index present.
1795
Note, that create_sort_index calls test_if_skip_sort_order and may
1796
finally replace sorting with index scan if there is a LIMIT clause in
1797
the query. XXX: it's never shown in EXPLAIN!
1798
OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
1800
if (create_sort_index(session, curr_join,
1801
curr_join->group_list ?
1802
curr_join->group_list : curr_join->order,
1803
curr_join->select_limit,
1804
(select_options & OPTION_FOUND_ROWS ?
1805
HA_POS_ERROR : unit->select_limit_cnt),
1806
curr_join->group_list ? true : false))
1809
sortorder= curr_join->sortorder;
1810
if (curr_join->const_tables != curr_join->tables &&
1811
!curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
1814
If no IO cache exists for the first table then we are using an
1815
INDEX SCAN and no filesort. Thus we should not remove the sorted
1816
attribute on the INDEX SCAN.
1822
/* XXX: When can we have here session->is_error() not zero? */
1823
if (session->is_error())
1825
error= session->is_error();
1828
curr_join->having= curr_join->tmp_having;
1829
curr_join->fields= curr_fields_list;
1831
session->set_proc_info("Sending data");
1832
result->send_fields(*curr_fields_list);
1833
error= do_select(curr_join, curr_fields_list, NULL);
1834
session->limit_found_rows= curr_join->send_records;
1836
/* Accumulate the counts from all join iterations of all join parts. */
1837
session->examined_row_count+= curr_join->examined_rows;
1840
With EXPLAIN EXTENDED we have to restore original ref_array
1841
for a derived table which is always materialized.
1842
Otherwise we would not be able to print the query correctly.
1844
if (items0 && (session->lex().describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1845
set_items_ref_array(items0);
1854
Return error that hold Join.
1858
select_lex->join= 0;
1862
if (join_tab != tmp_join->join_tab)
1864
JoinTable *tab, *end;
1865
for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
1868
tmp_join->tmp_join= 0;
1869
tmp_table_param.copy_field=0;
1870
return(tmp_join->destroy());
1875
exec_tmp_table1= NULL;
1876
exec_tmp_table2= NULL;
1883
Setup for execution all subqueries of a query, for which the optimizer
1884
chose hash semi-join.
1886
@details Iterate over all subqueries of the query, and if they are under an
1887
IN predicate, and the optimizer chose to compute it via hash semi-join:
1888
- try to initialize all data structures needed for the materialized execution
1889
of the IN predicate,
1890
- if this fails, then perform the IN=>EXISTS transformation which was
1891
previously blocked during Join::prepare.
1893
This method is part of the "code generation" query processing phase.
1895
This phase must be called after substitute_for_best_equal_field() because
1896
that function may replace items with other items from a multiple equality,
1897
and we need to reference the correct items in the index access method of the
1900
@return Operation status
1901
@retval false success.
1902
@retval true error occurred.
1904
bool Join::setup_subquery_materialization()
1906
for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
1907
un= un->next_unit())
1909
for (Select_Lex *sl= un->first_select(); sl; sl= sl->next_select())
1911
Item_subselect *subquery_predicate= sl->master_unit()->item;
1912
if (subquery_predicate &&
1913
subquery_predicate->substype() == Item_subselect::IN_SUBS)
1915
Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
1916
if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
1917
in_subs->setup_engine())
1926
Partially cleanup Join after it has executed: close index or rnd read
1927
(table cursors), free quick selects.
1929
This function is called in the end of execution of a Join, before the used
1930
tables are unlocked and closed.
1932
For a join that is resolved using a temporary table, the first sweep is
1933
performed against actual tables and an intermediate result is inserted
1934
into the temprorary table.
1935
The last sweep is performed against the temporary table. Therefore,
1936
the base tables and associated buffers used to fill the temporary table
1937
are no longer needed, and this function is called to free them.
1939
For a join that is performed without a temporary table, this function
1940
is called after all rows are sent, but before EOF packet is sent.
1942
For a simple SELECT with no subqueries this function performs a full
1943
cleanup of the Join and calls unlockReadTables to free used base
1946
If a Join is executed for a subquery or if it has a subquery, we can't
1947
do the full cleanup and need to do a partial cleanup only.
1948
- If a Join is not the top level join, we must not unlock the tables
1949
because the outer select may not have been evaluated yet, and we
1950
can't unlock only selected tables of a query.
1951
- Additionally, if this Join corresponds to a correlated subquery, we
1952
should not free quick selects and join buffers because they will be
1953
needed for the next execution of the correlated subquery.
1954
- However, if this is a Join for a [sub]select, which is not
1955
a correlated subquery itself, but has subqueries, we can free it
1956
fully and also free Joins of all its subqueries. The exception
1957
is a subquery in SELECT list, e.g: @n
1958
SELECT a, (select cmax(b) from t1) group by c @n
1959
This subquery will not be evaluated at first sweep and its value will
1960
not be inserted into the temporary table. Instead, it's evaluated
1961
when selecting from the temporary table. Therefore, it can't be freed
1962
here even though it's not correlated.
1965
Unlock tables even if the join isn't top level select in the tree
1967
void Join::join_free()
1969
Select_Lex_Unit *tmp_unit;
1972
Optimization: if not EXPLAIN and we are done with the Join,
1975
bool full= (select_lex->uncacheable.none() && ! session->lex().describe);
1976
bool can_unlock= full;
1980
for (tmp_unit= select_lex->first_inner_unit();
1982
tmp_unit= tmp_unit->next_unit())
1983
for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
1985
Item_subselect *subselect= sl->master_unit()->item;
1986
bool full_local= full && (!subselect ||
1987
(subselect->is_evaluated() &&
1988
!subselect->is_uncacheable()));
1990
If this join is evaluated, we can fully clean it up and clean up all
1991
its underlying joins even if they are correlated -- they will not be
1992
used any more anyway.
1993
If this join is not yet evaluated, we still must clean it up to
1994
close its table cursors -- it may never get evaluated, as in case of
1995
... HAVING false OR a IN (SELECT ...))
1996
but all table cursors must be closed before the unlock.
1998
sl->cleanup_all_joins(full_local);
1999
/* Can't unlock if at least one Join is still needed */
2000
can_unlock= can_unlock && full_local;
2004
We are not using tables anymore
2005
Unlock all tables. We may be in an INSERT .... SELECT statement.
2007
if (can_unlock && lock && session->open_tables.lock &&
2008
!(select_options & SELECT_NO_UNLOCK) &&
2009
!select_lex->subquery_in_having &&
2010
(select_lex == (session->lex().unit.fake_select_lex ?
2011
session->lex().unit.fake_select_lex : &session->lex().select_lex)))
2014
TODO: unlock tables even if the join isn't top level select in the
2017
session->unlockReadTables(lock); // Don't free join->lock
2026
Free resources of given join.
2028
@param fill true if we should free all resources, call with full==1
2029
should be last, before it this function can be called with
2033
With subquery this function definitely will be called several times,
2034
but even for simple query it can be called several times.
2036
void Join::cleanup(bool full)
2041
Only a sorted table may be cached. This sorted table is always the
2042
first non const table in join->table
2044
if (tables > const_tables) // Test for not-const tables
2046
table[const_tables]->free_io_cache();
2047
table[const_tables]->filesort_free_buffers(full);
2053
JoinTable *tab,*end;
2057
for (tab= join_tab, end= tab+tables; tab != end; tab++)
2063
for (tab= join_tab, end= tab+tables; tab != end; tab++)
2066
tab->table->cursor->ha_index_or_rnd_end();
2072
We are not using tables anymore
2073
Unlock all tables. We may be in an INSERT .... SELECT statement.
2078
tmp_table_param.copy_field= 0;
2079
group_fields.delete_elements();
2081
We can't call delete_elements() on copy_funcs as this will cause
2082
problems in free_elements() as some of the elements are then deleted.
2084
tmp_table_param.copy_funcs.clear();
2086
If we have tmp_join and 'this' Join is not tmp_join and
2087
tmp_table_param.copy_field's of them are equal then we have to remove
2088
pointer to tmp_table_param.copy_field from tmp_join, because it qill
2089
be removed in tmp_table_param.cleanup().
2093
tmp_join->tmp_table_param.copy_field ==
2094
tmp_table_param.copy_field)
2096
tmp_join->tmp_table_param.copy_field=
2097
tmp_join->tmp_table_param.save_copy_field= 0;
2099
tmp_table_param.cleanup();
2105
used only in Join::clear
2107
static void clear_tables(Join *join)
2110
must clear only the non-const tables, as const tables
2111
are not re-calculated.
2113
for (uint32_t i= join->const_tables; i < join->tables; i++)
2115
join->table[i]->mark_as_null_row(); // All fields are NULL
2120
Make an array of pointers to sum_functions to speed up
2121
sum_func calculation.
2128
bool Join::alloc_func_list()
2130
uint32_t func_count, group_parts;
2132
func_count= tmp_table_param.sum_func_count;
2134
If we are using rollup, we need a copy of the summary functions for
2137
if (rollup.getState() != Rollup::STATE_NONE)
2138
func_count*= (send_group_parts+1);
2140
group_parts= send_group_parts;
2142
If distinct, reserve memory for possible
2143
disctinct->group_by optimization
2145
if (select_distinct)
2147
group_parts+= fields_list.size();
2149
If the order_st clause is specified then it's possible that
2150
it also will be optimized, so reserve space for it too
2155
for (ord= order; ord; ord= ord->next)
2160
/* This must use calloc() as rollup_make_fields depends on this */
2161
sum_funcs= (Item_sum**) session->mem.calloc(sizeof(Item_sum**) * (func_count+1) +
2162
sizeof(Item_sum***) * (group_parts+1));
2163
sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
2164
return(sum_funcs == 0);
2168
Initialize 'sum_funcs' array with all Item_sum objects.
2170
@param field_list All items
2171
@param send_fields Items in select list
2172
@param before_group_by Set to 1 if this is called before GROUP BY handling
2173
@param recompute Set to true if sum_funcs must be recomputed
2180
bool Join::make_sum_func_list(List<Item> &field_list,
2181
List<Item> &send_fields,
2182
bool before_group_by,
2185
List<Item>::iterator it(field_list.begin());
2189
if (*sum_funcs && !recompute)
2190
return false; /* We have already initialized sum_funcs. */
2195
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
2196
(!((Item_sum*) item)->depended_from() ||
2197
((Item_sum *)item)->depended_from() == select_lex))
2198
*func++= (Item_sum*) item;
2200
if (before_group_by && rollup.getState() == Rollup::STATE_INITED)
2202
rollup.setState(Rollup::STATE_READY);
2203
if (rollup_make_fields(field_list, send_fields, &func))
2204
return true; // Should never happen
2206
else if (rollup.getState() == Rollup::STATE_NONE)
2208
for (uint32_t i=0 ; i <= send_group_parts ;i++)
2209
sum_funcs_end[i]= func;
2211
else if (rollup.getState() == Rollup::STATE_READY)
2212
return false; // Don't put end marker
2213
*func=0; // End marker
2217
/** Allocate memory needed for other rollup functions. */
2218
bool Join::rollup_init()
2221
tmp_table_param.quick_group= 0; // Can't create groups in tmp table
2222
rollup.setState(Rollup::STATE_INITED);
2225
Create pointers to the different sum function groups
2226
These are updated by rollup_make_fields()
2228
tmp_table_param.group_parts= send_group_parts;
2230
rollup.setNullItems((Item_null_result**) session->mem.alloc((sizeof(Item*) + sizeof(Item**) + sizeof(List<Item>) + ref_pointer_array_size) * send_group_parts));
2231
rollup.setFields((List<Item>*) (rollup.getNullItems() + send_group_parts));
2232
rollup.setRefPointerArrays((Item***) (rollup.getFields() + send_group_parts));
2233
Item** ref_array= (Item**) (rollup.getRefPointerArrays()+send_group_parts);
2236
Prepare space for field list for the different levels
2237
These will be filled up in rollup_make_fields()
2239
for (uint32_t i= 0 ; i < send_group_parts ; i++)
2241
rollup.getNullItems()[i]= new (session->mem) Item_null_result();
2242
List<Item> *rollup_fields= &rollup.getFields()[i];
2243
rollup_fields->clear();
2244
rollup.getRefPointerArrays()[i]= ref_array;
2245
ref_array+= all_fields.size();
2248
for (uint32_t i= 0 ; i < send_group_parts; i++)
2250
for (uint32_t j= 0 ; j < fields_list.size() ; j++)
2252
rollup.getFields()[i].push_back(rollup.getNullItems()[i]);
2256
List<Item>::iterator it(all_fields.begin());
2257
while (Item* item= it++)
2260
bool found_in_group= 0;
2262
for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
2264
if (*group_tmp->item == item)
2266
item->maybe_null= 1;
2268
if (item->const_item())
2271
For ROLLUP queries each constant item referenced in GROUP BY list
2272
is wrapped up into an Item_func object yielding the same value
2273
as the constant item. The objects of the wrapper class are never
2274
considered as constant items and besides they inherit all
2275
properties of the Item_result_field class.
2276
This wrapping allows us to ensure writing constant items
2277
into temporary tables whenever the result of the ROLLUP
2278
operation has to be written into a temporary table, e.g. when
2279
ROLLUP is used together with DISTINCT in the SELECT list.
2280
Usually when creating temporary tables for a intermidiate
2281
result we do not include fields for constant expressions.
2283
Item* new_item= new Item_func_rollup_const(item);
2284
new_item->fix_fields(session, NULL);
2285
*it.ref()= new_item;
2286
for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
2288
if (*tmp->item == item)
2289
*tmp->item= new_item;
2294
if (item->type() == Item::FUNC_ITEM && !found_in_group)
2296
bool changed= false;
2297
if (change_group_ref(session, (Item_func *) item, group_list, &changed))
2300
We have to prevent creation of a field in a temporary table for
2301
an expression that contains GROUP BY attributes.
2302
Marking the expression item as 'with_sum_func' will ensure this.
2305
item->with_sum_func= 1;
2312
Fill up rollup structures with pointers to fields to use.
2314
Creates copies of item_sum items for each sum level.
2316
@param fields_arg List of all fields (hidden and real ones)
2317
@param sel_fields Pointer to selected fields
2318
@param func Store here a pointer to all fields
2322
In this case func is pointing to next not used element.
2326
bool Join::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
2328
List<Item>::iterator it(fields_arg.begin());
2329
Item *first_field= &sel_fields.front();
2333
Create field lists for the different levels
2335
The idea here is to have a separate field list for each rollup level to
2336
avoid all runtime checks of which columns should be NULL.
2338
The list is stored in reverse order to get sum function in such an order
2339
in func that it makes it easy to reset them with init_sum_functions()
2341
Assuming: SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
2343
rollup.fields[0] will contain list where a,b,c is NULL
2344
rollup.fields[1] will contain list where b,c is NULL
2346
rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
2348
sum_funcs_end[0] points to all sum functions
2349
sum_funcs_end[1] points to all sum functions, except grand totals
2353
for (level=0 ; level < send_group_parts ; level++)
2356
uint32_t pos= send_group_parts - level -1;
2357
bool real_fields= 0;
2359
List<Item>::iterator new_it(rollup.getFields()[pos].begin());
2360
Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2363
/* Point to first hidden field */
2364
Item **ref_array= ref_array_start + fields_arg.size()-1;
2366
/* Remember where the sum functions ends for the previous level */
2367
sum_funcs_end[pos+1]= *func;
2369
/* Find the start of the group for this level */
2370
for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
2373
it= fields_arg.begin();
2374
while ((item= it++))
2376
if (item == first_field)
2378
real_fields= 1; // End of hidden fields
2379
ref_array= ref_array_start;
2382
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
2383
(!((Item_sum*) item)->depended_from() ||
2384
((Item_sum *)item)->depended_from() == select_lex))
2388
This is a top level summary function that must be replaced with
2389
a sum function that is reset for this level.
2391
NOTE: This code creates an object which is not that nice in a
2392
sub select. Fortunately it's not common to have rollup in
2395
item= item->copy_or_same(session);
2396
((Item_sum*) item)->make_unique();
2397
*(*func)= (Item_sum*) item;
2402
/* Check if this is something that is part of this group by */
2404
for (group_tmp= start_group, i= pos ;
2405
group_tmp ; group_tmp= group_tmp->next, i++)
2407
if (*group_tmp->item == item)
2410
This is an element that is used by the GROUP BY and should be
2411
set to NULL in this level
2413
Item_null_result *null_item= new (session->mem) Item_null_result();
2414
item->maybe_null= 1; // Value will be null sometimes
2415
null_item->result_field= item->get_tmp_table_field();
2424
(void) new_it++; // Point to next item
2425
new_it.replace(item); // Replace previous
2432
sum_funcs_end[0]= *func; // Point to last function
2437
Send all rollup levels higher than the current one to the client.
2441
SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
2444
@param idx Level we are on:
2445
- 0 = Total sum level
2446
- 1 = First group changed (a)
2447
- 2 = Second group changed (a,b)
2452
1 If send_data_failed()
2454
int Join::rollup_send_data(uint32_t idx)
2456
for (uint32_t i= send_group_parts ; i-- > idx ; )
2458
/* Get reference pointers to sum functions in place */
2459
memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i], ref_pointer_array_size);
2461
if ((!having || having->val_int()))
2463
if (send_records < unit->select_limit_cnt && do_send_rows && result->send_data(rollup.getFields()[i]))
2470
/* Restore ref_pointer_array */
2471
set_items_ref_array(current_ref_pointer_array);
2477
Write all rollup levels higher than the current one to a temp table.
2481
SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
2484
@param idx Level we are on:
2485
- 0 = Total sum level
2486
- 1 = First group changed (a)
2487
- 2 = Second group changed (a,b)
2488
@param table reference to temp table
2493
1 if write_data_failed()
2495
int Join::rollup_write_data(uint32_t idx, Table *table_arg)
2497
for (uint32_t i= send_group_parts ; i-- > idx ; )
2499
/* Get reference pointers to sum functions in place */
2500
memcpy(ref_pointer_array, rollup.getRefPointerArrays()[i],
2501
ref_pointer_array_size);
2502
if ((!having || having->val_int()))
2504
List<Item>::iterator it(rollup.getFields()[i].begin());
2505
while (Item* item= it++)
2507
if (item->type() == Item::NULL_ITEM && item->is_result_field())
2508
item->save_in_result_field(1);
2510
copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
2511
if (table_arg->cursor->insertRecord(table_arg->getInsertRecord()))
2513
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2518
/* Restore ref_pointer_array */
2519
set_items_ref_array(current_ref_pointer_array);
2525
clear results if there are not rows found for group
2526
(end_send_group/end_write_group)
2531
copy_fields(&tmp_table_param);
2535
Item_sum *func, **func_ptr= sum_funcs;
2536
while ((func= *(func_ptr++)))
2542
change select_result object of Join.
2544
@param res new select_result object
2551
bool Join::change_result(select_result *res)
2554
if (result->prepare(fields_list, select_lex->master_unit()))
2562
Cache constant expressions in WHERE, HAVING, ON conditions.
2565
void Join::cache_const_exprs()
2567
bool cache_flag= false;
2568
bool *analyzer_arg= &cache_flag;
2570
/* No need in cache if all tables are constant. */
2571
if (const_tables == tables)
2575
conds->compile(&Item::cache_const_expr_analyzer, (unsigned char **)&analyzer_arg,
2576
&Item::cache_const_expr_transformer, (unsigned char *)&cache_flag);
2579
having->compile(&Item::cache_const_expr_analyzer, (unsigned char **)&analyzer_arg,
2580
&Item::cache_const_expr_transformer, (unsigned char *)&cache_flag);
2582
for (JoinTable *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
2584
if (*tab->on_expr_ref)
2587
(*tab->on_expr_ref)->compile(&Item::cache_const_expr_analyzer,
2588
(unsigned char **)&analyzer_arg,
2589
&Item::cache_const_expr_transformer,
2590
(unsigned char *)&cache_flag);
2598
Process one record of the nested loop join.
2602
This function will evaluate parts of WHERE/ON clauses that are
2603
applicable to the partial record on hand and in case of success
2604
submit this record to the next level of the nested loop.
2606
enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error)
2608
bool not_used_in_distinct= join_tab->not_used_in_distinct;
2609
ha_rows found_records= join->found_records;
2610
COND *select_cond= join_tab->select_cond;
2612
if (error > 0 || (join->session->is_error())) // Fatal error
2613
return NESTED_LOOP_ERROR;
2615
return NESTED_LOOP_NO_MORE_ROWS;
2616
if (join->session->getKilled()) // Aborted by user
2618
join->session->send_kill_message();
2619
return NESTED_LOOP_KILLED;
2621
if (!select_cond || select_cond->val_int())
2624
There is no select condition or the attached pushed down
2625
condition is true => a match is found.
2628
while (join_tab->first_unmatched && found)
2631
The while condition is always false if join_tab is not
2632
the last inner join table of an outer join operation.
2634
JoinTable *first_unmatched= join_tab->first_unmatched;
2636
Mark that a match for current outer table is found.
2637
This activates push down conditional predicates attached
2638
to the all inner tables of the outer join.
2640
first_unmatched->found= 1;
2641
for (JoinTable *tab= first_unmatched; tab <= join_tab; tab++)
2643
if (tab->table->reginfo.not_exists_optimize)
2644
return NESTED_LOOP_NO_MORE_ROWS;
2645
/* Check all predicates that has just been activated. */
2647
Actually all predicates non-guarded by first_unmatched->found
2648
will be re-evaluated again. It could be fixed, but, probably,
2649
it's not worth doing now.
2651
if (tab->select_cond && !tab->select_cond->val_int())
2653
/* The condition attached to table tab is false */
2654
if (tab == join_tab)
2659
Set a return point if rejected predicate is attached
2660
not to the last table of the current nest level.
2662
join->return_tab= tab;
2663
return NESTED_LOOP_OK;
2668
Check whether join_tab is not the last inner table
2669
for another embedding outer join.
2671
if ((first_unmatched= first_unmatched->first_upper) &&
2672
first_unmatched->last_inner != join_tab)
2674
join_tab->first_unmatched= first_unmatched;
2677
JoinTable *return_tab= join->return_tab;
2678
join_tab->found_match= true;
2681
It was not just a return to lower loop level when one
2682
of the newly activated predicates is evaluated as false
2683
(See above join->return_tab= tab).
2685
join->examined_rows++;
2686
join->session->row_count++;
2690
enum enum_nested_loop_state rc;
2691
/* A match from join_tab is found for the current partial join. */
2692
rc= (*join_tab->next_select)(join, join_tab+1, 0);
2693
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
2695
if (return_tab < join->return_tab)
2696
join->return_tab= return_tab;
2698
if (join->return_tab < join_tab)
2699
return NESTED_LOOP_OK;
2701
Test if this was a SELECT DISTINCT query on a table that
2702
was not in the field list; In this case we can abort if
2703
we found a row, as no new rows can be added to the result.
2705
if (not_used_in_distinct && found_records != join->found_records)
2706
return NESTED_LOOP_NO_MORE_ROWS;
2709
join_tab->read_record.cursor->unlock_row();
2714
The condition pushed down to the table join_tab rejects all rows
2715
with the beginning coinciding with the current partial join.
2717
join->examined_rows++;
2718
join->session->row_count++;
2719
join_tab->read_record.cursor->unlock_row();
2721
return NESTED_LOOP_OK;
2726
Construct a NULL complimented partial join record and feed it to the next
2727
level of the nested loop. This function is used in case we have
2728
an OUTER join and no matching record was found.
2730
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab)
2733
The table join_tab is the first inner table of a outer join operation
2734
and no matches has been found for the current outer row.
2736
JoinTable *last_inner_tab= join_tab->last_inner;
2737
/* Cache variables for faster loop */
2739
for ( ; join_tab <= last_inner_tab ; join_tab++)
2741
/* Change the the values of guard predicate variables. */
2743
join_tab->not_null_compl= 0;
2744
/* The outer row is complemented by nulls for each inner tables */
2745
join_tab->table->restoreRecordAsDefault(); // Make empty record
2746
join_tab->table->mark_as_null_row(); // For group by without error
2747
select_cond= join_tab->select_cond;
2748
/* Check all attached conditions for inner table rows. */
2749
if (select_cond && !select_cond->val_int())
2750
return NESTED_LOOP_OK;
2754
The row complemented by nulls might be the first row
2755
of embedding outer joins.
2756
If so, perform the same actions as in the code
2757
for the first regular outer join row above.
2761
JoinTable *first_unmatched= join_tab->first_unmatched;
2762
if ((first_unmatched= first_unmatched->first_upper) && first_unmatched->last_inner != join_tab)
2764
join_tab->first_unmatched= first_unmatched;
2765
if (! first_unmatched)
2767
first_unmatched->found= 1;
2768
for (JoinTable *tab= first_unmatched; tab <= join_tab; tab++)
2770
if (tab->select_cond && !tab->select_cond->val_int())
2772
join->return_tab= tab;
2773
return NESTED_LOOP_OK;
2778
The row complemented by nulls satisfies all conditions
2779
attached to inner tables.
2780
Send the row complemented by nulls to be joined with the
2783
return (*join_tab->next_select)(join, join_tab+1, 0);
2786
enum_nested_loop_state flush_cached_records(Join *join, JoinTable *join_tab, bool skip_last)
2788
enum_nested_loop_state rc= NESTED_LOOP_OK;
2792
join_tab->table->null_row= 0;
2793
if (!join_tab->cache.records)
2795
return NESTED_LOOP_OK; /* Nothing to do */
2800
(void) join_tab->cache.store_record_in_cache(); // Must save this for later
2804
if (join_tab->use_quick == 2)
2806
delete join_tab->select->quick;
2807
join_tab->select->quick= 0;
2809
/* read through all records */
2810
if ((error=join_init_read_record(join_tab)))
2812
join_tab->cache.reset_cache_write();
2813
return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
2816
for (JoinTable *tmp=join->join_tab; tmp != join_tab ; tmp++)
2818
tmp->status=tmp->table->status;
2819
tmp->table->status=0;
2822
info= &join_tab->read_record;
2825
if (join->session->getKilled())
2827
join->session->send_kill_message();
2828
return NESTED_LOOP_KILLED;
2830
optimizer::SqlSelect *select= join_tab->select;
2831
if (rc == NESTED_LOOP_OK &&
2832
(!join_tab->cache.select || !join_tab->cache.select->skip_record()))
2835
join_tab->cache.reset_cache_read();
2836
for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
2838
join_tab->readCachedRecord();
2839
if (!select || !select->skip_record())
2843
rc= (join_tab->next_select)(join,join_tab+1,0);
2844
if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
2846
join_tab->cache.reset_cache_write();
2851
return NESTED_LOOP_ERROR;
2855
} while (!(error=info->read_record(info)));
2858
join_tab->readCachedRecord(); // Restore current record
2859
join_tab->cache.reset_cache_write();
2860
if (error > 0) // Fatal error
2861
return NESTED_LOOP_ERROR;
2862
for (JoinTable *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
2863
tmp2->table->status=tmp2->status;
2864
return NESTED_LOOP_OK;
2867
/*****************************************************************************
2869
Functions that end one nested loop iteration. Different functions
2870
are used to support GROUP BY clause and to redirect records
2871
to a table (e.g. in case of SELECT into a temporary table) or to the
2875
NESTED_LOOP_OK - the record has been successfully handled
2876
NESTED_LOOP_ERROR - a fatal error (like table corruption)
2878
NESTED_LOOP_KILLED - thread shutdown was requested while processing
2880
NESTED_LOOP_QUERY_LIMIT - the record has been successfully handled;
2881
additionally, the nested loop produced the
2882
number of rows specified in the LIMIT clause
2884
NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
2885
additionally, there is a cursor and the nested
2886
loop algorithm produced the number of rows
2887
that is specified for current cursor fetch
2889
All return values except NESTED_LOOP_OK abort the nested loop.
2890
*****************************************************************************/
2891
enum_nested_loop_state end_send(Join *join, JoinTable *, bool end_of_records)
2893
if (! end_of_records)
2896
if (join->having && join->having->val_int() == 0)
2897
return NESTED_LOOP_OK; // Didn't match having
2899
if (join->do_send_rows)
2900
error=join->result->send_data(*join->fields);
2902
return NESTED_LOOP_ERROR;
2903
if (++join->send_records >= join->unit->select_limit_cnt && join->do_send_rows)
2905
if (join->select_options & OPTION_FOUND_ROWS)
2907
JoinTable *jt=join->join_tab;
2908
if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
2909
&& !join->send_group_parts && !join->having && !jt->select_cond &&
2910
!(jt->select && jt->select->quick) &&
2911
(jt->table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
2914
/* Join over all rows in table; Return number of found rows */
2915
Table *table= jt->table;
2917
join->select_options^= OPTION_FOUND_ROWS;
2918
if (table->sort.record_pointers ||
2919
(table->sort.io_cache && table->sort.io_cache->inited()))
2921
/* Using filesort */
2922
join->send_records= table->sort.found_records;
2926
table->cursor->info(HA_STATUS_VARIABLE);
2927
join->send_records= table->cursor->stats.records;
2932
join->do_send_rows= 0;
2933
if (join->unit->fake_select_lex)
2934
join->unit->fake_select_lex->select_limit= 0;
2935
return NESTED_LOOP_OK;
2938
return NESTED_LOOP_QUERY_LIMIT; // Abort nicely
2940
else if (join->send_records >= join->fetch_limit)
2943
There is a server side cursor and all rows for
2944
this fetch request are sent.
2946
return NESTED_LOOP_CURSOR_LIMIT;
2950
return NESTED_LOOP_OK;
2953
enum_nested_loop_state end_write(Join *join, JoinTable *, bool end_of_records)
2955
Table *table= join->tmp_table;
2957
if (join->session->getKilled()) // Aborted by user
2959
join->session->send_kill_message();
2960
return NESTED_LOOP_KILLED;
2962
if (!end_of_records)
2964
copy_fields(&join->tmp_table_param);
2965
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2966
return NESTED_LOOP_ERROR;
2967
if (!join->having || join->having->val_int())
2970
join->found_records++;
2971
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2973
if (!table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2975
return NESTED_LOOP_OK;
2978
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2979
return NESTED_LOOP_ERROR; // Table is_full error
2981
if (++join->send_records >= join->tmp_table_param.end_write_records && join->do_send_rows)
2983
if (!(join->select_options & OPTION_FOUND_ROWS))
2984
return NESTED_LOOP_QUERY_LIMIT;
2985
join->do_send_rows= 0;
2986
join->unit->select_limit_cnt= HA_POS_ERROR;
2987
return NESTED_LOOP_OK;
2992
return NESTED_LOOP_OK;
2995
/** Group by searching after group record and updating it if possible. */
2996
enum_nested_loop_state end_update(Join *join, JoinTable *, bool end_of_records)
2998
Table *table= join->tmp_table;
3003
return NESTED_LOOP_OK;
3004
if (join->session->getKilled()) // Aborted by user
3006
join->session->send_kill_message();
3007
return NESTED_LOOP_KILLED;
3010
join->found_records++;
3011
copy_fields(&join->tmp_table_param); // Groups are copied twice.
3012
/* Make a key of group index */
3013
for (group=table->group ; group ; group=group->next)
3015
Item *item= *group->item;
3016
item->save_org_in_field(group->field);
3017
/* Store in the used key if the field was 0 */
3018
if (item->maybe_null)
3019
group->buff[-1]= (char) group->field->is_null();
3021
if (!table->cursor->index_read_map(table->getUpdateRecord(),
3022
join->tmp_table_param.group_buff,
3025
{ /* Update old record */
3026
table->restoreRecord();
3027
update_tmptable_sum_func(join->sum_funcs,table);
3028
if ((error= table->cursor->updateRecord(table->getUpdateRecord(),
3029
table->getInsertRecord())))
3031
table->print_error(error,MYF(0));
3032
return NESTED_LOOP_ERROR;
3034
return NESTED_LOOP_OK;
3038
Copy null bits from group key to table
3039
We can't copy all data as the key may have different format
3040
as the row data (for example as with VARCHAR keys)
3042
KeyPartInfo *key_part;
3043
for (group=table->group,key_part=table->key_info[0].key_part;
3045
group=group->next,key_part++)
3047
if (key_part->null_bit)
3048
memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
3050
init_tmptable_sum_functions(join->sum_funcs);
3051
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3052
return NESTED_LOOP_ERROR;
3053
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
3055
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
3056
return NESTED_LOOP_ERROR; // Table is_full error
3058
join->send_records++;
3059
return NESTED_LOOP_OK;
3062
/** Like end_update, but this is done with unique constraints instead of keys. */
3063
enum_nested_loop_state end_unique_update(Join *join, JoinTable *, bool end_of_records)
3065
Table *table= join->tmp_table;
3069
return NESTED_LOOP_OK;
3070
if (join->session->getKilled()) // Aborted by user
3072
join->session->send_kill_message();
3073
return NESTED_LOOP_KILLED;
3076
init_tmptable_sum_functions(join->sum_funcs);
3077
copy_fields(&join->tmp_table_param); // Groups are copied twice.
3078
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3079
return NESTED_LOOP_ERROR;
3081
if (!(error= table->cursor->insertRecord(table->getInsertRecord())))
3082
join->send_records++; // New group
3085
if ((int) table->get_dup_key(error) < 0)
3087
table->print_error(error,MYF(0));
3088
return NESTED_LOOP_ERROR;
3090
if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
3092
table->print_error(error,MYF(0));
3093
return NESTED_LOOP_ERROR;
3095
table->restoreRecord();
3096
update_tmptable_sum_func(join->sum_funcs,table);
3097
if ((error= table->cursor->updateRecord(table->getUpdateRecord(),
3098
table->getInsertRecord())))
3100
table->print_error(error,MYF(0));
3101
return NESTED_LOOP_ERROR;
3104
return NESTED_LOOP_OK;
3108
allocate group fields or take prepared (cached).
3110
@param main_join join of current select
3111
@param curr_join current join (join of current select or temporary copy
3119
static bool make_group_fields(Join *main_join, Join *curr_join)
3121
if (main_join->group_fields_cache.size())
3123
curr_join->group_fields= main_join->group_fields_cache;
3124
curr_join->sort_and_group= 1;
3128
if (alloc_group_fields(curr_join, curr_join->group_list))
3130
main_join->group_fields_cache= curr_join->group_fields;
3136
calc how big buffer we need for comparing group entries.
3138
static void calc_group_buffer(Join *join, Order *group)
3140
uint32_t key_length=0, parts=0, null_parts=0;
3144
for (; group ; group=group->next)
3146
Item *group_item= *group->item;
3147
Field *field= group_item->get_tmp_table_field();
3150
enum_field_types type;
3151
if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
3152
key_length+=MAX_BLOB_WIDTH; // Can't be used as a key
3153
else if (type == DRIZZLE_TYPE_VARCHAR)
3154
key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
3156
key_length+= field->pack_length();
3160
switch (group_item->result_type()) {
3162
key_length+= sizeof(double);
3166
key_length+= sizeof(int64_t);
3169
case DECIMAL_RESULT:
3170
key_length+= class_decimal_get_binary_size(group_item->max_length -
3171
(group_item->decimals ? 1 : 0),
3172
group_item->decimals);
3177
enum enum_field_types type= group_item->field_type();
3179
As items represented as DATE/TIME fields in the group buffer
3180
have STRING_RESULT result type, we increase the length
3181
by 8 as maximum pack length of such fields.
3183
if (field::isDateTime(type))
3190
Group strings are taken as varstrings and require an length field.
3191
A field is not yet created by create_tmp_field()
3192
and the sizes should match up.
3194
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3201
/* This case should never be choosen */
3203
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3209
if (group_item->maybe_null)
3213
join->tmp_table_param.group_length=key_length+null_parts;
3214
join->tmp_table_param.group_parts=parts;
3215
join->tmp_table_param.group_null_parts=null_parts;
3219
Get a list of buffers for saveing last group.
3221
Groups are saved in reverse order for easyer check loop.
3223
static bool alloc_group_fields(Join *join, Order *group)
3227
for (; group ; group=group->next)
3229
Cached_item *tmp= new_Cached_item(join->session, *group->item);
3232
join->group_fields.push_front(tmp);
3235
join->sort_and_group=1; /* Mark for do_select */
3239
static uint32_t cache_record_length(Join *join,uint32_t idx)
3242
JoinTable **pos,**end;
3243
Session *session=join->session;
3245
for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
3249
JoinTable *join_tab= *pos;
3250
if (!join_tab->used_fieldlength) /* Not calced yet */
3251
calc_used_field_length(session, join_tab);
3252
length+=join_tab->used_fieldlength;
3258
Get the number of different row combinations for subset of partial join
3262
join The join structure
3263
idx Number of tables in the partial join order (i.e. the
3264
partial join order is in join->positions[0..idx-1])
3265
found_ref Bitmap of tables for which we need to find # of distinct
3269
Given a partial join order (in join->positions[0..idx-1]) and a subset of
3270
tables within that join order (specified in found_ref), find out how many
3271
distinct row combinations of subset tables will be in the result of the
3274
This is used as follows: Suppose we have a table accessed with a ref-based
3275
method. The ref access depends on current rows of tables in found_ref.
3276
We want to count # of different ref accesses. We assume two ref accesses
3277
will be different if at least one of access parameters is different.
3278
Example: consider a query
3280
SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
3283
t1, ref access on t1.key=c1
3284
t2, ref access on t2.key=c2
3285
t3, ref access on t3.key=t1.field
3287
For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
3288
For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
3289
For t3: n_ref_scans = records_read(t1)*records_read(t2)
3290
n_distinct_ref_scans = #records_read(t1)
3292
The reason for having this function (at least the latest version of it)
3293
is that we need to account for buffering in join execution.
3295
An edge-case example: if we have a non-first table in join accessed via
3296
ref(const) or ref(param) where there is a small number of different
3297
values of param, then the access will likely hit the disk cache and will
3298
not require any disk seeks.
3300
The proper solution would be to assume an LRU disk cache of some size,
3301
calculate probability of cache hits, etc. For now we just count
3302
identical ref accesses as one.
3305
Expected number of row combinations
3307
static double prev_record_reads(Join *join, uint32_t idx, table_map found_ref)
3310
optimizer::Position *pos_end= join->getSpecificPosInPartialPlan(-1);
3311
for (optimizer::Position *pos= join->getSpecificPosInPartialPlan(idx - 1);
3315
if (pos->examinePosition(found_ref))
3317
found_ref|= pos->getRefDependMap();
3319
For the case of "t1 LEFT Join t2 ON ..." where t2 is a const table
3320
with no matching row we will get position[t2].records_read==0.
3321
Actually the size of output is one null-complemented row, therefore
3322
we will use value of 1 whenever we get records_read==0.
3325
- the above case can't occur if inner part of outer join has more
3326
than one table: table with no matches will not be marked as const.
3328
- Ideally we should add 1 to records_read for every possible null-
3329
complemented row. We're not doing it because: 1. it will require
3330
non-trivial code and add overhead. 2. The value of records_read
3331
is an inprecise estimate and adding 1 (or, in the worst case,
3332
#max_nested_outer_joins=64-1) will not make it any more precise.
3334
if (pos->getFanout() > DBL_EPSILON)
3335
found*= pos->getFanout();
3342
Set up join struct according to best position.
3344
static bool get_best_combination(Join *join)
3347
table_map used_tables;
3348
JoinTable *join_tab,*j;
3349
optimizer::KeyUse *keyuse;
3350
uint32_t table_count;
3351
Session *session=join->session;
3352
optimizer::Position cur_pos;
3354
table_count=join->tables;
3355
join->join_tab=join_tab= new (session->mem) JoinTable[table_count];
3358
used_tables= OUTER_REF_TABLE_BIT; // Outer row is already read
3359
for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
3362
cur_pos= join->getPosFromOptimalPlan(tablenr);
3363
*j= *cur_pos.getJoinTable();
3364
form=join->table[tablenr]=j->table;
3365
used_tables|= form->map;
3366
form->reginfo.join_tab=j;
3367
if (!*j->on_expr_ref)
3368
form->reginfo.not_exists_optimize=0; // Only with LEFT Join
3369
if (j->type == AM_CONST)
3370
continue; // Handled in make_join_stat..
3375
if (j->type == AM_SYSTEM)
3377
if (j->keys.none() || ! (keyuse= cur_pos.getKeyUse()))
3380
if (tablenr != join->const_tables)
3383
else if (create_ref_for_key(join, j, keyuse, used_tables))
3384
return true; // Something went wrong
3387
for (i=0 ; i < table_count ; i++)
3388
join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
3389
update_depend_map(join);
3393
/** Save const tables first as used tables. */
3394
static void set_position(Join *join,
3397
optimizer::KeyUse *key)
3399
optimizer::Position tmp_pos(1.0, /* This is a const table */
3404
join->setPosInPartialPlan(idx, tmp_pos);
3406
/* Move the const table as down as possible in best_ref */
3407
JoinTable **pos=join->best_ref+idx+1;
3408
JoinTable *next=join->best_ref[idx];
3409
for (;next != table ; pos++)
3411
JoinTable *tmp=pos[0];
3415
join->best_ref[idx]=table;
3419
Selects and invokes a search strategy for an optimal query plan.
3421
The function checks user-configurable parameters that control the search
3422
strategy for an optimal plan, selects the search method and then invokes
3423
it. Each specific optimization procedure stores the final optimal plan in
3424
the array 'join->best_positions', and the cost of the plan in
3427
@param join pointer to the structure providing all context info for
3429
@param join_tables set of the tables in the query
3436
static bool choose_plan(Join *join, table_map join_tables)
3438
uint32_t search_depth= join->session->variables.optimizer_search_depth;
3439
uint32_t prune_level= join->session->variables.optimizer_prune_level;
3440
bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
3442
join->cur_embedding_map.reset();
3443
reset_nj_counters(join->join_list);
3445
if (SELECT_STRAIGHT_JOIN option is set)
3446
reorder tables so dependent tables come after tables they depend
3447
on, otherwise keep tables in the order they were specified in the query
3449
Apply heuristic: pre-sort all access plans with respect to the number of
3452
internal::my_qsort(join->best_ref + join->const_tables,
3453
join->tables - join->const_tables, sizeof(JoinTable*),
3454
straight_join ? join_tab_cmp_straight : join_tab_cmp);
3457
optimize_straight_join(join, join_tables);
3461
if (search_depth == 0)
3462
/* Automatically determine a reasonable value for 'search_depth' */
3463
search_depth= determine_search_depth(join);
3464
if (greedy_search(join, join_tables, search_depth, prune_level))
3469
Store the cost of this query into a user variable
3470
Don't update last_query_cost for statements that are not "flat joins" :
3471
i.e. they have subqueries, unions or call stored procedures.
3472
TODO: calculate a correct cost for a query with subqueries and UNIONs.
3474
if (join->session->lex().is_single_level_stmt())
3475
join->session->status_var.last_query_cost= join->best_read;
3480
Find the best access path for an extension of a partial execution
3481
plan and add this path to the plan.
3483
The function finds the best access path to table 's' from the passed
3484
partial plan where an access path is the general term for any means to
3485
access the data in 's'. An access path may use either an index or a scan,
3486
whichever is cheaper. The input partial plan is passed via the array
3487
'join->positions' of length 'idx'. The chosen access method for 's' and its
3488
cost are stored in 'join->positions[idx]'.
3490
@param join pointer to the structure providing all context info
3492
@param s the table to be joined by the function
3493
@param session thread for the connection that submitted the query
3494
@param remaining_tables set of tables not included into the partial plan yet
3495
@param idx the length of the partial plan
3496
@param record_count estimate for the number of records returned by the
3498
@param read_time the cost of the partial plan
3503
static void best_access_path(Join *join,
3506
table_map remaining_tables,
3508
double record_count,
3511
optimizer::KeyUse *best_key= NULL;
3512
uint32_t best_max_key_part= 0;
3513
bool found_constraint= 0;
3514
double best= DBL_MAX;
3515
double best_time= DBL_MAX;
3516
double records= DBL_MAX;
3517
table_map best_ref_depends_map= 0;
3522
{ /* Use key if possible */
3523
Table *table= s->table;
3524
optimizer::KeyUse *keyuse= NULL;
3525
optimizer::KeyUse *start_key= NULL;
3526
double best_records= DBL_MAX;
3527
uint32_t max_key_part=0;
3529
/* Test how we can use keys */
3530
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key
3531
for (keyuse= s->keyuse; keyuse->getTable() == table; )
3533
key_part_map found_part= 0;
3534
table_map found_ref= 0;
3535
uint32_t key= keyuse->getKey();
3536
KeyInfo *keyinfo= table->key_info + key;
3537
/* Bitmap of keyparts where the ref access is over 'keypart=const': */
3538
key_part_map const_part= 0;
3539
/* The or-null keypart in ref-or-null access: */
3540
key_part_map ref_or_null_part= 0;
3542
/* Calculate how many key segments of the current key we can use */
3545
do /* For each keypart */
3547
uint32_t keypart= keyuse->getKeypart();
3548
table_map best_part_found_ref= 0;
3549
double best_prev_record_reads= DBL_MAX;
3551
do /* For each way to access the keypart */
3555
if 1. expression doesn't refer to forward tables
3556
2. we won't get two ref-or-null's
3558
if (! (remaining_tables & keyuse->getUsedTables()) &&
3559
! (ref_or_null_part && (keyuse->getOptimizeFlags() &
3560
KEY_OPTIMIZE_REF_OR_NULL)))
3562
found_part|= keyuse->getKeypartMap();
3563
if (! (keyuse->getUsedTables() & ~join->const_table_map))
3564
const_part|= keyuse->getKeypartMap();
3566
double tmp2= prev_record_reads(join, idx, (found_ref |
3567
keyuse->getUsedTables()));
3568
if (tmp2 < best_prev_record_reads)
3570
best_part_found_ref= keyuse->getUsedTables() & ~join->const_table_map;
3571
best_prev_record_reads= tmp2;
3573
if (rec > keyuse->getTableRows())
3574
rec= keyuse->getTableRows();
3576
If there is one 'key_column IS NULL' expression, we can
3577
use this ref_or_null optimisation of this field
3579
if (keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)
3580
ref_or_null_part|= keyuse->getKeypartMap();
3584
} while (keyuse->getTable() == table && keyuse->getKey() == key &&
3585
keyuse->getKeypart() == keypart);
3586
found_ref|= best_part_found_ref;
3587
} while (keyuse->getTable() == table && keyuse->getKey() == key);
3590
Assume that that each key matches a proportional part of table.
3593
continue; // Nothing usable found
3595
if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
3596
rec= MATCHING_ROWS_IN_OTHER_TABLE; // Fix for small tables
3599
found_constraint= 1;
3602
Check if we found full key
3604
if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
3607
max_key_part= UINT32_MAX;
3608
if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
3610
tmp = prev_record_reads(join, idx, found_ref);
3616
{ /* We found a const key */
3618
ReuseRangeEstimateForRef-1:
3619
We get here if we've found a ref(const) (c_i are constants):
3620
"(keypart1=c1) AND ... AND (keypartN=cN)" [ref_const_cond]
3622
If range optimizer was able to construct a "range"
3623
access on this index, then its condition "quick_cond" was
3624
eqivalent to ref_const_cond (*), and we can re-use E(#rows)
3625
from the range optimizer.
3627
Proof of (*): By properties of range and ref optimizers
3628
quick_cond will be equal or tighther than ref_const_cond.
3629
ref_const_cond already covers "smallest" possible interval -
3630
a singlepoint interval over all keyparts. Therefore,
3631
quick_cond is equivalent to ref_const_cond (if it was an
3632
empty interval we wouldn't have got here).
3634
if (table->quick_keys.test(key))
3635
records= (double) table->quick_rows[key];
3638
/* quick_range couldn't use key! */
3639
records= (double) s->records/rec;
3644
if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
3645
{ /* Prefer longer keys */
3647
((double) s->records / (double) rec *
3649
((double) (table->getShare()->max_key_length-keyinfo->key_length) /
3650
(double) table->getShare()->max_key_length)));
3652
records=2.0; /* Can't be as good as a unique */
3655
ReuseRangeEstimateForRef-2: We get here if we could not reuse
3656
E(#rows) from range optimizer. Make another try:
3658
If range optimizer produced E(#rows) for a prefix of the ref
3659
access we're considering, and that E(#rows) is lower then our
3660
current estimate, make an adjustment. The criteria of when we
3661
can make an adjustment is a special case of the criteria used
3662
in ReuseRangeEstimateForRef-3.
3664
if (table->quick_keys.test(key) &&
3665
const_part & (1 << table->quick_key_parts[key]) &&
3666
table->quick_n_ranges[key] == 1 &&
3667
records > (double) table->quick_rows[key])
3669
records= (double) table->quick_rows[key];
3672
/* Limit the number of matched rows */
3674
set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
3675
if (table->covering_keys.test(key))
3677
/* we can use only index tree */
3678
tmp= record_count * table->cursor->index_only_read_time(key, tmp);
3681
tmp= record_count * min(tmp,s->worst_seeks);
3687
Use as much key-parts as possible and a uniq key is better
3688
than a not unique key
3689
Set tmp to (previous record count) * (records / combination)
3691
if ((found_part & 1) &&
3692
(!(table->index_flags(key) & HA_ONLY_WHOLE_INDEX) ||
3693
found_part == PREV_BITS(uint, keyinfo->key_parts)))
3695
max_key_part= max_part_bit(found_part);
3697
ReuseRangeEstimateForRef-3:
3698
We're now considering a ref[or_null] access via
3699
(t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
3700
(same-as-above but with one cond replaced
3701
with "t.keypart_i IS NULL")] (**)
3703
Try re-using E(#rows) from "range" optimizer:
3704
We can do so if "range" optimizer used the same intervals as
3705
in (**). The intervals used by range optimizer may be not
3706
available at this point (as "range" access might have choosen to
3707
create quick select over another index), so we can't compare
3708
them to (**). We'll make indirect judgements instead.
3709
The sufficient conditions for re-use are:
3710
(C1) All e_i in (**) are constants, i.e. found_ref==false. (if
3711
this is not satisfied we have no way to know which ranges
3712
will be actually scanned by 'ref' until we execute the
3714
(C2) max #key parts in 'range' access == K == max_key_part (this
3715
is apparently a necessary requirement)
3717
We also have a property that "range optimizer produces equal or
3718
tighter set of scan intervals than ref(const) optimizer". Each
3719
of the intervals in (**) are "tightest possible" intervals when
3720
one limits itself to using keyparts 1..K (which we do in #2).
3721
From here it follows that range access used either one, or
3722
both of the (I1) and (I2) intervals:
3724
(t.keypart1=c1 AND ... AND t.keypartK=eK) (I1)
3725
(same-as-above but with one cond replaced
3726
with "t.keypart_i IS NULL") (I2)
3728
The remaining part is to exclude the situation where range
3729
optimizer used one interval while we're considering
3730
ref-or-null and looking for estimate for two intervals. This
3731
is done by last limitation:
3733
(C3) "range optimizer used (have ref_or_null?2:1) intervals"
3735
if (table->quick_keys.test(key) && !found_ref && //(C1)
3736
table->quick_key_parts[key] == max_key_part && //(C2)
3737
table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
3739
tmp= records= (double) table->quick_rows[key];
3743
/* Check if we have statistic about the distribution */
3744
if ((records= keyinfo->rec_per_key[max_key_part-1]))
3747
Fix for the case where the index statistics is too
3749
(1) We're considering ref(const) and there is quick select
3751
(2) and that quick select uses more keyparts (i.e. it will
3752
scan equal/smaller interval then this ref(const))
3753
(3) and E(#rows) for quick select is higher then our
3756
We'll use E(#rows) from quick select.
3758
Q: Why do we choose to use 'ref'? Won't quick select be
3759
cheaper in some cases ?
3760
TODO: figure this out and adjust the plan choice if needed.
3762
if (!found_ref && table->quick_keys.test(key) && // (1)
3763
table->quick_key_parts[key] > max_key_part && // (2)
3764
records < (double)table->quick_rows[key]) // (3)
3765
records= (double)table->quick_rows[key];
3772
Assume that the first key part matches 1% of the cursor
3773
and that the whole key matches 10 (duplicates) or 1
3775
Assume also that more key matches proportionally more
3777
This gives the formula:
3778
records = (x * (b-a) + a*c-b)/(c-1)
3780
b = records matched by whole key
3781
a = records matched by first key part (1% of all records?)
3782
c = number of key parts in key
3783
x = used key parts (1 <= x <= c)
3786
if (!(rec_per_key=(double)
3787
keyinfo->rec_per_key[keyinfo->key_parts-1]))
3788
rec_per_key=(double) s->records/rec+1;
3792
else if (rec_per_key/(double) s->records >= 0.01)
3796
double a=s->records*0.01;
3797
if (keyinfo->key_parts > 1)
3798
tmp= (max_key_part * (rec_per_key - a) +
3799
a*keyinfo->key_parts - rec_per_key)/
3800
(keyinfo->key_parts-1);
3803
set_if_bigger(tmp,1.0);
3805
records = (uint32_t) tmp;
3808
if (ref_or_null_part)
3810
/* We need to do two key searches to find key */
3816
ReuseRangeEstimateForRef-4: We get here if we could not reuse
3817
E(#rows) from range optimizer. Make another try:
3819
If range optimizer produced E(#rows) for a prefix of the ref
3820
access we're considering, and that E(#rows) is lower then our
3821
current estimate, make the adjustment.
3823
The decision whether we can re-use the estimate from the range
3824
optimizer is the same as in ReuseRangeEstimateForRef-3,
3825
applied to first table->quick_key_parts[key] key parts.
3827
if (table->quick_keys.test(key) &&
3828
table->quick_key_parts[key] <= max_key_part &&
3829
const_part & (1 << table->quick_key_parts[key]) &&
3830
table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
3831
const_part) ? 1 : 0) &&
3832
records > (double) table->quick_rows[key])
3834
tmp= records= (double) table->quick_rows[key];
3838
/* Limit the number of matched rows */
3839
set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
3840
if (table->covering_keys.test(key))
3842
/* we can use only index tree */
3843
tmp= record_count * table->cursor->index_only_read_time(key, tmp);
3846
tmp= record_count * min(tmp,s->worst_seeks);
3849
tmp= best_time; // Do nothing
3853
if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
3855
best_time= tmp + records/(double) TIME_FOR_COMPARE;
3857
best_records= records;
3858
best_key= start_key;
3859
best_max_key_part= max_key_part;
3860
best_ref_depends_map= found_ref;
3863
records= best_records;
3867
Don't test table scan if it can't be better.
3868
Prefer key lookup if we would use the same key for scanning.
3870
Don't do a table scan on InnoDB tables, if we can read the used
3871
parts of the row from any of the used index.
3872
This is because table scans uses index and we would not win
3873
anything by using a table scan.
3875
A word for word translation of the below if-statement in sergefp's
3876
understanding: we check if we should use table scan if:
3877
(1) The found 'ref' access produces more records than a table scan
3878
(or index scan, or quick select), or 'ref' is more expensive than
3880
(2) This doesn't hold: the best way to perform table scan is to to perform
3881
'range' access using index IDX, and the best way to perform 'ref'
3882
access is to use the same index IDX, with the same or more key parts.
3883
(note: it is not clear how this rule is/should be extended to
3884
index_merge quick selects)
3885
(3) See above note about InnoDB.
3886
(4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
3887
path, but there is no quick select)
3888
If the condition in the above brackets holds, then the only possible
3889
"table scan" access method is ALL/index (there is no quick select).
3890
Since we have a 'ref' access path, and FORCE INDEX instructs us to
3891
choose it over ALL/index, there is no need to consider a full table
3894
if ((records >= s->found_records || best > s->read_time) && // (1)
3895
! (s->quick && best_key && s->quick->index == best_key->getKey() && // (2)
3896
best_max_key_part >= s->table->quick_key_parts[best_key->getKey()]) &&// (2)
3897
! ((s->table->cursor->getEngine()->check_flag(HTON_BIT_TABLE_SCAN_ON_INDEX)) && // (3)
3898
! s->table->covering_keys.none() && best_key && !s->quick) && // (3)
3899
! (s->table->force_index && best_key && !s->quick)) // (4)
3900
{ // Check full join
3901
ha_rows rnd_records= s->found_records;
3903
If there is a filtering condition on the table (i.e. ref analyzer found
3904
at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
3905
preceding this table in the join order we're now considering), then
3906
assume that 25% of the rows will be filtered out by this condition.
3908
This heuristic is supposed to force tables used in exprZ to be before
3909
this table in join order.
3911
if (found_constraint)
3912
rnd_records-= rnd_records/4;
3915
If applicable, get a more accurate estimate. Don't use the two
3918
if (s->table->quick_condition_rows != s->found_records)
3919
rnd_records= s->table->quick_condition_rows;
3922
Range optimizer never proposes a RANGE if it isn't better
3923
than FULL: so if RANGE is present, it's always preferred to FULL.
3924
Here we estimate its cost.
3930
- read record range through 'quick'
3931
- skip rows which does not satisfy WHERE constraints
3933
We take into account possible use of join cache for ALL/index
3934
access (see first else-branch below), but we don't take it into
3935
account here for range/index_merge access. Find out why this is so.
3938
(s->quick->read_time +
3939
(s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
3943
/* Estimate cost of reading table. */
3944
tmp= s->table->cursor->scan_time();
3945
if (s->table->map & join->outer_join) // Can't use join cache
3948
For each record we have to:
3949
- read the whole table record
3950
- skip rows which does not satisfy join condition
3954
(s->records - rnd_records)/(double) TIME_FOR_COMPARE);
3958
/* We read the table as many times as join buffer becomes full. */
3959
tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
3961
(double) session->variables.join_buff_size));
3963
We don't make full cartesian product between rows in the scanned
3964
table and existing records because we skip all rows from the
3965
scanned table, which does not satisfy join condition when
3966
we read the table (see flush_cached_records for details). Here we
3967
take into account cost to read and skip these records.
3969
tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
3974
We estimate the cost of evaluating WHERE clause for found records
3975
as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
3976
tmp give us total cost of using Table SCAN
3978
if (best == DBL_MAX ||
3979
(tmp + record_count/(double) TIME_FOR_COMPARE*rnd_records <
3980
best + record_count/(double) TIME_FOR_COMPARE*records))
3983
If the table has a range (s->quick is set) make_join_select()
3984
will ensure that this will be used
3987
records= rnd_records;
3989
/* range/index_merge/ALL/index access method are "independent", so: */
3990
best_ref_depends_map= 0;
3994
/* Update the cost information for the current partial plan */
3995
optimizer::Position tmp_pos(records,
3999
best_ref_depends_map);
4000
join->setPosInPartialPlan(idx, tmp_pos);
4003
idx == join->const_tables &&
4004
s->table == join->sort_by_table &&
4005
join->unit->select_limit_cnt >= records)
4006
join->sort_by_table= (Table*) 1; // Must use temporary table
4012
Select the best ways to access the tables in a query without reordering them.
4014
Find the best access paths for each query table and compute their costs
4015
according to their order in the array 'join->best_ref' (thus without
4016
reordering the join tables). The function calls sequentially
4017
'best_access_path' for each table in the query to select the best table
4018
access method. The final optimal plan is stored in the array
4019
'join->best_positions', and the corresponding cost in 'join->best_read'.
4021
@param join pointer to the structure providing all context info for
4023
@param join_tables set of the tables in the query
4026
This function can be applied to:
4027
- queries with STRAIGHT_JOIN
4028
- internally to compute the cost of an arbitrary QEP
4030
Thus 'optimize_straight_join' can be used at any stage of the query
4031
optimization process to finalize a QEP as it is.
4033
static void optimize_straight_join(Join *join, table_map join_tables)
4036
optimizer::Position partial_pos;
4037
uint32_t idx= join->const_tables;
4038
double record_count= 1.0;
4039
double read_time= 0.0;
4041
for (JoinTable **pos= join->best_ref + idx ; (s= *pos) ; pos++)
4043
/* Find the best access method from 's' to the current partial plan */
4044
best_access_path(join, s, join->session, join_tables, idx,
4045
record_count, read_time);
4046
/* compute the cost of the new plan extended with 's' */
4047
partial_pos= join->getPosFromPartialPlan(idx);
4048
record_count*= partial_pos.getFanout();
4049
read_time+= partial_pos.getCost();
4050
join_tables&= ~(s->table->map);
4054
read_time+= record_count / (double) TIME_FOR_COMPARE;
4055
partial_pos= join->getPosFromPartialPlan(join->const_tables);
4056
if (join->sort_by_table &&
4057
partial_pos.hasTableForSorting(join->sort_by_table))
4058
read_time+= record_count; // We have to make a temp table
4059
join->copyPartialPlanIntoOptimalPlan(idx);
4060
join->best_read= read_time;
4064
Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
4066
The search procedure uses a hybrid greedy/exhaustive search with controlled
4067
exhaustiveness. The search is performed in N = card(remaining_tables)
4068
steps. Each step evaluates how promising is each of the unoptimized tables,
4069
selects the most promising table, and extends the current partial QEP with
4070
that table. Currenly the most 'promising' table is the one with least
4071
expensive extension.\
4073
There are two extreme cases:
4074
-# When (card(remaining_tables) < search_depth), the estimate finds the
4075
best complete continuation of the partial QEP. This continuation can be
4076
used directly as a result of the search.
4077
-# When (search_depth == 1) the 'best_extension_by_limited_search'
4078
consideres the extension of the current QEP with each of the remaining
4081
All other cases are in-between these two extremes. Thus the parameter
4082
'search_depth' controlls the exhaustiveness of the search. The higher the
4083
value, the longer the optimizaton time and possibly the better the
4084
resulting plan. The lower the value, the fewer alternative plans are
4085
estimated, but the more likely to get a bad QEP.
4087
All intermediate and final results of the procedure are stored in 'join':
4088
- join->positions : modified for every partial QEP that is explored
4089
- join->best_positions: modified for the current best complete QEP
4090
- join->best_read : modified for the current best complete QEP
4091
- join->best_ref : might be partially reordered
4093
The final optimal plan is stored in 'join->best_positions', and its
4094
corresponding cost in 'join->best_read'.
4097
The following pseudocode describes the algorithm of 'greedy_search':
4100
procedure greedy_search
4101
input: remaining_tables
4106
(t, a) = best_extension(pplan, remaining_tables);
4107
pplan = concat(pplan, (t, a));
4108
remaining_tables = remaining_tables - t;
4109
} while (remaining_tables != {})
4114
where 'best_extension' is a placeholder for a procedure that selects the
4115
most "promising" of all tables in 'remaining_tables'.
4116
Currently this estimate is performed by calling
4117
'best_extension_by_limited_search' to evaluate all extensions of the
4118
current QEP of size 'search_depth', thus the complexity of 'greedy_search'
4119
mainly depends on that of 'best_extension_by_limited_search'.
4122
If 'best_extension()' == 'best_extension_by_limited_search()', then the
4123
worst-case complexity of this algorithm is <=
4124
O(N*N^search_depth/search_depth). When serch_depth >= N, then the
4125
complexity of greedy_search is O(N!).
4128
In the future, 'greedy_search' might be extended to support other
4129
implementations of 'best_extension', e.g. some simpler quadratic procedure.
4131
@param join pointer to the structure providing all context info
4133
@param remaining_tables set of tables not included into the partial plan yet
4134
@param search_depth controlls the exhaustiveness of the search
4135
@param prune_level the pruning heuristics that should be applied during
4143
static bool greedy_search(Join *join,
4144
table_map remaining_tables,
4145
uint32_t search_depth,
4146
uint32_t prune_level)
4148
double record_count= 1.0;
4149
double read_time= 0.0;
4150
uint32_t idx= join->const_tables; // index into 'join->best_ref'
4152
uint32_t size_remain; // cardinality of remaining_tables
4153
optimizer::Position best_pos;
4154
JoinTable *best_table; // the next plan node to be added to the curr QEP
4156
/* number of tables that remain to be optimized */
4157
size_remain= internal::my_count_bits(remaining_tables);
4160
/* Find the extension of the current QEP with the lowest cost */
4161
join->best_read= DBL_MAX;
4162
if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
4163
read_time, search_depth, prune_level))
4166
if (size_remain <= search_depth)
4169
'join->best_positions' contains a complete optimal extension of the
4170
current partial QEP.
4175
/* select the first table in the optimal extension as most promising */
4176
best_pos= join->getPosFromOptimalPlan(idx);
4177
best_table= best_pos.getJoinTable();
4179
Each subsequent loop of 'best_extension_by_limited_search' uses
4180
'join->positions' for cost estimates, therefore we have to update its
4183
join->setPosInPartialPlan(idx, best_pos);
4186
We need to make best_extension_by_limited_search aware of the fact
4187
that it's not starting from top level, but from a rather specific
4188
position in the list of nested joins.
4190
check_interleaving_with_nj (best_table);
4194
/* find the position of 'best_table' in 'join->best_ref' */
4196
JoinTable *pos= join->best_ref[best_idx];
4197
while (pos && best_table != pos)
4198
pos= join->best_ref[++best_idx];
4199
assert((pos != NULL)); // should always find 'best_table'
4200
/* move 'best_table' at the first free position in the array of joins */
4201
std::swap(join->best_ref[idx], join->best_ref[best_idx]);
4203
/* compute the cost of the new plan extended with 'best_table' */
4204
optimizer::Position partial_pos= join->getPosFromPartialPlan(idx);
4205
record_count*= partial_pos.getFanout();
4206
read_time+= partial_pos.getCost();
4208
remaining_tables&= ~(best_table->table->map);
4216
Find a good, possibly optimal, query execution plan (QEP) by a possibly
4219
The procedure searches for the optimal ordering of the query tables in set
4220
'remaining_tables' of size N, and the corresponding optimal access paths to
4221
each table. The choice of a table order and an access path for each table
4222
constitutes a query execution plan (QEP) that fully specifies how to
4225
The maximal size of the found plan is controlled by the parameter
4226
'search_depth'. When search_depth == N, the resulting plan is complete and
4227
can be used directly as a QEP. If search_depth < N, the found plan consists
4228
of only some of the query tables. Such "partial" optimal plans are useful
4229
only as input to query optimization procedures, and cannot be used directly
4232
The algorithm begins with an empty partial plan stored in 'join->positions'
4233
and a set of N tables - 'remaining_tables'. Each step of the algorithm
4234
evaluates the cost of the partial plan extended by all access plans for
4235
each of the relations in 'remaining_tables', expands the current partial
4236
plan with the access plan that results in lowest cost of the expanded
4237
partial plan, and removes the corresponding relation from
4238
'remaining_tables'. The algorithm continues until it either constructs a
4239
complete optimal plan, or constructs an optimal plartial plan with size =
4242
The final optimal plan is stored in 'join->best_positions'. The
4243
corresponding cost of the optimal plan is in 'join->best_read'.
4246
The procedure uses a recursive depth-first search where the depth of the
4247
recursion (and thus the exhaustiveness of the search) is controlled by the
4248
parameter 'search_depth'.
4251
The pseudocode below describes the algorithm of
4252
'best_extension_by_limited_search'. The worst-case complexity of this
4253
algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
4254
the complexity of greedy_search is O(N!).
4257
procedure best_extension_by_limited_search(
4258
pplan in, // in, partial plan of tables-joined-so-far
4259
pplan_cost, // in, cost of pplan
4260
remaining_tables, // in, set of tables not referenced in pplan
4261
best_plan_so_far, // in/out, best plan found so far
4262
best_plan_so_far_cost,// in/out, cost of best_plan_so_far
4263
search_depth) // in, maximum size of the plans being considered
4265
for each table T from remaining_tables
4267
// Calculate the cost of using table T as above
4268
cost = complex-series-of-calculations;
4270
// Add the cost to the cost so far.
4273
if (pplan_cost >= best_plan_so_far_cost)
4274
// pplan_cost already too great, stop search
4277
pplan= expand pplan by best_access_method;
4278
remaining_tables= remaining_tables - table T;
4279
if (remaining_tables is not an empty set
4283
best_extension_by_limited_search(pplan, pplan_cost,
4286
best_plan_so_far_cost,
4291
best_plan_so_far_cost= pplan_cost;
4292
best_plan_so_far= pplan;
4299
When 'best_extension_by_limited_search' is called for the first time,
4300
'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
4301
The actual implementation provides a way to optionally use pruning
4302
heuristic (controlled by the parameter 'prune_level') to reduce the search
4303
space by skipping some partial plans.
4306
The parameter 'search_depth' provides control over the recursion
4307
depth, and thus the size of the resulting optimal plan.
4309
@param join pointer to the structure providing all context info
4311
@param remaining_tables set of tables not included into the partial plan yet
4312
@param idx length of the partial QEP in 'join->positions';
4313
since a depth-first search is used, also corresponds
4314
to the current depth of the search tree;
4315
also an index in the array 'join->best_ref';
4316
@param record_count estimate for the number of records returned by the
4318
@param read_time the cost of the best partial plan
4319
@param search_depth maximum depth of the recursion and thus size of the
4321
(0 < search_depth <= join->tables+1).
4322
@param prune_level pruning heuristics that should be applied during
4324
(values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
4331
static bool best_extension_by_limited_search(Join *join,
4332
table_map remaining_tables,
4334
double record_count,
4336
uint32_t search_depth,
4337
uint32_t prune_level)
4339
Session *session= join->session;
4340
if (session->getKilled()) // Abort
4344
'join' is a partial plan with lower cost than the best plan so far,
4345
so continue expanding it further with the tables in 'remaining_tables'.
4348
double best_record_count= DBL_MAX;
4349
double best_read_time= DBL_MAX;
4350
optimizer::Position partial_pos;
4352
for (JoinTable **pos= join->best_ref + idx ; (s= *pos) ; pos++)
4354
table_map real_table_bit= s->table->map;
4355
if ((remaining_tables & real_table_bit) &&
4356
! (remaining_tables & s->dependent) &&
4357
(! idx || ! check_interleaving_with_nj(s)))
4359
double current_record_count, current_read_time;
4362
psergey-insideout-todo:
4363
when best_access_path() detects it could do an InsideOut scan or
4364
some other scan, have it return an insideout scan and a flag that
4365
requests to "fork" this loop iteration. (Q: how does that behave
4366
when the depth is insufficient??)
4368
/* Find the best access method from 's' to the current partial plan */
4369
best_access_path(join, s, session, remaining_tables, idx,
4370
record_count, read_time);
4371
/* Compute the cost of extending the plan with 's' */
4372
partial_pos= join->getPosFromPartialPlan(idx);
4373
current_record_count= record_count * partial_pos.getFanout();
4374
current_read_time= read_time + partial_pos.getCost();
4376
/* Expand only partial plans with lower cost than the best QEP so far */
4377
if ((current_read_time +
4378
current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
4380
restore_prev_nj_state(s);
4385
Prune some less promising partial plans. This heuristic may miss
4386
the optimal QEPs, thus it results in a non-exhaustive search.
4388
if (prune_level == 1)
4390
if (best_record_count > current_record_count ||
4391
best_read_time > current_read_time ||
4392
(idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
4394
if (best_record_count >= current_record_count &&
4395
best_read_time >= current_read_time &&
4396
/* TODO: What is the reasoning behind this condition? */
4397
(! (s->key_dependent & remaining_tables) ||
4398
partial_pos.isConstTable()))
4400
best_record_count= current_record_count;
4401
best_read_time= current_read_time;
4406
restore_prev_nj_state(s);
4411
if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
4412
{ /* Recursively expand the current partial plan */
4413
std::swap(join->best_ref[idx], *pos);
4414
if (best_extension_by_limited_search(join,
4415
remaining_tables & ~real_table_bit,
4417
current_record_count,
4422
std::swap(join->best_ref[idx], *pos);
4426
'join' is either the best partial QEP with 'search_depth' relations,
4427
or the best complete QEP so far, whichever is smaller.
4429
partial_pos= join->getPosFromPartialPlan(join->const_tables);
4430
current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
4431
if (join->sort_by_table &&
4432
partial_pos.hasTableForSorting(join->sort_by_table))
4433
/* We have to make a temp table */
4434
current_read_time+= current_record_count;
4435
if ((search_depth == 1) || (current_read_time < join->best_read))
4437
join->copyPartialPlanIntoOptimalPlan(idx + 1);
4438
join->best_read= current_read_time - 0.001;
4441
restore_prev_nj_state(s);
4448
Heuristic procedure to automatically guess a reasonable degree of
4449
exhaustiveness for the greedy search procedure.
4451
The procedure estimates the optimization time and selects a search depth
4452
big enough to result in a near-optimal QEP, that doesn't take too long to
4453
find. If the number of tables in the query exceeds some constant, then
4454
search_depth is set to this constant.
4456
@param join pointer to the structure providing all context info for
4460
This is an extremely simplistic implementation that serves as a stub for a
4461
more advanced analysis of the join. Ideally the search depth should be
4462
determined by learning from previous query optimizations, because it will
4463
depend on the CPU power (and other factors).
4466
this value should be determined dynamically, based on statistics:
4467
uint32_t max_tables_for_exhaustive_opt= 7;
4470
this value could be determined by some mapping of the form:
4471
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
4474
A positive integer that specifies the search depth (and thus the
4475
exhaustiveness) of the depth-first search algorithm used by
4478
static uint32_t determine_search_depth(Join *join)
4480
uint32_t table_count= join->tables - join->const_tables;
4481
uint32_t search_depth;
4482
/* TODO: this value should be determined dynamically, based on statistics: */
4483
uint32_t max_tables_for_exhaustive_opt= 7;
4485
if (table_count <= max_tables_for_exhaustive_opt)
4486
search_depth= table_count+1; // use exhaustive for small number of tables
4489
TODO: this value could be determined by some mapping of the form:
4490
depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
4492
search_depth= max_tables_for_exhaustive_opt; // use greedy search
4494
return search_depth;
4497
static void make_simple_join(Join *join,Table *tmp_table)
4500
Reuse Table * and JoinTable if already allocated by a previous call
4501
to this function through Join::exec (may happen for sub-queries).
4503
if (!join->table_reexec)
4505
join->table_reexec= new (join->session->mem) Table*;
4507
join->tmp_join->table_reexec= join->table_reexec;
4509
if (!join->join_tab_reexec)
4511
join->join_tab_reexec= new (join->session->mem) JoinTable;
4513
join->tmp_join->join_tab_reexec= join->join_tab_reexec;
4515
Table** tableptr= join->table_reexec;
4516
JoinTable* join_tab= join->join_tab_reexec;
4518
join->join_tab=join_tab;
4519
join->table=tableptr; tableptr[0]=tmp_table;
4521
join->const_tables=0;
4522
join->const_table_map=0;
4523
join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
4524
join->tmp_table_param.func_count=0;
4525
join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
4526
join->first_record=join->sort_and_group=0;
4527
join->send_records=(ha_rows) 0;
4529
join->row_limit=join->unit->select_limit_cnt;
4530
join->do_send_rows = (join->row_limit) ? 1 : 0;
4532
join_tab->table=tmp_table;
4534
join_tab->select_cond=0;
4536
join_tab->type= AM_ALL; /* Map through all records */
4537
join_tab->keys.set(); /* test everything in quick */
4539
join_tab->on_expr_ref=0;
4540
join_tab->last_inner= 0;
4541
join_tab->first_unmatched= 0;
4542
join_tab->ref.key = -1;
4543
join_tab->not_used_in_distinct=0;
4544
join_tab->read_first_record= join_init_read_record;
4545
join_tab->join=join;
4546
join_tab->ref.key_parts= 0;
4547
join_tab->read_record.init();
4548
tmp_table->status=0;
4549
tmp_table->null_row=0;
4553
Fill in outer join related info for the execution plan structure.
4555
For each outer join operation left after simplification of the
4556
original query the function set up the following pointers in the linear
4557
structure join->join_tab representing the selected execution plan.
4558
The first inner table t0 for the operation is set to refer to the last
4559
inner table tk through the field t0->last_inner.
4560
Any inner table ti for the operation are set to refer to the first
4561
inner table ti->first_inner.
4562
The first inner table t0 for the operation is set to refer to the
4563
first inner table of the embedding outer join operation, if there is any,
4564
through the field t0->first_upper.
4565
The on expression for the outer join operation is attached to the
4566
corresponding first inner table through the field t0->on_expr_ref.
4567
Here ti are structures of the JoinTable type.
4569
EXAMPLE. For the query:
4573
(t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
4574
ON (t1.a=t2.a AND t1.b=t3.b)
4578
given the execution plan with the table order t1,t2,t3,t4
4579
is selected, the following references will be set;
4580
t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
4581
t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
4582
on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
4583
*t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
4585
@param join reference to the info fully describing the query
4588
The function assumes that the simplification procedure has been
4589
already applied to the join query (see simplify_joins).
4590
This function can be called only after the execution plan
4593
static void make_outerjoin_info(Join *join)
4595
for (uint32_t i=join->const_tables ; i < join->tables ; i++)
4597
JoinTable *tab=join->join_tab+i;
4598
Table *table=tab->table;
4599
TableList *tbl= table->pos_in_table_list;
4600
TableList *embedding= tbl->getEmbedding();
4602
if (tbl->outer_join)
4605
Table tab is the only one inner table for outer join.
4606
(Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
4607
is in the query above.)
4609
tab->last_inner= tab->first_inner= tab;
4610
tab->on_expr_ref= &tbl->on_expr;
4611
tab->cond_equal= tbl->cond_equal;
4613
tab->first_upper= embedding->getNestedJoin()->first_nested;
4615
for ( ; embedding ; embedding= embedding->getEmbedding())
4617
/* Ignore sj-nests: */
4618
if (!embedding->on_expr)
4620
NestedJoin *nested_join= embedding->getNestedJoin();
4621
if (!nested_join->counter_)
4624
Table tab is the first inner table for nested_join.
4625
Save reference to it in the nested join structure.
4627
nested_join->first_nested= tab;
4628
tab->on_expr_ref= &embedding->on_expr;
4629
tab->cond_equal= tbl->cond_equal;
4630
if (embedding->getEmbedding())
4631
tab->first_upper= embedding->getEmbedding()->getNestedJoin()->first_nested;
4633
if (!tab->first_inner)
4634
tab->first_inner= nested_join->first_nested;
4635
if (++nested_join->counter_ < nested_join->join_list.size())
4637
/* Table tab is the last inner table for nested join. */
4638
nested_join->first_nested->last_inner= tab;
4644
static bool make_join_select(Join *join,
4645
optimizer::SqlSelect *select,
4648
Session *session= join->session;
4649
optimizer::Position cur_pos;
4652
add_not_null_conds(join);
4653
table_map used_tables;
4654
if (cond) /* Because of QUICK_GROUP_MIN_MAX_SELECT */
4655
{ /* there may be a select without a cond. */
4656
if (join->tables > 1)
4657
cond->update_used_tables(); // Tablenr may have changed
4658
if (join->const_tables == join->tables &&
4659
session->lex().current_select->master_unit() ==
4660
&session->lex().unit) // not upper level SELECT
4661
join->const_table_map|=RAND_TABLE_BIT;
4662
{ // Check const tables
4664
make_cond_for_table(cond,
4665
join->const_table_map,
4667
for (JoinTable *tab= join->join_tab+join->const_tables;
4668
tab < join->join_tab+join->tables ; tab++)
4670
if (*tab->on_expr_ref)
4672
JoinTable *cond_tab= tab->first_inner;
4673
COND *tmp= make_cond_for_table(*tab->on_expr_ref,
4674
join->const_table_map,
4678
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
4681
tmp->quick_fix_field();
4682
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
4683
new Item_cond_and(cond_tab->select_cond,
4685
if (! cond_tab->select_cond)
4687
cond_tab->select_cond->quick_fix_field();
4690
if (const_cond && ! const_cond->val_int())
4692
return 1; // Impossible const condition
4696
used_tables=((select->const_tables=join->const_table_map) |
4697
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
4698
for (uint32_t i=join->const_tables ; i < join->tables ; i++)
4700
JoinTable *tab=join->join_tab+i;
4702
first_inner is the X in queries like:
4703
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
4705
JoinTable *first_inner_tab= tab->first_inner;
4706
table_map current_map= tab->table->map;
4707
bool use_quick_range=0;
4711
Following force including random expression in last table condition.
4712
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
4714
if (i == join->tables-1)
4715
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
4716
used_tables|=current_map;
4718
if (tab->type == AM_REF && tab->quick &&
4719
(uint32_t) tab->ref.key == tab->quick->index &&
4720
tab->ref.key_length < tab->quick->max_used_key_length)
4722
/* Range uses longer key; Use this instead of ref on key */
4727
tab->ref.key_parts= 0; // Don't use ref key.
4728
cur_pos= join->getPosFromOptimalPlan(i);
4729
cur_pos.setFanout(tab->quick->records);
4731
We will use join cache here : prevent sorting of the first
4732
table only and sort at the end.
4734
if (i != join->const_tables && join->tables > join->const_tables + 1)
4738
if (join->full_join and not session->lex().current_select->is_cross and not cond)
4740
my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
4746
tmp= make_cond_for_table(cond,used_tables,current_map, 0);
4747
if (cond && !tmp && tab->quick)
4749
if (tab->type != AM_ALL)
4752
Don't use the quick method
4753
We come here in the case where we have 'key=constant' and
4754
the test is removed by make_cond_for_table()
4762
Hack to handle the case where we only refer to a table
4763
in the ON part of an OUTER JOIN. In this case we want the code
4764
below to check if we should use 'quick' instead.
4766
tmp= new Item_int((int64_t) 1,1); // Always true
4770
if (tmp || !cond || tab->type == AM_REF || tab->type == AM_REF_OR_NULL ||
4771
tab->type == AM_EQ_REF)
4773
optimizer::SqlSelect *sel= tab->select= ((optimizer::SqlSelect*)session->mem.memdup(select, sizeof(*select)));
4775
If tab is an inner table of an outer join operation,
4776
add a match guard to the pushed down predicate.
4777
The guard will turn the predicate on only after
4778
the first match for outer tables is encountered.
4783
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
4784
a cond, so neutralize the hack above.
4786
if (! (tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
4788
tab->select_cond=sel->cond=tmp;
4791
tab->select_cond= sel->cond= NULL;
4793
sel->head=tab->table;
4796
/* Use quick key read if it's a constant and it's not used
4798
if (tab->needed_reg.none() && tab->type != AM_EQ_REF
4799
&& (tab->type != AM_REF || (uint32_t) tab->ref.key == tab->quick->index))
4801
sel->quick=tab->quick; // Use value from get_quick_...
4802
sel->quick_keys.reset();
4803
sel->needed_reg.reset();
4811
uint32_t ref_key= static_cast<uint32_t>(sel->head->reginfo.join_tab->ref.key + 1);
4812
if (i == join->const_tables && ref_key)
4814
if (tab->const_keys.any() &&
4815
tab->table->reginfo.impossible_range)
4818
else if (tab->type == AM_ALL && ! use_quick_range)
4820
if (tab->const_keys.any() &&
4821
tab->table->reginfo.impossible_range)
4822
return 1; // Impossible range
4824
We plan to scan all rows.
4825
Check again if we should use an index.
4826
We could have used an column from a previous table in
4827
the index if we are using limit and this is the first table
4830
cur_pos= join->getPosFromOptimalPlan(i);
4831
if ((cond && (! ((tab->keys & tab->const_keys) == tab->keys) && i > 0)) ||
4832
(! tab->const_keys.none() && (i == join->const_tables) &&
4833
(join->unit->select_limit_cnt < cur_pos.getFanout()) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
4835
/* Join with outer join condition */
4836
COND *orig_cond= sel->cond;
4837
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
4840
We can't call sel->cond->fix_fields,
4841
as it will break tab->on_expr if it's AND condition
4842
(fix_fields currently removes extra AND/OR levels).
4843
Yet attributes of the just built condition are not needed.
4844
Thus we call sel->cond->quick_fix_field for safety.
4846
if (sel->cond && ! sel->cond->fixed)
4847
sel->cond->quick_fix_field();
4849
if (sel->test_quick_select(session, tab->keys,
4850
used_tables & ~ current_map,
4851
(join->select_options &
4854
join->unit->select_limit_cnt), 0,
4858
Before reporting "Impossible WHERE" for the whole query
4859
we have to check isn't it only "impossible ON" instead
4861
sel->cond=orig_cond;
4862
if (! *tab->on_expr_ref ||
4863
sel->test_quick_select(session, tab->keys,
4864
used_tables & ~ current_map,
4865
(join->select_options &
4868
join->unit->select_limit_cnt),0,
4870
return 1; // Impossible WHERE
4873
sel->cond=orig_cond;
4875
/* Fix for EXPLAIN */
4878
cur_pos= join->getPosFromOptimalPlan(i);
4879
cur_pos.setFanout(static_cast<double>(sel->quick->records));
4884
sel->needed_reg= tab->needed_reg;
4885
sel->quick_keys.reset();
4887
if (!((tab->checked_keys & sel->quick_keys) == sel->quick_keys) ||
4888
!((tab->checked_keys & sel->needed_reg) == sel->needed_reg))
4890
tab->keys= sel->quick_keys;
4891
tab->keys|= sel->needed_reg;
4892
tab->use_quick= (!sel->needed_reg.none() &&
4893
(select->quick_keys.none() ||
4895
(select->quick->records >= 100L)))) ?
4897
sel->read_tables= used_tables & ~current_map;
4899
if (i != join->const_tables && tab->use_quick != 2)
4900
{ /* Read with cache */
4902
(tmp=make_cond_for_table(cond,
4903
join->const_table_map |
4907
tab->cache.select= (optimizer::SqlSelect*)session->mem.memdup(sel, sizeof(optimizer::SqlSelect));
4908
tab->cache.select->cond= tmp;
4909
tab->cache.select->read_tables= join->const_table_map;
4916
Push down conditions from all on expressions.
4917
Each of these conditions are guarded by a variable
4918
that turns if off just before null complemented row for
4919
outer joins is formed. Thus, the condition from an
4920
'on expression' are guaranteed not to be checked for
4921
the null complemented row.
4924
/* First push down constant conditions from on expressions */
4925
for (JoinTable *join_tab= join->join_tab+join->const_tables;
4926
join_tab < join->join_tab+join->tables ; join_tab++)
4928
if (*join_tab->on_expr_ref)
4930
JoinTable *cond_tab= join_tab->first_inner;
4931
tmp= make_cond_for_table(*join_tab->on_expr_ref,
4932
join->const_table_map,
4936
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
4939
tmp->quick_fix_field();
4940
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
4941
new Item_cond_and(cond_tab->select_cond,tmp);
4942
if (! cond_tab->select_cond)
4944
cond_tab->select_cond->quick_fix_field();
4948
/* Push down non-constant conditions from on expressions */
4949
JoinTable *last_tab= tab;
4950
while (first_inner_tab && first_inner_tab->last_inner == last_tab)
4953
Table tab is the last inner table of an outer join.
4954
An on expression is always attached to it.
4956
COND *on_expr= *first_inner_tab->on_expr_ref;
4958
table_map used_tables2= (join->const_table_map |
4959
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
4960
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
4962
current_map= tab->table->map;
4963
used_tables2|= current_map;
4964
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
4968
JoinTable *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
4970
First add the guards for match variables of
4971
all embedding outer join operations.
4973
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
4978
Now add the guard turning the predicate off for
4979
the null complemented row.
4981
tmp_cond= new Item_func_trig_cond(tmp_cond,
4985
tmp_cond->quick_fix_field();
4986
/* Add the predicate to other pushed down predicates */
4987
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
4988
new Item_cond_and(cond_tab->select_cond,
4990
if (! cond_tab->select_cond)
4992
cond_tab->select_cond->quick_fix_field();
4995
first_inner_tab= first_inner_tab->first_upper;
5003
Plan refinement stage: do various set ups for the executioner
5006
make_join_readinfo()
5007
join Join being processed
5008
options Join's options (checking for SELECT_DESCRIBE,
5009
SELECT_NO_JOIN_CACHE)
5010
no_jbuf_after Don't use join buffering after table with this number.
5013
Plan refinement stage: do various set ups for the executioner
5014
- set up use of join buffering
5015
- push index conditions
5016
- increment counters
5021
true - Out of memory
5023
static void make_join_readinfo(Join& join)
5027
for (uint32_t i= join.const_tables; i < join.tables; i++)
5029
JoinTable *tab=join.join_tab+i;
5030
Table *table=tab->table;
5031
tab->read_record.table= table;
5032
tab->read_record.cursor= table->cursor;
5033
tab->next_select=sub_select; /* normal select */
5035
TODO: don't always instruct first table's ref/range access method to
5036
produce sorted output.
5038
tab->sorted= sorted;
5039
sorted= false; // only first must be sorted
5041
if (tab->insideout_match_tab)
5043
tab->insideout_buf= join.session->mem.alloc(tab->table->key_info[tab->index].key_length);
5046
optimizer::AccessMethodFactory::create(tab->type)->getStats(*table, *tab);
5049
join.join_tab[join.tables-1].next_select= NULL; /* Set by do_select */
5052
/** Update the dependency map for the tables. */
5053
static void update_depend_map(Join *join)
5055
JoinTable *join_tab=join->join_tab, *end=join_tab+join->tables;
5057
for (; join_tab != end ; join_tab++)
5059
table_reference_st *ref= &join_tab->ref;
5060
table_map depend_map= 0;
5061
Item **item=ref->items;
5063
for (i=0 ; i < ref->key_parts ; i++,item++)
5064
depend_map|=(*item)->used_tables();
5065
ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
5066
depend_map&= ~OUTER_REF_TABLE_BIT;
5067
for (JoinTable **tab=join->map2table; depend_map; tab++,depend_map>>=1 )
5070
ref->depend_map|=(*tab)->ref.depend_map;
5075
/** Update the dependency map for the sort order. */
5076
static void update_depend_map(Join *join, Order *order)
5078
for (; order ; order=order->next)
5080
table_map depend_map;
5081
order->item[0]->update_used_tables();
5082
order->depend_map=depend_map=order->item[0]->used_tables();
5083
// Not item_sum(), RAND() and no reference to table outside of sub select
5084
if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
5085
&& !order->item[0]->with_sum_func)
5087
for (JoinTable **tab=join->map2table; depend_map; tab++, depend_map>>=1)
5090
order->depend_map|=(*tab)->ref.depend_map;
5097
Remove all constants and check if order_st only contains simple
5100
simple_order is set to 1 if sort_order only uses fields from head table
5101
and the head table is not a LEFT JOIN table.
5103
@param join Join handler
5104
@param first_order List of SORT or GROUP order
5105
@param cond WHERE statement
5106
@param change_list Set to 1 if we should remove things from list.
5107
If this is not set, then only simple_order is
5109
@param simple_order Set to 1 if we are only using simple expressions
5112
Returns new sort order
5114
static Order *remove_constants(Join *join,Order *first_order, COND *cond, bool change_list, bool *simple_order)
5116
if (join->tables == join->const_tables)
5117
return change_list ? 0 : first_order; // No need to sort
5119
Order *order,**prev_ptr;
5120
table_map first_table= join->join_tab[join->const_tables].table->map;
5121
table_map not_const_tables= ~join->const_table_map;
5124
prev_ptr= &first_order;
5125
*simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
5127
/* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
5129
update_depend_map(join, first_order);
5130
for (order=first_order; order ; order=order->next)
5132
table_map order_tables=order->item[0]->used_tables();
5133
if (order->item[0]->with_sum_func)
5134
*simple_order=0; // Must do a temp table to sort
5135
else if (!(order_tables & not_const_tables))
5137
if (order->item[0]->with_subselect)
5138
order->item[0]->val_str(&order->item[0]->str_value);
5139
continue; // skip const item
5143
if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
5148
if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
5152
if ((ref=order_tables & (not_const_tables ^ first_table)))
5154
if (!(order_tables & first_table) &&
5155
only_eq_ref_tables(join,first_order, ref))
5159
*simple_order=0; // Must do a temp table to sort
5164
*prev_ptr= order; // use this entry
5165
prev_ptr= &order->next;
5169
if (prev_ptr == &first_order) // Nothing to sort/group
5171
return(first_order);
5174
static void return_zero_rows(Join *join, select_result *result, TableList *tables, List<Item> &fields, bool send_row, uint64_t select_options, const char *info, Item *having)
5176
if (select_options & SELECT_DESCRIBE)
5178
optimizer::ExplainPlan planner(join, false, false, false, info);
5179
planner.printPlan();
5187
for (TableList *table= tables; table; table= table->next_leaf)
5188
table->table->mark_as_null_row(); // All fields are NULL
5189
if (having && having->val_int() == 0)
5192
result->send_fields(fields);
5195
List<Item>::iterator it(fields.begin());
5196
while (Item* item= it++)
5197
item->no_rows_in_result();
5198
result->send_data(fields);
5200
result->send_eof(); // Should be safe
5201
/* Update results for FOUND_ROWS */
5202
join->session->limit_found_rows= join->session->examined_row_count= 0;
5206
Simplify joins replacing outer joins by inner joins whenever it's
5209
The function, during a retrieval of join_list, eliminates those
5210
outer joins that can be converted into inner join, possibly nested.
5211
It also moves the on expressions for the converted outer joins
5212
and from inner joins to conds.
5213
The function also calculates some attributes for nested joins:
5217
- on_expr_dep_tables
5218
The first two attributes are used to test whether an outer join can
5219
be substituted for an inner join. The third attribute represents the
5220
relation 'to be dependent on' for tables. If table t2 is dependent
5221
on table t1, then in any evaluated execution plan table access to
5222
table t2 must precede access to table t2. This relation is used also
5223
to check whether the query contains invalid cross-references.
5224
The forth attribute is an auxiliary one and is used to calculate
5226
As the attribute dep_tables qualifies possibles orders of tables in the
5227
execution plan, the dependencies required by the straight join
5228
modifiers are reflected in this attribute as well.
5229
The function also removes all braces that can be removed from the join
5230
expression without changing its meaning.
5233
An outer join can be replaced by an inner join if the where condition
5234
or the on expression for an embedding nested join contains a conjunctive
5235
predicate rejecting null values for some attribute of the inner tables.
5239
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
5241
the predicate t2.b < 5 rejects nulls.
5242
The query is converted first to:
5244
SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
5246
then to the equivalent form:
5248
SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
5252
Similarly the following query:
5254
SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
5259
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
5263
One conversion might trigger another:
5265
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
5266
LEFT JOIN t3 ON t3.b=t2.b
5267
WHERE t3 IS NOT NULL =>
5268
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
5269
WHERE t3 IS NOT NULL AND t3.b=t2.b =>
5270
SELECT * FROM t1, t2, t3
5271
WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
5274
The function removes all unnecessary braces from the expression
5275
produced by the conversions.
5278
SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
5280
finally is converted to:
5282
SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
5287
It also will remove braces from the following queries:
5289
SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
5290
SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
5293
The benefit of this simplification procedure is that it might return
5294
a query for which the optimizer can evaluate execution plan with more
5295
join orders. With a left join operation the optimizer does not
5296
consider any plan where one of the inner tables is before some of outer
5300
The function is implemented by a recursive procedure. On the recursive
5301
ascent all attributes are calculated, all outer joins that can be
5302
converted are replaced and then all unnecessary braces are removed.
5303
As join list contains join tables in the reverse order sequential
5304
elimination of outer joins does not require extra recursive calls.
5307
Remove all semi-joins that have are within another semi-join (i.e. have
5308
an "ancestor" semi-join nest)
5311
Here is an example of a join query with invalid cross references:
5313
SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
5316
@param join reference to the query info
5317
@param join_list list representation of the join to be converted
5318
@param conds conditions to add on expressions for converted joins
5319
@param top true <=> conds is the where condition
5322
- The new condition, if success
5325
static COND *simplify_joins(Join *join, List<TableList> *join_list, COND *conds, bool top)
5327
NestedJoin *nested_join;
5328
TableList *prev_table= 0;
5329
List<TableList>::iterator li(join_list->begin());
5332
Try to simplify join operations from join_list.
5333
The most outer join operation is checked for conversion first.
5335
while (TableList* table= li++)
5337
table_map used_tables;
5338
table_map not_null_tables= (table_map) 0;
5340
if ((nested_join= table->getNestedJoin()))
5343
If the element of join_list is a nested join apply
5344
the procedure to its nested join list first.
5348
Item *expr= table->on_expr;
5350
If an on expression E is attached to the table,
5351
check all null rejected predicates in this expression.
5352
If such a predicate over an attribute belonging to
5353
an inner table of an embedded outer join is found,
5354
the outer join is converted to an inner join and
5355
the corresponding on expression is added to E.
5357
expr= simplify_joins(join, &nested_join->join_list, expr, false);
5359
if (!table->prep_on_expr || expr != table->on_expr)
5363
table->on_expr= expr;
5364
table->prep_on_expr= expr->copy_andor_structure(join->session);
5367
nested_join->used_tables= (table_map) 0;
5368
nested_join->not_null_tables=(table_map) 0;
5369
conds= simplify_joins(join, &nested_join->join_list, conds, top);
5370
used_tables= nested_join->used_tables;
5371
not_null_tables= nested_join->not_null_tables;
5375
if (!table->prep_on_expr)
5376
table->prep_on_expr= table->on_expr;
5377
used_tables= table->table->map;
5379
not_null_tables= conds->not_null_tables();
5382
if (table->getEmbedding())
5384
table->getEmbedding()->getNestedJoin()->used_tables|= used_tables;
5385
table->getEmbedding()->getNestedJoin()->not_null_tables|= not_null_tables;
5388
if (!table->outer_join || (used_tables & not_null_tables))
5391
For some of the inner tables there are conjunctive predicates
5392
that reject nulls => the outer join can be replaced by an inner join.
5394
table->outer_join= 0;
5397
/* Add ON expression to the WHERE or upper-level ON condition. */
5400
conds= and_conds(conds, table->on_expr);
5401
conds->top_level_item();
5402
/* conds is always a new item as both cond and on_expr existed */
5403
assert(!conds->fixed);
5404
conds->fix_fields(join->session, &conds);
5407
conds= table->on_expr;
5408
table->prep_on_expr= table->on_expr= 0;
5416
Only inner tables of non-convertible outer joins
5417
remain with on_expr.
5421
table->setDepTables(table->getDepTables() | table->on_expr->used_tables());
5422
if (table->getEmbedding())
5424
table->setDepTables(table->getDepTables() & ~table->getEmbedding()->getNestedJoin()->used_tables);
5426
Embedding table depends on tables used
5427
in embedded on expressions.
5429
table->getEmbedding()->setOnExprDepTables(table->getEmbedding()->getOnExprDepTables() & table->on_expr->used_tables());
5432
table->setDepTables(table->getDepTables() & ~table->table->map);
5437
//If this is straight join, set prev table to be dependent on all tables
5438
//from this nested join, so that correct join order is selected.
5439
if ((test(join->select_options & SELECT_STRAIGHT_JOIN)) ||
5440
prev_table->straight)
5441
prev_table->setDepTables(prev_table->getDepTables() | used_tables);
5442
if (prev_table->on_expr)
5444
prev_table->setDepTables(prev_table->getDepTables() | table->getOnExprDepTables());
5445
table_map prev_used_tables= prev_table->getNestedJoin() ?
5446
prev_table->getNestedJoin()->used_tables :
5447
prev_table->table->map;
5449
If on expression contains only references to inner tables
5450
we still make the inner tables dependent on the outer tables.
5451
It would be enough to set dependency only on one outer table
5452
for them. Yet this is really a rare case.
5454
if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
5455
prev_table->setDepTables(prev_table->getDepTables() | used_tables);
5462
Flatten nested joins that can be flattened.
5463
no ON expression and not a semi-join => can be flattened.
5465
li= join_list->begin();
5466
while (TableList* table= li++)
5468
nested_join= table->getNestedJoin();
5469
if (nested_join && !table->on_expr)
5471
List<TableList>::iterator it(nested_join->join_list.begin());
5472
while (TableList* tbl= it++)
5474
tbl->setEmbedding(table->getEmbedding());
5475
tbl->setJoinList(table->getJoinList());
5477
li.replace(nested_join->join_list);
5483
static int remove_duplicates(Join *join, Table *entry,List<Item> &fields, Item *having)
5485
entry->reginfo.lock_type=TL_WRITE;
5487
/* Calculate how many saved fields there is in list */
5488
uint32_t field_count= 0;
5489
List<Item>::iterator it(fields.begin());
5490
while (Item* item=it++)
5492
if (item->get_tmp_table_field() && ! item->const_item())
5496
if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
5497
{ // only const items with no OPTION_FOUND_ROWS
5498
join->unit->select_limit_cnt= 1; // Only send first row
5501
Field **first_field=entry->getFields() + entry->getShare()->sizeFields() - field_count;
5502
uint32_t offset= field_count ? entry->getField(entry->getShare()->sizeFields() - field_count)->offset(entry->getInsertRecord()) : 0;
5503
uint32_t reclength= entry->getShare()->getRecordLength() - offset;
5505
entry->free_io_cache(); // Safety
5506
entry->cursor->info(HA_STATUS_VARIABLE);
5508
if (entry->getShare()->db_type() == heap_engine ||
5509
(!entry->getShare()->blob_fields &&
5510
((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->cursor->stats.records < join->session->variables.sortbuff_size)))
5512
error= remove_dup_with_hash_index(join->session, entry, field_count, first_field, reclength, having);
5516
error= remove_dup_with_compare(join->session, entry, first_field, offset, having);
5518
free_blobs(first_field);
5523
Function to setup clauses without sum functions.
5525
static int setup_without_group(Session *session,
5526
Item **ref_pointer_array,
5530
List<Item> &all_fields,
5534
bool *hidden_group_fields)
5537
nesting_map save_allow_sum_func=session->lex().allow_sum_func;
5539
session->lex().allow_sum_func&= ~(1 << session->lex().current_select->nest_level);
5540
res= session->setup_conds(tables, conds);
5542
session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
5543
res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields, order);
5544
session->lex().allow_sum_func&= ~(1 << session->lex().current_select->nest_level);
5545
res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields, group, hidden_group_fields);
5546
session->lex().allow_sum_func= save_allow_sum_func;
5551
Calculate the best possible join and initialize the join structure.
5558
static bool make_join_statistics(Join *join, TableList *tables, COND *conds, DYNAMIC_ARRAY *keyuse_array)
5563
uint32_t table_count;
5564
uint32_t const_count;
5566
table_map found_const_table_map;
5567
table_map all_table_map;
5568
table_map found_ref;
5572
Table **table_vector= NULL;
5573
JoinTable *stat= NULL;
5574
JoinTable *stat_end= NULL;
5576
JoinTable **stat_ref= NULL;
5577
optimizer::KeyUse *keyuse= NULL;
5578
optimizer::KeyUse *start_keyuse= NULL;
5579
table_map outer_join= 0;
5580
vector<optimizer::SargableParam> sargables;
5581
JoinTable *stat_vector[MAX_TABLES+1];
5582
optimizer::Position *partial_pos;
5584
table_count= join->tables;
5585
stat= (JoinTable*) join->session->mem.calloc(sizeof(JoinTable)*table_count);
5586
stat_ref= new (join->session->mem) JoinTable*[MAX_TABLES];
5587
table_vector= new (join->session->mem) Table*[2 * table_count];
5589
join->best_ref=stat_vector;
5591
stat_end=stat+table_count;
5592
found_const_table_map= all_table_map=0;
5597
s++, tables= tables->next_leaf, i++)
5599
TableList *embedding= tables->getEmbedding();
5602
s->const_keys.reset();
5603
s->checked_keys.reset();
5604
s->needed_reg.reset();
5605
table_vector[i]=s->table=table=tables->table;
5606
table->pos_in_table_list= tables;
5607
assert(table->cursor);
5608
error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5611
table->print_error(error, MYF(0));
5614
table->quick_keys.reset();
5615
table->reginfo.join_tab=s;
5616
table->reginfo.not_exists_optimize=0;
5617
memset(table->const_key_parts, 0, sizeof(key_part_map)*table->getShare()->sizeKeys());
5618
all_table_map|= table->map;
5620
s->info=0; // For describe
5622
s->dependent= tables->getDepTables();
5623
s->key_dependent= 0;
5624
table->quick_condition_rows= table->cursor->stats.records;
5626
s->on_expr_ref= &tables->on_expr;
5627
if (*s->on_expr_ref)
5629
/* s is the only inner table of an outer join */
5630
if (!table->cursor->stats.records && !embedding)
5632
s->dependent= 0; // Ignore LEFT JOIN depend.
5633
set_position(join, const_count++, s, NULL);
5636
outer_join|= table->map;
5637
s->embedding_map.reset();
5638
for (;embedding; embedding= embedding->getEmbedding())
5639
s->embedding_map|= embedding->getNestedJoin()->nj_map;
5642
if (embedding && !(false && ! embedding->getEmbedding()))
5644
/* s belongs to a nested join, maybe to several embedded joins */
5645
s->embedding_map.reset();
5648
NestedJoin *nested_join= embedding->getNestedJoin();
5649
s->embedding_map|= nested_join->nj_map;
5650
s->dependent|= embedding->getDepTables();
5651
embedding= embedding->getEmbedding();
5652
outer_join|= nested_join->used_tables;
5657
if ((table->cursor->stats.records <= 1) && !s->dependent &&
5658
(table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
5659
!join->no_const_tables)
5661
set_position(join, const_count++, s, NULL);
5665
join->outer_join=outer_join;
5667
if (join->outer_join)
5670
Build transitive closure for relation 'to be dependent on'.
5671
This will speed up the plan search for many cases with outer joins,
5672
as well as allow us to catch illegal cross references/
5673
Warshall's algorithm is used to build the transitive closure.
5674
As we use bitmaps to represent the relation the complexity
5675
of the algorithm is O((number of tables)^2).
5677
for (i= 0; i < table_count; i++)
5680
table= stat[i].table;
5682
if (!table->reginfo.join_tab->dependent)
5685
for (j= 0, s= stat; j < table_count; j++, s++)
5687
if (s->dependent & table->map)
5689
table_map was_dependent= s->dependent;
5690
s->dependent |= table->reginfo.join_tab->dependent;
5691
if (i > j && s->dependent != was_dependent)
5699
/* Catch illegal cross references for outer joins */
5700
for (i= 0, s= stat ; i < table_count ; i++, s++)
5702
if (s->dependent & s->table->map)
5704
join->tables=0; // Don't use join->table
5705
my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
5708
if (outer_join & s->table->map)
5709
s->table->maybe_null= 1;
5711
s->key_dependent= s->dependent;
5715
if (conds || outer_join)
5716
update_ref_and_keys(join->session, keyuse_array, stat, join->tables, conds, join->cond_equal, ~outer_join, join->select_lex, sargables);
5718
/* Read tables with 0 or 1 rows (system tables) */
5719
join->const_table_map= 0;
5721
optimizer::Position *p_pos= join->getFirstPosInPartialPlan();
5722
optimizer::Position *p_end= join->getSpecificPosInPartialPlan(const_count);
5723
while (p_pos < p_end)
5726
s= p_pos->getJoinTable();
5728
join->const_table_map|=s->table->map;
5729
if ((tmp= s->joinReadConstTable(p_pos)))
5732
return 1; // Fatal error
5735
found_const_table_map|= s->table->map;
5739
/* loop until no more const tables are found */
5743
more_const_tables_found:
5748
We only have to loop from stat_vector + const_count as
5749
set_position() will move all const_tables first in stat_vector
5752
for (JoinTable **pos= stat_vector+const_count; (s= *pos); pos++)
5757
If equi-join condition by a key is null rejecting and after a
5758
substitution of a const table the key value happens to be null
5759
then we can state that there are no matches for this equi-join.
5761
if ((keyuse= s->keyuse) && *s->on_expr_ref && s->embedding_map.none())
5764
When performing an outer join operation if there are no matching rows
5765
for the single row of the outer table all the inner tables are to be
5766
null complemented and thus considered as constant tables.
5767
Here we apply this consideration to the case of outer join operations
5768
with a single inner table only because the case with nested tables
5769
would require a more thorough analysis.
5770
TODO. Apply single row substitution to null complemented inner tables
5771
for nested outer join operations.
5773
while (keyuse->getTable() == table)
5775
if (! (keyuse->getVal()->used_tables() & ~join->const_table_map) &&
5776
keyuse->getVal()->is_null() && keyuse->isNullRejected())
5779
table->mark_as_null_row();
5780
found_const_table_map|= table->map;
5781
join->const_table_map|= table->map;
5782
set_position(join, const_count++, s, NULL);
5783
goto more_const_tables_found;
5789
if (s->dependent) // If dependent on some table
5791
// All dep. must be constants
5792
if (s->dependent & ~(found_const_table_map))
5794
if (table->cursor->stats.records <= 1L &&
5795
(table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
5796
!table->pos_in_table_list->getEmbedding())
5800
join->const_table_map|=table->map;
5801
set_position(join, const_count++, s, NULL);
5802
partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5803
if ((tmp= s->joinReadConstTable(partial_pos)))
5806
return 1; // Fatal error
5809
found_const_table_map|= table->map;
5813
/* check if table can be read by key or table only uses const refs */
5814
if ((keyuse=s->keyuse))
5817
while (keyuse->getTable() == table)
5819
start_keyuse= keyuse;
5820
key= keyuse->getKey();
5821
s->keys.set(key); // QQ: remove this ?
5828
if (keyuse->getVal()->type() != Item::NULL_ITEM &&
5829
! keyuse->getOptimizeFlags())
5831
if (! ((~found_const_table_map) & keyuse->getUsedTables()))
5832
const_ref.set(keyuse->getKeypart());
5834
refs|= keyuse->getUsedTables();
5835
eq_part.set(keyuse->getKeypart());
5838
} while (keyuse->getTable() == table && keyuse->getKey() == key);
5840
if (is_keymap_prefix(eq_part, table->key_info[key].key_parts) &&
5841
! table->pos_in_table_list->getEmbedding())
5843
if ((table->key_info[key].flags & (HA_NOSAME)) == HA_NOSAME)
5845
if (const_ref == eq_part)
5846
{ // Found everything for ref.
5850
join->const_table_map|= table->map;
5851
set_position(join, const_count++, s, start_keyuse);
5852
if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
5854
partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5855
if ((tmp=s->joinReadConstTable(partial_pos)))
5858
return 1; // Fatal error
5861
found_const_table_map|= table->map;
5865
found_ref|= refs; // Table is const if all refs are const
5867
else if (const_ref == eq_part)
5868
s->const_keys.set(key);
5873
} while (join->const_table_map & found_ref && ref_changed);
5876
Update info on indexes that can be used for search lookups as
5877
reading const tables may has added new sargable predicates.
5879
if (const_count && ! sargables.empty())
5881
BOOST_FOREACH(vector<optimizer::SargableParam>::reference iter, sargables)
5883
Field& field= *iter.getField();
5884
JoinTable *join_tab= field.getTable()->reginfo.join_tab;
5885
key_map possible_keys= field.key_start & field.getTable()->keys_in_use_for_query;
5886
bool is_const= true;
5887
for (uint32_t j= 0; j < iter.getNumValues(); j++)
5888
is_const&= iter.isConstItem(j);
5890
join_tab[0].const_keys|= possible_keys;
5894
/* Calc how many (possible) matched records in each table */
5896
for (s=stat ; s < stat_end ; s++)
5898
if (s->type == AM_SYSTEM || s->type == AM_CONST)
5900
/* Only one matching row */
5901
s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
5904
/* Approximate found rows and time to read them */
5905
s->found_records=s->records=s->table->cursor->stats.records;
5906
s->read_time=(ha_rows) s->table->cursor->scan_time();
5909
Set a max range of how many seeks we can expect when using keys
5910
This is can't be to high as otherwise we are likely to use
5913
s->worst_seeks= min((double) s->found_records / 10,
5914
(double) s->read_time*3);
5915
if (s->worst_seeks < 2.0) // Fix for small tables
5919
Add to stat->const_keys those indexes for which all group fields or
5920
all select distinct fields participate in one index.
5922
add_group_and_distinct_keys(join, s);
5924
if (s->const_keys.any() &&
5925
!s->table->pos_in_table_list->getEmbedding())
5928
optimizer::SqlSelect *select= NULL;
5929
select= optimizer::make_select(s->table, found_const_table_map, found_const_table_map, *s->on_expr_ref ? *s->on_expr_ref : conds, 1, &error);
5932
records= get_quick_record_count(join->session, select, s->table, &s->const_keys, join->row_limit);
5933
s->quick=select->quick;
5934
s->needed_reg=select->needed_reg;
5937
if (records == 0 && s->table->reginfo.impossible_range)
5940
Impossible WHERE or ON expression
5941
In case of ON, we mark that the we match one empty NULL row.
5942
In case of WHERE, don't set found_const_table_map to get the
5943
caller to abort with a zero row result.
5945
join->const_table_map|= s->table->map;
5946
set_position(join, const_count++, s, NULL);
5948
if (*s->on_expr_ref)
5950
/* Generate empty row */
5951
s->info= "Impossible ON condition";
5952
found_const_table_map|= s->table->map;
5954
s->table->mark_as_null_row(); // All fields are NULL
5957
if (records != HA_POS_ERROR)
5959
s->found_records=records;
5960
s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
5966
join->join_tab=stat;
5967
join->map2table=stat_ref;
5968
join->table= join->all_tables=table_vector;
5969
join->const_tables=const_count;
5970
join->found_const_table_map=found_const_table_map;
5972
/* Find an optimal join order of the non-constant tables. */
5973
if (join->const_tables != join->tables)
5975
optimize_keyuse(join, keyuse_array);
5976
// @note c_str() is not likely to be valid here if dtrace expects it to
5977
// exist for any period of time.
5978
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_START(join->session->getQueryString()->c_str(), join->session->thread_id);
5979
bool res= choose_plan(join, all_table_map & ~join->const_table_map);
5980
DRIZZLE_QUERY_OPT_CHOOSE_PLAN_DONE(res ? 1 : 0);
5986
join->copyPartialPlanIntoOptimalPlan(join->const_tables);
5987
join->best_read= 1.0;
5989
/* Generate an execution plan from the found optimal join order. */
5990
return join->session->getKilled() || get_best_combination(join);
5994
Assign each nested join structure a bit in the nested join bitset.
5996
Assign each nested join structure (except "confluent" ones - those that
5997
embed only one element) a bit in the nested join bitset.
5999
@param join Join being processed
6000
@param join_list List of tables
6001
@param first_unused Number of first unused bit in the nest joing bitset before the
6005
This function is called after simplify_joins(), when there are no
6006
redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
6007
we will not run out of bits in the nested join bitset.
6010
First unused bit in the nest join bitset after the call.
6012
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
6014
List<TableList>::iterator li(join_list->begin());
6015
while (TableList* table= li++)
6017
if (NestedJoin* nested_join= table->getNestedJoin())
6020
It is guaranteed by simplify_joins() function that a nested join
6021
that has only one child is either
6022
- a single-table view (the child is the underlying table), or
6023
- a single-table semi-join nest
6025
We don't assign bits to such sj-nests because
6026
1. it is redundant (a "sequence" of one table cannot be interleaved
6028
2. we could run out of bits in the nested join bitset otherwise.
6030
if (nested_join->join_list.size() != 1)
6032
/* Don't assign bits to sj-nests */
6034
nested_join->nj_map.set(first_unused++);
6035
first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
6040
return(first_unused);
6045
Return table number if there is only one table in sort order
6046
and group and order is compatible, else return 0.
6048
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
6053
a= b; // Only one need to be given
6057
for (; a && b; a=a->next,b=b->next)
6059
if (!(*a->item)->eq(*b->item,1))
6061
map|= a->item[0]->used_tables();
6063
if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
6066
for (; !(map & tables->table->map); tables= tables->next_leaf) {};
6067
if (map != tables->table->map)
6068
return NULL; // More than one table
6069
return tables->table;
6073
Set NestedJoin::counter=0 in all nested joins in passed list.
6075
Recursively set NestedJoin::counter=0 for all nested joins contained in
6076
the passed join_list.
6078
@param join_list List of nested joins to process. It may also contain base
6079
tables which will be ignored.
6081
static void reset_nj_counters(List<TableList> *join_list)
6083
List<TableList>::iterator li(join_list->begin());
6084
while (TableList* table= li++)
6086
NestedJoin *nested_join;
6087
if ((nested_join= table->getNestedJoin()))
6089
nested_join->counter_= 0;
6090
reset_nj_counters(&nested_join->join_list);
6096
Return 1 if second is a subpart of first argument.
6098
If first parts has different direction, change it to second part
6099
(group is sorted like order)
6101
static bool test_if_subpart(Order *a, Order *b)
6103
for (; a && b; a=a->next,b=b->next)
6105
if ((*a->item)->eq(*b->item,1))
6114
Nested joins perspective: Remove the last table from the join order.
6116
The algorithm is the reciprocal of check_interleaving_with_nj(), hence
6117
parent join nest nodes are updated only when the last table in its child
6118
node is removed. The ASCII graphic below will clarify.
6120
%A table nesting such as <tt> t1 x [ ( t2 x t3 ) x ( t4 x t5 ) ] </tt>is
6121
represented by the below join nest tree.
6129
t1 x [ (t2 x t3) x (t4 x t5) ]
6132
At the point in time when check_interleaving_with_nj() adds the table t5 to
6133
the query execution plan, QEP, it also directs the node named NJ2 to mark
6134
the table as covered. NJ2 does so by incrementing its @c counter
6135
member. Since all of NJ2's tables are now covered by the QEP, the algorithm
6136
proceeds up the tree to NJ1, incrementing its counter as well. All join
6137
nests are now completely covered by the QEP.
6139
restore_prev_nj_state() does the above in reverse. As seen above, the node
6140
NJ1 contains the nodes t2, t3, and NJ2. Its counter being equal to 3 means
6141
that the plan covers t2, t3, and NJ2, @e and that the sub-plan (t4 x t5)
6142
completely covers NJ2. The removal of t5 from the partial plan will first
6143
decrement NJ2's counter to 1. It will then detect that NJ2 went from being
6144
completely to partially covered, and hence the algorithm must continue
6145
upwards to NJ1 and decrement its counter to 2. %A subsequent removal of t4
6146
will however not influence NJ1 since it did not un-cover the last table in
6150
restore_prev_nj_state()
6151
last join table to remove, it is assumed to be the last in current
6156
Remove the last table from the partial join order and update the nested
6157
joins counters and join->cur_embedding_map. It is ok to call this
6158
function for the first table in join order (for which
6159
check_interleaving_with_nj has not been called)
6161
@param last join table to remove, it is assumed to be the last in current
6165
static void restore_prev_nj_state(JoinTable *last)
6167
TableList *last_emb= last->table->pos_in_table_list->getEmbedding();
6168
Join *join= last->join;
6169
for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6171
NestedJoin *nest= last_emb->getNestedJoin();
6173
bool was_fully_covered= nest->is_fully_covered();
6175
if (--nest->counter_ == 0)
6176
join->cur_embedding_map&= ~nest->nj_map;
6178
if (!was_fully_covered)
6181
join->cur_embedding_map|= nest->nj_map;
6186
Create a condition for a const reference and add this to the
6187
currenct select for the table.
6189
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab)
6191
if (!join_tab->ref.key_parts)
6194
Item_cond_and *cond=new Item_cond_and();
6195
Table *table=join_tab->table;
6197
for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
6199
Field *field=table->getField(table->key_info[join_tab->ref.key].key_part[i].fieldnr - 1);
6200
Item *value=join_tab->ref.items[i];
6201
cond->add(new Item_func_equal(new Item_field(field), value));
6203
if (session->is_fatal_error)
6207
cond->fix_fields(session, (Item**)&cond);
6209
if (join_tab->select)
6211
cond->add(join_tab->select->cond);
6212
join_tab->select_cond=join_tab->select->cond=cond;
6214
else if ((join_tab->select= optimizer::make_select(join_tab->table, 0, 0, cond, 0, &error)))
6215
join_tab->select_cond=cond;
6220
static void free_blobs(Field **ptr)
6222
for (; *ptr ; ptr++)
6224
if ((*ptr)->flags & BLOB_FLAG)
6225
((Field_blob *) (*ptr))->free();
6230
@} (end of group Query_Optimizer)
6233
} /* namespace drizzled */