创建表,以及联合索引 b_c_d
CREATE TABLE tab_test
(
id
int(11) NOT NULL,
a
int(11) NOT NULL,
b
int(11) NOT NULL,
c
varchar(255) NOT NULL,
d
int(11) NOT NULL,
PRIMARY KEY (id
),
KEY idx_b_c_d
(b
,c
,d
) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
能命中的索引情况。
//【左匹配】,b_c_d 索引
1 explain select * from tab_test where b = 1;
2 explain select * from tab_test where b = 1 and c= ‘e’; //交换 b,c 的位置,也能命中索引,应
为查询优化器会交换他们的位置。
3 explain select * from tab_test where c= ‘e’ and b = 1 and d= 1;
//【左匹配+其它字段】也能命中 b_c_d 索引。
1 explain select * from tab_test where b = 1 and a=6;
2 explain select * from tab_test where b = 1 and c= ‘e’ and a=6;
3 explain select * from tab_test w