中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

Mysql索引的使用-組合索引+跳躍條件

發布時間:2020-08-11 06:31:00 來源:ITPUB博客 閱讀:373 作者:Steven1981 欄目:MySQL數據庫

關于MYSQL組合索引的使用,官方對下面的例子的說法是可以使用索引:

KEY(key_part1,key_part2,key_part3)
select .... from table where key_part1='xxx' and key_part3='yyy';

從MYSQL的執行計劃看,確實也是使用索引;

但在實際的優化過程中,我們只是簡單的關注是否使用了這個索引是不夠的。

[@more@]

我們需要關注的是:
對key_part3這個關鍵字過濾的時候,是否用到了索引?

下面我們來創建一個例子:
CREATE TABLE `im_message_201001_12` (
`msg_id` bigint(20) NOT NULL default '0',
`time` datetime NOT NULL,
`owner` varchar(64) collate latin1_bin NOT NULL,
`other` varchar(64) collate latin1_bin NOT NULL,
`content` varchar(8000) collate latin1_bin default NULL,
PRIMARY KEY (`msg_id`),
KEY `im_msg_own_oth_tim_ind` (`owner`,`other`,`time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;

查詢語句:
select count(distinct concat('ab',content)) dis ,count(*) all from im_message_201001_12
where
owner='huaniaoyuchong83'
and time between '2010-01-01 00:00:00' and '2010-02-01 00:00:00' ;

我們看到,查詢的條件,對索引來說是跳躍的。
這對ORACLE來說并不是難事。SQL優化器會在索引里完成對time字段的過濾。
用HINT:/*+INDEX_SS(TABLE INDEX_NAME)*/ 可以來輔助。
但對MYSQL來說,你可能并不知道,是什么時候對time字段進行過濾的。
當然我們希望是通過索引來過濾TIME字段。這樣最后回表的次數就會少一些。

在測試過程中,我們通過觀察MYSQL的Innodb_buffer_pool_read_requests(邏輯讀)變量的變化,來推測結果。
注意以下查詢過程中,條件time的變化,以及變量Innodb_buffer_pool_read_requests的變化


#######測試環境:
OS:RHEL 4.7 X86_64
MYSQL 5.0.51a / 5.1.40

請在開始下面測試前,運行:
select count(distinct concat('c',content)),count(*) from im_message_201001_11 where owner='huaniaoyuchong83' ;
以讓所有結果都在CACHE里;


#######開始第一次測試
show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 136566076 |
+----------------------------------+-----------+
1 row in set (0.02 sec)

select count(distinct concat('c',content)),count(*) from im_message_201001_11 where owner='huaniaoyuchong83' and time between '2010-01-01 00:00:00' and '2010-02-01 00:00:00' ;

+-------------------------------------+----------+
| count(distinct concat('c',content)) | count(*) |
+-------------------------------------+----------+
| 35644 | 44397 |
+-------------------------------------+----------+
1 row in set (1.40 sec)

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 136742193 |
+----------------------------------+-----------+
1 row in set (0.02 sec)

select 136742193-136566076 ;
+---------------------+
| 136742193-136566076 |
+---------------------+
| 176117 |
+---------------------+
1 row in set (0.00 sec)


#######開始第二次測試
show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 136742194 |
+----------------------------------+-----------+
1 row in set (0.02 sec)

select count(distinct concat('c',content)),count(*) from im_message_201001_11 where owner='huaniaoyuchong83' and time between '2010-01-01 00:00:00' and '2010-01-05 00:00:00' ;

+-------------------------------------+----------+
| count(distinct concat('c',content)) | count(*) |
+-------------------------------------+----------+
| 3679 | 4097 |
+-------------------------------------+----------+
1 row in set (0.74 sec)

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 136916032 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select 136916032-136742194;
+---------------------+
| 136916032-136742194 |
+---------------------+
| 173838 |
+---------------------+
1 row in set (0.00 sec)

#######開始第三次測試


show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 136916033 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select count(distinct concat('c',content)),count(*) from im_message_201001_11 where owner='huaniaoyuchong83' and time between '2010-01-01 00:00:00' and '2010-01-01 00:00:00' ;

+-------------------------------------+----------+
| count(distinct concat('c',content)) | count(*) |
+-------------------------------------+----------+
| 0 | 0 |
+-------------------------------------+----------+
1 row in set (0.85 sec)

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137086323 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select 137086323-136916033;
+---------------------+
| 137086323-136916033 |
+---------------------+
| 170290 |
+---------------------+
1 row in set (0.00 sec)


#######開始第四次測試

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137086324 |
+----------------------------------+-----------+
1 row in set (0.02 sec)

select count(*) from im_message_201001_11 where owner='huaniaoyuchong83' and time between '2010-01-01 00:00:00' and '2010-02-01 00:00:00' ;
+----------+
| count(*) |
+----------+
| 44397 |
+----------+
1 row in set (0.05 sec)

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137092204 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select 137092204-137086324 ;
+---------------------+
| 137092204-137086324 |
+---------------------+
| 5880 |
+---------------------+
1 row in set (0.00 sec)

#######開始第五次測試

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137092205 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select count(*) from im_message_201001_11 where owner='huaniaoyuchong83' ;
+----------+
| count(*) |
+----------+
| 44397 |
+----------+
1 row in set (0.04 sec)

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137098085 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select 137098085-137092205 ;
+---------------------+
| 137098085-137092205 |
+---------------------+
| 5880 |
+---------------------+
1 row in set (0.00 sec)


#######開始第六次測試

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137098131 |
+----------------------------------+-----------+
1 row in set (0.02 sec)

select count(*) from im_message_201001_11 where owner='huaniaoyuchong83' and time between '2010-01-01 00:00:00' and '2010-01-05 00:00:00' ;
+----------+
| count(*) |
+----------+
| 4097 |
+----------+
1 row in set (0.05 sec)

show session status like 'Innodb_buffer_pool_read_requests';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| Innodb_buffer_pool_read_requests | 137104011 |
+----------------------------------+-----------+
1 row in set (0.01 sec)

select 137104011-137098131;
+---------------------+
| 137104011-137098131 |
+---------------------+
| 5880 |
+---------------------+
1 row in set (0.00 sec)

####### 分析結果

前三次查詢,從索引檢索后需要回表:
time 結果行數 邏輯讀
30天 44397 176117
5天 4097 173838
1天 0 170290

后三次查詢,從索引檢索后不需要回表
time 結果行數 邏輯讀
30天 44397 5880
無time條件 44397 5880
5天 4097 5880

從數據來看,
select count(*) 這樣的查詢,有或者沒有time條件,邏輯讀是一樣,都不用回表。
這里也說明這種情況MYSQL是用索引進行time字段的過濾。

select count(distinct concat('c',content)),count(*), 這樣的查詢,用到了索引以外的字段,是必需回表的。
但通過邏輯讀發現,不管查詢結果是多少行,邏輯讀都差不多,在17W左右。
特別是結果行為0時,如果是通過索引過濾time,那么邏輯讀應該接近5900,而不是17W。
這也說明,這種情況下,MYSQL沒有使用索引來對TIME字段進行過濾;

所以MYSQL對相同WHERE條件的查詢,還采用了不同的優化程序;但MS這個優化有點問題。


對這樣的索引,需要優化,可以。 調整索引順序(`owner`,`time`,`other`)。
但是這僅僅是對一個SQL的優化。
你還要考慮到系統里還有很多其他類似的SQL需要用到這個索引。 所以在優化時,需要評估所有的SQL。

向AI問一下細節

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

AI

五寨县| 彭阳县| 巴马| 财经| 普格县| 宁安市| 河源市| 绥芬河市| 铜川市| 乌兰察布市| 高台县| 鹤岗市| 平乐县| 北流市| 大港区| 天镇县| 客服| 平遥县| 岳普湖县| 原平市| 天水市| 泉州市| 岢岚县| 西安市| 罗山县| 马公市| 姚安县| 舞阳县| 体育| 望江县| 木里| 安徽省| 奎屯市| 全州县| 康保县| 蕲春县| 额敏县| 平邑县| 安阳市| 达孜县| 类乌齐县|