您好,欢迎访问一九零五行业门户网

mysql多列索引详解

创建多列索引 在t_user表id,username,email字段上创建多列索引(该表只有此索引): alter table t_user add index user_index(id, username, email); 能够利用该索引的查询 符合leftmost index prefixes原则的查询 select * from t_user where id = 40;se
创建多列索引
在t_user表id,username,email字段上创建多列索引(该表只有此索引):
alter table t_user add index user_index(id, username, email);
能够利用该索引的查询
符合leftmost index prefixes原则的查询
select * from t_user where id = 40;select * from t_user where id between 10 and 50;select * from t_user where id in (30, 31, 32);select * from t_user where id = 40 and username = '侯西阳';select * from t_user where id = 40 and username = '侯西阳' and email = 'xiyang.hou@gmail.com';select * from t_user where id > 40 and username > 't';
不能利用以上索引的查询
不符合leftmost index prefixes原则的查询
select * from t_user where username = '侯西阳';select * from t_user where username = '侯西阳' and email = 'xiyang.hou@gmail.om';
or查询
select * from t_user where id = 40 or username = '侯西阳';
不能使用索引的解决方案
在where语句后面的查询字段建立单个索引及多列索引,注意leftmost index prefixes原则,避免建立重复索引
or查询使用union来连接查询结果,并在对应的字段上建立索引
原文地址:mysql多列索引详解, 感谢原作者分享。
其它类似信息

推荐信息