Types, creation, and deletion of MySQL database indexes

MySQL index

●The index is a sorted list, in which the value of the index and the physical address of the row of the data containing this value are stored (similar to the C language linked list points to the memory address of the data record through the pointer).
●After using the index, you don't need to scan the entire table to locate the data of a row, but first find the physical address corresponding to the row of data through the index table and then access the corresponding data, so it can speed up the query speed of the database.
●The index is like a table of contents of a book, you can quickly find the content you need according to the page number in the table of contents.
● Index is a method of sorting the values ​​of one or several columns in a table.
●The purpose of indexing is to speed up the search or sorting of records in the table.

1.1 The role of the index

●After setting up a suitable index, the database uses various fast positioning technologies to greatly speed up the query speed. This is the main reason for creating an index.
●When the table is very large or the query involves multiple tables, the use of indexes can increase the query speed by thousands of times.
●Can reduce the IO cost of the database, and the index can also reduce the sorting cost of the database.
●By creating a unique index, the uniqueness of each row of data in the data table can be guaranteed.
●Can speed up the connection between the table and the table.
●When using grouping and sorting, the time for grouping and sorting can be greatly reduced.

1.2 Side effects of indexing

●The index requires additional disk space.
For the MyISAM engine, the index file and the data file are separated, and the index file is used to save the address of the data record. The InnoDB engine table data file itself is the index file.
●It takes more time to insert and modify data, because the index will also change accordingly.

1.3 The principle basis for creating an index

Indexes can increase the speed of database queries, but they are not suitable for creating indexes under all circumstances. Because the index itself consumes system resources, if there is an index, the database will first perform an index query and then locate a specific data row. If the index is used improperly, it will increase the burden on the database.
●The primary key and foreign key of the table must have indexes. Because the primary key is unique, the foreign key is related to the primary key of the main table, which can be quickly located when querying.
● Tables with more than 300 rows of records should have indexes. If there is no index, the table needs to be traversed for each query, which will seriously affect the performance of the database.
● For tables that are frequently connected to other tables, an index should be established on the connection field.
●Fields with poor uniqueness are not suitable for indexing.
● Fields that are updated too frequently are not suitable for indexing.
●Fields that often appear in the where clause, especially the fields of large tables, should be indexed.
● Indexes should be built on highly selective fields.
● Indexes should be built on small fields. Do not build indexes for large text fields or even long fields.

Second, the classification and creation of the index

create table member (id int(10),name varchar(10),cardid varchar(10),phone int(11),address varchar(50),remark text);

Insert picture description here
Insert picture description here

2.1 Ordinary index

The most basic index type, there are no restrictions such as uniqueness.

2.1.1 Create index directly

CREATE INDEX 索引名 ON 表名(列名[ (length)]);

#(列名(length)):length是可选项,下同。如果忽略length的值,则使用整个列的值作为索引。如果指定使用列的前length个字符来创建索引,这样有利于减小索引文件的大小。
#索引名建议以“index"结尾。

create index name_index on member (name);
show create table member;
show index from member;
show keys from member;

Insert picture description here

2.1.2 Modify table creation

ALTER TABLE 表名 ADD INDEX 索引名 (列名);

Example

alter table member add INDEX cardid_index (cardid);
select cardid from member;      #自动进行排序

Insert picture description here
Insert picture description here

2.1.3 Specify the index when creating a table

CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));

Insert picture description here

2.2 Unique index

Similar to a normal index, but the difference is that each value of the unique index column is unique. The unique index allows null values ​​(note that it is different from the primary key). If it is created with a composite index, the combination of column values ​​must be unique.添加唯一键将自动创建唯一索引。

2.2.1 Create a unique index directly

CREATE UNIQUE INDEX 索引名 ON 表名 (列名);
create unique index cardid_index on member(cardid);

Insert picture description here

2.2.2 Modify the table method to create an index

ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);

Insert picture description here

2.2.3 Specify the index when creating a table

CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名));

Insert picture description here

2.3 Primary key index

It is a special unique index and must be designated as "PRIMARY KEY". A table can only have one primary key, and no null values ​​are allowed.添加主键将自动创建主键索引。

2.3.1 Specify the index when creating a table

CREATE TABLE 表名([...],PRIMARY KEY (列名));

Insert picture description here

2.3.2 Modify the table method to create an index

ALTER TABLE 表名 ADD PRIMARY KEY (列名);
alter table member add primary key (id);

Insert picture description here

2.4 Composite index (single-column index and multi-column index)

It can be an index created on a single column or an index created on multiple columns. Need to meet the leftmost principle, because the where condition of the select statement is executed from left to right in turn, so in使用select 语句查询时where条件使用的字段顺序必须和组合索引中的排序一致,否则索引将不会生效。

CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型, INDEX 索引名 (列名1, 列名2,列名3));
select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3=';

2.5 Full Text Index (FULLTEXT)

It is suitable for fuzzy query and can be used to retrieve text information in an article. Before MySQL version 5.6, FULLTEXT index can only be used in MyISAM engine. After version 5.6, innodb engine also supports FULLTEXT index.全文索引可以在CHAR、VARCHAR 或者TEXT类型的列上创建。每个表只允许有一个全文索引。

2.5.1 Create index directly

CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);

Insert picture description here

2.5.2 Modify table creation

ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);
alter table member add fulltext remark_index (remark);

Insert picture description here
Insert picture description here

2.5.3 Specify the index when creating a table

CREATE TABLE 表名 (字段1 数据类型[...], FULLTEXT 索引名 (列名));
#数据类型可以为CHAR、VARCHAR 或者TEXT

Insert picture description here

2.5.4 Use full-text index query

SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');

insert into member2 values(1,'zhangsan',123123,123123,'nanjing','this is test!');
insert into member2 values(2,'lisi',456456,456456,'beijing','this is xyw!');
insert into member2 values(3,'wangwu',789789,78979,'shanghai','this is xyw test!');
select * from member2 where match(remark) against('test');

Insert picture description here
Insert picture description here
Insert picture description here

2.6 View Index

show index from 表名;
show keys from 表名;

Insert picture description here
Insert picture description here

2.6.1 The meaning of each field

Table:表的名称。
Non_unique:如果索引不能包括重复词,则为0;如果可以,则为1。
Key_name:索引的名称。
Seq_in_index:索引中的列序号,从1开始。
Column_name:列名称。
Collation:列以什么方式存储在索引中。在MySQL中,有值'A'(升序)或NULL(无分类)。
Cardinality:索引中唯一值数目的估计值。
Sub_part:如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL.
Packed:指示关键字如何被压缩。如果没有被压缩,则为NULL。
Null:如果列含有NULL, 则含有YES。如果没有,则该列含有NO。
Index_type:用过的索引方法( BTREE,FULLTEXT, HASH, RTREE) 。
Comment:备注。

2.7 Delete index

2.7.1 Directly delete the index

DROP INDEX 索引名 ON 表名;

Insert picture description here
Insert picture description here
Insert picture description here

2.7.2 Modify table method to delete index

ALTER TABLE 表名 DROP INDEX 索引名;

Insert picture description here

2.7.3 Delete the primary key index

ALTER TABLE 表名 DROP PRIMARY KEY;

Insert picture description here

Three, case

For example, make a membership card system for a shopping mall. This system has a membership table with the following fields:

会员编号 INT
会员姓名 VARCHAR(10)
会员身份证号码 VARCHAR(18) 
会员电话 INT(11)
会员住址 VARCHAR(50)
会员备注信息 TEXT
create table member (id int(10),name varchar(10),cardid varchar(10),phone int(11),address varchar(50),remark text);
alter table member add primary key (id);
create index name_index on member (name);
create unique index cardid_index on member (cardid);
alter table member add fulltext remark_index (remark);

Insert picture description here

Then the membership number, as the primary key, use the PRIMARY KEY
member name. If you want to build an index, it is the ordinary INDEX
member ID number. If you want to build an index, you can choose UNIQUE (unique, no repetition)
member remarks , If you need to build an index, you can choose FULLTEXT, full-text search.
However, FULLTEXT works best when searching for a long article. Used in relatively short text, if it is only one or two lines, ordinary INDEX can also be used.

Guess you like

Origin blog.csdn.net/IvyXYW/article/details/113645904