当前位置:网站首页>MySQL index types B-tree and hash
MySQL index types B-tree and hash
2022-07-03 08:40:00 【Programmer small circle】
Hash The particularity of index structure , Its retrieval efficiency is very high , The index can be retrieved at one time , Unlike B-Tree The index needs to be from the root node to the branch node , Finally, you can access the page node for many times IO visit , therefore Hash The query efficiency of index is much higher than B-Tree Indexes .
Maybe a lot of people have questions again , since Hash Index is more efficient than B-Tree Much higher , Why don't everyone use Hash Index and use B-Tree Index? ? Everything has two sides ,Hash The index is the same , although Hash Index efficiency is high , however Hash Because of its particularity, the index itself also brings a lot of Limitations and drawbacks , There are mainly the following .
1).Hash Index can only satisfy "=","IN" and "<=>" Inquire about , Can't use range query .
because Hash Index comparison is to carry out Hash After the operation Hash value , So it can only be used for equivalent filtering , Can't be used for range based filtering , Because after the corresponding Hash After algorithm processing Hash The relationship between the size of the value , There's no guarantee of and Hash It's exactly the same before the operation .
2).Hash Index can't be used to avoid sorting data .
because Hash What the index stores is the process Hash After calculation Hash value , and Hash The magnitude of the value does not necessarily relate to Hash The key value before the operation is exactly the same , So the database can't use the index data to avoid any sort operation ;
3).Hash Index can't query with partial key .
For composite indexes ,Hash The index is calculating Hash When the value is combined, the index key is combined and then calculated together Hash value , Not separately Hash value , So when you query by combining one or more index keys in front of the index ,Hash Index can't be used .
4).Hash Index can't avoid table scanning at any time .
I already know that ,Hash Index is to pass the index key through Hash After the operation , take Hash The result of the calculation Hash The value and the corresponding row pointer information are stored in a Hash In the table , Because different index keys are the same Hash value ( Hash Collisions ), So even if you meet someone Hash Number of records of key value data , I can't go from Hash Direct query in index , You still need to access the actual data in the table for corresponding comparison , And get the corresponding result .
5).Hash Index encountered a lot of Hash When the values are equal, the performance is not necessarily better than B-Tree High index .
For index keys with low selectivity , If you create Hash Indexes , Then there will be a lot of record pointer information stored in the same Hash Correlation of values . In this way, it will be very troublesome to locate a record , Will waste many times table data access , And the overall performance is low
In short , Hash index is to use a certain hash algorithm , Convert the key value to a new hash value , You don't need to search like B+ The tree searches from root node to leaf node step by step , Only one hash algorithm is needed to locate the corresponding location immediately , Very fast .
B+ Obvious differences between tree index and hash index difference yes :
1). If it is equivalent query , So hash index has an absolute advantage , Because it only needs to go through the algorithm once to find the corresponding key value ; Yes, of course , The premise is , Key values are unique . If the key value is not unique , You need to find the key first , Then scan back according to the list , Until we find the corresponding data ;
2). You can also see from the diagram , If it's range search , At this time, hash index is useless , Because it used to be an ordered key , After hash algorithm , It could become a discontinuity , There is no way to use the index to complete the range query ;
3). Empathy , Hash index also can't use index to complete sorting , as well as like ‘xxx%’ Such a partial fuzzy query ( This kind of partial fuzzy query , In fact, it is also range query in essence );
4). Hash index also does not support multi column joint index The leftmost matching rule ;
5).B+ The key search efficiency of tree index is average , Unlike B Trees fluctuate so much , In the case of a large number of duplicate key values , The efficiency of hash index is also very low , Because of the so-called hash collision problem .
边栏推荐
猜你喜欢

OpenGL learning notes

Simple demo of solving BP neural network by gradient descent method

Redis的数据结构
![[concurrent programming] working mechanism and type of thread pool](/img/51/d21428a7c95c0a5177e8198742e78c.jpg)
[concurrent programming] working mechanism and type of thread pool

数据分析练习题

Monotonic stack -42 Connect rainwater

【更新中】微信小程序学习笔记_3

Unity learning notes

Explain sizeof, strlen, pointer, array and other combination questions in detail

UE4 source code reading_ Bone model and animation system_ Animation process
随机推荐
【Rust 笔记】09-特型与泛型
Unity editor expansion - the framework and context of unity imgui
Golang time format sorting
UE4 source code reading_ Bone model and animation system_ Animation node
Intersectionpicker in osgearth
Display terrain database on osgearth ball
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
了解小程序的笔记 2022/7/3
Location of package cache downloaded by unity packagemanager
Simple demo of solving BP neural network by gradient descent method
Osgearth topographic shading map drawing
MySQL 8
【Rust 笔记】07-结构体
Animation_ IK overview
单调栈-84. 柱状图中最大的矩形
matlab神經網絡所有傳遞函數(激活函數)公式詳解
Pit & ADB wireless debugging of vivo real machine debugging
请求参数的发送和接收
Visual Studio (VS) shortcut keys
Unity editor expansion - controls, layouts