前缀索引
? ? ? ? ? ? ?當索引是很長的字符序列時,這個索引將會很占內存,而且會很慢,這時候就會用到前綴索引了。所謂的前綴索引就是去索引的前面幾個字母作為索引,但是要降低索引的重復率,索引我們還必須要判斷前綴索引的重復率。先看這樣一張表:
mysql> select * from test; +----------+-------+ | name | score | +----------+-------+ | zhangsan | 123 | | wangwu | 345 | | zhaoliu | 234 | | lisisi | 687 | +----------+-------+ 4 rows in set (0.08 sec)如果以name作為索引,當name對應的字符串很長時,就要考慮索引的占用空間和效率問題。這時候就需要引入前綴索引,在使用前綴索引時,首先要去比較重復率。
mysql> select 1.0*count(distinct name)/count(*) from test; +-----------------------------------+ | 1.0*count(distinct name)/count(*) | +-----------------------------------+ | 1.00000 | +-----------------------------------+ 1 row in set (0.00 sec)mysql> select 1.0*count(distinct left(name,2))/count(*) from test; +-------------------------------------------+ | 1.0*count(distinct left(name,2))/count(*) | +-------------------------------------------+ | 0.75000 | +-------------------------------------------+ 1 row in set (0.00 sec)mysql> select 1.0*count(distinct left(name,1))/count(*) from test; +-------------------------------------------+ | 1.0*count(distinct left(name,1))/count(*) | +-------------------------------------------+ | 0.75000 | +-------------------------------------------+ 1 row in set (0.00 sec)mysql> select 1.0*count(distinct left(name,3))/count(*) from test; +-------------------------------------------+ | 1.0*count(distinct left(name,3))/count(*) | +-------------------------------------------+ | 0.75000 | +-------------------------------------------+ 1 row in set (0.00 sec)mysql> select 1.0*count(distinct left(name,4))/count(*) from test; +-------------------------------------------+ | 1.0*count(distinct left(name,4))/count(*) | +-------------------------------------------+ | 1.00000 | +-------------------------------------------+ 1 row in set (0.00 sec)mysql> select 1.0*count(distinct left(name,2))/count(*) from test; +-------------------------------------------+ | 1.0*count(distinct left(name,2))/count(*) | +-------------------------------------------+ | 0.75000 | +-------------------------------------------+ 1 row in set (0.00 sec)mysql> select 1.0*count(distinct left(name,5))/count(*) from test; +-------------------------------------------+ | 1.0*count(distinct left(name,5))/count(*) | +-------------------------------------------+ | 1.00000 | +-------------------------------------------+ 1 row in set (0.00 sec)其中left函數為字符串截取函數。
select 1.0*count(distinct name)/count(*) from test這是比較整個name的重復率,當時這是最好的情況。然后分別截取name字符的前幾個字母,最后選取的計算值要接近整個取整個name時得出的計算值,然后再選中占用空間小的。由上面執行的結果可知應選中name的前4個字母作為索引最為適合。
創建索引:
mysql> alter table test add key(name(4)); Query OK, 4 rows affected (0.15 sec) Records: 4 Duplicates: 0 Warnings: 0隨后就可以正常按name字符進行查找了。
?
?
總結
- 上一篇: 利用索引优化查询
- 下一篇: linux命令端口探测