Overview of B-Tree Indexes

    The default index data structure used by the Mysql InnoDB engine is the B+ tree, but the term "B tree" is still used here instead, because Mysql also uses this keyword in create table and other statements, and the index in Mysql is at the storage engine layer. It is not implemented by the server layer, so there is no unified indexing standard, and the underlying storage engine may also use different storage structures.
    Storage engines use B-tree indexes in different ways. For example, MyISAM uses prefix compression to make indexes smaller, but InnoDB stores the data in its original format. Another example is the MyISAM index refers to the indexed row by the physical location of the data, while InnoDB refers to the indexed row based on the primary key.
    A B-tree usually means that all values ​​are stored in order and that every leaf is the same distance from the root. Because B-trees are stored sequentially for indexed columns, they are great for finding range data.
    Suppose there is such a table:
CREATE TABLE People(
    last_name   varchar(50)    not null,
    first_name  varchar(50)    not null,
    dob         date           not null,
    gender      enum('m', 'f') not null,
    key(last_name, first_name, dob)
);

    Here for each row of data in the table, the index contains the values ​​of the last_name, first_name and dob columns. The following diagram shows how the index organizes the storage of data.

    An index sorts multiple values ​​according to the order of the columns when the index was defined in the CREATE TABLE statement. B-tree indexes are suitable for full key value, key value range or key prefix lookup, where key prefix lookup is only suitable for lookups according to the leftmost prefix. The indexes mentioned here are valid for the following types of queries.
    (1) Full value matching refers to matching all columns in the index.
    (2) Match the leftmost prefix, for example the index above can be used to find all people with the last name Allen.
    (3) Match the column prefix, that is, only match the beginning of the value of a certain column. For example the above index can be used to find all people with a surname starting with J.
    (4) Matching range values, such as the above index can be used to find all people whose last name is between Allen and Barrymore.
    (5) Exactly match a column and range match another column, for example, the above index can be used to find all people whose last name is Allen and whose first name starts with K.
    Because the nodes in the index tree are ordered, the index can also be used for ORDER BY operations. As long as it meets the query types listed above, the index can meet the corresponding sorting requirements.
    However, the B-tree index also has some limitations:
    (1) If the search is not started according to the leftmost column of the index, the index cannot be used.
    (2) Columns in the index cannot be skipped. For example, the index above cannot be used to find people whose last name is Smith and who was born on a specific date. If you do not specify first_name, mysql can only use the first column of the index.
    (3) If there is a range query of a certain column in the query, all columns to the right of it cannot be searched using index optimization. For example, the query condition "WHERE last_name='Smith' AND first_name LIKE 'J%' AND dob='1976-12-23'" will only use the first two columns of the index, because the LIKE here is a range condition (but the server can use the remaining columns for other purposes).
    It can be seen that the order of index columns is very important and should be considered when optimizing performance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326071290&siteId=291194637