4561
4563
tmp->quick_fix_field();
4562
4564
cond_tab->select_cond= !cond_tab->select_cond ? tmp :
4563
new Item_cond_and(cond_tab->select_cond,
4565
new Item_cond_and(cond_tab->select_cond,
4565
4567
if (!cond_tab->select_cond)
4567
4569
cond_tab->select_cond->quick_fix_field();
4570
4572
if (const_cond && !const_cond->val_int())
4572
return(1); // Impossible const condition
4574
return(1); // Impossible const condition
4576
4578
used_tables=((select->const_tables=join->const_table_map) |
4577
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
4579
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
4578
4580
for (uint32_t i=join->const_tables ; i < join->tables ; i++)
4580
4582
JoinTable *tab=join->join_tab+i;
4582
first_inner is the X in queries like:
4583
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
4584
first_inner is the X in queries like:
4585
SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
4585
4587
JoinTable *first_inner_tab= tab->first_inner;
4586
4588
table_map current_map= tab->table->map;
4587
4589
bool use_quick_range=0;
4591
Following force including random expression in last table condition.
4592
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
4593
Following force including random expression in last table condition.
4594
It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
4594
4596
if (i == join->tables-1)
4595
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
4597
current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
4596
4598
used_tables|=current_map;
4598
4600
if (tab->type == AT_REF && tab->quick &&
4599
(uint32_t) tab->ref.key == tab->quick->index &&
4600
tab->ref.key_length < tab->quick->max_used_key_length)
4601
(uint32_t) tab->ref.key == tab->quick->index &&
4602
tab->ref.key_length < tab->quick->max_used_key_length)
4602
/* Range uses longer key; Use this instead of ref on key */
4604
/* Range uses longer key; Use this instead of ref on key */
4606
4608
tab->ref.key= -1;
4607
tab->ref.key_parts=0; // Don't use ref key.
4608
join->best_positions[i].records_read= rows2double(tab->quick->records);
4609
tab->ref.key_parts=0; // Don't use ref key.
4610
cur_pos= join->getPosFromOptimalPlan(i);
4611
cur_pos.records_read= rows2double(tab->quick->records);
4610
We will use join cache here : prevent sorting of the first
4611
table only and sort at the end.
4613
We will use join cache here : prevent sorting of the first
4614
table only and sort at the end.
4613
4616
if (i != join->const_tables && join->tables > join->const_tables + 1)
4614
4617
join->full_join= 1;
4643
4646
if (tmp || !cond || tab->type == AT_REF || tab->type == AT_REF_OR_NULL ||
4644
4647
tab->type == AT_EQ_REF)
4646
SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
4647
session->memdup((unsigned char*) select,
4650
return(1); // End of memory
4649
SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
4650
session->memdup((unsigned char*) select,
4653
return(1); // End of memory
4652
If tab is an inner table of an outer join operation,
4653
add a match guard to the pushed down predicate.
4654
The guard will turn the predicate on only after
4655
the first match for outer tables is encountered.
4655
If tab is an inner table of an outer join operation,
4656
add a match guard to the pushed down predicate.
4657
The guard will turn the predicate on only after
4658
the first match for outer tables is encountered.
4657
4660
if (cond && tmp)
4660
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
4661
a cond, so neutralize the hack above.
4663
Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
4664
a cond, so neutralize the hack above.
4663
4666
if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
4665
4668
tab->select_cond=sel->cond=tmp;
4666
4669
/* Push condition to storage engine if this is enabled
4667
4670
and the condition is not guarded */
4668
4671
tab->table->file->pushed_cond= NULL;
4669
if (session->variables.engine_condition_pushdown)
4672
if (session->variables.engine_condition_pushdown)
4671
4674
COND *push_cond=
4672
4675
make_cond_for_table(tmp, current_map, current_map, 0);
4682
4685
tab->select_cond= sel->cond= NULL;
4684
sel->head=tab->table;
4687
/* Use quick key read if it's a constant and it's not used
4689
if (tab->needed_reg.none() && tab->type != AT_EQ_REF
4690
&& (tab->type != AT_REF || (uint32_t) tab->ref.key == tab->quick->index))
4692
sel->quick=tab->quick; // Use value from get_quick_...
4693
sel->quick_keys.reset();
4694
sel->needed_reg.reset();
4702
uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
4703
if (i == join->const_tables && ref_key)
4705
if (tab->const_keys.any() &&
4706
tab->table->reginfo.impossible_range)
4709
else if (tab->type == AT_ALL && ! use_quick_range)
4711
if (tab->const_keys.any() &&
4712
tab->table->reginfo.impossible_range)
4713
return(1); // Impossible range
4715
We plan to scan all rows.
4716
Check again if we should use an index.
4717
We could have used an column from a previous table in
4718
the index if we are using limit and this is the first table
4721
if ((cond && (!((tab->keys & tab->const_keys) == tab->keys) && i > 0)) ||
4722
(!tab->const_keys.none() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
4724
/* Join with outer join condition */
4725
COND *orig_cond=sel->cond;
4726
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
4729
We can't call sel->cond->fix_fields,
4730
as it will break tab->on_expr if it's AND condition
4731
(fix_fields currently removes extra AND/OR levels).
4732
Yet attributes of the just built condition are not needed.
4733
Thus we call sel->cond->quick_fix_field for safety.
4735
if (sel->cond && !sel->cond->fixed)
4736
sel->cond->quick_fix_field();
4738
if (sel->test_quick_select(session, tab->keys,
4739
used_tables & ~ current_map,
4740
(join->select_options &
4743
join->unit->select_limit_cnt), 0,
4687
sel->head=tab->table;
4690
/* Use quick key read if it's a constant and it's not used
4692
if (tab->needed_reg.none() && tab->type != AT_EQ_REF
4693
&& (tab->type != AT_REF || (uint32_t) tab->ref.key == tab->quick->index))
4695
sel->quick=tab->quick; // Use value from get_quick_...
4696
sel->quick_keys.reset();
4697
sel->needed_reg.reset();
4705
uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
4706
if (i == join->const_tables && ref_key)
4708
if (tab->const_keys.any() &&
4709
tab->table->reginfo.impossible_range)
4712
else if (tab->type == AT_ALL && ! use_quick_range)
4714
if (tab->const_keys.any() &&
4715
tab->table->reginfo.impossible_range)
4716
return(1); // Impossible range
4718
We plan to scan all rows.
4719
Check again if we should use an index.
4720
We could have used an column from a previous table in
4721
the index if we are using limit and this is the first table
4724
cur_pos= join->getPosFromOptimalPlan(i);
4725
if ((cond && (!((tab->keys & tab->const_keys) == tab->keys) && i > 0)) ||
4726
(!tab->const_keys.none() && (i == join->const_tables) &&
4727
(join->unit->select_limit_cnt < cur_pos.records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
4729
/* Join with outer join condition */
4730
COND *orig_cond=sel->cond;
4731
sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
4734
We can't call sel->cond->fix_fields,
4735
as it will break tab->on_expr if it's AND condition
4736
(fix_fields currently removes extra AND/OR levels).
4737
Yet attributes of the just built condition are not needed.
4738
Thus we call sel->cond->quick_fix_field for safety.
4740
if (sel->cond && !sel->cond->fixed)
4741
sel->cond->quick_fix_field();
4743
if (sel->test_quick_select(session, tab->keys,
4744
used_tables & ~ current_map,
4745
(join->select_options &
4748
join->unit->select_limit_cnt), 0,
4747
Before reporting "Impossible WHERE" for the whole query
4748
we have to check isn't it only "impossible ON" instead
4752
Before reporting "Impossible WHERE" for the whole query
4753
we have to check isn't it only "impossible ON" instead
4750
4755
sel->cond=orig_cond;
4751
4756
if (!*tab->on_expr_ref ||
4752
4757
sel->test_quick_select(session, tab->keys,
4753
used_tables & ~ current_map,
4754
(join->select_options &
4757
join->unit->select_limit_cnt),0,
4759
return(1); // Impossible WHERE
4758
used_tables & ~ current_map,
4759
(join->select_options &
4762
join->unit->select_limit_cnt),0,
4764
return(1); // Impossible WHERE
4762
sel->cond=orig_cond;
4767
sel->cond=orig_cond;
4764
/* Fix for EXPLAIN */
4766
join->best_positions[i].records_read= (double)sel->quick->records;
4770
sel->needed_reg=tab->needed_reg;
4771
sel->quick_keys.reset();
4769
/* Fix for EXPLAIN */
4772
cur_pos= join->getPosFromOptimalPlan(i);
4773
cur_pos.records_read= (double)sel->quick->records;
4778
sel->needed_reg=tab->needed_reg;
4779
sel->quick_keys.reset();
4773
4781
if (!((tab->checked_keys & sel->quick_keys) == sel->quick_keys) ||
4774
4782
!((tab->checked_keys & sel->needed_reg) == sel->needed_reg))
4776
tab->keys= sel->quick_keys;
4784
tab->keys= sel->quick_keys;
4777
4785
tab->keys|= sel->needed_reg;
4778
tab->use_quick= (!sel->needed_reg.none() &&
4779
(select->quick_keys.none() ||
4781
(select->quick->records >= 100L)))) ?
4783
sel->read_tables= used_tables & ~current_map;
4785
if (i != join->const_tables && tab->use_quick != 2)
4786
{ /* Read with cache */
4786
tab->use_quick= (!sel->needed_reg.none() &&
4787
(select->quick_keys.none() ||
4789
(select->quick->records >= 100L)))) ?
4791
sel->read_tables= used_tables & ~current_map;
4793
if (i != join->const_tables && tab->use_quick != 2)
4794
{ /* Read with cache */
4788
4796
(tmp=make_cond_for_table(cond,
4789
join->const_table_map |
4793
tab->cache.select=(SQL_SELECT*)
4794
session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
4795
tab->cache.select->cond=tmp;
4796
tab->cache.select->read_tables=join->const_table_map;
4797
join->const_table_map |
4801
tab->cache.select=(SQL_SELECT*)
4802
session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
4803
tab->cache.select->cond=tmp;
4804
tab->cache.select->read_tables=join->const_table_map;
4803
Push down conditions from all on expressions.
4804
Each of these conditions are guarded by a variable
4805
that turns if off just before null complemented row for
4806
outer joins is formed. Thus, the condition from an
4807
'on expression' are guaranteed not to be checked for
4808
the null complemented row.
4811
Push down conditions from all on expressions.
4812
Each of these conditions are guarded by a variable
4813
that turns if off just before null complemented row for
4814
outer joins is formed. Thus, the condition from an
4815
'on expression' are guaranteed not to be checked for
4816
the null complemented row.
4811
4819
/* First push down constant conditions from on expressions */
4812
4820
for (JoinTable *join_tab= join->join_tab+join->const_tables;
4813
join_tab < join->join_tab+join->tables ; join_tab++)
4821
join_tab < join->join_tab+join->tables ; join_tab++)
4815
4823
if (*join_tab->on_expr_ref)
4817
4825
JoinTable *cond_tab= join_tab->first_inner;
4818
4826
tmp= make_cond_for_table(*join_tab->on_expr_ref,
4819
join->const_table_map,
4827
join->const_table_map,
4823
4831
tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
4837
4845
while (first_inner_tab && first_inner_tab->last_inner == last_tab)
4840
Table tab is the last inner table of an outer join.
4841
An on expression is always attached to it.
4848
Table tab is the last inner table of an outer join.
4849
An on expression is always attached to it.
4843
4851
COND *on_expr= *first_inner_tab->on_expr_ref;
4845
4853
table_map used_tables2= (join->const_table_map |
4846
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
4847
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
4854
OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
4855
for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
4849
4857
current_map= tab->table->map;
4850
4858
used_tables2|= current_map;
4851
4859
COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
4855
4863
JoinTable *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
4857
First add the guards for match variables of
4858
all embedding outer join operations.
4865
First add the guards for match variables of
4866
all embedding outer join operations.
4860
4868
if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
4865
Now add the guard turning the predicate off for
4866
the null complemented row.
4873
Now add the guard turning the predicate off for
4874
the null complemented row.
4868
4876
tmp_cond= new Item_func_trig_cond(tmp_cond,
4872
4880
tmp_cond->quick_fix_field();
4873
/* Add the predicate to other pushed down predicates */
4881
/* Add the predicate to other pushed down predicates */
4874
4882
cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
4875
new Item_cond_and(cond_tab->select_cond,
4883
new Item_cond_and(cond_tab->select_cond,
4877
4885
if (!cond_tab->select_cond)
4879
4887
cond_tab->select_cond->quick_fix_field();