MySQL-Index Detailed Explanation (2)

♥️Author : Xiao Liu at Station C

♥️Personal homepage: Xiao Liu's homepage

♥️ Share cloud computing network operation and maintenance classroom notes every day, hard work may not be rewarded, but there will be gains, come on! Work hard together for a better life!

♥️The tree is thousands of feet tall, and the fallen leaves return to the roots. Life is not easy, but the true love in the world

Table of contents

3 Index classification

3.1 Index classification

 3.2 Clustered Index & Secondary Index

 Clustered index selection rules:

 The specific process is as follows:

4 Index Syntax

1). Create index

2). View index

3). Delete index


3 index classification

3.1 Index classification

In the MySQL database, the specific types of indexes are mainly divided into the following categories: primary key index, unique index, regular index, and full-text index.

 3.2 Clustered Index & Secondary Index

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

 Clustered index selection rules :

If a primary key exists, the primary key index is a clustered index.

If no primary key exists, the first unique ( 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, InnoDB will automatically generate a rowid as a hidden clustered index
lead.
The specific structure of clustered index and secondary index is as follows:
The data of this row hangs under the leaf node of the clustered index.
The leaf node of the secondary index hangs the primary key value corresponding to the field value.
Next, let's analyze what the specific search process looks like when we execute the following SQL statement.

 The specific process is as follows :

. Since the query is based on the name field, first perform a matching query in the secondary index of the name field based on name='Arm'
try to find. 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 * , so at this time, it is also necessary to search for the record corresponding to 10 in the clustered index according to the primary key value 10 , and finally
Finally find the row corresponding to 10 .

 .Finally get the data of this row, just return it directly.

Back-to-table query: This kind of search data in the secondary index first, find the primary key value, and then go to the clustered index according to the primary key value to get
The way of data is called back table query

4 Index Syntax

1). Create an index

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

2). View index

SHOW INDEX FROM table_name ;

3). Delete the index

DROP INDEX index_name ON table_name ;
Case presentation :
First create a table tb_user and query test data.
create table tb_user(
id int primary key auto_increment comment '主键',
name varchar(50) not null comment '用户名',
phone varchar(11) not null comment '手机号',
email varchar(100) comment '邮箱',
profession varchar(11) comment '专业',
age tinyint unsigned comment '年龄',
gender char(1) comment '性别 , 1: 男, 2: 女',
status char(1) comment '状态',
createtime datetime comment '创建时间'
) comment '系统用户表';
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('吕布', '17799990000', '[email protected]', '软件工程', 23, '1',
'6', '2001-02-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('曹操', '17799990001', '[email protected]', '通讯工程', 33,
'1', '0', '2001-03-05 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('赵云', '17799990002', '[email protected]', '英语', 34, '1',
'2', '2002-03-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('孙悟空', '17799990003', '[email protected]', '工程造价', 54,
'1', '0', '2001-07-02 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('花木兰', '17799990004', '[email protected]', '软件工程', 23,
'2', '1', '2001-04-22 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('大乔', '17799990005', '[email protected]', '舞蹈', 22, '2',
'0', '2001-02-07 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('露娜', '17799990006', '[email protected]', '应用数学', 24,
'2', '0', '2001-02-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('程咬金', '17799990007', '[email protected]', '化工', 38,
'1', '5', '2001-05-23 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('项羽', '17799990008', '[email protected]', '金属材料', 43,
'1', '0', '2001-09-18 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('白起', '17799990009', '[email protected]', '机械工程及其自动
化', 27, '1', '2', '2001-08-16 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('韩信', '17799990010', '[email protected]', '无机非金属材料工
程', 27, '1', '0', '2001-06-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('荆轲', '17799990011', '[email protected]', '会计', 29, '1',
'0', '2001-05-11 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('兰陵王', '17799990012', '[email protected]', '工程造价',
44, '1', '1', '2001-04-09 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('狂铁', '17799990013', '[email protected]', '应用数学', 43,
'1', '2', '2001-04-10 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('貂蝉', '17799990014', '[email protected]', '软件工程', 40,
'2', '3', '2001-02-12 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('妲己', '17799990015', '[email protected]', '软件工程', 31,
'2', '0', '2001-01-30 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('芈月', '17799990016', '[email protected]', '工业经济', 35,
'2', '0', '2000-05-03 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('嬴政', '17799990017', '[email protected]', '化工', 38, '1',
'1', '2001-08-08 00:00:00');
INSERT INTO tb_user (name, phone, email, profession, age, gender, status,
createtime) VALUES ('狄仁杰', '17799990018', '[email protected]', '国际贸易',
30, '1', '0', '2007-03-12 00:00:00');
The data inserted in the table structure is as follows:

After the data is ready, next, we will complete the following requirements:
A. The name field is a name field, and 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-null and unique, and a unique index is created for this field.
CREATE UNIQUE INDEX idx_user_phone ON tb_user(phone);
C. Create a joint index for profession , age , status .
CREATE INDEX idx_user_pro_age_sta ON tb_user(profession,age,status);
D. Build an appropriate index for email to improve query efficiency
CREATE INDEX idx_email ON tb_user(email);
After completing the above requirements, we will check all the index data of the tb_user table.

♥️Following is the driving force for my creation

♥️Like, is the greatest recognition for me

♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone

Guess you like

Origin blog.csdn.net/lzl10211345/article/details/131131446
Recommended