当前位置:网站首页>Read mysql45 lecture - index
Read mysql45 lecture - index
2022-06-25 16:24:00 【Hurry to Friday】
Index is to improve the efficiency of query
The index I understand is to store data in a certain way , In this way, the desired data can be queried faster according to the characteristics of the storage mode ,
This special storage method is the data structure , Common data structures are arrays , Linked list , Hashtable , Trees , queue , Stacks and so on .
Hashtable
Hash table is a kind of hash table with key value (key-value) Structure of stored data . for example key=monday,value= Monday ; First, according to the custom hash function monday A value is calculated 128, Then in the array array Subscript is 128 On Monday ; But maybe the hash function will calculate different values to get the same number , At this time, multiple values in the same position form a linked list .
The values calculated by the hash function here are usually not sequential values , The advantage is that it can be calculated directly when adding key Then put it in the array , It is more suitable for equivalent query ; But if you need to query a range of values (key=monday,key=friday), We need to key=monday To key=friday All the values of are queried .
Ordered array
If you use an ordered array to store data , Convenient interval query , Equivalent query is also very convenient . But it takes more space and time to insert and add . So an ordered array is suitable for storing data that normally does not change , For example, last year, Wanglaoji sold a lot in the snack bar .
The core of database underlying storage is based on data model , That is, the actual use of data structures .
InnoDB The index of
stay InnoDB in , Tables are stored by index in primary key order ,InnoDB Used B+ Tree index model , So the data is stored in B+ In the tree .
Create a table :
create table T( id int primary key, k int not null, name varchar(16), index (k))engine=InnoDB;

InnoDB The index types of are divided into primary key index and non primary key index .
You can see that the value stored in the leaf node of the primary key index on the left is the entire row of data (R1~R5), Primary keys are also called clustered indexes
On the right, the non primary key index leaf node stores ID Value , Non primary key indexes are also called secondary indexes .
The difference between query based on primary key index and general index :
- select * from table where id = 5 Primary key query , Search the index of the primary key directly , That's the primary key id Composed of B+ Trees
- select * from table where k=6 General index query , First, query k The values on this field B+ Trees , find id, And then go back to according to id Composed of B+ The tree queries the entire row of values .
That is, one more step , This process is called back to table , Therefore, primary key queries should be used as much as possible .
B+ Tree to maintain the order of the index , Therefore, there will be maintenance operations when inserting data , For example, in the picture above , Insert a id The value is 400 The data of , You need to id=500,id=600 The position of the data is moved back .
During normal table creation, the primary key is set to auto increment primary key ,( namely : NOTNULL PRIMARY KEY AUTO_INCREMENT.), So when inserting new data , There is no need to specify the primary key value , The database will automatically obtain the current maximum primary key value, and then +1 Assign to new data . However, the primary keys of some tables are not self incremented , Instead, use fields with business logic as primary keys .
Consider whether to use the ID number as the primary key of the personnel table or the auto increment primary key ?
If you use the ID number as the primary key , Then the value stored in the secondary index needs 20 Bytes , But if you use a self incrementing primary key , The value stored in the secondary index is 4 byte (int),8 byte (bigint), You can see that the space occupied by the self incrementing primary key index is still a little smaller .
It is suitable for the background of using business fields as primary keys :
- Only one index
- The index is the only index
In this case, there is no need to consider the secondary index . But normally, it is convenient to add a primary key automatically .
边栏推荐
- MT60B1G16HC-48B:A美光内存颗粒FBGA代码D8BNK[通俗易懂]
- JS add custom attributes to elements
- mysql整体架构和语句的执行流程
- Dart syntax
- Gold three silver four, an article to solve the resume and interview
- Message format of Modbus (PLC)
- 20省市公布元宇宙路线图
- Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
- Native JS dynamically add elements
- Flutter assembly
猜你喜欢

Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution

教务系统开发(PHP+MySQL)

Vscode有什么好用的插件?

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官

Prototype chain analysis

Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
Create raspberry PI image file of raspberry pie

Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO

Xinlou: Huawei's seven-year building journey of sports health
Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
随机推荐
Prototype mode
What plug-ins are available for vscade?
Multiple decorators decorate a function
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE
Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
Dart syntax
炮打司令部,别让一个UI框架把你毁了
Overall MySQL architecture and statement execution process
f_ Read function [easy to understand]
Golang open source streaming media audio and video network transmission service -lal
有哪些新手程序员不知道的小技巧?
Mixed density network (MDN) for multiple regression explanation and code example
The paid video at station B caused the up master to lose more than ten thousand fans
JS add custom attributes to elements
iVX低代码平台系列详解 -- 概述篇(一)
Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
不要小看了积分商城,它的作用可以很大!
User registration, information writing to file
Common APIs and exception mechanisms
The release of autok3s v0.5.0 continues to be simple and friendly