Commit 64e6d7b1 authored by unknown's avatar unknown

Fix for BUG#12672.


mysql-test/r/group_min_max.result:
  Test for BUG#12672.
mysql-test/t/group_min_max.test:
  Test for BUG#12672.
sql/opt_range.cc:
  Some storage managers like InnoDB always retrieve the value of
  the primary key (if any) with any other key. For such storage
  managers the optimizer marks an index as covering if that index
  and the primary key contain all query fields. This is good for
  reading data from an index, but not for lookup. The patch detects
  such cases and tests whether an index is really a covering index
  by itself, without the primary key. If yes, then that index can
  be used for the index skip algorithm of the GROUP_MIN_MAX
  access method.
parent 3a65c1b1
......@@ -1972,6 +1972,26 @@ a
b
c
d
create table bug12672 (
pk_col int auto_increment primary key, a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(64) default ' '
) engine=innodb;
insert into bug12672 (a1, a2, b, c, d, dummy) select * from t1;
create index idx12672_0 on bug12672 (a1);
create index idx12672_1 on bug12672 (a1,a2,b,c);
create index idx12672_2 on bug12672 (a1,a2,b);
analyze table t1;
Table Op Msg_type Msg_text
test.t1 analyze status Table is already up to date
explain select distinct a1 from bug12672 where pk_col not in (1,2,3,4);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE bug12672 range PRIMARY PRIMARY 4 NULL 93 Using where; Using temporary
select distinct a1 from bug12672 where pk_col not in (1,2,3,4);
a1
a
b
c
d
drop table bug12672;
drop table t1;
drop table t2;
drop table t3;
......
......@@ -651,6 +651,25 @@ select a1 from t1 where a2 = 'b' group by a1;
explain select distinct a1 from t1 where a2 = 'b';
select distinct a1 from t1 where a2 = 'b';
#
# Bug #12672: primary key implcitly included in every innodb index
#
create table bug12672 (
pk_col int auto_increment primary key, a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(64) default ' '
) engine=innodb;
insert into bug12672 (a1, a2, b, c, d, dummy) select * from t1;
create index idx12672_0 on bug12672 (a1);
create index idx12672_1 on bug12672 (a1,a2,b,c);
create index idx12672_2 on bug12672 (a1,a2,b);
analyze table t1;
explain select distinct a1 from bug12672 where pk_col not in (1,2,3,4);
select distinct a1 from bug12672 where pk_col not in (1,2,3,4);
drop table bug12672;
drop table t1;
drop table t2;
......
......@@ -7016,6 +7016,7 @@ get_best_group_min_max(PARAM *param, SEL_TREE *tree)
ha_rows cur_quick_prefix_records= 0;
uint cur_param_idx;
key_map cur_used_key_parts;
uint pk= param->table->s->primary_key;
for (uint cur_index= 0 ; cur_index_info != cur_index_info_end ;
cur_index_info++, cur_index++)
......@@ -7024,6 +7025,45 @@ get_best_group_min_max(PARAM *param, SEL_TREE *tree)
if (!table->used_keys.is_set(cur_index))
goto next_index;
/*
If the current storage manager is such that it appends the primary key to
each index, then the above condition is insufficient to check if the
index is covering. In such cases it may happen that some fields are
covered by the PK index, but not by the current index. Since we can't
use the concatenation of both indexes for index lookup, such an index
does not qualify as covering in our case. If this is the case, below
we check that all query fields are indeed covered by 'cur_index'.
*/
if (pk < MAX_KEY && cur_index != pk &&
(table->file->table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
{
/* For each table field */
for (uint i= 0; i < table->s->fields; i++)
{
Field *cur_field= table->field[i];
/*
If the field is used in the current query, check that the
field is covered by some keypart of the current index.
*/
if (thd->query_id == cur_field->query_id)
{
bool is_covered= FALSE;
KEY_PART_INFO *key_part= cur_index_info->key_part;
KEY_PART_INFO *key_part_end= key_part + cur_index_info->key_parts;
for (; key_part != key_part_end ; key_part++)
{
if (key_part->field == cur_field)
{
is_covered= TRUE;
break;
}
}
if (!is_covered)
goto next_index;
}
}
}
/*
Check (GA1) for GROUP BY queries.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment