Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
47
Fork
23
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
DevLens
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
索引的数据结构(b树,hash)
Backlog
#IAJKZA
陌生人
owner
Opened this issue
2024-08-13 10:10
<p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">索引的数据结构和具体存储引擎的实现有关,在MySQL中使用较多的索引有 Hash索引,B+树索引等,而我们经常使用的InnoDB存储引擎的默认索引实现为:B+树索引。对于哈希索引来说,底层的数据结构就是哈希表,因此在绝大多数需求为单条记录查询的时候,可以选择哈希索引,查询性能最快;其余大部分场景,建议选择BTree索引。 </span></p><p><span style="color: rgb(225, 60, 57); font-family: 微软雅黑;"><strong>B树索引 </strong></span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">mysql通过存储引擎取数据,基本上90%的人用的就是InnoDB了,按照实现方式分,InnoDB的索引类型目前只有两种:BTREE(B树)索引和HASH索引。B树索引是Mysql数据库中使用最频繁的索引类型,基本所有存储引擎都支持BTree索引。通常我们说的索引不出意外指的就是(B树)索引(实际是用B+树实现的,因为在查看表索引时,mysql一律打印BTREE,所以简称为B树索引) </span></p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710384196434.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710384196434.png" data-href="" style=""/></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">查询方式: </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">主键索引区:PI(关联保存的时数据的地址)按主键查询,普通索引区:si(关联的id的地址,然后再到达上面的地址)。所以按主键查询,速度最快 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">B+tree性质: </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">n棵子tree的节点包含n个关键字,不用来保存数据而是保存数据的索引。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">所有的叶子结点中包含了全部关键字的信息,及指向含这些关键字记录的指针,且叶子结点本身依关键字的大小自小而大顺序链接。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">所有的非终端结点可以看成是索引部分,结点中仅含其子树中的最大(或最小)关键字。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">B+ 树中,数据对象的插入和删除仅在叶节点上进行。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">B+树有2个头指针,一个是树的根节点,一个是最小关键码的叶节点。 </span></p><p><span style="color: rgb(225, 60, 57); font-family: 微软雅黑;"><strong>哈希索引 </strong></span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">简要说下,类似于数据结构中简单实现的HASH表(散列表)一样,当我们在mysql中用哈希索引时,主要就是通过Hash算法(常见的Hash算法有直接定址法、平方取中法、折叠法、除数取余法、随机数法),将数据库字段数据转换成定长的Hash值,与这条数据的行指针一并存入Hash表的对应位置;如果发生Hash碰撞(两个不同关键字的Hash值相同),则在对应Hash键下以链表形式存储。当然这只是简略模拟图。 </span></p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710385338447.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710385338447.png" data-href="" style=""/></p><p><br></p>
<p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">索引的数据结构和具体存储引擎的实现有关,在MySQL中使用较多的索引有 Hash索引,B+树索引等,而我们经常使用的InnoDB存储引擎的默认索引实现为:B+树索引。对于哈希索引来说,底层的数据结构就是哈希表,因此在绝大多数需求为单条记录查询的时候,可以选择哈希索引,查询性能最快;其余大部分场景,建议选择BTree索引。 </span></p><p><span style="color: rgb(225, 60, 57); font-family: 微软雅黑;"><strong>B树索引 </strong></span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">mysql通过存储引擎取数据,基本上90%的人用的就是InnoDB了,按照实现方式分,InnoDB的索引类型目前只有两种:BTREE(B树)索引和HASH索引。B树索引是Mysql数据库中使用最频繁的索引类型,基本所有存储引擎都支持BTree索引。通常我们说的索引不出意外指的就是(B树)索引(实际是用B+树实现的,因为在查看表索引时,mysql一律打印BTREE,所以简称为B树索引) </span></p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710384196434.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710384196434.png" data-href="" style=""/></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">查询方式: </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">主键索引区:PI(关联保存的时数据的地址)按主键查询,普通索引区:si(关联的id的地址,然后再到达上面的地址)。所以按主键查询,速度最快 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">B+tree性质: </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">n棵子tree的节点包含n个关键字,不用来保存数据而是保存数据的索引。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">所有的叶子结点中包含了全部关键字的信息,及指向含这些关键字记录的指针,且叶子结点本身依关键字的大小自小而大顺序链接。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">所有的非终端结点可以看成是索引部分,结点中仅含其子树中的最大(或最小)关键字。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">B+ 树中,数据对象的插入和删除仅在叶节点上进行。 </span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">B+树有2个头指针,一个是树的根节点,一个是最小关键码的叶节点。 </span></p><p><span style="color: rgb(225, 60, 57); font-family: 微软雅黑;"><strong>哈希索引 </strong></span></p><p><span style="color: rgb(77, 77, 77); font-family: 微软雅黑;">简要说下,类似于数据结构中简单实现的HASH表(散列表)一样,当我们在mysql中用哈希索引时,主要就是通过Hash算法(常见的Hash算法有直接定址法、平方取中法、折叠法、除数取余法、随机数法),将数据库字段数据转换成定长的Hash值,与这条数据的行指针一并存入Hash表的对应位置;如果发生Hash碰撞(两个不同关键字的Hash值相同),则在对应Hash键下以链表形式存储。当然这只是简略模拟图。 </span></p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710385338447.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710385338447.png" data-href="" style=""/></p><p><br></p>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
MySql
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
Branches (
-
)
Tags (
-
)
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register