有時候須要索引很長的字符列,這會讓索引變得大且慢。一般能夠索引開始的部分字符,這樣能夠大大節約索引空間,從而提升索引效率。但這樣也會下降索引的選擇性。索引的選擇性是指不重複的索引值(也稱爲基數,cardinality)和數據表的記錄總數的比值,範圍從1/#T到1之間。索引的選擇性越高則查詢效率越高,由於選擇性高的索引可讓MySQL在查找時過濾掉更多的行。惟一索引的選擇性是1,這是最好的索引選擇性,性能也是最好的。mysql
通常狀況下某個前綴的選擇性也是足夠高的,足以知足查詢性能。對於BLOB,TEXT,或者很長的VARCHAR類型的列,必須使用前綴索引,由於MySQL不容許索引這些列的完整長度。sql
訣竅在於要選擇足夠長的前綴以保證較高的選擇性,同時又不能太長(以便節約空間)。前綴應該足夠長,以使得前綴索引的選擇性接近於索引的整個列。換句話說,前綴的」基數「應該接近於完整的列的」基數「。數據庫
爲了決定前綴的合適長度,須要找到最多見的值的列表,而後和最多見的前綴列表進行比較。下面的示例是mysql官方提供的示例數據庫函數
下載地址以下:性能
http://downloads.mysql.com/docs/sakila-db.zipspa
在示例數據庫sakila中並無合適的例子,因此從表city中生成一個示例表,這樣就有足夠數據進行演示:code
mysql> select database(); +------------+ | database() | +------------+ | sakila | +------------+ 1 row in set (0.00 sec) mysql> create table city_demo (city varchar(50) not null); Query OK, 0 rows affected (0.02 sec) mysql> insert into city_demo (city) select city from city; Query OK, 600 rows affected (0.08 sec) Records: 600 Duplicates: 0 Warnings: 0 mysql> insert into city_demo (city) select city from city_demo; Query OK, 600 rows affected (0.07 sec) Records: 600 Duplicates: 0 Warnings: 0 mysql> update city_demo set city = ( select city from city order by rand() limit 1); Query OK, 1199 rows affected (0.95 sec) Rows matched: 1200 Changed: 1199 Warnings: 0 mysql>
由於這裏使用了rand()函數,因此你的數據會與個人不一樣,固然那不影響聰明的你。blog
首先找到最多見的城市列表:索引
mysql> select count(*) as cnt, city from city_demo group by city order by cnt desc limit 10; +-----+--------------+ | cnt | city | +-----+--------------+ | 8 | Garden Grove | | 7 | Escobar | | 7 | Emeishan | | 6 | Amroha | | 6 | Tegal | | 6 | Lancaster | | 6 | Jelets | | 6 | Ambattur | | 6 | Yingkou | | 6 | Monclova | +-----+--------------+ rows in set (0.01 sec) mysql>
注意到查詢結果,上面每一個值都出現了6-8次。如今查找到頻繁出現的城市前綴。先從3個前綴字母開始,而後4個,5個,6個:ip
mysql> select count(*) as cnt,left(city,3) as pref from city_demo group by pref order by cnt desc limit 10; +-----+------+ | cnt | pref | +-----+------+ | 25 | San | | 15 | Cha | | 12 | Bat | | 12 | Tan | | 11 | al- | | 11 | Gar | | 11 | Yin | | 10 | Kan | | 10 | Sou | | 10 | Bra | +-----+------+ 10 rows in set (0.00 sec) mysql> select count(*) as cnt,left(city,4) as pref from city_demo group by pref order by cnt desc limit 10; +-----+------+ | cnt | pref | +-----+------+ | 12 | San | | 10 | Sout | | 8 | Chan | | 8 | Sant | | 8 | Gard | | 7 | Emei | | 7 | Esco | | 6 | Ying | | 6 | Amro | | 6 | Lanc | +-----+------+ 10 rows in set (0.01 sec) mysql> select count(*) as cnt,left(city,5) as pref from city_demo group by pref order by cnt desc limit 10; +-----+-------+ | cnt | pref | +-----+-------+ | 10 | South | | 8 | Garde | | 7 | Emeis | | 7 | Escob | | 6 | Amroh | | 6 | Yingk | | 6 | Moncl | | 6 | Lanca | | 6 | Jelet | | 6 | Tegal | +-----+-------+ 10 rows in set (0.01 sec)
mysql> select count(*) as cnt,left(city,6) as pref from city_demo group by pref order by cnt desc limit 10; +-----+--------+ | cnt | pref | +-----+--------+ | 8 | Garden | | 7 | Emeish | | 7 | Escoba | | 6 | Amroha | | 6 | Yingko | | 6 | Lancas | | 6 | Jelets | | 6 | Tegal | | 6 | Monclo | | 6 | Ambatt | +-----+--------+ rows in set (0.00 sec) mysql>
經過上面改變不一樣前綴長度發現,當前綴長度爲6時,這個前綴的選擇性就接近完整列的選擇性了。甚至是同樣的。
固然還有另外更方便的方法,那就是計算完整列的選擇性,並使其前綴的選擇性接近於完整列的選擇性。下面顯示如何計算完整列的選擇性:
mysql> select count(distinct city) / count(*) from city_demo; +---------------------------------+ | count(distinct city) / count(*) | +---------------------------------+ | 0.4283 | +---------------------------------+ row in set (0.05 sec) mysql>
能夠在一個查詢中針對不一樣前綴長度的選擇性進行計算,這對於大表很是有用,下面給出如何在同一個查詢中計算不一樣前綴長度的選擇性:
mysql> select count(distinct left(city,3))/count(*) as sel3, -> count(distinct left(city,4))/count(*) as sel4, -> count(distinct left(city,5))/count(*) as sel5, -> count(distinct left(city,6))/count(*) as sel6 -> from city_demo; +--------+--------+--------+--------+ | sel3 | sel4 | sel5 | sel6 | +--------+--------+--------+--------+ | 0.3367 | 0.4075 | 0.4208 | 0.4267 | +--------+--------+--------+--------+ 1 row in set (0.01 sec) mysql>
能夠看見當索引前綴爲6時的基數是0.4267,已經接近完整列選擇性0.4283。
在上面的示例中,已經找到了合適的前綴長度,下面建立前綴索引:
mysql> alter table city_demo add key (city(6)); Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from city_demo where city like 'Jinch%'; +----+-------------+-----------+-------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | city_demo | range | city | city | 20 | NULL | 2 | Using where | +----+-------------+-----------+-------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec)
能夠看見正確使用剛建立的索引。
前綴索引是一種能使索引更小,更快的有效辦法,但另外一方面也有其缺點:
mysql沒法使用其前綴索引作ORDER BY和GROUP BY,也沒法使用前綴索引作覆蓋掃描。