当前位置:网站首页>Mysql 45讲学习笔记(十一)字符串字段怎么加索引
Mysql 45讲学习笔记(十一)字符串字段怎么加索引
2022-07-04 06:33:00 【孔汤姆】
一.前缀索引
- 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索引回表看一下,因为系统不确定前缀索引定义是否截断了完整信息。
索引越长,占用的磁盘空间越大,相同数据页能放下的索引值越小,搜索效率越低。
四.身份证场景(字段前面想同后面区分度高)
- 使用倒叙存储,如果存储身份证号的时候把他们倒过来
- 使用 hash 字段
使用倒叙存储和使用hash字段两种方式的异同点
相同点
- 都不支持范围查询
不同点
- 占用的额外空间来看,倒叙存储方式在主键索引,不会消耗额外的存储空间
- hash字段方法需要增加一个字段
CPU消耗方面:
- 倒叙方式需要额外调用一次reverse函数
- hash字段需要额外调用一次哈希函数,reverse 函数额外消耗的 CPU 资源会更小些
查询效率:
- hash 字段方式的查询性能相对更稳定一些。因为 crc32 算出来的值虽然有冲突的概率,但是概率非常小
- 倒序存储方式毕竟还是用的前缀索引的方式,也就是说还是会增加扫描行数
边栏推荐
- R统计绘图-随机森林分类分析及物种丰度差异检验组合图
- Operator < <> > fool test case
- QT releases multilingual International Translation
- Considerations for testing a website
- MySQL information_ Schema database
- buuctf-pwn write-ups (8)
- C语言练习题(递归)
- C réaliser des jeux de serpents gourmands
- STM32 单片机ADC 电压计算
- Functions in C language (detailed explanation)
猜你喜欢

Abap:ooalv realizes the function of adding, deleting, modifying and checking

2022 wechat enterprise mailbox login entry introduction, how to open and register enterprise wechat enterprise mailbox?

Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane

云原生——上云必读之SSH篇(常用于远程登录云服务器)

JSON Web Token----JWT和传统session登录认证对比

C language - Blue Bridge Cup - Snake filling

Arcpy uses the updatelayer function to change the symbol system of the layer

Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.

After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people

C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
随机推荐
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
7. Agency mode
Common JS tool Libraries
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
Fundamentals of SQL database operation
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Can the out of sequence message complete TCP three handshakes
Redis面试题集
leetcode 310. Minimum Height Trees
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
C réaliser des jeux de serpents gourmands
uniapp 自定义环境变量
C realize Snake games
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
buuctf-pwn write-ups (8)
Weekly summary (*63): about positive energy
ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
The sorting in C language realizes the number sorting method from small to large