关于MySQL多余索引的问题以及索引覆盖的问题
CREATE TABLE `t2` (
`x` char(20) DEFAULT NULL,
`y` char(20) DEFAULT NULL,
`z` char(20) DEFAULT NULL,
KEY `i1` (`x`) USING BTREE,
KEY `i2` (`x`,`y`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
查询计划
DESCRIBE select x, y from t2 where x=’sdf’ and y=’fdsaf’
DESCRIBE select x, y from t2 where x=’sdf’
DESCRIBE select x from t2 where x=’sdf’
使用的索引都是i1呢, 导致DESCRIBE select x,y from t2 where x=’sdf’没有使用到覆盖索引
坛子里的大牛哥哥们, 这是一个什么情况啊
select x,y from t2 where x=’sdf’这个用i1代价应该会比i2小
select x,y from t2 where x=’sdf’ and y=’xx’ 这个肯定就会是用i2了
呵呵,这个问题比较典型的,还是用实验事实说明
首先你在i1上有个单独的索引,同时也有个覆盖索引。
理论上
DESCRIBE select x, y from t2 where x=’sdf’ and y=’fdsaf’
是可以用到覆盖索引的
下面我把单独索引去掉了
-
SQL code
mysql> DESCRIBE select x, y from t2 where x='sdf' and y='fdsaf' G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t2 type: ref possible_keys: i2 key: i2 key_len: 122 ref: const,const rows: 1 Extra: Using where; Using index 1 row in set (0.00 sec)