mysql多列索引详解

WBOY
發布: 2016-06-07 16:38:40
原創
1333 人瀏覽過

创建多列索引 在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来连接查询结果,并在对应的字段上建立索引

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!