当前位置:网站首页>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 算出来的值虽然有冲突的概率,但是概率非常小
- 倒序存储方式毕竟还是用的前缀索引的方式,也就是说还是会增加扫描行数
边栏推荐
- Distributed cap theory
- C實現貪吃蛇小遊戲
- 分布式CAP理论
- Component、Container容器常用API详解:Frame、Panel、ScrollPane
- Tree DP
- [Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
- 【问题记录】03 连接MySQL数据库提示:1040 Too many connections
- MySQL information_ Schema database
- buuctf-pwn write-ups (8)
- lightroom 导入图片灰色/黑色矩形 多显示器
猜你喜欢
C语言练习题(递归)
Notes and notes
High performance parallel programming and optimization | lesson 02 homework at home
实用的小工具指令
MySQL installation and configuration
P26-P34 third_ template
Weekly summary (*63): about positive energy
QT 获取随机颜色值设置label背景色 代码
如何实现视频平台会员多账号登录
Abap:ooalv realizes the function of adding, deleting, modifying and checking
随机推荐
Component、Container容器常用API详解:Frame、Panel、ScrollPane
实用的小工具指令
如何实现视频平台会员多账号登录
198. House raiding
SQL injection SQL lab 11~22
Json Web token - jwt vs. Traditional session login Authentication
MySQL learning notes 3 - JDBC
ES6 模块化
Matlab remainder
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)
微信小程序使用rich-text中图片宽度超出问题
[problem record] 03 connect to MySQL database prompt: 1040 too many connections
uniapp 自定義環境變量
Dimension and format of data
Internet of things protocol ZigBee ZigBee module uses the concept of protocol stack
Option (024) - do all objects have prototypes?
JSON Web Token----JWT和傳統session登錄認證對比
Background and current situation of domestic CDN acceleration
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)