MySQL 索引分类中单列索引的示例分析
一个已分区的表不支持全文本索引,空间索引,外键索引,分区表上的主索引和唯一索引必须包含分区表达式中用到的所有列 。
索引分类: 单列索引
如果 explain 的结果中看到type = all ,或者key = null ,那么可以判断该查询用了扫描了整张表
mysql> EXPLAIN
select * from np_order_lyz m where m.order_id = '296285' ;
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra|
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | m | ALL | NULL | NULL | NULL| NULL | 45241 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set
添加索引
alter table np_order_lyz add INDEX (order_id) ;
mysql> EXPLAIN
select * from np_order_lyz m where m.order_id = '296285' ;
现在只有一行了 ,索引呗用到了,key位order_id ,估计读取的行数比以前少很多,查询速度就加快了
mysql> EXPLAIN
select * from np_order_lyz m where m.order_id = '296285' ;
+----+-------------+-------+------+---------------+----------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref| rows | Extra |
+----+-------------+-------+------+---------------+----------+---------+-------+------+-----------------------+
| 1 | SIMPLE | m | ref | order_id | order_id | 8| const |1 | Using index condition |
+----+-------------+-------+------+---------------+----------+---------+-------+------+-----------------------+
1 row in set
注意 :索引可以重复创建,但是创建重复的索引会产生性能开销,所以我们应尽量避免创建重复索引,发现重复索引时,应删除重复索引,只保留一个。