当前位置:网站首页>MySQL index
MySQL index
2022-06-13 04:23:00 【leejie*.】
MySQL Indexes
What is index ?
Definition : An index is a data structure that is organized and quickly searched , It helps the database to search data efficiently . Out of data , The database system also maintains a data structure that satisfies a specific search algorithm ( Extra storage space ), These data structures point to data in some way , In this way, efficient search algorithms can be realized on these data structures . This data structure is called an index .
The index is usually stored on the disk in the form of index file , At present, most indexes adopt B+ Tree construction .
Index type
primary key
The value in the index column must be unique , No null values are allowed ;
General index
Basic index type , Allows you to insert duplicate and null values in a column that defines an index .
unique index
The value in the index column must be unique , But null is allowed .
Full-text index
Only in text type CHAR,VARCHAR,TEXT Create full text index on type field . When the field length is large , If you create a normal index , It's going on like The efficiency of fuzzy query is low , At this point, you can create a full-text index . MyISAM and InnoDB Full text index can be used in .
Index data structure
mysql The data structure of the index is B+ Trees ,B Trees and B+ The main difference between trees is whether non leaf nodes store data .
B+ Trees : Only leaf nodes store data , Non leaf nodes store only key values . Leaf nodes are connected by two-way pointers , The bottom leaf node forms a bidirectional ordered list .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-yaCQYqoD-1654767182523)(C:\Users\Leejie\Desktop\leejie\ note \image\B+Tree.png)]
B+ The bottom leaf node of the tree contains all the index entries .B+ Tree lookup data . Because the data is stored on the lowest leaf node , So every search needs to retrieve the leaf node to query the data . So when you need to query the data, you need to query the disk every time IO It's directly related to tree height , Because the data is put into the leaf node , Therefore, the number of indexes stored in the disk block where the index is placed will increase , So relative B Trees ,B+ The height of a tree is theoretically higher than B The trees should be short . There are also cases where the index covers the query , In the index, the data satisfies all the data needed by the current query statement , At this point, you just need to find the index and return immediately , There is no need to retrieve the lowest leaf node .
Storage engine
MySQL Index implementation of two kinds of storage engine of :MyISAM Index and InnoDB Indexes .
myISAM Indexes
myISAM Index data files and index files are stored separately .MyISAM Use B+ Tree when building an index tree , The key value stored in the leaf node is the value of the index column , The data is the disk address of the index line .
Be careful :MyISAM In the query , The inode will be cached in MySQL In cache , Data caching depends on the operating system's own cache , So it's not always the disk that goes .
InnoDB Indexes
InnoDB The primary key index of is the cluster index . Every InnoDB Each table has a clustered index , The leaf node stores the entire row of records .
InnoDB The rules for creating an index are as follows :
- Define a primary key on a table PRIMARY KEY,InnoDB Reference the primary key index as a cluster index .
- If the table does not have a primary key defined ,InnoDB Will choose the first not for NULL The unique index column of is used as the cluster index .
- If neither of them ,InnoDB Will use a 6 Implicit fields of byte long integers ROWID Cluster index is constructed by clustering fields . The ROWID Fields are automatically incremented when a new row is inserted .
All indexes except clustered indexes are called secondary indexes . In the middle InnoDB, The leaf node in the secondary index stores the primary key values of the row . When searching ,InnoDB Use this primary key value to search for row records in the cluster index .
To use a secondary index, you need to retrieve the index twice : First retrieve the secondary index to get the primary key , Then use the primary key to retrieve the records from the primary index . According to the primary key obtained in the secondary index tree species id, The process of retrieving data from the primary key index tree is called Return to the table for query .
Overlay index
Overlay index is a common optimization method . Because when using secondary indexes , We can only get the primary key value , It is equivalent to obtaining data and then querying the primary key index according to the primary key to get the data . But imagine the next situation , on top abc_innodb When the composite index query in the table , If I just need abc Field , Does that mean that when we query the leaf node of the composite index, we can directly return , You don't have to go back to the table . This is the case of overlay index .
边栏推荐
- Detailed explanation of KOA development process
- [test development] file compression project practice
- Redis
- Click change color to change subscript
- [automated test] what you need to know about unittest
- [test development] use case
- Google Chrome browser reports an error: net:: err_ BLOCKED_ BY_ CLIENT
- web自动化测试之webdriver api总结
- Use the visual studio code terminal to execute the command, and the prompt "because running scripts is prohibited on this system" will give an error
- 10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
猜你喜欢
Use ASE encryption and decryption cache encapsulation in Vue project
Promise combined with await
[automated test] what you need to know about unittest
Tree array explanation
MCU: pcf8591 hardware interface
VGA display based on de2-115 platform
Use the visual studio code terminal to execute the command, and the prompt "because running scripts is prohibited on this system" will give an error
重读经典:《End-to-End Object Detection with Transformers》
Manage PC startup items
Redis数据持久化
随机推荐
Forgotten fleeting years
[Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster
Solution to failure to download files by wechat scanning QR code
Redis数据持久化
Milliards de données pour déterminer si un élément existe
Redis data persistence
Common ways to traverse map sets
【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
建模杂谈系列143 数据处理、分析与决策系统开发的梳理
[test development] file compression project practice
The most detailed swing transformer mask of window attachment in history -- Shaoshuai
Hugo blog building tutorial
Common terms of electromagnetic compatibility
Single chip microcomputer: pcf8591 application program
Simple static web page + animation (small case)
dumi 搭建文档型博客
高等数学(第七版)同济大学 习题1-3 个人解答
Single chip microcomputer: MODBUS multi computer communication program design
解答私信@田田WX //2022-6-12 C语言 51单片机LED模拟交通灯
高等数学(第七版)同济大学 习题1-2 个人解答