mysql-(Advanced)-Index

index

Overview

Strengths and weaknesses

  • Advantage
  1. Improve data indexing efficiency and reduce data IO costs
  2. Sort by index, reduce sorting cost and reduce CPU consumption
  • Disadvantage
  1. Take up hard disk space
  2. Increase the maintenance index time and reduce the speed of updating the table

Index structure

MYSQL index structure

  • Indexes are implemented at the MySQL storage engine layer. Each storage engine index is not necessarily the same. Currently, MySQL provides four indexes:
  1. BTREE index: the most common index type, most indexes support B-tree index
  2. HASH index: only supported by Memory engine, easy to use
  3. R-TREE index (spatial index): a special index of the MyISAM engine, mainly used for geospatial data types
  4. Full-text index (full-text index): A special index of the MyISAM engine, mainly used for full-text indexing. InnoDB has been supported since mysql5.6
    Engine support for index

B-tree [ TODO : increase the concept of B-tree, the concept of B-tree will be summarized in other chapters]

B-tree (B-tree)

B+ tree

the difference

B+tree in MYSQL

  • Optimize the classic B+ tree, add a linked list pointer to the adjacent leaf node on the original basis, form a B+ tree with sequential pointers, and improve interval access performance.
    MYSQL-B tree structure

Index classification

Classification (each indexing method needs to clarify the relationship between them)

  • Normal index: This is the most basic index, it has no restrictions
  • Full-text index: Provide effective support for complex word search in string data
  • Unique index: Similar to a normal index , the difference is: the value of the index column must be unique, but null values ​​are allowed. If it is a composite index, the combination of column values ​​must be unique
  • Primary key index: In order to maintain the relationship between the database table and the table, the special unique index does not allow null values. Generally, when the primary key is specified when the table is built, the primary key index will be created . CREATE INDEX cannot be used to create the primary key index. Use ALTER TABLE instead.
  • Single-value index: an index contains only a single column, a table can have multiple single-column indexes
  • Composite index: an index contains multiple columns, and multiple combinations are indexes

Data storage method

  • Clustered index: The physical order of the rows in the table is the same as the logical (index) order of the key values
  • Non-clustered index: the difference with the clustered index is whether the order of the table records is consistent with the order of the index

Index type relationship

Index relationship

Clustered index and non-clustered index

  • When a database record contains multiple fields, a B+ tree can only store the primary key. If the non-primary key field is retrieved, the primary key index will lose its effect and become a sequential search again. At this time, a second set of indexes should be established on the second column to be retrieved. This index is organized by independent B+ trees.Clustering and non-clustering are used to solve the problem of multiple B+ trees accessing the same set of table data
  • InnoDBUsing a clustered index ,MyISMUsing non-clustered index
  • I found a lot of information, almost all use words to describe the difference between the two. It is more intuitive to use pictures of other people’s blog posts to reflect the difference between the two. The picture below shows the difference between clustering and non-clustering.
    Clustered and non-clustered
  • We focus on clustered indexes. It seems that the efficiency of clustered indexes is obviously lower than that of non-clustered indexes, because every time you use the auxiliary index to search, you need to go through two B+ tree lookups. Isn't this superfluous?What are the advantages of clustered indexes?

Advantages of clustered index

  1. Since the row data and the leaf nodes are stored together, the primary key and row data are loaded into the memory together. If the leaf node is found, the row data can be returned immediately. If the data is organized according to the primary key Id,Get data faster
  2. The advantage of using the primary key as a "pointer" for the secondary index instead of using the address value as a pointer is thatReduce the maintenance work of auxiliary indexes when there is row movement or data page split, Using the primary key value as a pointer will make the secondary index take up more space, in exchange for the benefitInnoDB does not need to update this "pointer" in the auxiliary index when moving rows. That is to say, the position of the row will change with the modification of the data in the database, using a clustered indexIt can be guaranteed that no matter how the nodes of the primary key B+ tree change, the auxiliary index tree will not be affected.

Index usage scenarios (TODO)

Index Syntax (TODO)

Index Design Principles (TODO)

  1. Selection table: Create indexes for tables with high query frequency and large data volume
  2. == Select the field ==: Extract from the where clause, select the most commonly used combination with the best filtering effect
  3. Select index: Try to use a unique index, the higher the degree of discrimination, the higher the indexing efficiency
  4. Minimize the number of indexes, The more indexes, the greater the maintenance cost
  5. Use short indexes whenever possible
  6. Use the leftmost prefix, A composite index composed of N columns is equivalent to creating N indexes. If the first few fields that make up the index are used in the where clause of the query, then this query SQL can use the composite index to improve the query effectiveness.

Guess you like

Origin blog.csdn.net/DALAOS/article/details/112863050