Home  >  Article  >  Backend Development  >  mysql 索引问题

mysql 索引问题

WBOY
WBOYOriginal
2016-06-06 20:32:281221browse

一个表最多能同时几个索引,比如a和b字段都有索引,where a=1 and b=1,这两个会同时使用吗?还有a=1 and c=1,c是普通字段,这个时候会用到a索引嘛,还有联合索引有那些要注意的,什么情况下联合索引会用不上。由于不方便测试,感谢大神分享经验!

回复内容:

一个表最多能同时几个索引,比如a和b字段都有索引,where a=1 and b=1,这两个会同时使用吗?还有a=1 and c=1,c是普通字段,这个时候会用到a索引嘛,还有联合索引有那些要注意的,什么情况下联合索引会用不上。由于不方便测试,感谢大神分享经验!

1.一条SQL语句只能用一个索引,所以where a=1 and b=1 你可以考虑用组合索引
2.a=1 and c=1,c是普通字段 会走a索引
3.联合索引是从左往右使用。可用部分联合索引
4.在SQL语句前加explain来查看语句用索引的情况

数据库的索引可以有多个,针对单个字段,也可以是针对多个字段的,比如一些查询组合。分别针对a和b建德索引,在查询a=1和b=1的时候不如,针对a,b的组合索引的速度快。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn