Mysql Advanced-Index (Part 1)

Table of contents

Index overview

 Index structure

data structure

Binary tree

 red black tree

 B-Tree

 B+Tree

 Hash

 Index classification

Clustered index & secondary index

Clustered index selection rules:

 specific structure

Index basic syntax

 SQL performance analysis

SQL execution frequency

Slow query log 

profile details

explain



Index overview

introduce:

Index ( index ) is to help MySQL obtain efficiently The data structure of the data ( ordered ) . In addition to data, the database system also maintains data structures that satisfy specific search algorithms. These data structures reference (point to) the data in some way, so that advanced implementations can be implemented on these data structures Search algorithm , this data structure is an index. It can be simply understood that if we arrange the specific order first and then search, the search speed will be greatly improved
Demo:
Table structure and its data

 Suppose we want to execute the SQL statement: select * from user where age = 45;

  • No index situation

is equivalent to a full table scan. When scanning from the first row to the end, it will continue to traverse when data that meets the conditions is encountered, and then scan the entire table. This is equivalent to an array traversal, and the time complexity is very high. Poor performance

  • There is an index situation
If we create an index for this table, assuming that the index structure is a binary tree , then it means that a>The following picture only needs to be compared three times to get 45 greatly improves the search efficiency without the need for global traversal. , becoming similar to a binary search The search algorithm can divide the data range into two rangesThis field creates a binary tree index structure. That is, the age value of the left subtree <root node<right subtree, age

 Features:

  • Advantage
Improve the efficiency of data retrieval , and reduce the IO cost of the database
Sort data through index columns to reduce the cost of data sorting, Reduce CPU consumption (no need to query data frequently with low performance) .
  • Disadvantages                                      

The index column also takes up space.

The index greatly improves the query efficiency, but also reduces the speed of updating the table , such as performing INSERT on the table , UPDATE , DELETE , the efficiency is reduced because < /span> . When updating the table, the index must also be updated

 Index structure

MySQL’s index is implemented at the storage engine layer. Different storage engines have different index structures. MySQL All index structures supported mainly include the following:

How different storage engines support index structures, What we usually call indexes, unless otherwise specified, refers to indexes organized in a B+ tree structure.

data structure

Binary tree

The index structure of MySQL adopts the data structure of a binary tree. The ideal structure is as follows:
But if the primary key is inserted sequentially, then The binary tree will degenerate into a one-way linked list , and the linked list must be traversed every time it is retrieved, which greatly reduces the retrieval efficiency.

Choosing a binary tree as the index structure has the following disadvantages:
When is inserted sequentially, will form a linked list, and the query performance is greatly reduced.
In the case of large data volume,the level is deeper and the retrieval speed is slow

 red black tree

Since the binary tree will always deepen the level, we can think of the red-black tree as a self-balancing binary tree, which can make up for this shortcoming well.In this way, even if data is inserted sequentially, eventually The data structure formed is also a balanced binary tree, but because the red-black tree is also a binary tree, there will also be problems with deeper levels, a>:The structure is as follows

 B-Tree

B-Tree, B-tree is a multi-fork path search tree. Compared with binary trees, each node of B-tree can have multiple branches, that is, multi-fork. Taking a b-tree with a maximum degree (max-degree) of 5 (order 5) as an example, each node of this B-tree can store up to 4 keys and 5 pointers, than The binary tree has three more keys and can take three more branches, that is, five ranges. The degree of a tree is the number of child nodes of a node

 

 Entering the b-tree visual demonstration website can help us understand the evolution process of b-tree. The fifth-order b-tree selected hereB-Tree Visualization

 When inserting 900, since each node can only store four keys, fission will occur at this time. After inserting 900, after the five elements are sorted, the middle element is selected as the parent node, and the middle element is used as the dividing line to split into two nodes

 

 Features:

  • A 5-order B-tree stores up to 4 keys per node, corresponding to 5 pointers.
  • Once the number of keys stored in a node reaches 5, it will fission, and the intermediate elements will split upward.
  • In a B-tree, both non-leaf nodes and leaf nodes store data.

 B+Tree

B+Tree is a variant of B-Tree. Let’s take a b+tree with a maximum degree (max-degree) of 4 (4th order) as an example to take a look at its structural diagram.

The part framed in green is theindex part, onlyIt plays the role of indexing data and does not store data.

The part framed in red is thedata storage part, in which< a i=4>Specific data should be stored in leaf nodes

 When the inserted element reaches fission, a one-way linked list will be formed between the leaf nodes. Note thatThe traditional b+ tree forms a one-way linked list

 Compared with B-Tree, B+Tree mainly has the following three differences:

All data will appear in leaf nodes.

Leaf nodes form a one-way linked list.

Non-leaf nodes only serve to index data, and specific data are stored in leaf nodes.

 MySQL index data structure optimizes the classic B+Tree. On the basis of the original B+Tree, adds a linked list pointer pointing to the adjacent leaf node, which is a doubly linked list, which forms a sequential pointer B+Tree improves the performance of interval access and facilitates sorting.

 Hash

In addition to supporting B+Tree indexes, MySQL also supports an index type---Hash index.

structure:

The hash index uses a certain hash algorithm to convert the key value into a new hash value, maps it to the corresponding slot, and then stores it in the hash table. If there is a hash conflict, a one-way linked list will be formed. After slot, similar to hashtable

 Hash collision situation

 Features:

A. Hash index can only be used forpeer-to-peer comparison(=, in),not Support range query(between, >, <,...)

B. Unable to use index to complete sorting operation

C. The query efficiency is high, usually (there is no hash conflict)Only one retrieval is required, and the efficiency is usually high In B+tree index

 Index classification

MySQL database mainly divides the specific types of indexes into the following categories: primary key index, unique index, regular index, and full-text index.
Classification meaning Features Keywords
primary key index
Index created on the primary key in the table
默认抨创built , Tadano
has a because The primary key can only have
one
PRIMARY
unique index
Avoid duplication of values ​​in a data column in the same table
There can be multiple
UNIQUE
regular index
Quickly locate specific data, (for example, when logging in with a mobile phone verification code, query the user with a mobile phone number. In this scenario, the mobile phone number can be defined as a regular index)
There can be multiple
Full text index
Full-text index looks for keywords in the text rather than comparing values ​​in the index
There can be multiple
FULL TEXT

Shu index&Two-tier index

 In the InnoDB storage engine, according to the storage form of the index, it can be divided into the following two types:

Classification meaning Features
Clustered Index (ClusteredIndex)
Putting data storage and index together, the leaf nodes of the index structure store row data
Must have , but only
There is one
Secondary Index (Secondary Index)
Store data and index separately. The leaf nodes of the index structure are associated with the corresponding primary key, and the corresponding primary key index will point to Real row data
There can be multiple

Clustered index selection rules:

In short, a clustered index is a must. If there is no primary key index, a unique index will be used. If there is neither, it will be randomly generated.

  • If a primary key exists,the primary key index is a clustered index.
  • If there is no primary key, the firstunique (UNIQUE) index will be used as the clustered index .
  • If the table does not have a primary key, or does not have a suitable unique index, thenInnoDB will automatically generate one rowidas a hidden cluster index.

 specific structure

The leaf node of the clustered index hangs the specific data of this row. is equivalent to a first-level pointer, pointing directly to the data itself .
The leaf node of the secondary index hangs the primary key value corresponding to the field value, which points to the clustered index, is equivalent to the secondary pointer .

When we use the secondary index name to query data, the specific execution process is:

. Because the query is based on the name field , so first according to name='Arm' to name Perform matching search in the secondary index of the field . However, only the primary key value 10 corresponding to Arm can be found in the secondary index.
. Since the data returned by the query is all row data , so at this time, you also need to search in the clustered index based on primary key value 10 a> , and finally found the row corresponding to 10 corresponding record 10.
. Finally get the data of this row and return it directly.

 This process is also called table return query

First search the data in the secondary index to find the primary key value, and then go to the clustered index to obtain the data based on the primary key value.
The way of data is called return table query.

Index basic syntax

Create index

CREATE [ UNIQUE | FULLTEXT ] INDEX index_name ON table_name ( index_col_name,... ) ;

 View index

SHOW INDEX FROM table_name ;

 Delete index

DROP INDEX index_name ON table_name ;

 Create a table tb_user and query test data.

The requirements are as follows:

A. The name field is a name field. The value of this field may be repeated. Create an index for this field.

create index idx_user_name on tb_user(name)

 B. The value of the phone number field is non-empty and unique. Create a unique index for this field.

create unique index idx_user_phone on tb_user(phone);

 C. Create joint indexes for profession, age, and status.

create index idx_user_pro_age_sta on tb_user(profession,age,status)

 D. Establish a suitable index for email to improve query efficiency.

create index idx_user_email on tb_user(email);

 View the created index.The naming convention of the index should be idx_table name_column name

Guess you like

Origin blog.csdn.net/weixin_64133130/article/details/134000228