mysql 多列索引學習-經典實例

索引優化 ,b-tree
假設某個表有一個聯合索引(c1,c2,c3,c4) 如下 只能使用該聯合索引的c1,c2,c3部分
A. where c1 = x and c2 = x and c4>x and c3 = x
B. where c1 = x and c2 = x and c4=x order by c3
C. where c1 = x and c4 = x group by c3,c2
D. where c1 = ? and c5 = ? order by c2,c3
E. where c1 = ? and c2 = ? and c5=? order by c2,c3

實驗:
#utf8 一個字符3個字節 , 注意:order by(索引能發揮都是要 按順序查找, desc就用不上)
create table t6(
c1 char(1) not null default '',
c2 char(1) not null default '',
c3 char(1) not null default '',
c4 char(1) not null default '',
c5 char(1) not null default '',
key(c1,c2,c3,c4)
)engine myisam charset=utf8

#插入數據
insert into t6 values ('a','b','c','d','e'),('A','b','c','d','e'),('a','B','c','d','e');


A-E都否使用索引? 爲何

--------------------------------------

A 能用到 c1,c2,c3,c4 , mysql優化器會把A語句優化(不影響語意) where c1 = x and c2 = x and c3 = x and c4>x

explain select * from t6 where c1='a' and c2='b' and c4>'a' and c3='c'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t6
         type: range
possible_keys: c1
          key: c1
      key_len: 12
          ref: NULL
         rows: 2
        Extra: Using index condition
1 row in set (0.00 sec)

key_len: 12 #表明4個索引所有用上( c1,c2,c3,c4 ) 4個索引 * 3字節

------------------------
------------------------

B 只能用到 c1,c2, c3排序
explain select * from t6 where c1='a' and c2='b' and c4='d' order by c3\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t6
         type: ref
possible_keys: c1
          key: c1
      key_len: 6
          ref: const,const
         rows: 2
        Extra: Using index condition; Using where
1 row in set (0.00 sec)
key_len: 12 #2個索引用上( c1,c2) 2個索引 * 3字節

ps:這裏c3索引用在了排序上
能夠經過下面來比較
explain select * from t6 where c1='a' and c2='b' and c4='d' order by c5\G
注意觀察Extra : Using filesort

------------------------
------------------------

C 只能用到 c1
explain select * from t6 where c1 = 'a' and c4 = 'd' group by c3,c2\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t6
         type: ref
possible_keys: c1
          key: c1
      key_len: 3
          ref: const
         rows: 2
        Extra: Using index condition; Using where; Using temporary; Using filesort
1 row in set (0.00 sec)

key_len: 12 #用上1個索引( c1) 1個索引 * 3字節
Extra: Using temporary->使用到臨時表

詳解 有group by語句通常要先按分組字段順序排列,若是此字段沒排序好,mysql內部會先用臨時表排序
explain select * from t6 where c1 = 'a' and c4 = 'd' group by c2,c3\G
由於查找用到c1, 正好c2是順序,c3 不會創建臨時表

----------------------
----------------------

D 只能用到 c1,  c2,c3排序
explain select * from t6 where c1 = 'a' and c5 = 'e' order by c2,c3\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t6
         type: ref
possible_keys: c1
          key: c1
      key_len: 3
          ref: const
         rows: 2
        Extra: Using index condition; Using where
1 row in set (0.00 sec)
key_len: 12 #用上1個索引( c1) 1個索引 * 3字節
Extra:發現沒有用文件排序 , (c2,c3順序)正好用上

----------------------------
----------------------------

E 只能用到 c1,c2,c3
explain select * from t6 where c1 = 'a' and c2='b' and c5 = 'e' order by c2,c3\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t6
         type: ref
possible_keys: c1
          key: c1
      key_len: 6
          ref: const,const
         rows: 2
        Extra: Using index condition; Using where
1 row in set (0.00 sec)
key_len: 12 #用上2個索引( c1,c2) 2個索引 * 3字節
Extra:發現沒有用文件排序 , (c2,c3順序)正好用上

倒過來:
explain select * from t6 where c1 = 'a' and c2='b' and c5 = 'e' order by c3,c2\G
Extra:沒用到文件排序 , 由於查找的時候 已經找到了c2 ,是一個常量

對比

explain select * from t6 where c1 = 'a' and c5 = 'e' order by c3,c2\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t6
         type: ref
possible_keys: c1
          key: c1
      key_len: 3
          ref: const
         rows: 2
        Extra: Using index condition; Using where; Using filesort
1 row in set (0.00 sec)
Extra: 文件排序
相關文章
相關標籤/搜索