MySQLLelove 2019-06-27
CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `realname` varchar(10) NOT NULL DEFAULT '', `sex` tinyint(4) NOT NULL DEFAULT '0', `age` tinyint(4) NOT NULL DEFAULT '0', `job` varchar(10) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `test_realname_sex_age_index` (`realname`,`sex`,`age`), KEY `sex_index` (`sex`), KEY `age_index` (`age`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
EXPLAIN SELECT * FROM test WHERE realname = 'wen' ORDER BY sex DESC ,age DESC;不能利用联合索引排序,因为sex和age的排序方向不一致
EXPLAIN SELECT * FROM test WHERE realname = 'wen' ORDER BY sex DESC ,age ASC ;
EXPLAIN SELECT * FROM test WHERE realname = 'wen' ORDER BY sex,job;
EXPLAIN SELECT * FROM test WHERE realname = 'wen' ORDER BY age;
EXPLAIN SELECT * FROM test WHERE realname = 'wen' ORDER BY sex;
EXPLAIN SELECT * FROM test WHERE realname > 'wen' ORDER BY sex,age;
EXPLAIN SELECT * FROM test WHERE realname BETWEEN 'wen1' AND 'wen2' ORDER BY sex,age;
EXPLAIN SELECT * FROM test WHERE realname = 'wen' AND sex IN (1,2) ORDER BY age;
EXPLAIN SELECT * FROM test WHERE realname > 'qqq' ORDER BY sex;能,满足最左原则
EXPLAIN SELECT * FROM test WHERE realname = 'qqq' ORDER BY sex;
EXPLAIN SELECT * FROM test WHERE sex = 1 ORDER BY age;使用了范围
EXPLAIN SELECT * FROM test WHERE age > 17 ORDER BY age;
EXPLAIN SELECT * FROM test ORDER BY id;不能
EXPLAIN SELECT * FROM test ORDER BY sex;