87
87
KEYUSE *keyuse, table_map used_tables,
88
88
KEY_PART_INFO *key_part, uchar *key_buff,
90
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
91
90
static void make_outerjoin_info(JOIN *join);
92
91
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
93
92
static void make_join_readinfo(JOIN *join, ulonglong options);
109
108
void *table_join_idx);
110
109
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
111
110
COND *conds, bool top);
112
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
111
static bool check_interleaving_with_nj(JOIN_TAB *next);
113
112
static void restore_prev_nj_state(JOIN_TAB *last);
114
113
static void reset_nj_counters(List<TABLE_LIST> *join_list);
115
114
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
1676
1675
We have to test for 'conds' here as the WHERE may not be constant
1677
1676
even if we don't have any tables for prepared statements or if
1678
1677
conds uses something like 'rand()'.
1678
If the HAVING clause is either impossible or always true, then
1679
JOIN::having is set to NULL by optimize_cond.
1680
In this case JOIN::exec must check for JOIN::having_value, in the
1681
same way it checks for JOIN::cond_value.
1680
1683
if (cond_value != Item::COND_FALSE &&
1684
having_value != Item::COND_FALSE &&
1681
1685
(!conds || conds->val_int()) &&
1682
1686
(!having || having->val_int()))
1875
1879
/* Free first data from old join */
1876
1880
curr_join->join_free();
1877
if (make_simple_join(curr_join, curr_tmp_table))
1881
if (curr_join->make_simple_join(this, curr_tmp_table))
1878
1882
DBUG_VOID_RETURN;
1879
1883
calc_group_buffer(curr_join, group_list);
1880
1884
count_field_types(select_lex, &curr_join->tmp_table_param,
1997
2001
curr_join->select_distinct=0;
1999
2003
curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2000
if (make_simple_join(curr_join, curr_tmp_table))
2004
if (curr_join->make_simple_join(this, curr_tmp_table))
2001
2005
DBUG_VOID_RETURN;
2002
2006
calc_group_buffer(curr_join, curr_join->group_list);
2003
2007
count_field_types(select_lex, &curr_join->tmp_table_param,
2459
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
2463
make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, COND *conds,
2460
2464
DYNAMIC_ARRAY *keyuse_array)
2468
TABLE_LIST *tables= tables_arg;
2464
2469
uint i,table_count,const_count,key;
2465
2470
table_map found_const_table_map, all_table_map, found_ref, refs;
2466
2471
key_map const_ref, eq_part;
2498
2503
table_vector[i]=s->table=table=tables->table;
2499
2504
table->pos_in_table_list= tables;
2500
2505
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2503
table->file->print_error(error, MYF(0));
2508
table->file->print_error(error, MYF(0));
2506
2511
table->quick_keys.clear_all();
2507
2512
table->reginfo.join_tab=s;
2607
2612
if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
2608
2613
conds, join->cond_equal,
2609
2614
~outer_join, join->select_lex, &sargables))
2612
2617
/* Read tables with 0 or 1 rows (system tables) */
2613
2618
join->const_table_map= 0;
2744
2749
set_position(join,const_count++,s,start_keyuse);
2745
2750
if (create_ref_for_key(join, s, start_keyuse,
2746
2751
found_const_table_map))
2748
2753
if ((tmp=join_read_const_table(s,
2749
2754
join->positions+const_count-1)))
2752
DBUG_RETURN(1); // Fatal error
2757
goto error; // Fatal error
2755
2760
found_const_table_map|= table->map;
2883
2888
/* Generate an execution plan from the found optimal join order. */
2884
2889
DBUG_RETURN(join->thd->killed || get_best_combination(join));
2893
Need to clean up join_tab from TABLEs in case of error.
2894
They won't get cleaned up by JOIN::cleanup() because JOIN::join_tab
2895
may not be assigned yet by this function (which is building join_tab).
2896
Dangling TABLE::reginfo.join_tab may cause part_of_refkey to choke.
2898
for (tables= tables_arg; tables; tables= tables->next_leaf)
2899
tables->table->reginfo.join_tab= NULL;
4890
4906
join->positions[idx]= best_pos;
4909
Update the interleaving state after extending the current partial plan
4911
We are doing this here because best_extension_by_limited_search reverts
4912
the interleaving state to the one of the non-extended partial plan
4915
IF_DBUG(bool is_interleave_error= )
4916
check_interleaving_with_nj (best_table);
4917
/* This has been already checked by best_extension_by_limited_search */
4918
DBUG_ASSERT(!is_interleave_error);
4892
4920
/* find the position of 'best_table' in 'join->best_ref' */
4894
4922
JOIN_TAB *pos= join->best_ref[best_idx];
4909
DBUG_EXECUTE("opt", print_plan(join, join->tables,
4937
DBUG_EXECUTE("opt", print_plan(join, idx,
4910
4938
record_count, read_time, read_time,
4912
4940
} while (TRUE);
5064
5092
table_map real_table_bit= s->table->map;
5065
5093
if ((remaining_tables & real_table_bit) &&
5066
5094
!(remaining_tables & s->dependent) &&
5067
(!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
5095
(!idx || !check_interleaving_with_nj(s)))
5069
5097
double current_record_count, current_read_time;
5211
5239
table_map real_table_bit=s->table->map;
5212
5240
if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
5213
(!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
5241
(!idx|| !check_interleaving_with_nj(s)))
5215
5243
double records, best;
5216
5244
best_access_path(join, s, thd, rest_tables, idx, record_count,
5673
make_simple_join(JOIN *join,TABLE *tmp_table)
5701
@details Initialize a JOIN as a query execution plan
5702
that accesses a single table via a table scan.
5704
@param parent contains JOIN_TAB and TABLE object buffers for this join
5705
@param tmp_table temporary table
5707
@retval FALSE success
5708
@retval TRUE error occurred
5711
JOIN::make_simple_join(JOIN *parent, TABLE *tmp_table)
5677
DBUG_ENTER("make_simple_join");
5713
DBUG_ENTER("JOIN::make_simple_join");
5680
5716
Reuse TABLE * and JOIN_TAB if already allocated by a previous call
5681
5717
to this function through JOIN::exec (may happen for sub-queries).
5683
if (!join->table_reexec)
5685
if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
5686
DBUG_RETURN(TRUE); /* purecov: inspected */
5688
join->tmp_join->table_reexec= join->table_reexec;
5690
if (!join->join_tab_reexec)
5692
if (!(join->join_tab_reexec=
5693
(JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
5694
DBUG_RETURN(TRUE); /* purecov: inspected */
5696
join->tmp_join->join_tab_reexec= join->join_tab_reexec;
5698
tableptr= join->table_reexec;
5699
join_tab= join->join_tab_reexec;
5719
if (!parent->join_tab_reexec &&
5720
!(parent->join_tab_reexec= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB))))
5721
DBUG_RETURN(TRUE); /* purecov: inspected */
5701
join->join_tab=join_tab;
5702
join->table=tableptr; tableptr[0]=tmp_table;
5704
join->const_tables=0;
5705
join->const_table_map=0;
5706
join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
5707
join->tmp_table_param.func_count=0;
5708
join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
5709
join->first_record=join->sort_and_group=0;
5710
join->send_records=(ha_rows) 0;
5712
join->row_limit=join->unit->select_limit_cnt;
5713
join->do_send_rows = (join->row_limit) ? 1 : 0;
5723
join_tab= parent->join_tab_reexec;
5724
table= &parent->table_reexec[0]; parent->table_reexec[0]= tmp_table;
5728
tmp_table_param.field_count= tmp_table_param.sum_func_count=
5729
tmp_table_param.func_count= 0;
5730
tmp_table_param.copy_field= tmp_table_param.copy_field_end=0;
5731
first_record= sort_and_group=0;
5732
send_records= (ha_rows) 0;
5734
row_limit= unit->select_limit_cnt;
5735
do_send_rows= row_limit ? 1 : 0;
5715
5737
join_tab->cache.buff=0; /* No caching */
5716
5738
join_tab->table=tmp_table;
7645
Check if we eliminated all the predicates of the level, e.g.
7646
(a=a AND b=b AND a=a).
7648
if (!args->elements &&
7649
!cond_equal.current_level.elements &&
7651
return new Item_int((longlong) 1, 1);
7622
7653
List_iterator_fast<Item_equal> it(cond_equal.current_level);
7623
7654
while ((item_equal= it++))
8802
8830
TRUE Requested join order extension not allowed.
8805
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
8833
static bool check_interleaving_with_nj(JOIN_TAB *next_tab)
8807
8835
TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
8808
JOIN *join= last_tab->join;
8836
JOIN *join= next_tab->join;
8810
8838
if (join->cur_embedding_map & ~next_tab->embedding_map)