在有多个索引的域上,mysql从索引的最左边开始,按顺序使用,比如下边的例子
select userid, username, password where userid = 1 and username = 'aa';
其中userid,username为复合索引,如果要让索引起作用必须依据索引的顺序使用,索引(userid,username),查询的时候可以 where userid = 1,或者where userid = 1 and username = 'aa',如果打乱顺序使用索引,比如 where username = 'aa' 或者 where username = 'aa' and userid = 1,则索引将不再其作用
select userid, username, password where userid = 1 and username = 'aa';
其中userid,username为复合索引,如果要让索引起作用必须依据索引的顺序使用,索引(userid,username),查询的时候可以 where userid = 1,或者where userid = 1 and username = 'aa',如果打乱顺序使用索引,比如 where username = 'aa' 或者 where username = 'aa' and userid = 1,则索引将不再其作用