MySQL index (advanced)

MySQL index

I. Overview

MySQL's official definition of
index is: index (index) is a data structure (ordered) that helps MySQL obtain data efficiently . In addition to data, the database system also maintains data structures that meet specific search algorithms. These data structures reference (point to) data in a certain way , so that advanced search algorithms can be implemented on these data structures. This data structure is an index. . As shown in the following schematic diagram:
Supplementary knowledge: the specific of the binary tree storage node is left small and right large . Those smaller than the node data are placed on the left, and those larger than the node data are placed on the right.
For example, query data 3 requires full-text search to find , But if you want to find data 3 after indexing, you only need to search 3 times to find it. Then how does the index find the corresponding data? As shown in the figure, it points to the data by reference.
Insert picture description here
On the left is the data table. There are two columns of seven records. The leftmost one is the physical address of the data record (note that logically adjacent records are not necessarily physically adjacent on the disk). In order to speed up the search of Col2, a binary search tree as shown on the right can be maintained. Each node contains an index key and a pointer to the physical address of the corresponding data record, so that the corresponding data can be quickly obtained by using the binary search.

Generally, the index itself is also very large, and it is impossible to store all of it in memory, so the index is often stored on the disk in the form of an index file. Indexes are the most common tool used in databases to improve performance.

Two index structure

1 Index structure

Indexes are implemented in the storage engine layer of MySQL , not at the server layer. Therefore, the indexes of each storage engine are not necessarily the same, and not all storage engines support all index types. MySQL currently provides the following four indexes:

(1) BTREE index: The most common type of index, most of the indexes support B-tree index.

(2) HASH index: Only supported by the Memory engine, with simple usage scenarios.

(3) R-tree index (spatial index): Spatial index is a special index type of MyISAM engine. It is mainly used for geospatial data types. It is usually used less and will not be specially introduced.

(4) Full-text (full -text index): Full-text index is also a special index type of MyISAM, which is mainly used for full-text index. InnoDB supports full-text index from Mysql5.6 version.


MyISAM, InnoDB, Memory three storage engine support for various types of indexes

BTREE index Supported Supported HASH index Not supported Not supported Supported R-tree index Not supported Supported Not supported Full-text supported after version 5.6 Supported Not supported

The indexes we usually talk about, unless otherwise specified, refer to the indexes organized by the B+ tree (multiple search tree, not necessarily binary) structure. Among them, the clustered index, composite index, prefix index, and unique index all use B+tree indexes by default, which are collectively referred to as indexes.

2 Index advantages and disadvantages

Advantage

1) Similar to the catalog index of books, it improves the efficiency of data retrieval and reduces the IO cost of the database.

2) Sort data by index column, reduce the cost of data sorting, and reduce CPU consumption.

Disadvantage

1) In fact, the index is also a table, the primary key and index fields are stored in the table, and point to the record of the entity class, so the index column also takes up space.

2) Although the index greatly improves the query efficiency, it also reduces the speed of updating the table, such as INSERT, UPDATE, and DELETE on the table. Because when updating the table, MySQL not only saves the data, but also saves the index file every time the index file is updated to add the index column field, it will adjust the index information after the key value changes brought about by the update.

Three BTREE structure and demonstration process

Btree is also called multi-way balanced search tree. An m-forked Btree has the following characteristics:
(1) Each node in the tree contains at most m children
(2) Except for the root node and leaf nodes, each node has at least [ceil (M/2)] Children
(3) If the root node is not a leaf node, it has at least 2 children
(4) All leaf nodes are in the same layer
(5) Each non-leaf node has n keys and n+1 Pointer composition, where [ceil(m/2)-1]<= n <= m-1, when n>m-1, the intermediate node splits into parent nodes, and the two nodes split into child nodes.
Take a 5-point number as an example , As shown in the figure:
Number of keys: 2<=n<=4, when n>4, the intermediate node is split into parent nodes, and the nodes on both sides split into child nodes. The
data is: CNGAHEKQMFWLTZDPRXYS
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Four B+Tree and B+Tree structure in MySQL

(1) B+Tree is a variant of BTree. The difference between the two is as follows:
① The number of n forks B+Tree contains up to n keys, and BTree contains up to n-1 keys.
② The leaf nodes of B+Tree store all key information. , Arranged in order of key size

The structure diagram of the index part B+Tree of all non-leaf nodes that can be regarded as keys is as follows:
Insert picture description here

(2) The MySQL index data structure optimizes the classic B+Tree, adding a linked list pointer to adjacent leaf nodes, forming a B+Tree with sequential pointers, improving the access performance of the interval.
Schematic diagram of the structure of MySQL's B+Tree:
Insert picture description here

Classification of Five Indexes

(1) Single-value index: that is, an index contains only a single column, and a table cannot have multiple single-column indexes
(2) Unique index: the value of the index column must be unique, but control is allowed, such as mobile phone number, bank card number, etc. It must be unique
(3) Composite index: that is, an index contains multiple columns, such as mobile phone number and bank card number. If the data in a table has multiple fields always appearing at the same time when querying, these fields can be used as a composite index

Six index syntax

1 Create an index

(1) Grammar

CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index name
[USING index type]
ON the name of the table to create the index (the field to create the index);

(2) Case

createt index idx_city_name on city(city_name);

2 View index

(1) Grammar

SHOW INDEX FORM to create the table name of the index;

(2) Case

show index from city;

3 Delete index

(1) Grammar

DROP INDEX index name ON table name;

(2) Case

drop index idx_city_name on city;

4 ALTER command to create an index

(1) Grammar

① alter table table name add primary key (field); add primary key index, which must be unique and cannot be null
② alter table table name add unique index name (field); unique index can be null
③ alter table table name add index index name ( Field); add ordinary index, it can appear multiple times
④ alter table table name add fulltext index name (field); add full text index

5 Use of Index

(1) Full-value matching
Specify specific pointers for all columns in the index. In this case, the index takes effect and the execution efficiency is high.
Insert picture description here
(2) The leftmost prefix rule (climbing the stairs)
If the index introduces multiple columns, the leftmost prefix rule must be observed. The query starts from the leftmost column of the index and does not skip the indexed columns, that is, the column on the left side of the index is included ( Similar to the stairs on the first floor).
Insert picture description here
(3) Try to use a covering index (including only the fields of the index column) to avoid select * and generate back-to-table queries.
Insert picture description here
Additional points:

using index: appears when using a covering index.
using where: when looking up and using an index, you need to go back to the table to query data.
using index condition: search and use the index, you need to go back to the table to query the data
using index; using where: search and use the index, but the required data is in Can be found in the index column, otherwise it will return to the table query

6 Index failure

(1) Range query, the column index on the right is invalid
Insert picture description here

(2) Use arithmetic operations in the index column, the index is invalid
Insert picture description here
(3) String type does not add single quotation marks, the index is invalid
Insert picture description here
(4) or index is invalid
Insert picture description here
(5) like "**%** conditions" Index invalidation
Insert picture description here
Use a covering index to solve the ambiguity The index failure problem of
Insert picture description here
(6) null and is null indexes sometimes fail, and sometimes they do not. Mainly look at the table data. If most of the data volume has value, then null will go to index, if most of them are null, go is null index
(7) In take index, not in index is invalid
Insert picture description here

Seven index design principles

(1) Index the tables with high query frequency and large amount of data.
(2) The best candidate column of the index field is obtained from the conditions of the where clause. If there are many combinations in the where clause, select common, The combination of columns with the best filtering effect becomes a composite index.
(3) Use a unique index. The higher the degree of discrimination, the higher the efficiency of using the index.
(4) The index is not the better. The more the index, the corresponding DML such as insert, update, and delete. For tables that operate more frequently, it will increase maintenance costs and reduce the efficiency of DML operations.
(5) Use a short index. After the index is created, the hard disk is used for storage. Improving the IO efficiency of index access can improve the overall access efficiency. The index field is relatively short, so a given size of storage block can store more index values , To improve the efficiency of mysql access to IO.
(6) Using the leftmost prefix, a composite index composed of N columns is equivalent to creating N indexes. When the where clause uses the fields of the composite index, the query efficiency will be improved.
E.g:

创建复合索引:
create index idx_name_email_status on tb_seller(name,email,status);
相当于:
	对name创建索引;
	对name,email创建索引;
	对name,email,Status创建索引;

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114787376
Recommended