一、普通索引
这是最基本的索引,它没有任何限制,也是我们大多数情况下用到的索引。
1、创建普通索引和删除索引的语法
#创建普通索引语法:create index index_name on table_name(列名);
#删除索引语法:drop index title_index on table_name;
注:同表的索引不能重名 不同表的可以重名
2、验证使用普通索引效果
(1)创建索引前执行一个select语句,条件是非主键字段。
select * from test where proposalno='TDAA201321020000037968'; -- 执行时间14.857s
explain select * from test where proposalno='TDAA201321020000037968';
(2)给条件字段创建普通索引并查看
create index title_index on test(proposalno);
show index from test;
(3)再次执行之前的select和explain语句
select * from test where proposalno='TDAA201321020000037968'; -- 查询时间0.026s
explain select * from test where proposalno='TDAA201321020000037968';
可以看到type=ref,查询性能得到了提升,key=title_index说明用到了索引。
二、唯一索引
1、创建并查看唯一索引
create unique index proposalno_index on test(proposalno);
show index from test;
2、 再次执行之前的select和explain语句
select * from test where proposalno='TDAA201321020000037968'; -- 0.024s
explain select * from test where proposalno='TDAA201321020000037968';
可以看到type=const,比之前的普通索引的执行计划type=ref,性能又有了提升。key=proposalno_index,说明也用到了索引。