当前位置:网站首页>MySQL index (I)
MySQL index (I)
2022-07-19 14:40:00 【After all, Yin Jianwei】
Indexes
1、 Index Overview
Indexes (index) Help MySL Data structure for efficient data acquisition ( Orderly ). Out of data , The database system also maintains a data structure that satisfies a specific search algorithm , These data structures are referenced in some way ( Point to ) data , In this way, advanced search algorithms can be implemented on these data structures , This data structure is the index .

remarks : The above binary tree index structure is only a schematic diagram , Not a real index structure .
Advantages and disadvantages :
| advantage | Inferiority |
|---|---|
| Improve the efficiency of data retrieval , Reduce the IO cost | Index columns also take up space . |
| Sort data through index columns , Reduce the cost of sorting data , Reduce CPU Consumption of | Indexing improves query efficiency , At the same time, it also reduces the speed of updating tables , For example, row the table Insert、Update、Delete when , Low efficiency . |
2、 Index structure
Mysql Indexing is implemented at the storage engine level , Different storage engines have different structures , It mainly includes the following :
| Index structure | describe |
|---|---|
| B+Tree Indexes | The most common type of index , Most engines support B+ Tree index |
| Hash Indexes | The underlying data structure is realized by hash table , Only the exact index matches the columns , Range query is not supported |
| R-Tree( Spatial index ) | The spatial index is MyISAM A special index type of the engine , Mainly used for geospatial data types , Usually used less |
| Full-text( Full-text index ) | It's a way of building inverted indexes , How to quickly match documents . Be similar to Lucene,Solr,ES |
| Indexes | InnoDB | MyISAM | Memory |
|---|---|---|---|
| B+Tree Indexes | Support | Support | Support |
| Hash Indexes | I won't support it | I won't support it | Support |
| R-Tree Indexes | I won't support it | Support | I won't support it |
| Full-text Indexes | 5.6 Support in the future | Support | I won't support it |
What we usually call index , If not specified , Generally speaking, it means B+ Index of tree structure organization .
Disadvantages of binary tree : On sequential insertion , It will form a linked list , Query performance is greatly reduced . In case of large amount of data , Deeper levels , The retrieval speed is slow .
Red and black trees : In case of large amount of data , Deeper levels , The retrieval speed is slow .


be relative to B-Tree difference :
1、 All data will appear in the leaf node
2、 Leaf nodes form a one-way linked list

Hash Indexes :
Hash Index characteristics :
1、Hash Indexes can only be used for peer-to-peer comparisons (=,in), Range query is not supported (between,>,<,…)
2、 Cannot complete sort operation with index
3、 High query efficiency , Usually only one search is needed , Efficiency is usually higher than B+Tree Indexes
Storage engine support :
stay MySQL in , Support hash The index is Memory engine , and InnoDB It has adaptive function hash function ,hash The index is the storage engine B+Tree The index is automatically built under specified conditions .
Why? InnoDB The storage engine chooses to use B+Tree Index structure ?
- Relative to a binary tree , Fewer levels , High search efficiency ;
- about B-Tree, Whether leaf nodes or non leaf nodes , Data will be saved , This results in fewer key values stored in a page , The pointer decreases , Save a lot of data as well , Can only increase the height of the tree , Resulting in reduced performance
- relative hash Indexes ,B+Tree Support range matching and sorting operations
3、 Index classification
| classification | meaning | characteristic | keyword |
|---|---|---|---|
| primary key | The index created for the primary key in the table | Automatically created by default , There can only be one | PRIMARY |
| unique index | Avoid duplicate values in a data column in the same table | There can be multiple | UNIQUE |
| General index | Quickly locate specific data | There can be multiple | |
| Full-text index | The full-text index looks up the keywords in the text , Instead of comparing the values in the index | There can be multiple | FULLTEXT |
stay InnoDB In the storage engine , According to the storage form of the index , It can be divided into the following two types :
| classification | meaning | characteristic |
|---|---|---|
| Clustered index (Clustered Index) | Store the data in the index and put it in one piece , The leaf node of the index structure holds the row data | There has to be , And there's only one |
| Secondary indexes (Secondary Index) | Separate data from index , The leaf node of the index structure is associated with the corresponding primary key | There can be multiple |
Clustered index selection rules :
- If there is a primary key , A primary key index is a clustered index .
- If there is no primary key , The first unique index will be used (UNIQUE) As a clustered index .
- If the table does not have a primary key , Or a suitable unique index , be InnoDB It will automatically generate a rowid As a hidden clustered index .

hypothesis Query name Arm Of : select * from user where name = ‘Arm’;
He will first go to the secondary index to query the primary key id, Then query the clustered index and find id = 10 Row data , This is also called a table query .
reflection :
1、 once SQL sentence , Which is more efficient ? Why? ?
select * from user where id = 10;
select * from user where name = 'arm';
# remarks :id Primary key ,name Fields are indexed
This is very simple ,id Primary key , explain id Is clustered index , You only need to query once to get row data
And in accordance with name To query, you need to query twice , Return to the table for query
边栏推荐
- Excellent jar package startup shell script collection
- Méthode de compilation de la courbe RPS d'O'Neill (originale par le Dr Tao)
- Optimal biking strategy [DP + two points]
- Tree and bipartite graph [thinking]
- Unity uses a map to control the transparency of the material's main map
- Si446 usage record (III): match function
- Database SQL Server
- Qchartview overwrites the previous control when it is added in qgridlayout
- 详解C语言动态内存管理
- 96. Different binary search trees
猜你喜欢

STM32 positioning hardfault location method and encountered situation in keil environment

贝塞尔曲线简单介绍

抽象类与派生类

Redis源码与设计剖析 -- 1.简单动态字符串

【MQTT从入门到提高系列 | 06】MQTT3.1.1之SUBSCRIBE订阅工作流
![[mqtt from getting started to improving series | 06] subscribe subscription workflow of mqtt3.1.1](/img/bf/9f8d8b2a73406970941fce33d3e754.png)
[mqtt from getting started to improving series | 06] subscribe subscription workflow of mqtt3.1.1

Redis source code and design analysis -- 2 Linked list

The manual is not complete. How to manually dig out the monitoring information of tongweb?
![Can [C language - user defined type] be adjusted like this?](/img/f0/313cd86d875f6d1ffc516ab8de3477.png)
Can [C language - user defined type] be adjusted like this?

matplotlib绘制多折线图(解决matplotlib中文无法显示问题)
随机推荐
96. Different binary search trees
Redis source code and design analysis -- 2 Linked list
Qchartview overwrites the previous control when it is added in qgridlayout
Mvcc multi version concurrency control
273. Grading - acwing question bank [DP]
数据库的增删改查
Colliding Mice碰撞老鼠工程分析
Colliding mice collision engineering analysis
洛谷:P3092 [USACO13NOV]No Change G(状压+二分,独特的状态定义,不写会后悔一辈子的题)
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
Minuterie logicielle à puce unique v2.0
Opencv template
ClassNotFoundException:com. tongweb. geronimo. osgi. locator. ProviderLocator
Optimizer of pytoch framework optimizer
Optimal Biking Strategy【DP + 二分】
Read the paper: temporary graph networks for deep learning on dynamic graphs
Redis源码与设计剖析 -- 1.简单动态字符串
ShanDong Multi-University Training #3
P8346 the clearest air and sea [undirected graph topology]
JVM性能优化