日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

MySQL 优化之 index_merge (索引合并)

發(fā)布時(shí)間:2024/4/17 数据库 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL 优化之 index_merge (索引合并) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

深入理解 index merge 是使用索引進(jìn)行優(yōu)化的重要基礎(chǔ)之一。理解了 index merge 技術(shù),我們才知道應(yīng)該如何在表上建立索引。

1. 為什么會(huì)有index merge

我們的 where 中可能有多個(gè)條件(或者join)涉及到多個(gè)字段,它們之間進(jìn)行 AND 或者 OR,那么此時(shí)就有可能會(huì)使用到 index merge 技術(shù)。index merge 技術(shù)如果簡(jiǎn)單的說(shuō),其實(shí)就是:對(duì)多個(gè)索引分別進(jìn)行條件掃描,然后將它們各自的結(jié)果進(jìn)行合并(intersect/union)

MySQL5.0之前,一個(gè)表一次只能使用一個(gè)索引,無(wú)法同時(shí)使用多個(gè)索引分別進(jìn)行條件掃描。但是從5.1開(kāi)始,引入了 index merge 優(yōu)化技術(shù),對(duì)同一個(gè)表可以使用多個(gè)索引分別進(jìn)行條件掃描。

相關(guān)文檔:http://dev.mysql.com/doc/refman/5.6/en/index-merge-optimization.html (注意該文檔中說(shuō)的有幾處錯(cuò)誤)

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: 同一個(gè)表的多個(gè)索引的范圍掃描可以對(duì)結(jié)果進(jìn)行合并,合并方式分為三種:union, intersection, 以及它們的組合(先內(nèi)部intersect然后在外面union)。



index merge 算法根據(jù)合并算法的不同分成了三種:intersect, union, sort_union.?

2. index merge 之 intersect

簡(jiǎn)單而言,index intersect merge就是多個(gè)索引條件掃描得到的結(jié)果進(jìn)行交集運(yùn)算。顯然在多個(gè)索引提交之間是 AND 運(yùn)算時(shí),才會(huì)出現(xiàn) index intersect merge. 下面兩種where條件或者它們的組合時(shí)會(huì)進(jìn)行 index intersect merge:



3. index merge 之 union

簡(jiǎn)單而言,index uion merge就是多個(gè)索引條件掃描,對(duì)得到的結(jié)果進(jìn)行并集運(yùn)算,顯然是多個(gè)條件之間進(jìn)行的是 OR 運(yùn)算。

下面幾種類型的 where 條件,以及他們的組合可能會(huì)使用到 index union merge算法:

1)?條件使用到復(fù)合索引中的所有字段或者左前綴字段(對(duì)單字段索引也適用)

2)?主鍵上的任何范圍條件

3)?任何符合 index intersect merge 的where條件;

上面三種 where 條件進(jìn)行 OR 運(yùn)算時(shí),可能會(huì)使用 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.(多個(gè)條件掃描進(jìn)行 OR 運(yùn)算,但是不符合 index union merge算法的,此時(shí)可能會(huì)使用 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. 對(duì) index merge 的進(jìn)一步優(yōu)化

index merge使得我們可以使用到多個(gè)索引同時(shí)進(jìn)行掃描,然后將結(jié)果進(jìn)行合并。聽(tīng)起來(lái)好像是很好的功能,但是如果出現(xiàn)了 index intersect merge,那么一般同時(shí)也意味著我們的索引建立得不太合理,因?yàn)?index intersect merge 是可以通過(guò)建立 復(fù)合索引進(jìn)行更一步優(yōu)化的。



7. 復(fù)合索引的最左前綴原則

上面我們說(shuō)到,對(duì)復(fù)合索引的非最左前綴字段進(jìn)行 OR 運(yùn)算,是無(wú)法使用到復(fù)合索引的


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 都是獨(dú)立的索引

select count(*) from?AAA ?where?coupon_act_id = 100476; ? 結(jié)果為12360行

select count(*) from AAA where?deleted=0; ?結(jié)果為1300W行

從上面的解釋我們可以看出來(lái),index merge其實(shí)就是分別通過(guò)對(duì)兩個(gè)獨(dú)立的index進(jìn)行過(guò)濾之后,將過(guò)濾之后的結(jié)果聚合在一起,然后在返回結(jié)果集。


在我們的這個(gè)例子中,由于deleted字段的過(guò)濾性不好,故返回的rows依然很多,所以造成的很多的磁盤read,導(dǎo)致了cpu的負(fù)載非常的高,直接就出現(xiàn)了延遲。

ps:其實(shí)在這個(gè)case中,并不需要加2個(gè)條件的index,只需要將deleted這個(gè)index干掉,直接使用coupon_act_id這個(gè)index即可,畢竟這個(gè)index的過(guò)濾的結(jié)果集已經(jīng)很小了。

或者通過(guò)關(guān)閉index intersect功能也可以。

轉(zhuǎn)載于:https://www.cnblogs.com/hankyoon/p/11012620.html

總結(jié)

以上是生活随笔為你收集整理的MySQL 优化之 index_merge (索引合并)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。