diff --git a/mysql-test/r/key.result b/mysql-test/r/key.result index b8387389075..28824de94ce 100644 --- a/mysql-test/r/key.result +++ b/mysql-test/r/key.result @@ -222,3 +222,16 @@ explain select 1 from t1 where id =2; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ref id id 4 const 1 Using where; Using index drop table t1; +CREATE TABLE t1 (numeropost mediumint(8) unsigned NOT NULL default '0', numreponse int(10) unsigned NOT NULL auto_increment, PRIMARY KEY (numeropost,numreponse), UNIQUE KEY numreponse (numreponse)); +INSERT INTO t1 (numeropost,numreponse) VALUES ('1','1'),('1','2'),('2','3'),('2','4'); +SELECT numeropost FROM t1 WHERE numreponse='1'; +numeropost +1 +EXPLAIN SELECT numeropost FROM t1 WHERE numreponse='1'; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 const numreponse numreponse 4 const 1 Using index +FLUSH TABLES; +SELECT numeropost FROM t1 WHERE numreponse='1'; +numeropost +1 +drop table t1; diff --git a/mysql-test/t/key.test b/mysql-test/t/key.test index 4fa85477318..8d399abfec9 100644 --- a/mysql-test/t/key.test +++ b/mysql-test/t/key.test @@ -216,3 +216,15 @@ explain select name from t1 where id =2; ALTER TABLE t1 DROP PRIMARY KEY, ADD INDEX (id); explain select 1 from t1 where id =2; drop table t1; + +# +# Test of problem with key read (Bug #3666) +# + +CREATE TABLE t1 (numeropost mediumint(8) unsigned NOT NULL default '0', numreponse int(10) unsigned NOT NULL auto_increment, PRIMARY KEY (numeropost,numreponse), UNIQUE KEY numreponse (numreponse)); +INSERT INTO t1 (numeropost,numreponse) VALUES ('1','1'),('1','2'),('2','3'),('2','4'); +SELECT numeropost FROM t1 WHERE numreponse='1'; +EXPLAIN SELECT numeropost FROM t1 WHERE numreponse='1'; +FLUSH TABLES; +SELECT numeropost FROM t1 WHERE numreponse='1'; +drop table t1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 2cfeb171153..abe239e7d4d 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -3638,7 +3638,7 @@ make_join_readinfo(JOIN *join, uint options) table->status=STATUS_NO_RECORD; tab->read_first_record= join_read_const; tab->read_record.read_record= join_no_more_records; - if (table->used_keys.is_set(tab->index) && + if (table->used_keys.is_set(tab->ref.key) && !table->no_keyread) { table->key_read=1; @@ -5967,7 +5967,7 @@ join_read_const_table(JOIN_TAB *tab, POSITION *pos) } else { - if (!table->key_read && table->used_keys.is_set(tab->index) && + if (!table->key_read && table->used_keys.is_set(tab->ref.key) && !table->no_keyread) { table->key_read=1;