创建索引的语法
create index 索引名 on 表名( 字段名) ;
未使用索引优化前查询
mysql> explain select last_name from employees where last_name like 'm%' ;
+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+
| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 107 | Using where |
+
添加索引后查询
mysql> explain select last_name from employees where last_name like 'm%' ;
+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+
| 1 | SIMPLE | employees | range | find_name | find_name | 53 | NULL | 10 | Using where ; Using index |
+
很明显,添加索引后我们的查询效率更高了,这里我们主要看rows的不同,所以我们可以得出,当数据量很庞大的时候,索引带来的好处是可想而知的。
##### id:指出在整个查询中select的位置
##### table:存放所查询的表名
##### type:连接类型,该列中存储很多值,范围从const到all
##### possible_keys:指出为了提高查询速度,在Mysql中可以使用的索引
##### key:指出实际使用的键
##### rows:返回查询结果所检验的行数
##### extra:包含一些其他信息,设计Mysql如何处理查询语句