Table of contents
3. Rationale for creating indexes
4. Classification and creation of indexes
● Specify when creating the table
(4) Combined index (single-column index and multi-column index)
(5) Full text index (FULLTEXT)
● Specify the index when creating the table
●Modify the table and delete the index
1. MySQL index
1. The concept of index
●The index is a sorted list, in which the index value and the physical address of the row containing the data are stored (similar to the linked list in C language pointing to the memory address of the data record through the pointer).
●After using the index, you don't need to scan the whole table to locate the data of a certain row, but first find the physical address corresponding to the data in the row through the index table and then access the corresponding data, so the query speed of the database can be accelerated.
●The index is like the catalog of a book, you can quickly find the required content according to the page number in the catalog.
● An index is a method for sorting a column or several column values in a table.
● The purpose of indexing is to speed up the search or sorting of records in the table.
2. The role of the index
● After setting up a suitable index, the database can greatly speed up the query speed by using various fast positioning technologies, which is the main reason for creating all of them.
● 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.
● It 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 of grouping and sorting can be greatly reduced.
Side effects of indexes:
● Indexes require 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 table data files of the InnoDB engine are themselves index files.
● It takes more time to insert and modify data, because the index also changes accordingly.
3. Rationale for creating indexes
Indexes can improve the speed of database queries, but it is not suitable to create indexes in all cases. Because the index itself consumes system resources, if there is an index, the database will perform index query first, and then locate the 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 associated with the primary key of the subtable, which can be quickly located when querying.
● Tables with more than 300 rows should have indexes. If there is no index, the table needs to be traversed, which will seriously affect the performance of the database.
●For tables that are often connected with other tables, indexes should be established on the connection fields.
●Fields with poor uniqueness are not suitable for indexing.
● Fields that are updated too frequently are not suitable for indexing.
●The fields that often appear in the where clause, especially the fields of large tables, should be indexed.
● Indexes should be built on fields with high selectivity.
● Indexes should be built on small fields, and do not build indexes for large text fields or even super-long fields.
4. Classification and creation of indexes
(1) Ordinary index
The most basic index type, without restrictions such as uniqueness.
●Create index directly
CREATE INDEX 索引名 ON 表名 (列名[(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, this will help reduce the size of the index file.
#The index name is recommended to end with "_index".
Check if the index is successful
●Modify table creation
ALTER TABLE 表名 ADD INDEX 索引名 (列名);
● Specify the index when creating the table
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. Unique indexes allow null values (note that they are different from primary keys). 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 a unique index directly:
CREATE UNIQUE INDEX 索引名 ON 表名(列名);
●Modify table creation
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);
● Specify when creating the table
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名));
(3) Primary key index
is a special unique index that must be specified as "PRIMARY KEY". A table can only have one primary key, and null values are not allowed. Adding a primary key will automatically create a primary key index.
● Specify when creating the table
CREATE TABLE 表名 ([...],PRIMARY KEY (列名));
●Modify table creation
ALTER TABLE 表名 ADD PRIMARY KEY (列名);
(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 met, because the where condition of the select statement is executed from left to right in sequence, so when using the select statement to query, the order of the fields used in the where condition must be consistent with the sorting in the composite index, otherwise the index will not take effect.
CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,INDEX 索引名 (列名1,列名2,列名3));
select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3='...';
(5) Full text index (FULLTEXT)
It is suitable for fuzzy query and can be used to retrieve text information in an article. Before MySQL5.6,
the FULLTEXT index can only be used for the MyISAM engine, and after the 5.6 version, the innodb engine also supports the FULLTEXT index. Full-text indexes can be created on columns of type CHAR, VARCHAR, or TEXT. Only one full-text index is allowed per table.
●Create index directly
CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);
●Modify table creation
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);
● Specify the index when creating the table
CREATE TABLE 表名 (字段1 数据类型[,...],FULLTEXT 索引名 (列名));
#数据类型可以为 CHAR、VARCHAR 或者 TEXT
● Use full-text index query
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');
The meaning of each field is as follows:
Table | table name |
Not_unique | 0 if the index cannot include duplicate words; 1 if it can |
Key_name | index name |
Seq_in_index | The column number in the index, starting from 1 |
Column_name | column name |
Collation | How the column is stored in the index. In mysq, there is value 'A' (ascending) or NULL (no sorting) |
Cardinality | An estimate of the number of unique values in the index |
Sub_part | If the column is only partially indexed, the number of characters indexed. NULL if the entire column is indexed |
Packed | Indicates how keywords are compressed. NULL if not compressed |
Null | Contains YES if the column contains NULL. If not, the column contains NO |
Index_type | used index method (BTREE, FULLTEXT, HASH, RTREE) |
Comment | Remark |
6. delete index
● Delete the index directly
DROP INDEX 索引名 ON 表名;
●Modify the table and delete the index
ALTER TABLE 表名 DROP INDEX 索引名;
●Delete the primary key index
ALTER TABLE 表名 DROP PRIMARY KEY;