Skip to content

Commit b87c342

Browse files
author
Varun Gupta
committed
MDEV-11172: EXPLAIN shows non-sensical value for key_len with type=index
The issue happens when the secondary keys are extended with primary key parts. Inside the function TABLE_SHARE::init_from_binary_frm_image() adds the length bytes for the primary key key parts to the length of the secondary key. This is not needed because when the extended keys are used we recalculate the length for the used key parts. Also removed TABLE_SHARE::total_key_length as it is not used in the code Apporved-by: Monty <monty@mariadb.org>
1 parent 0921656 commit b87c342

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

mysql-test/r/innodb_ext_key.result

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,19 @@ EXPLAIN
790790
}
791791
drop table t1;
792792
SET optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
793+
#
794+
# MDEV-11172: EXPLAIN shows non-sensical value for key_len with type=index
795+
#
796+
CREATE TABLE t1(a INT);
797+
INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
798+
CREATE TABLE t2 (
799+
pk VARCHAR(50),
800+
a VARCHAR(20),
801+
KEY k1(a),
802+
PRIMARY KEY(pk)
803+
)ENGINE=INNODB;
804+
INSERT INTO t2 SELECT a,a FROM t1;
805+
EXPLAIN SELECT pk FROM t2 FORCE INDEX(k1);
806+
id select_type table type possible_keys key key_len ref rows Extra
807+
1 SIMPLE t2 index NULL k1 23 NULL 10 Using index
808+
DROP TABLE t1,t2;

mysql-test/t/innodb_ext_key.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,22 @@ explain format= json
606606
select * from t1 force index(k1) where f2 <= 5 and pk2 <=5 and pk1 = 'abc' and f1 <= '3';
607607
drop table t1;
608608
SET optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
609+
610+
--echo #
611+
--echo # MDEV-11172: EXPLAIN shows non-sensical value for key_len with type=index
612+
--echo #
613+
614+
CREATE TABLE t1(a INT);
615+
INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
616+
617+
CREATE TABLE t2 (
618+
pk VARCHAR(50),
619+
a VARCHAR(20),
620+
KEY k1(a),
621+
PRIMARY KEY(pk)
622+
)ENGINE=INNODB;
623+
624+
INSERT INTO t2 SELECT a,a FROM t1;
625+
EXPLAIN SELECT pk FROM t2 FORCE INDEX(k1);
626+
627+
DROP TABLE t1,t2;

sql/structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class engine_option_value;
9090
struct ha_index_option_struct;
9191

9292
typedef struct st_key {
93-
uint key_length;/* Tot length of key */
93+
uint key_length;/* total length of user defined key parts */
9494
ulong flags; /* dupp key and pack flags */
9595
uint user_defined_key_parts; /* How many key_parts */
9696
uint usable_key_parts; /* Should normally be = user_defined_key_parts */

sql/table.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,12 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
22732273
key_part->null_bit= field->null_bit;
22742274
key_part->store_length+=HA_KEY_NULL_LENGTH;
22752275
keyinfo->flags|=HA_NULL_PART_KEY;
2276+
2277+
/*
2278+
This branch is executed only for user defined key parts of the
2279+
secondary indexes.
2280+
*/
2281+
DBUG_ASSERT(i < keyinfo->user_defined_key_parts);
22762282
keyinfo->key_length+= HA_KEY_NULL_LENGTH;
22772283
}
22782284
if (field->type() == MYSQL_TYPE_BLOB ||
@@ -2285,7 +2291,8 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
22852291
else
22862292
key_part->key_part_flag|= HA_VAR_LENGTH_PART;
22872293
key_part->store_length+=HA_KEY_BLOB_LENGTH;
2288-
keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
2294+
if (i < keyinfo->user_defined_key_parts)
2295+
keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
22892296
}
22902297
if (field->type() == MYSQL_TYPE_BIT)
22912298
key_part->key_part_flag|= HA_BIT_PART;
@@ -2378,7 +2385,6 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write,
23782385

23792386
set_if_bigger(share->max_key_length,keyinfo->key_length+
23802387
keyinfo->user_defined_key_parts);
2381-
share->total_key_length+= keyinfo->key_length;
23822388
/*
23832389
MERGE tables do not have unique indexes. But every key could be
23842390
an unique index on the underlying MyISAM table. (Bug #10400)

sql/table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ struct TABLE_SHARE
773773
uint rec_buff_length; /* Size of table->record[] buffer */
774774
uint keys, key_parts;
775775
uint ext_key_parts; /* Total number of key parts in extended keys */
776-
uint max_key_length, max_unique_length, total_key_length;
776+
uint max_key_length, max_unique_length;
777777
uint uniques; /* Number of UNIQUE index */
778778
uint db_create_options;/* Create options from database */
779779
uint db_options_in_use;/* Options in use */

0 commit comments

Comments
 (0)