当前位置:网站首页>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 算出来的值虽然有冲突的概率,但是概率非常小
- 倒序存储方式毕竟还是用的前缀索引的方式,也就是说还是会增加扫描行数
边栏推荐
- JS execution mechanism
- QT releases multilingual International Translation
- C language exercises (recursion)
- 报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
- Fundamentals of SQL database operation
- Arcpy 利用updatelayer函数改变图层的符号系统
- Uniapp custom environment variables
- ADC voltage calculation of STM32 single chip microcomputer
- Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
- [openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
猜你喜欢

Uniapp custom environment variables

198. House raiding

SQL injection SQL lab 11~22

Can the out of sequence message complete TCP three handshakes

Component、Container容器常用API详解:Frame、Panel、ScrollPane

Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
![[backpack DP] backpack problem](/img/7e/1ead6fd0ab61806ce971e1612b4ed6.jpg)
[backpack DP] backpack problem

R statistical mapping - random forest classification analysis and species abundance difference test combination diagram

AWT介绍

Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
随机推荐
Learning multi-level structural information for small organ segmentation
双色球案例
【问题记录】03 连接MySQL数据库提示:1040 Too many connections
Weekly summary (*63): about positive energy
198. House raiding
regular expression
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
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)
2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers
MySQL installation and configuration
InputStream/OutputStream(文件的输入输出)
Inputstream/outputstream (input and output of file)
树形dp
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
Overview of convolutional neural network structure optimization
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage