495
500
keyuse Pointer to possible keys
496
501
*****************************************************************************/
499
Merge new key definitions to old ones, remove those not used in both.
501
This is called for OR between different levels.
503
To be able to do 'ref_or_null' we merge a comparison of a column
504
and 'column IS NULL' to one test. This is useful for sub select queries
505
that are internally transformed to something like:.
508
SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
511
KEY_FIELD::null_rejecting is processed as follows: @n
512
result has null_rejecting=true if it is set for both ORed references.
514
- (t2.key = t1.field OR t2.key = t1.field) -> null_rejecting=true
515
- (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
518
The result of this is that we're missing some 'ref' accesses.
519
OptimizerTeam: Fix this
521
static KEY_FIELD *merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end, uint32_t and_level)
523
if (start == new_fields)
524
return start; // Impossible or
525
if (new_fields == end)
526
return start; // No new fields, skip all
528
KEY_FIELD *first_free=new_fields;
530
/* Mark all found fields in old array */
531
for (; new_fields != end ; new_fields++)
533
for (KEY_FIELD *old=start ; old != first_free ; old++)
535
if (old->field == new_fields->field)
538
NOTE: below const_item() call really works as "!used_tables()", i.e.
539
it can return false where it is feasible to make it return true.
541
The cause is as follows: Some of the tables are already known to be
542
const tables (the detection code is in make_join_statistics(),
543
above the update_ref_and_keys() call), but we didn't propagate
544
information about this: Table::const_table is not set to true, and
545
Item::update_used_tables() hasn't been called for each item.
546
The result of this is that we're missing some 'ref' accesses.
547
TODO: OptimizerTeam: Fix this
549
if (!new_fields->val->const_item())
552
If the value matches, we can use the key reference.
553
If not, we keep it until we have examined all new values
555
if (old->val->eq(new_fields->val, old->field->binary()))
557
old->level= and_level;
558
old->optimize= ((old->optimize & new_fields->optimize &
559
KEY_OPTIMIZE_EXISTS) |
560
((old->optimize | new_fields->optimize) &
561
KEY_OPTIMIZE_REF_OR_NULL));
562
old->null_rejecting= (old->null_rejecting &&
563
new_fields->null_rejecting);
566
else if (old->eq_func && new_fields->eq_func &&
567
old->val->eq_by_collation(new_fields->val,
568
old->field->binary(),
569
old->field->charset()))
572
old->level= and_level;
573
old->optimize= ((old->optimize & new_fields->optimize &
574
KEY_OPTIMIZE_EXISTS) |
575
((old->optimize | new_fields->optimize) &
576
KEY_OPTIMIZE_REF_OR_NULL));
577
old->null_rejecting= (old->null_rejecting &&
578
new_fields->null_rejecting);
580
else if (old->eq_func && new_fields->eq_func &&
581
((old->val->const_item() && old->val->is_null()) ||
582
new_fields->val->is_null()))
584
/* field = expression OR field IS NULL */
585
old->level= and_level;
586
old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
588
Remember the NOT NULL value unless the value does not depend
591
if (!old->val->used_tables() && old->val->is_null())
592
old->val= new_fields->val;
593
/* The referred expression can be NULL: */
594
old->null_rejecting= 0;
599
We are comparing two different const. In this case we can't
600
use a key-lookup on this so it's better to remove the value
601
and let the range optimzier handle it
603
if (old == --first_free) // If last item
605
*old= *first_free; // Remove old value
606
old--; // Retry this value
611
/* Remove all not used items */
612
for (KEY_FIELD *old=start ; old != first_free ;)
614
if (old->level != and_level)
615
{ // Not used in all levels
616
if (old == --first_free)
618
*old= *first_free; // Remove old value
627
Add a possible key to array of possible keys if it's usable as a key
629
@param key_fields Pointer to add key, if usable
630
@param and_level And level, to be stored in KEY_FIELD
631
@param cond Condition predicate
632
@param field Field used in comparision
633
@param eq_func True if we used =, <=> or IS NULL
634
@param value Value used for comparison with field
635
@param usable_tables Tables which can be used for key optimization
636
@param sargables IN/OUT Array of found sargable candidates
639
If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
640
table, we store this to be able to do not exists optimization later.
643
*key_fields is incremented if we stored a key in the array
645
static void add_key_field(KEY_FIELD **key_fields,
652
table_map usable_tables,
653
SARGABLE_PARAM **sargables)
655
uint32_t exists_optimize= 0;
656
if (!(field->flags & PART_KEY_FLAG))
658
// Don't remove column IS NULL on a LEFT JOIN table
659
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
660
!field->table->maybe_null || field->null_ptr)
661
return; // Not a key. Skip it
662
exists_optimize= KEY_OPTIMIZE_EXISTS;
663
assert(num_values == 1);
667
table_map used_tables=0;
669
for (uint32_t i=0; i<num_values; i++)
671
used_tables|=(value[i])->used_tables();
672
if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
677
if (!(usable_tables & field->table->map))
679
if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
680
!field->table->maybe_null || field->null_ptr)
681
return; // Can't use left join optimize
682
exists_optimize= KEY_OPTIMIZE_EXISTS;
686
JoinTable *stat=field->table->reginfo.join_tab;
687
key_map possible_keys= field->key_start;
688
possible_keys&= field->table->keys_in_use_for_query;
689
stat[0].keys|= possible_keys; // Add possible keys
692
Save the following cases:
694
Field LIKE constant where constant doesn't start with a wildcard
695
Field = field2 where field2 is in a different table
702
stat[0].key_dependent|= used_tables;
705
for (uint32_t i=0; i<num_values; i++)
707
if (!(is_const&= value[i]->const_item()))
711
stat[0].const_keys|= possible_keys;
715
Save info to be able check whether this predicate can be
716
considered as sargable for range analisis after reading const tables.
717
We do not save info about equalities as update_const_equal_items
718
will take care of updating info on keys from sargable equalities.
721
(*sargables)->field= field;
722
(*sargables)->arg_value= value;
723
(*sargables)->num_values= num_values;
726
We can't always use indexes when comparing a string index to a
727
number. cmp_type() is checked to allow compare of dates to numbers.
728
eq_func is NEVER true when num_values > 1
733
Additional optimization: if we're processing
734
"t.key BETWEEN c1 AND c1" then proceed as if we were processing
736
TODO: This is a very limited fix. A more generic fix is possible.
738
A) Make equality propagation code be able to handle BETWEEN
739
(including cases like t1.key BETWEEN t2.key AND t3.key)
740
B) Make range optimizer to infer additional "t.key = c" equalities
741
and use them in equality propagation process (see details in
744
if ((cond->functype() != Item_func::BETWEEN) ||
745
((Item_func_between*) cond)->negated ||
746
!value[0]->eq(value[1], field->binary()))
751
if (field->result_type() == STRING_RESULT)
753
if ((*value)->result_type() != STRING_RESULT)
755
if (field->cmp_type() != (*value)->result_type())
761
We can't use indexes if the effective collation
762
of the operation differ from the field collation.
764
if (field->cmp_type() == STRING_RESULT &&
765
((Field_str*)field)->charset() != cond->compare_collation())
772
For the moment eq_func is always true. This slot is reserved for future
773
extensions where we want to remembers other things than just eq comparisons
776
/* Store possible eq field */
777
(*key_fields)->field= field;
778
(*key_fields)->eq_func= eq_func;
779
(*key_fields)->val= *value;
780
(*key_fields)->level= and_level;
781
(*key_fields)->optimize= exists_optimize;
783
If the condition has form "tbl.keypart = othertbl.field" and
784
othertbl.field can be NULL, there will be no matches if othertbl.field
786
We use null_rejecting in add_not_null_conds() to add
787
'othertbl.field IS NOT NULL' to tab->select_cond.
789
(*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
790
cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
791
((*value)->type() == Item::FIELD_ITEM) &&
792
((Item_field*)*value)->field->maybe_null());
793
(*key_fields)->cond_guard= NULL;
798
Add possible keys to array of possible keys originated from a simple
801
@param key_fields Pointer to add key, if usable
802
@param and_level And level, to be stored in KEY_FIELD
803
@param cond Condition predicate
804
@param field Field used in comparision
805
@param eq_func True if we used =, <=> or IS NULL
806
@param value Value used for comparison with field
807
Is NULL for BETWEEN and IN
808
@param usable_tables Tables which can be used for key optimization
809
@param sargables IN/OUT Array of found sargable candidates
812
If field items f1 and f2 belong to the same multiple equality and
813
a key is added for f1, the the same key is added for f2.
816
*key_fields is incremented if we stored a key in the array
818
static void add_key_equal_fields(KEY_FIELD **key_fields,
821
Item_field *field_item,
825
table_map usable_tables,
826
SARGABLE_PARAM **sargables)
828
Field *field= field_item->field;
829
add_key_field(key_fields, and_level, cond, field,
830
eq_func, val, num_values, usable_tables, sargables);
831
Item_equal *item_equal= field_item->item_equal;
835
Add to the set of possible key values every substitution of
836
the field for an equal field included into item_equal
838
Item_equal_iterator it(*item_equal);
842
if (!field->eq(item->field))
844
add_key_field(key_fields, and_level, cond, item->field,
845
eq_func, val, num_values, usable_tables,
852
static void add_key_fields(JOIN *join,
853
KEY_FIELD **key_fields,
856
table_map usable_tables,
857
SARGABLE_PARAM **sargables)
859
if (cond->type() == Item_func::COND_ITEM)
861
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
862
KEY_FIELD *org_key_fields= *key_fields;
864
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
868
add_key_fields(join, key_fields, and_level, item, usable_tables,
870
for (; org_key_fields != *key_fields ; org_key_fields++)
871
org_key_fields->level= *and_level;
876
add_key_fields(join, key_fields, and_level, li++, usable_tables,
881
KEY_FIELD *start_key_fields= *key_fields;
883
add_key_fields(join, key_fields, and_level, item, usable_tables,
885
*key_fields=merge_key_fields(org_key_fields,start_key_fields,
886
*key_fields,++(*and_level));
893
Subquery optimization: Conditions that are pushed down into subqueries
894
are wrapped into Item_func_trig_cond. We process the wrapped condition
895
but need to set cond_guard for KeyUse elements generated from it.
898
if (cond->type() == Item::FUNC_ITEM &&
899
((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
901
Item *cond_arg= ((Item_func*)cond)->arguments()[0];
902
if (!join->group_list && !join->order &&
904
join->unit->item->substype() == Item_subselect::IN_SUBS &&
905
!join->unit->is_union())
907
KEY_FIELD *save= *key_fields;
908
add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
910
// Indicate that this ref access candidate is for subquery lookup:
911
for (; save != *key_fields; save++)
912
save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
918
/* If item is of type 'field op field/constant' add it to key_fields */
919
if (cond->type() != Item::FUNC_ITEM)
921
Item_func *cond_func= (Item_func*) cond;
922
switch (cond_func->select_optimize()) {
923
case Item_func::OPTIMIZE_NONE:
925
case Item_func::OPTIMIZE_KEY:
929
if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
930
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
932
values= cond_func->arguments()+1;
933
if (cond_func->functype() == Item_func::NE_FUNC &&
934
cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
935
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
937
assert(cond_func->functype() != Item_func::IN_FUNC ||
938
cond_func->argument_count() != 2);
939
add_key_equal_fields(key_fields, *and_level, cond_func,
940
(Item_field*) (cond_func->key_item()->real_item()),
942
cond_func->argument_count()-1,
943
usable_tables, sargables);
945
if (cond_func->functype() == Item_func::BETWEEN)
947
values= cond_func->arguments();
948
for (uint32_t i= 1 ; i < cond_func->argument_count() ; i++)
950
Item_field *field_item;
951
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
953
!(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
955
field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
956
add_key_equal_fields(key_fields, *and_level, cond_func,
957
field_item, 0, values, 1, usable_tables,
964
case Item_func::OPTIMIZE_OP:
966
bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
967
cond_func->functype() == Item_func::EQUAL_FUNC);
969
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
970
!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
972
add_key_equal_fields(key_fields, *and_level, cond_func,
973
(Item_field*) (cond_func->arguments()[0])->real_item(),
975
cond_func->arguments()+1, 1, usable_tables,
978
if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
979
cond_func->functype() != Item_func::LIKE_FUNC &&
980
!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
982
add_key_equal_fields(key_fields, *and_level, cond_func,
983
(Item_field*) (cond_func->arguments()[1])->real_item(), equal_func,
984
cond_func->arguments(),1,usable_tables,
989
case Item_func::OPTIMIZE_NULL:
990
/* column_name IS [NOT] NULL */
991
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
992
!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
994
Item *tmp=new Item_null;
995
if (unlikely(!tmp)) // Should never be true
997
add_key_equal_fields(key_fields, *and_level, cond_func,
998
(Item_field*) (cond_func->arguments()[0])->real_item(),
999
cond_func->functype() == Item_func::ISNULL_FUNC,
1000
&tmp, 1, usable_tables, sargables);
1003
case Item_func::OPTIMIZE_EQUAL:
1004
Item_equal *item_equal= (Item_equal *) cond;
1005
Item *const_item= item_equal->get_const();
1006
Item_equal_iterator it(*item_equal);
1011
For each field field1 from item_equal consider the equality
1012
field1=const_item as a condition allowing an index access of the table
1013
with field1 by the keys value of field1.
1015
while ((item= it++))
1017
add_key_field(key_fields, *and_level, cond_func, item->field,
1018
true, &const_item, 1, usable_tables, sargables);
1024
Consider all pairs of different fields included into item_equal.
1025
For each of them (field1, field1) consider the equality
1026
field1=field2 as a condition allowing an index access of the table
1027
with field1 by the keys value of field2.
1029
Item_equal_iterator fi(*item_equal);
1030
while ((item= fi++))
1032
Field *field= item->field;
1033
while ((item= it++))
1035
if (!field->eq(item->field))
1037
add_key_field(key_fields, *and_level, cond_func, field,
1038
true, (Item **) &item, 1, usable_tables,
1050
505
Add all keys with uses 'field' for some keypart.