MySQL建立複合索引

haoge0205發表於2014-09-10
在MySQL資料庫中,建立複合索引的時候,不知道在建立過程中哪個列在前面,哪個列該在後面,用以下方式即可:


select count(distinct first_name)/count(*) as first_name_selectivity,
count(distinct last_name)/count(*) as last_name_selectivity,
count(*)
from actor\G


mysql> select count(distinct first_name)/count(*) as first_name_selectivity,
    -> count(distinct last_name)/count(*) as last_name_selectivity,
    -> count(*)
    -> from actor\G
*************************** 1. row ***************************
first_name_selectivity: 0.6400
 last_name_selectivity: 0.6050
              count(*): 200
1 row in set (0.01 sec)


first_name_selectivity: 0.6400,因此 first_name 列的選擇性更高,所以答案將其作為索引列的第一列:

mysql> alter table yoon add key (first_name,last_name);




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-1266975/,如需轉載,請註明出處,否則將追究法律責任。

相關文章