创建表的sql语句如下
create table if not exists users(user_id mediumint unsigned not null auto_increment,username varchar(30) not null,pass char(40) not null,first_name varchar(20) not null,last_name varchar(40) not null,email varchar(60) not null,primary key (user_id),unique (username),unique (email),index login (pass, email))engine = innodb
上面创建了一个index索引
index login (pass, email)
想请问这个索引创建了以后如何使用呢,查询的时候会有什么不同的语法出现吗 这个索引的名字是login,到时候直接用login查询而不需要pass和email了?求各位大大解答一下啊
解决方案
Mysql的索引分为单列索引与组合索引,对于组合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。
你创建的index索引应该是一个组合索引,建立的组合索引相当于建立了下面两组索引:
- pass,email
- pass
一般mysql默认会选择最优的索引来查询。
下面的查询会使用到你建立的索引:
select * from users where pass = "password" and email = "emailaddress";select * from users where pass = "password";
下面的查询不会使用到你建立的索引:
select * from users where email = "emailaddress";