1. 程式人生 > >B-Tree索引 和 hash索引區別

B-Tree索引 和 hash索引區別

1:索引型別

  1.1 B-tree索引

  注: 名叫btree索引,大的方面看,都用的平衡樹,但具體的實現上, 各引擎稍有不同,

比如,嚴格的說,NDB引擎,使用的是T-tree

  Myisam,innodb中,預設用B-tree索引

 

但抽象一下---B-tree系統,可理解為”排好序的快速查詢結構”. 

 

1.2 hash索引

     在memory表裡,預設是hash索引, hash的理論查詢時間複雜度為O(1)

 

疑問: 既然hash的查詢如此高效,為什麼不都用hash索引?

答:

1:hash函式計算後的結果,是隨機的,如果是在磁碟上放置資料,

比主鍵為id為例, 那麼隨著id的增長, id對應的行,在磁碟上隨機放置.

2: 不法對範圍查詢進行優化.

3: 無法利用字首索引. 比如 在btree中, field列的值“hellopworld”,並加索引

查詢 xx=helloword,自然可以利用索引, xx=hello,也可以利用索引. (左字首索引)

因為hash(‘helloword’),和hash(‘hello’),兩者的關係仍為隨機

4: 排序也無法優化.

5: 必須回行.就是說 通過索引拿到資料位置,必須回到表中取資料

 

2: btree索引的常見誤區

 2.1 在where條件常用的列上都加上索引

  例: where cat_id=3 and price>100 ; //查詢第3個欄目,100元以上的商品

  誤: cat_id上,和, price上都加上索引.

  錯: 只能用上cat_id或Price索引,因為是獨立的索引,同時只能用上1個.

 

 2.2 在多列上建立索引後,查詢哪個列,索引都將發揮作用

誤: 多列索引上,索引發揮作用,需要滿足左字首要求.

以 index(a,b,c) 為例,

語句

索引是否發揮作用

Where a=3

是,只使用了a列

Where a=3 and b=5

是,使用了a,b列

Where a=3 and b=5 and c=4

是,使用了abc

Where b=3  /  where c=4

Where a=3 and c=4

a列能發揮索引,c不能

Where a=3 and b>10 and c=7

A能利用,b能利用, C不能利用

同上,where a=3 and b like ‘xxxx%’ and c=7

A能用,B能用,C不能用

 

 

      

為便於理解, 假設ABC各10米長的木板, 河面寬30米.

全值索引是則木板長10米,

Like,左字首及範圍查詢, 則木板長6米,

 

自己拼接一下,能否過河對岸,就知道索引能否利用上.

如上例中, where a=3 and b>10, and c=7,

A板長10米,A列索引發揮作用

A板正常接B板, B板索引發揮作用

B板短了,接不到C板, C列的索引不發揮作用.

 

多列索引經典題目:

http://www.zixue.it/thread-9218-1-4.html

 

假設某個表有一個聯合索引(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=x and c5=x order by c2,c3

E where c1=x and c2=x and c5=? order by c2,c3

 

create table t4 (

c1 tinyint(1) not null default 0,

c2 tinyint(1) not null default 0,

c3 tinyint(1) not null default 0,

c4 tinyint(1) not null default 0,

c5 tinyint(1) not null default 0,
index c1234(c1,c2,c3,c4)

);

insert into t4 values (1,3,5,6,7),(2,3,9,8,3),(4,3,2,7,5);

 

對於A:

c1=x and c2=x and c4>x and c3=x  <==等價==> c1=x and c2=x and c3=x and c4>x

因此 c1,c2,c3,c4都能用上. 如下:

mysql> explain select * from t4 where c1=1 and c2=2 and c4>3 and c3=3 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: range

possible_keys: c1234

          key: c1234

      key_len: 4 #可以看出c1,c2,c3,c4索引都用上

          ref: NULL

         rows: 1

        Extra: Using where

 

對於B: select * from t4 where c1=1 and c2=2 and c4=3 order by c3

c1 ,c2索引用上了,在c2用到索引的基礎上,c3是排好序的,因此不用額外排序.

而c4沒發揮作用.

mysql> explain select * from t4 where c1=1 and c2=2 and c4=3 order by c3 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: ref

possible_keys: c1234

          key: c1234

      key_len: 2

          ref: const,const

         rows: 1

        Extra: Using where

1 row in set (0.00 sec)

 

mysql> explain select * from t4 where c1=1 and c2=2 and c4=3 order by c5 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: ref

possible_keys: c1234

          key: c1234

      key_len: 2

          ref: const,const

         rows: 1

        Extra: Using where; Using filesort

1 row in set (0.00 sec)

 

對於 C: 只用到c1索引,因為group by c3,c2的順序無法利用c2,c3索引

mysql> explain select * from t4 where c1=1 and c4=2 group by c3,c2 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: ref

possible_keys: c1234

          key: c1234

      key_len: 1 #只用到c1,因為先用c3後用c2分組,導致c2,c3索引沒發揮作用

          ref: const

         rows: 1

        Extra: Using where; Using temporary; Using filesort

1 row in set (0.00 sec)

 

mysql> explain select * from t4 where c1=1 and c4=2 group by c2,c3 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: ref

possible_keys: c1234

          key: c1234

      key_len: 1

          ref: const

         rows: 1

        Extra: Using where

1 row in set (0.00 sec)

 

D語句: C1確定的基礎上,c2是有序的,C2之下C3是有序的,因此c2,c3發揮的排序的作用.

因此,沒用到filesort

mysql> explain select * from t4 where c1=1 and c5=2 order by c2,c3 \G 

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: ref

possible_keys: c1234

          key: c1234

      key_len: 1

          ref: const

         rows: 1

        Extra: Using where

1 row in set (0.00 sec)

 

E: 這一句等價與 elect * from t4 where c1=1 and c2=3 and c5=2 order by c3;

因為c2的值既是固定的,參與排序時並不考慮

 

mysql> explain select * from t4 where c1=1 and c2=3 and c5=2 order by c2,c3 \G

*************************** 1. row ***************************

           id: 1

  select_type: SIMPLE

        table: t4

         type: ref

possible_keys: c1234

          key: c1234

      key_len: 2

          ref: const,const

         rows: 1

        Extra: Using where

1 row in set (0.00 sec)

 

一道面試題:

有商品表, 有主鍵,goods_id,  欄目列 cat_id, 價格price

說:在價格列上已經加了索引,但按價格查詢還是很慢,

問可能是什麼原因,怎麼解決?

 

答: 在實際場景中,一個電商網站的商品分類很多,直接在所有商品中,按價格查商品,是極少的,一般客戶都來到分類下,然後再查.

 

改正: 去掉單獨的Price列的索引, 加 (cat_id,price)複合索引

再查詢.