共計 2816 個字符,預計需要花費 8 分鐘才能閱讀完成。
這篇文章主要介紹“MySQL 優化中 index_merge 有什么作用”,在日常操作中,相信很多人在 MySQL 優化中 index_merge 有什么作用問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MySQL 優化中 index_merge 有什么作用”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
1. 為什么會有 index merge
我們的 where 中可能有多個條件 (或者 join) 涉及到多個字段,它們之間進行 AND 或者 OR,那么此時就有可能會使用到 index merge 技術。index merge 技術如果簡單的說,其實就是:對多個索引分別進行條件掃描,然后將它們各自的結果進行合并(intersect/union)。
MySQL5.0 之前,一個表一次只能使用一個索引,無法同時使用多個索引分別進行條件掃描。但是從 5.1 開始,引入了 index merge 優化技術,對同一個表可以使用多個索引分別進行條件掃描。
相關文檔:http://dev.mysql.com/doc/refman/5.6/en/index-merge-optimization.html (注意該文檔中說的有幾處錯誤)
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.
In EXPLAIN output, the Index Merge method appears as index_merge in the type column. In this case, the key column contains a list of indexes used, and key_len contains a list of the longest key parts for those indexes.
index merge: 同一個表的多個索引的范圍掃描可以對結果進行合并,合并方式分為三種:union, intersection, 以及它們的組合(先內部 intersect 然后在外面 union)。
index merge 算法根據合并算法的不同分成了三種:intersect, union, sort_union.
2. index merge 之 intersect
簡單而言,index intersect merge 就是多個索引條件掃描得到的結果進行交集運算。顯然在多個索引提交之間是 AND 運算時,才會出現 index intersect merge. 下面兩種 where 條件或者它們的組合時會進行 index intersect merge:
3. index merge 之 union
簡單而言,index uion merge 就是多個索引條件掃描,對得到的結果進行并集運算,顯然是多個條件之間進行的是 OR 運算。
下面幾種類型的 where 條件,以及他們的組合可能會使用到 index union merge 算法:
1) 條件使用到復合索引中的所有字段或者左前綴字段(對單字段索引也適用)
2) 主鍵上的任何范圍條件
3) 任何符合 index intersect merge 的 where 條件;
上面三種 where 條件進行 OR 運算時,可能會使用 index union merge 算法。
4. index merge 之 sort_union
This access algorithm is employed when the WHERE clause was converted to several range conditions combined by OR, but for which the Index Merge method union algorithm is not applicable.(多個條件掃描進行 OR 運算,但是不符合 index union merge 算法的,此時可能會使用 sort_union 算法)
5. index merge 的局限
1)If your query has a complex WHERE clause with deep AND/OR nesting and MySQL does not choose the optimal plan, try distributing terms using the following identity laws:
6. 對 index merge 的進一步優化
index merge 使得我們可以使用到多個索引同時進行掃描,然后將結果進行合并。聽起來好像是很好的功能,但是如果出現了 index intersect merge,那么一般同時也意味著我們的索引建立得不太合理,因為 index intersect merge 是可以通過建立 復合索引進行更一步優化的。
7. 復合索引的最左前綴原則
上面我們說到,對復合索引的非最左前綴字段進行 OR 運算,是無法使用到復合索引的
SQL 如下:
select cd.coupon_id, count(1) total from AAA cd
where cd.coupon_act_id = 100476 and cd.deleted=0 and cd.pick_time is not null
group by cd.coupon_id ;
在 AAA 表中,coupon_act_id 和 deleted 都是獨立的索引
select count(*) from AAA where coupon_act_id = 100476; 結果為 12360 行
select count(*) from AAA where deleted=0; 結果為 1300W 行
從上面的解釋我們可以看出來,index merge 其實就是分別通過對兩個獨立的 index 進行過濾之后,將過濾之后的結果聚合在一起,然后在返回結果集。
在我們的這個例子中,由于 deleted 字段的過濾性不好,故返回的 rows 依然很多,所以造成的很多的磁盤 read,導致了 cpu 的負載非常的高,直接就出現了延遲。
ps:其實在這個 case 中,并不需要加 2 個條件的 index,只需要將 deleted 這個 index 干掉,直接使用 coupon_act_id 這個 index 即可,畢竟這個 index 的過濾的結果集已經很小了。
或者通過關閉 index intersect 功能也可以。
到此,關于“MySQL 優化中 index_merge 有什么作用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!