MySQL 優化之 index_merge (索引合並)


深入理解 index merge 是使用索引進行優化的重要基礎之一。理解了 index merge 技術,我們才知道應該如何在表上建立索引。

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功能也可以。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM