91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql聯合索引的選擇性解析

發布時間:2021-09-15 21:06:02 來源:億速云 閱讀:131 作者:chen 欄目:MySQL數據庫

本篇內容介紹了“mysql聯合索引的選擇性解析”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

通過下面的實驗來探討下聯合索引的使用選擇性:

版本:percona 5.6.27

create table test(

a int,

b int,

c int,

name varchar(32),

PRIMARY key(a),

key index_a_b_c(a,b,c)) ENGINE=INNODB

insert into test values(1,1,1,3,'leo');

insert into test values(2,1,2,1,'mike' );

insert into test values(3,1,3,1,'exo' );

insert into test values(4,1,2,3,'jhon' );

insert into test values(5,1,1,3,'lucy' );

insert into test values(6,2,2,3,'leo' );

insert into test values(7,3,1,2,'dv' );

insert into test values(8,2,1,3,'men' );

一:where條件對聯合索引的選擇性

mysql> explain select * from test where a=2;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | NULL  |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

mysql> explain select * from test where a=2 and b=1;

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+

| id | select_type | table | type | possible_keys | key         | key_len | ref         | rows | Extra |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 10      | const,const |    1 | NULL  |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+

mysql> explain select * from test where a=2 and b=2 and c=3;

+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

| id | select_type | table | type | possible_keys | key         | key_len | ref               | rows | Extra |

+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 15      | const,const,const |    1 | NULL  |

+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

這三個是正常的使用方法,都走了索引

mysql> explain select * from test where a=2 and c=3;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra                 |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using index condition |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+

1 row in set (0.00 sec)

如果把b漏掉,同樣走了索引

mysql> explain select * from test where b=2 and c=3;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain select * from test where c=3;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

如果把a漏掉,則不會走索引

結論:必須有聯合索引的第一個字段作為wehre條件

二:聯合索引排序選擇性

聯合索引的排序會按照(a,b,c)的順序進行排序

測試數據在聯合索引的排序會是(1,1,3), (1,2,1), (1,2,3), (1,3,1), (2,1,3), (2,2,3), (3,1,2)順序存儲

mysql> explain select * from test where a=2 order by b;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra       |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using where |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

mysql> explain select * from test where a=2 order by c;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra                       |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using where; Using filesort |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

可以看出第二個Using filesort使用了臨時表排序了,效率低。從聯合索引的排序就可以知道當指定a的值的時候,這些值會按b的值排序,不是按c的值排序,故order by b不用再filesort排序,反之order by b需要重新排序。

所以select * from test where a=2 and b=2 order by c;不會 filesort排序

mysql> explain select * from test where a=2 and b=2 order by c;

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref         | rows | Extra       |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 10      | const,const |    1 | Using where |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+

結論:當針對聯合索引中的某個字段進行排序的時候,最優的方法是有聯合索引排序字段之前的字段過濾條件

“mysql聯合索引的選擇性解析”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

平定县| 交口县| 新闻| 云安县| 孟州市| 射洪县| 苏州市| 凌海市| 兰坪| 台江县| 杭州市| 丁青县| 确山县| 城市| 资阳市| 孝昌县| 天柱县| 东乡| 敦化市| 垣曲县| 鹤峰县| 吴川市| 加查县| 伊宁县| 鸡泽县| 临猗县| 苏尼特右旗| 会理县| 桃江县| 青阳县| 江陵县| 忻州市| 罗甸县| 南漳县| 时尚| 临沧市| 汉川市| 平顺县| 沽源县| 常德市| 宣武区|