mysql中的联合索引

联合索引又叫复合索引。对于复合索引:Mysql从左到右的使用索引中的字段,
一个查询可以只使用索引中的一部份,但只能是最左侧部分。
例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c 3种组合进行查找,
但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效。

如:建立 姓名、年龄、性别的复合索引。
select * from user where name=’zhangsan’ and age = ‘1’; 使用到索引 
select * from user where age=’1’ and sex=’男’;不使用索引
联合索引和单个索引的区别:
如果我们创建了(area, age,salary)的复合索引,那么其实相当于创建了:
(area,age,salary),(area,age)、(area)三个索引,这被称为最佳左前缀
特性。因此我们在创建复合索引时应该将最常用作限制条件的列放在最左边,依次递减。
例:
select * from test where area='11'
select * from test where area='11' and age=1
select * from test where area='11' and age=1 and salary=2.0
以上有索引
select * from test where age=11
select * from test where age=1 and salary=2.0
以上无索引
-----------------------------------
如果在查询中需要匹配多个字段的条件,可以把这几个字段做个联合索引,效率要比在每个字段上加索引高多了

猜你喜欢

转载自blog.csdn.net/z_alvin/article/details/82784666
今日推荐