MySQL index, transaction and storage engine (detailed interpretation)

MySQL index, transaction and storage engine (detailed interpretation)

One, MySQL index

(1) The concept of index

●The index is a sorted list, in which the value of the index and the physical address of the row containing the value of the data are stored (similar to the C language linked list points to the memory address of the data record through a pointer).
●After using the index, you can locate the data of a row without scanning the entire table, 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.

(2) The role of index

●After setting up the appropriate index, the database uses various fast positioning technologies to greatly speed up the query speed, which is the main reason for creating all.
●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. 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 also changes accordingly.

(3) Principle basis for index creation

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 not used properly, 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 child table, which can be quickly located during query.
● Tables with more than 300 rows of records should have indexes. If there is no index, the table needs to be traversed again, which will seriously affect the performance of the database.
● For tables that are frequently connected with 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.

(4) Classification and creation of indexes

1. Ordinary index

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

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

●Directly create index
CREATE INDEX index name ON table name (column name [(length)]);
#(column name (length)): length is optional, the same below. If the value of length is omitted, the value of the entire column is used as the index. If you specify to use the length characters before the column to create the index, it will help reduce the size of the index file.
#Index name is recommended to end with "_index".

show create table member; #查看索引具体情况

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

●修改表方式创建
ALTER TABLE 表名 ADD INDEX 索引名 (列名);

Insert picture description here

Insert picture description here

●创建表的时候指定索引
CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));

2. Unique index

Similar to a normal index, but the difference is that each value of a 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. Adding a unique key will automatically create a unique index.

●直接创建唯一索引:
CREATE UNIQUE INDEX 索引名 ON 表名(列名);
create unique index cardid_index on member (cardid);
●修改表方式创建
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);
show create table member; #查看索引具体情况

Insert picture description here

Insert picture description here

●创建表的时候指定
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名));

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. Adding a primary key will automatically create a primary key index.

●创建表的时候指定
CREATE TABLE 表名 ([...],PRIMARY KEY (列名));
●修改表方式创建
ALTER TABLE 表名 ADD PRIMARY KEY (列名); 

Insert picture description here
Insert picture description here

4. Combined 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. The leftmost principle needs to be satisfied. Because the where conditions of the select statement are executed from left to right in turn, the order of the fields used in the where conditions must be consistent with the order in the composite index when using the select statement to query, otherwise the index will not take effect.

CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,INDEX 索引名 (列名1,列名2,列名3));
alter table member add index mem_index (phone,address);

Insert picture description here

select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3='...';

Insert picture description here

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. Full-text indexes can be created on columns of CHAR, VARCHAR, or TEXT type. Only one full-text index is allowed per table.

●直接创建索引
CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);
●修改表方式创建
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);

Insert picture description here
Insert picture description here

●创建表的时候指定索引
CREATE TABLE 表名 (字段1 数据类型[,...],FULLTEXT 索引名 (列名));
#数据类型可以为 CHAR、VARCHAR 或者 TEXT
●使用全文索引查询
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');

Insert picture description here

6. View the index

show index from 表名;
show keys from 表名;
show index from member\G; #竖列显示

The meaning of each field is as follows:

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

Insert picture description here

7, delete the index

show create table member; 
#删之前先查一下

Insert picture description here

●直接删除索引
DROP INDEX 索引名 ON 表名;

Insert picture description here

Insert picture description here

●修改表方式删除索引
ALTER TABLE 表名 DROP INDEX 索引名;

Insert picture description here

●删除主键索引
ALTER TABLE 表名 DROP PRIMARY KEY;

Insert picture description here

Case: For example, build a membership card system for a shopping mall. This system has a membership table with the following fields:
membership number INT
member name VARCHAR(10)
member ID number VARCHAR(18)
member phone VARCHAR(10)
member address VARCHAR(50)
member remarks information TEXT
then member number, as the primary key , Use the PRIMARY KEY
member name, if you want to build an index, then it is the ordinary INDEX
member ID number, if you want to build an index, then you can choose UNIQUE (unique, no duplicate)
member remarks, if you need to build an index , You can choose FULLTEXT, full text search.
But 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.

To be continued

Guess you like

Origin blog.csdn.net/weixin_51573771/article/details/113188467