天天看點

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

轉載于:https://www.cnblogs.com/hankyoon/p/11012620.html