对字符串键索引DISTINCT的使用

会列出所有不同的手机号(account)

SELECT DISTINCT account FROM USER

会列出所有不同的手机号(account)个数

SELECT COUNT(DISTINCT account) AS L FROM USE

会列出以手机号前两位做索引,索引的个数

SELECT COUNT(DISTINCT LEFT(account,2)) AS L FROM `user`

以手机号做索引

alter table user add index index1(account)

以手机号前6位做索引

alter table user add index index1(account(6))

像身份证这种前面字符串重复率大的  可以选择倒序存储

select field_list from t where id_card = reverse('input_id_card_string')

或者用hash字段(既在表里添加一个字段存储hash值,并对其建立索引,插入或者查询的时候利用crc32()这个函数)

要注意hash冲突 所以查询条件加上and id_card='input_id_card_string'

mysql> select field_list from t where id_card_crc=crc32('input_id_card_string') and id_card='input_id_card_string'
 

猜你喜欢

转载自blog.csdn.net/qq_38020553/article/details/86539540
今日推荐