優(yōu)先使用單獨索引,單獨索引里有重復項時才用到聯(lián)合索引
能用單獨索引時虐急,MySQL會認為沒必要用到組合索引
https://bbs.csdn.net/topics/391036574?list=lz
我明白了皆警,是不是因為我測試插入的10W條數(shù)據(jù)中舟铜,隨機生成的gameCode字段值都不一樣尚猿,所以mysql認為用gameCode索引就可以了逼蒙,沒必要用到組合索引,我試著把gameCode改為有重復的吓坚,就用到組合索引了
例子
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`course` varchar(50) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`) USING BTREE,
KEY `idx_course` (`course`) USING BTREE,
KEY `name_course_score` (`name`,`course`,`score`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO student(`name`, `course`, `score`) VALUES ('甲', '數(shù)學', '1');
EXPLAIN SELECT * FROM student WHERE `name`='甲' AND course='數(shù)學';
# 索引: idx_name
INSERT INTO student(`name`, `course`, `score`) VALUES ('甲', '語文', '1');
EXPLAIN SELECT * FROM student WHERE `name`='甲' AND course='數(shù)學';
# 索引: idx_course
INSERT INTO student(`name`, `course`, `score`) VALUES ('乙', '數(shù)學', '1');
EXPLAIN SELECT * FROM student WHERE `name`='甲' AND course='數(shù)學';
# 索引: name_course_score