Mysql index summary (1)

definition

Definition: Also called "Key" in mysql, it is a data structure used by the storage engine to quickly find records.

Understanding: The index is like a table of contents of a book, and then find the page number of the corresponding chapter. In mysql, the storage engine uses the index in a similar way, first find the corresponding value in the index, and then find the corresponding data row according to the matching index record.

Example:
select id from t_sign where staff_id='123'

If there is an index on the staff_id column, mysql will use the index to find the row with staff_id 123 and return the data row

Type of index

In mysql, the index is implemented in the storage engine layer instead of the server layer. Therefore, there is no uniform index standard. There are several types of indexes supported by Mysql


Write picture description here


Mainly talk about B-Tree index

Write picture description here

Create a People table

 CREATE TABLE People(
    last_name varchar(50) not null,
    first_name varchar(50) not null,
    dob        date        ,
    gender enum('m','f')  not null,
    key(last_name,first_name,dob)
 )

Write picture description here

Write picture description here

-- 全值搜索
select *from people where last_name='Allen' and first_name='Cuba' and dob='2018-03-07'

-- 匹配最左前缀
 select *from people where last_name='Allen'

-- 匹配列前缀
select *from people where last_name like "A%"

-- 匹配范围(要求是查询姓在Allen和Barrymore之间的人)
select *from people where last_name BETWEEN  "Allen" and "Barraymore"

-- 精确匹配某一列并范围匹配另外一列
select * from people where last_name='Allen' and first_name like "K%"



Restrictions on B-Tree index

1. If you do not start the search according to the leftmost column of the index, it cannot be used. For example, if you directly insert first_name=xxx, you cannot use the index.
2. You cannot skip the columns in the index. The query conditions cannot be last_name=xxx and birthday=xxx, and the middle first_name cannot be skipped, so that it can only be searched according to the index of the first column
. 3. If there is a range query of a column in the query, all the columns on the right cannot use index optimization Find
example: where last_name = 'Allen' and first_name like 'J%' and birthday = '2018-02-15'
this query is valid as long as the first two columns of the index

to sum up

This blog can be regarded as a reading note for "High Performance Mysql". The code in it also has its own practice. Sometimes you can't leave an impression just by reading the book. Only by doing it yourself can you understand what it is talking about.

Guess you like

Origin blog.csdn.net/cd18333612683/article/details/79468440
Recommended