MySQL Prefix index

MySQL prefix index can effectively reduce the size of the index file, increase the speed of the index. But the prefix index also has its disadvantages: MySQL can not use the prefix index in a GROUP BY or ORDER BY, nor can they be used as a covering index (Covering Index).

The establishment of the prefix index of examples:

View Code
1 # 语法
2 ALTER TABLE table_name ADD KEY(column_name(prefix_length));
3 
4 # 示例
5 ALTER TABLE city ADD KEY(cityname(7));

One example of computing a full column selectivity:

View Code
1 # 全列选择性
2 SELECT COUNT(DISTINCT column_name) / COUNT(*) FROM table_name;
3 
4 # 测试某一长度前缀的选择性
5 SELECT COUNT(DISTINCT LEFT(column_name, prefix_length)) / COUNT(*) FROM table_name;

* The prefix of the column selective selective closer to full time, the better the index effect.

Reproduced in: https: //my.oschina.net/weisenz/blog/200617

Guess you like

Origin blog.csdn.net/weixin_33806914/article/details/91920861
Recommended