mysql 官方的文档乱得一匹,找了半天找到了答案。
很多人(包括 OP )觉得不走索引是因为 Mysql 文档这么写的: If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but do not use an index to perform lookups because (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3). 这里是说 not use an index to perform lookups
另一处在 Skip Scan Range Access 一节是这么写的:
CREATE TABLE t1 (f1 INT NOT NULL, f2 INT NOT NULL, PRIMARY KEY(f1, f2));
INSERT INTO t1 VALUES
(1,1), (1,2), (1,3), (1,4), (1,5),
(2,1), (2,2), (2,3), (2,4), (2,5);
INSERT INTO t1 SELECT f1, f2 + 5 FROM t1;
INSERT INTO t1 SELECT f1, f2 + 10 FROM t1;
INSERT INTO t1 SELECT f1, f2 + 20 FROM t1;
INSERT INTO t1 SELECT f1, f2 + 40 FROM t1;
ANALYZE TABLE t1;
EXPLAIN SELECT f1, f2 FROM t1 WHERE f2 > 40;
To execute this query, MySQL can choose an index scan to fetch all rows (the index includes all columns to be selected), then apply the f2 > 40 condition from the WHERE clause to produce the final result set. 这里提到会使用索引扫描。
所以意思就是会用索引扫描但是不会用索引查找(lookup). 所以索引还有其他用途吗?一会 index scan 一会 use an index to lookup. 还有 use an index to do 其他的吗? 中文里的走索引意思不只指的用索引查询(lookup).
此外:在这个表中聚集索引叶子结点存的是 id, f1, f2 。 联合索引也是存的 f1, f2, id 。这里为啥会扫联合索引而不是聚集索引。这俩索引在这个表下 size 应该是差不多的吧。