rt,mysql 版本 5.7,建立表:
CREATE TABLE `just_for_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`str1` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
`str2` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
`str3` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
`str4` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `aaa` (`str1`,`str2`,`str3`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
简单插 2 条数
INSERT INTO `temp_information_extract`.`just_for_test`( `str1`, `str2`, `str3`, `str4`) VALUES ( 'a', 'b', 'c', 'd');
INSERT INTO `temp_information_extract`.`just_for_test`( `str1`, `str2`, `str3`, `str4`) VALUES ( 'a', 'c', 'b', 'd');
执行下面查询语句
explain select str1,str2,str3 from just_for_test where str2 > 'b'
结果: 1 SIMPLE just_for_test index aaa 249 2 50.00 Using where; Using index
md 表格好像用不了??不纠结了。。 我几个问题是,这个为何会使用了联合索引 aaa ?因为按照最左前缀的原则,没办法从第二个 key 开始查。 还是说查询优化器比较了使用全表扫描和这个 aaa 索引,发现 aaa 存在索引覆盖,比全表效率高,然后就硬用这个联合索引?这个联合索引还是得进行全部值遍历?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.