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,注:索引不是越多越好,索引是以文件形式存储的,如果是大表数据,仅索引就会占用太多的磁盘空间,索引多的时候,数据更新变慢,更新数据的时候,也要更新对应的索引
此时虽然在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,注:索引不是越多越好,索引是以文件形式存储的,如果是大表数据,仅索引就会占用太多的磁盘空间,索引多的时候,数据更新变慢,更新数据的时候,也要更新对应的索引
相关文章推荐
- MySQL集群:主从数据库配置 实现查询负载
- Mysql视图
- mysql 更改引擎
- 使用MySQL正则表达式 __MySQL必知必会
- mysql账户,权限配置
- mysql自动生成备份
- 远程mysql数据库很慢
- 分表保持id自增方式
- mysql索引之唯一索引
- distinct使用方法
- 常用存储过程
- 通过读取excel数据和mysql数据库数据做对比(一)-win环境准备
- mysql常用函数
- mysql常用指令
- Mysql分库分表方案
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
- MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互
- mysql mysql_list_dbs()函数
- xampp中mysql数据库导入最大限制的简单解决
- PDO基本操作Mysql