mysql設定合適的索引長度

來源:互聯網
上載者:User

標籤:

 

理想的索引:

  1. 相對於寫操作來說,表查詢很頻繁的表建立索引

  2. 欄位區分度高

  3. 長度小(合適的長度,不是越小越好)

  4. 盡量能夠覆蓋常用欄位

 

這些條件綜合起來才能夠達到最優索引,本次我們著重聊一下建立合適長度的索引,索引的長度直接影響索引檔案的大小,因此會影響增刪改查的速度

給字元類型的欄位設定長度欄位查詢時區分度要高,如果欄位只是設定了一個那麼回查詢很多相似的匹配度不高,長度要恰到好處,否則太長索引檔案就會大,因此

要在區分度和長度上做一個平衡。

1.先來看一下沒設定索引的查詢

mysql> explain select id,title from b2b_goods where title="測試商品";

+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table     | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | b2b_goods | ALL  | NULL          | NULL | NULL    | NULL | 5061 | Using where |

+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

總結:發現該語句沒有使用到索引,掃描了5061條資料

 

2.給title欄位建立索引

mysql> alter table b2b_goods add index index_title(`title`);

Query OK, 0 rows affected (0.19 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> explain select id,title from b2b_goods where title="測試商品";

+----+-------------+-----------+------+---------------+-------------+---------+-------+------+--------------------------+

| id | select_type | table     | type | possible_keys | key         | key_len | ref   | rows | Extra                    |

+----+-------------+-----------+------+---------------+-------------+---------+-------+------+--------------------------+

|  1 | SIMPLE      | b2b_goods | ref  | index_title   | index_title | 150     | const |    1 | Using where; Using index |

+----+-------------+-----------+------+---------------+-------------+---------+-------+------+--------------------------+

1 row in set (0.00 sec)

總結:發現該語句掃描條數降低了成了一條,說明是對的,但發現索引長度(key_len)過長這樣當更新時是比較占記憶體的。

 

3.設定區分度高的並且長度適合的索引

習慣的演算法:

select count(distinct left(`title`,num))/count(*) from b2b_goods;

這裡num是指截取的長度,實際上也可以發現設定該長度的查詢度,比例越大說明越良好

(1).設定6個長度

mysql> select count(distinct left(`title`,6))/count(*) from b2b_goods;

+------------------------------------------+

| count(distinct left(`title`,6))/count(*) |

+------------------------------------------+

|                                   0.7718 |

+------------------------------------------+

1 row in set (0.01 sec)

 

(2).設定13個長度

mysql> select count(distinct left(`title`,13))/count(*) from b2b_goods;

+-------------------------------------------+

| count(distinct left(`title`,13))/count(*) |

+-------------------------------------------+

|                                    0.8288 |

+-------------------------------------------+

1 row in set (0.01 sec)

 

(3).設定25個長度

mysql> select count(distinct left(`title`,25))/count(*) from b2b_goods;

+-------------------------------------------+

| count(distinct left(`title`,25))/count(*) |

+-------------------------------------------+

|                                    0.8562 |

+-------------------------------------------+

1 row in set (0.01 sec)

(4).設定30個長度

mysql> select count(distinct left(`title`,30))/count(*) from b2b_goods;

+-------------------------------------------+

| count(distinct left(`title`,30))/count(*) |

+-------------------------------------------+

|                                    0.8573 |

+-------------------------------------------+

1 row in set (0.01 sec)

(5).設定35個長度

mysql> select count(distinct left(`title`,35))/count(*) from b2b_goods;

+-------------------------------------------+

| count(distinct left(`title`,35))/count(*) |

+-------------------------------------------+

|                                    0.8573 |

+-------------------------------------------+

1 row in set (0.01 sec)


總結:發現設定欄位的長度剛開始越來越大的時候匹配度高,到一定值變化就地區平緩,發現截取30個和35個沒什麼區分,綜上所述並保持適當的長度
我們建立一個長度為25的索引

4.建立區分度高長度適中的索引

alter table b2b_goods  drop index index_title;

alter table b2b_goods add index index_title(`title`(25));

mysql>  explain select id,title from b2b_goods where title="測試商品";

+----+-------------+-----------+------+---------------+-------------+---------+-------+------+-------------+

| id | select_type | table     | type | possible_keys | key         | key_len | ref   | rows | Extra       |

+----+-------------+-----------+------+---------------+-------------+---------+-------+------+-------------+

|  1 | SIMPLE      | b2b_goods | ref  | index_title   | index_title | 75      | const |    1 | Using where |

+----+-------------+-----------+------+---------------+-------------+---------+-------+------+-------------+

1 row in set (0.00 sec)

總結:跟之前沒有設定長度的比較key_len由150減少為75,掃描條數依然是一條,相比之下這個長度是優秀的索引長度。

 

mysql設定合適的索引長度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.