alter table 表名 add index 索引名(字段名);
#案例
alter table classes add index my_name(name);
#给classes表中的name列添加名为my_name的索引主键会自动创建索引,外键约束也会自动创建索引
drop index 索引名 on 表名;
#案例
drop index my_name on classes;
#删除classes表中的my_name索引联合索引又叫复合索引,是MySQL的InnoDB引擎中的一个索引方式,如果一个系统频繁地使用相同的几个字段查询结果,就可以考虑建立这几个字段的联合索引来提高查询效率。
alter table 表名 add index 索引名(字段名1,字段名2);在使用联合索引时要注意有个最左前缀原则,最左前缀原则就是要考虑查询的字段的顺序,只有遵守这个原则才能最大地提高查询的效率。
比如index(name,age)支持name或者name,age组合查询,不支持age单独查询
select * from students where name = '张三'; #使用了联合索引
select * from students where name = '张三' and age = 10; #使用了联合索引
select * from students where age = 10; #没有使用联合索引原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。