Index classification | Index creation, deletion | Index design principles

1. Index classification

1.1 Index classification from different angles

  • Functional logic: common index, unique index, primary key index, full-text index;
  • Physical implementation: clustered index, non-clustered index;
  • The number of active columns: single column index, joint index;

1.2 Overview of different indexes

1) Ordinary index

  • It is used to improve retrieval efficiency and can be created on any data type;

2) Unique index

  • Use the UNIQUE parameter to set the index as a unique index;
  • It is required that the role column can be empty, but it must be unique and cannot be repeated;
  • A data table can have multiple unique indexes;
  • Unique index can quickly locate records;

3) Primary key index

  • A special unique index requires that the primary key index cannot be empty, which can be regarded as using the UNIQUE parameter + NOT NULL parameter to set the index as the primary key index;
  • There can only be one primary key index in the data table, and the primary key index is the clustered index;

4) Single column index

  • Indexes acting on a single field;
  • There can be multiple single-column indexes in the data table;

5) Joint index

  • Indexes acting on multiple fields;
  • When using the joint index, follow the leftmost prefix principle;

6) Full text index

  • You can use FULL TEXT to set full-text index, also known as full-text search, which is a key technology used by search engines;
  • Use word segmentation technology to count keyword frequency and importance, and filter expected search results;
  • Suitable for large text datasets;
  • When querying string-type fields with a large amount of data, using full-text indexes can improve query speed;

2. Create|delete index

2.1 Create an index implicitly when creating a table

  • When creating a table by adding constraints, such as primary key constraints, unique constraints, foreign key constraints, the corresponding index will be created by default;

2.2 Explicitly create indexes when creating tables

  • Create indexes explicitly through parameters such as INDEX when creating a table;
  • Basic syntax:
CREATE TABLE table_name 
[col_name data_type]
[UNIQUE | FULLTEXT | SPATIAL] [INDEX | KEY] [index_name] (col_name [length]) [ASC |
DESC]
  • UNIQUE , FULLTEXT and SPATIAL are optional parameters, representing unique index, full-text index and spatial index respectively;
  • INDEX and KEY are synonyms, both have the same function, and are used to specify the creation of an index;
  • index_name specifies the name of the index, which is an optional parameter. If not specified, MySQL defaults to col_name as the index name;
  • col_name is the field column that needs to be indexed, which must be selected from multiple columns defined in the data table;
  • length is an optional parameter, indicating the length of the index, and only string type fields can specify the index length;
  • ASC or DESC specifies ascending or descending order of index value storage.
  • Ways to view the index: 1) Way 1: show create table 表名;2) Way 2: show index from 表名;3) Way 3:explain SQL语句,如查询语句;
  • Note:
    1) When creating a unique index, a unique constraint is automatically added to the corresponding field, and vice versa;
    2) To create a primary key index, you need to add it in the form of a primary key constraint;
    3) To create a full-text index, you need to pay attention to only acting on CHAR, Fields of type VARCHAR and TEXT;
    4) Pay attention to the joint index. The order of the fields affects the composition of the final B+ tree. The keywords in the B+ tree data page are sorted and compared in turn according to the order of these fields. When using the joint index, you need to follow the most The left prefix matching principle, otherwise the index will not hit;

2.3 Explicitly create indexes after creating tables

2.3.1 Method 1: ALTER TABLE ADD

ALTER TABLE table_name 
ADD [UNIQUE | FULLTEXT | SPATIAL] [INDEX | KEY][index_name] (col_name[length],...) [ASC | DESC]

2.3.2 Method 2: CREATE INDEX ON

CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
ON table_name (col_name[length],...) [ASC | DESC]

2.4 Delete index

2.4.1 Method 1: ALTER TABLE DROP

ALTER TABLE table_name DROP INDEX index_name;

2.4.2 Method 2: DROP INDEX ON

DROP INDEX index_name ON table_name;

2.4.3 Precautions for deleting indexes

  • The index name of the primary key index is always PRIMARY KEY;
  • The unique index with AUTO _INCREMENT constraint cannot be dropped, because AUTO _INCREMENT acts on the primary key constraint or unique constraint column;
  • When a column that builds an index is deleted, the column is also deleted from the index, and when all the columns that make up the index are deleted, the index is also deleted;

3. New features of MySQL 8.0

  • Support descending index, hidden index;

3.1 Descending Index

  • The keywords of the index can be set in descending order through the DESC parameter when creating the index;
  • The performance of fields with descending order requirements has been improved;

3.2 Hidden Index

  • You can set whether the index is hidden by specifying the INVISIBLE parameter when creating the index;
  • The optimizer cannot use hidden indexes;
  • The operation of first setting the index as a hidden index and then deleting the index is called soft deletion;
  • The primary key index cannot be set as a hidden index;
  • Note: Although hidden indexes cannot be used by the optimizer, they are updated in real time like normal indexes, which will still affect the performance of data update operations;

3.2.1 Create a hidden index

3.3.1.1 Setting Index Visibility When Creating a Table

CREATE TABLE tablename(
propname1 type1[CONSTRAINT1],
propname2 type2[CONSTRAINT2],
……
propnamen typen,
INDEX [indexname](propname1 [(length)]) INVISIBLE
);

3.3.1.2 Setting Index Visibility After Creating a Table

Method 1: ALTER TABLE ADD

ALTER TABLE tablename
ADD INDEX indexname (propname [(length)]) INVISIBLE

Method 2: CREATE INDEX ON

CREATE INDEX indexname
ON tablename(propname[(length)]) INVISIBLE;

3.2.2 Modify index visibility

ALTER TABLE tablename ALTER INDEX index_name INVISIBLE; #切换成隐藏索引
ALTER TABLE tablename ALTER INDEX index_name VISIBLE; #切换成非隐藏索引

4. Index Design Principles

4.1 Situations suitable for creating indexes

  • 1) Fields with unique restrictions;
  • 2) Fields often used as WHERE filter conditions;
  • 3) Frequently GROUP BY and ORDER BY fields;
  • 4) WHERE condition field of UPDATE and DELETE;
  • 5) The DISTINCT field needs to create an index;
  • 6) When multi-table JOIN connection operation, precautions for creating indexes: ①Create indexes for WHERE filter condition fields; ②Create indexes for connection fields;
  • 7) Use the data type of the column to create an index with a small memory footprint;
  • 8) To create an index using a string prefix, you can use count(distinct left(column name, index length))/count(*) to determine the prefix length;
  • 9) Columns with high discrimination (high hashability) are suitable as indexes;
  • 10) Put the most frequently used columns to the left of the joint index;
  • 11) In the case of creating indexes for multiple fields, joint indexes are better than single-value indexes;

4.2 Situations that are not suitable for index creation

  • 1) Do not set indexes for fields that are not used in where;
  • 2) It is best not to use indexes for tables with a small amount of data;
  • 3) Do not create indexes on columns with a large amount of repeated data;
  • 4) Avoid creating too many indexes on frequently updated tables;
  • 5) It is not recommended to use unordered values ​​as indexes;
  • 6) Delete indexes that are no longer used or rarely used;
  • 7) Do not define redundant or duplicate indexes;

Guess you like

Origin blog.csdn.net/qq_43665602/article/details/131612499
Recommended