共計 3703 個字符,預計需要花費 10 分鐘才能閱讀完成。
本篇內容介紹了“mysql 索引合并的說明和使用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
什么是索引合并
下面我們看下 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 列會顯示出所有使用的索引。如下:
index_merge_sql
使用索引合并的示例
數據表結構
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 select * from test;
+—-+————+————+————+————+
| id | key1_part1 | key1_part2 | key2_part1 | key2_part2 |
+—-+————+————+————+————+
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 2 | 1 |
| 3 | 1 | 1 | 2 | 2 |
| 4 | 1 | 1 | 3 | 2 |
| 5 | 1 | 1 | 3 | 3 |
| 6 | 1 | 1 | 4 | 3 |
| 7 | 1 | 1 | 4 | 4 |
| 8 | 1 | 1 | 5 | 4 |
| 9 | 1 | 1 | 5 | 5 |
| 10 | 2 | 1 | 1 | 1 |
| 11 | 2 | 2 | 1 | 1 |
| 12 | 3 | 2 | 1 | 1 |
| 13 | 3 | 3 | 1 | 1 |
| 14 | 4 | 3 | 1 | 1 |
| 15 | 4 | 4 | 1 | 1 |
| 16 | 5 | 4 | 1 | 1 |
| 17 | 5 | 5 | 1 | 1 |
| 18 | 5 | 5 | 3 | 3 |
| 19 | 5 | 5 | 3 | 1 |
| 20 | 5 | 5 | 3 | 2 |
| 21 | 5 | 5 | 3 | 4 |
| 22 | 6 | 6 | 3 | 3 |
| 23 | 6 | 6 | 3 | 4 |
| 24 | 6 | 6 | 3 | 5 |
| 25 | 6 | 6 | 3 | 6 |
| 26 | 6 | 6 | 3 | 7 |
| 27 | 1 | 1 | 3 | 6 |
| 28 | 1 | 2 | 3 | 6 |
| 29 | 1 | 3 | 3 | 6 |
+—-+————+————+————+————+
29 rows 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 進行索引合并,取交集后,就發現只有一條記錄適合。應該查詢效率會更快。
“mysql 索引合并的說明和使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!