InnoDB 是数据和索引存储在一块的
辅助索引树/二级索引树
key是辅助索引的值,data是所在记录行的主键值。要搜索的如果在辅助树上没有,将回表。
假如现在要执行这句话 select * from student where age=20 ORDER BY name;
。可以采用多列索引,需要注意的是索引顺序要和筛选顺序对得上。
mysql> CREATE INDEX name_age_idx on student(age,name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from student where age=20 ORDER BY name;
+----+-------------+---------+------------+------+---------------+--------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+--------------+---------+-------+------+----------+-----------------------+
| 1 | SIMPLE | student | NULL | ref | name_age_idx | name_age_idx | 1 | const | 2 | 100.00 | Using index condition |
+----+-------------+---------+------------+------+---------------+--------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)