溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

mysql索引合并的說(shuō)明和使用

發(fā)布時(shí)間:2021-09-13 15:57:36 來(lái)源:億速云 閱讀:185 作者:chen 欄目:MySQL數(shù)據(jù)庫(kù)

本篇內(nèi)容介紹了“mysql索引合并的說(shuō)明和使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!


什么是索引合并
   下面我們看下mysql文檔中對(duì)索引合并的說(shuō)明:
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.
   根據(jù)官方文檔中的說(shuō)明,我們可以了解到:

   1、索引合并是把幾個(gè)索引的范圍掃描合并成一個(gè)索引。

   2、索引合并的時(shí)候,會(huì)對(duì)索引進(jìn)行并集,交集或者先交集再并集操作,以便合并成一個(gè)索引。

   3、這些需要合并的索引只能是一個(gè)表的。不能對(duì)多表進(jìn)行索引合并。

怎么確定使用了索引合并
   在使用explain對(duì)sql語(yǔ)句進(jìn)行操作時(shí),如果使用了索引合并,那么在輸出內(nèi)容的type列會(huì)顯示 index_merge,key列會(huì)顯示出所有使用的索引。如下:

   index_merge_sql

使用索引合并的示例
數(shù)據(jù)表結(jié)構(gòu)
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)
數(shù)據(jù)
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)
   從上面的兩個(gè)案例大家可以發(fā)現(xiàn),相同模式的sql語(yǔ)句,可能有時(shí)能使用索引,有時(shí)不能使用索引。是否能使用索引,取決于mysql查詢優(yōu)化器對(duì)統(tǒng)計(jì)數(shù)據(jù)分析后,是否認(rèn)為使用索引更快。


   因此,單純的討論一條sql是否可以使用索引有點(diǎn)片面,還需要考慮數(shù)據(jù)。


注意事項(xiàng)
   mysql5.6.7之前的版本遵守range優(yōu)先的原則。也就是說(shuō),當(dāng)一個(gè)索引的一個(gè)連續(xù)段,包含所有符合查詢要求的數(shù)據(jù)時(shí),哪怕索引合并能提供效率,也不再使用索引合并。舉個(gè)例子:


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)
   上面符合查詢要求的結(jié)果只有一條,而這一條記錄被索引key2所包含。


   可以看到這條sql語(yǔ)句使用了key2索引。但是這個(gè)并不是最快的執(zhí)行方式。其實(shí),把索引key1和索引key2進(jìn)行索引合并,取交集后,就發(fā)現(xiàn)只有一條記錄適合。應(yīng)該查詢效率會(huì)更快。

“mysql索引合并的說(shuō)明和使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI