Index of MySQL database basic teaching

One, the concept of 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 linked list of the C language through the pointer to the memory address of the data record)
  • 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 the query speed of the database can be accelerated.
  • The index is like a catalog of a wooden book, you can quickly find the content you need according to the page number in the catalog.
  • An 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.

Second, the advantages of indexes

  • 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 the creation of all.
  • When the table is large or the query involves multiple tables, the use of indexes can increase the query speed by thousands of times.
  • The IO cost of the database can be reduced, and the index can also reduce the sorting cost of the database.
  • By creating a unique index, you can ensure the uniqueness of each row of data in the data table.
  • 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.

Three, the shortcomings of the index

  • 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 lnnoDB engine table data file itself is the index file.
  • It takes more time to insert and modify data, because the index also changes accordingly.

Fourth, 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 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 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 index creation.
  • The fields that often appear in the where clause, especially the fields of large tables, should be indexed.
  • Create indexes on the fields that are frequently GROUPO BY and ORDER BY.
  • 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.

V. Classification and creation of indexes

Ready to work

Insert picture description here

5.1 General Index

The most basic index type, there are no 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 first length
characters of the column to create an index, it will help reduce the size of the index file.
It is recommended that the index name end with "_index".

Insert picture description here

  • Create by modifying the table
ALTER TABLE 表名 ADD INDEX 索引名 (列名);

Insert picture description here

  • Specify the index when creating the table
CREATE TABLE 表名( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));

Insert picture description here

5.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 a unique index directly
CREATE UNTQUE INDEX 索引名 ON 表名(列名);

Insert picture description here

  • Create by modifying the table
ALTER TABLE 表名 ADD UNIQUE 索引名(列名);

Insert picture description here

  • Specify when creating the table
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[...],UNTQUE 索引名(列名));

Insert picture description here

5.3 Primary key index

It is a special unique index and must be designated as "PRLINARY 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.

  • Specify when creating the table
CREATE TABLE表名( [ ... ],PRIMARY KEY(列名));

Insert picture description here

  • Create by modifying the table
ALTERTABLE表名ADD PRIMARY KEY(列名);

5.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. The leftmost principle needs to be satisfied, because
the where conditions of the select statement are executed from left to right in turn, so
the field order 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 TABIE 表名(列名1 数据类型,列名2 数据类型,列名3 数据类,INDEX 索引名 (列名1,列名2,列名3));

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

Insert picture description here
Insert picture description here

5.5 Full Text Index (FULLTEXT)

It is suitable for fuzzy query and can be used to retrieve text information in an article. Before MySQT5.6 version,
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 CHAR, VARCHAR or TEXT type columns. Only one full-text index is allowed per table.

  • Create index directly
CREATF FULLTEXT INDEX 索引名 ON 表名 (列名);

Insert picture description here

  • Create by modifying the table
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);

Insert picture description here

  • Specify the index when creating the table
CREATE TABLE 表名(字段1 数据类型[,...], FULLTEXT 索引名 (列名));

The data type can be CHAR, VARCHAR or TEXT

  • Use full-text index query
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST ('查询内容');

Insert picture description here

Six, view the index

show index from 表名;
show index from 表名\G; 竖向显示表索引信息
show keys from 表名;
show keys from 表名\G;

Insert picture description here

The meaning of each field is as follows

Table The name of the table
Non_unique If the index cannot include repeated words, it is 0; if it can, it is 1
Key_name The name of the index
seq_in_index The column number in the index, starting from 1
Co1umn_name Column name
Collation How the column is stored in the index. In MysQL, there are values',' (ascending order) or NULL (no classification)
Cardinality Estimated number of unique values ​​in the index
Sub_part If the column is only partially indexed, it is the number of characters indexed. If the entire column is indexed, it is NOLL
Packed Indicates how the keywords are compressed. If it is not compressed, then NULL
Null If the column contains NULL, it contains YES. If not, the column contains NO
Index_type Used index methods (BTREE, FULLTEXT, HASH, RIREE)
Comment Remarks

Seven, delete the index

  • Delete index directly
DROP INDEX 索引名 ON 表名;

Insert picture description here

  • Modify the table to delete the index
ALTER TABLE 表名 DROP INDEX 索引名;

Insert picture description here
Insert picture description here

  • Delete the primary key index
ALTER TABLE 表名 DROP PRIMARY KEY;

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/113754390