国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:31:23
字體:
來源:轉載
供稿:網友
       關于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。

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广宁县| 西充县| 江达县| 长子县| 鄱阳县| 高密市| 永兴县| 多伦县| 黄石市| 邛崃市| 东城区| 什邡市| 武定县| 邢台县| 苗栗县| 平乐县| 建宁县| 静安区| 钟祥市| 黔东| 上栗县| 乌海市| 香格里拉县| 德保县| 长沙市| 大庆市| 象州县| 香港 | 邹平县| 桂阳县| 黑河市| 奉节县| 油尖旺区| 岳池县| 梁山县| 江源县| 岗巴县| 贵阳市| 天长市| 郁南县| 寻甸|