什么是索引合並?
下面我們看下mysql文檔中對索引合並的說明:
The Index Merge method is used to retrieve rows with several range scans and to merge their results into one. The merge can produce unions, intersections, or unions-of-intersections of its underlying scans. This access method merges index scans from a single table; it does not merge scans across multiple tables.
根據官方文檔中的說明,我們可以了解到:
1、索引合並是把幾個索引的范圍掃描合並成一個索引。
2、索引合並的時候,會對索引進行並集,交集或者先交集再並集操作,以便合並成一個索引。
3、這些需要合並的索引只能是一個表的。不能對多表進行索引合並。
怎么確定使用了索引合並?
在使用explain對sql語句進行操作時,如果使用了索引合並,那么在輸出內容的type列會顯示 index_merge,key列會顯示出所有使用的索引。如下:
使用索引合並的示例
數據表結構:
mysql> show create table test\G *************************** 1. row *************************** Table: test Create Table: CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `key1_part1` int(11) NOT NULL DEFAULT '0', `key1_part2` int(11) NOT NULL DEFAULT '0', `key2_part1` int(11) NOT NULL DEFAULT '0', `key2_part2` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `key1` (`key1_part1`,`key1_part2`), KEY `key2` (`key2_part1`,`key2_part2`) ) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
數據
使用索引合並的案例
mysql> explain select * from test where (key1_part1=4 and key1_part2=4) or key2_part1=4\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: index_merge possible_keys: key1,key2 key: key1,key2 key_len: 8,4 ref: NULL rows: 3 Extra: Using sort_union(key1,key2); Using where 1 row in set (0.00 sec)
未使用索引合並的案例
mysql> explain select * from test where (key1_part1=1 and key1_part2=1) or key2_part1=4\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: ALL possible_keys: key1,key2 key: NULL key_len: NULL ref: NULL rows: 29 Extra: Using where 1 row in set (0.00 sec)
從上面的兩個案例大家可以發現,相同模式的sql語句,可能有時能使用索引,有時不能使用索引。是否能使用索引,取決於mysql查詢優化器對統計數據分析后,是否認為使用索引更快。因此,單純的討論一條sql是否可以使用索引有點片面,還需要考慮數據。
注意事項
mysql5.6.7之前的版本遵守range優先的原則。也就是說,當一個索引的一個連續段,包含所有符合查詢要求的數據時,哪怕索引合並能提供效率,也不再使用索引合並。舉個例子:
mysql> explain select * from test where (key1_part1=1 and key1_part2=1) and key2_part1=1\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: ref possible_keys: key1,key2 key: key2 key_len: 4 ref: const rows: 9 Extra: Using where 1 row in set (0.00 sec)
上面符合查詢要求的結果只有一條,而這一條記錄被索引key2所包含。
可以看到這條sql語句使用了key2索引。但是這個並不是最快的執行方式。其實,把索引key1和索引key2進行索引合並,取交集后,就發現只有一條記錄適合。應該查詢效率會更快。
tips:這條sql語句未在mysql5.6.7之后版本執行驗證,以上為理論推導。有興趣的話,您可以到mysql5.6.7之后版本上驗證下。