Skip to content

索引合并

https://dev.mysql.com/doc/refman/5.7/en/index-merge-optimization.html

什么是索引合并?

MySQL5.1版本开始,引入了一个新的特性:索引合并优化(Index merge optimization),简称"索引合并"。

那么什么是索引合并呢?

在没有引入索引合并优化之前,对于单表来说,一次查询只能使用一个索引;而在引入索引合并优化后,MySQL就支持在单表查询中,对于一些查询,可以同时使用多个索引进行查询优化。

简单来说,在查询(尤其是范围查询)中,可以同时使用多个索引进行数据筛选,然后对多个索引的结果取交集或者并集,抑或是先交集再并集,然后将最终结果返回。

注意:

  • 索引合并只能应用于单表,而不能对多表进行索引合并。
  • 索引合并不适用于全文索引。

索引合并主要使用三种合并算法,以支持不同的应用场景:

  • Index Merge Intersection Access Algorithm,适用and场景。
  • Index Merge Union Access Algorithm,适用where条件是等值判断,且多个条件是or关系。
  • Index Merge Sort-Union Access Algorithm,适用where条件是范围查询,且多个条件是or关系。
sql
create table merge_tb(
	id int not null primary key auto_increment,
	a int, 
	b int,
	c int,
	d int,
	x int,
	y int,
	z int,
	key com_idx(a, b, c),
	key idx_1(x),
	key idx_2(y)
) engine=innodb charset=utf8;

insert into merge_tb(a,b,c,d,x,y,z)values
(1, 1, 1, 1, 1, 1, 1),(2, 2, 2, 2, 2, 2, 2),
(3, 3, 3, 3, 3, 3, 3),(4, 4, 4, 4, 4, 4, 4),
(5, 5, 5, 5, 5, 5, 5),(6, 6, 6, 6, 6, 6, 6),
(7, 7, 7, 7, 7, 7, 7),(8, 8, 8, 8, 8, 8, 8),
(9, 9, 9, 9, 9, 9, 9),(10, 10, 10, 10, 10, 10, 10);

Index Merge Intersection Access Algorithm

索引合并 VS 联合索引


that's all,see also: