当前位置:网站首页>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 .
边栏推荐
- Redis data structure
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- 二进制转十进制,十进制转二进制
- 【Rust笔记】05-错误处理
- 【Rust笔记】02-所有权
- Redis的数据结构
- UE4 call DLL
- Unity editor expansion - scrolling list
- Visual Studio (VS) shortcut keys
- Unity editor expansion - draw lines
猜你喜欢
[rust notes] 02 ownership
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Mall management system of database application technology course design
Graphics_ Games101/202 learning notes
[concurrent programming] consistency hash
OpenGL learning notes
Collection interface
Kunlunbase meetup is waiting for you!
MySQL 8
随机推荐
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Development experience and experience
Intersectionpicker in osgearth
Visual Studio (VS) shortcut keys
Unity editor expansion - draw lines
Unity Editor Extension - event handling
Unity4.3.1 engine source code compilation process
matlab神經網絡所有傳遞函數(激活函數)公式詳解
Location of package cache downloaded by unity packagemanager
Binary to decimal, decimal to binary
[cloud native] introduction and use of feign of microservices
Conversion between string and int types in golang
[rust notes] 09- special types and generics
Swagger document configuration
Find the intersection of line segments
Redis的数据结构
Exe file running window embedding QT window
Monotonic stack -84 The largest rectangle in the histogram
Why can void * be a general pointer
【Rust 笔记】07-结构体