We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MySQL 是支持前缀索引的,也就是说,你可以定义字符串的一部分作为索引
mysql> alter table SUser add index index2(email(6));
使用前缀索引,定义好长度,就可以做到既节省空间,又不用额外增加太多的查询成本,但如果长度不合适就会导致查询的次数过多,可以通过统计索引上有多少个不同的值来判断要使用多长的前缀
mysql> select count(distinct left(email,4))as L4, count(distinct left(email,5))as L5, count(distinct left(email,6))as L6, count(distinct left(email,7))as L7, from SUser;
而且使用前缀索引之后,就无法使用覆盖索引,因为系统并不确定前缀索引的定义是否截断了完整信息,所以还得去 ID 索引查询一遍
遇到前缀的区分度不够好的情况时,可以使用倒序存储
mysql> select field_list from t where id_card = reverse('input_id_card_string');
在表上新建一个 hash 字段,存储字符串的 hash 值
都不支持范围查询,倒序存储的字段上创建的索引是按照倒序字符串的方式排序的,hash 只支持等值查询
The text was updated successfully, but these errors were encountered:
git-zjx
No branches or pull requests
前缀索引
MySQL 是支持前缀索引的,也就是说,你可以定义字符串的一部分作为索引
使用前缀索引,定义好长度,就可以做到既节省空间,又不用额外增加太多的查询成本,但如果长度不合适就会导致查询的次数过多,可以通过统计索引上有多少个不同的值来判断要使用多长的前缀
而且使用前缀索引之后,就无法使用覆盖索引,因为系统并不确定前缀索引的定义是否截断了完整信息,所以还得去 ID 索引查询一遍
其他方式
倒序存储
遇到前缀的区分度不够好的情况时,可以使用倒序存储
hash 字段
在表上新建一个 hash 字段,存储字符串的 hash 值
使用倒序存储和使用 hash 字段这两种方法的异同点
相同点
都不支持范围查询,倒序存储的字段上创建的索引是按照倒序字符串的方式排序的,hash 只支持等值查询
不同点
The text was updated successfully, but these errors were encountered: