当前位置:网站首页>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 :

  1. Define a primary key on a table PRIMARY KEY,InnoDB Reference the primary key index as a cluster index .
  2. 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 .
  3. 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 .

原网站

版权声明
本文为[leejie*.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130415385851.html