您的位置:首页 > 数据库 > MySQL

mysql索引之组合索引

2015-09-15 15:00 609 查看
  多列索引又称组合索引,在mysql的查询操作中,我们经常会遇到多个搜索条件,如:$sql = "select * from article where content='ma4' and title = 'ma4'";

此时虽然在title和content都建立了单列索引,但查询时仅应用到一个索引,不能满足最优化索引的需求,此时我们可以建立组合索引,如:

create index title_content on article(title(10),content(10));

当再次查询该sql时,mysql会选择最优的索引进行查询,但mysql执行多列索引的时候会执行最左前缀原则;

  $sql = "select * from article where content='ma4' and title = 'ma4' and writer='ma4'";

  create index content_title_writer on article(content(100),title(100),writer(100));

此时建立了title_content_writer的组合索引,由于最左前缀原则,有以下几种情况可以使用该索引:

1:$sql = "select * from article where content='ma4' and title = 'ma4' and writer='ma4'";

2:$sql = "select * from article where content='ma4' and title = 'ma4' ";

3,$sql = "select * from article where content='ma4' and writer='ma4'";

即最左前缀要是content,注:索引不是越多越好,索引是以文件形式存储的,如果是大表数据,仅索引就会占用太多的磁盘空间,索引多的时候,数据更新变慢,更新数据的时候,也要更新对应的索引
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: