A quick introduction to MySQL index

This article first appeared on https://antoniopeng.com

What is an index

MySQL official definition of index: Index (Index) is a data structure that helps MySQL efficiently obtain data. Therefore, the index is essentially a data structure . The purpose of indexing is to improve query efficiency, which can be compared with such forms as dictionaries and book catalogs.

It can be simply understood as the row quickly find data structure good sequence . In addition to the data, the system also maintains a database to meet the specific data structure algorithms query , these data point to data structures in some way. In this way, advanced search algorithms can be implemented on these data structures, which is an index.

In general, the index is relatively large, it can not all be stored in memory, so the index tends to index file form stored on disk.

The indexes usually mentioned are B-tree indexes unless otherwise specified. Among them, clustered index, secondary index, covering index, prefix index, and unique index all use B-tree by default.

Command can show index from table_nameview the table of the index case

Advantages and disadvantages of indexes

advantage

  • Similar to the bibliographic index of the university library, improve the efficiency of data retrieval and reduce the IO cost of the database
  • Sort data by index column, reduce the cost of sorting data, and thus reduce CPU consumption

Disadvantages

  • The index is actually a table, the table holds the primary key and the index field, and points to the record of the entity table, so the index column also takes up space
  • Although the index greatly improves the query efficiency, but reduces the speed of updating the table, such as insert, update, and delete operations. Because when updating the table, MySQL not only saves the data, but also saves the index column fields that the index file updates each time, and after the update operation, the information of the corresponding field index is updated
  • Indexing is only one factor to improve query efficiency. If your MySQL has a large number of data tables, you need to spend time researching to build the best index or optimize query statements

Index classification

The index is mainly divided into the following three categories:

  • Single value index: an index contains only a single column, a table can have multiple single value indexes
  • Unique index: the value of the index column must be unique, but null values ​​are allowed, and the primary key is the unique index
  • Compound index: an index contains multiple columns

The index structure is divided into the following four categories:

  • BTREE index
  • Hash index
  • Full-Text Index
  • R-Text Index

Basic syntax usage

Create index

Create a normal index

CREATE INDEX index_name ON table_name (column_name(length))

You can also add indexes by modifying the table structure

ALTER TABLE table_name ADD INDEX index_name (column_name(length))

Note: If it is a field of type char or varchar, length can be less than the actual length of the field; if it is a type of blob or text, length must be specified

Other ways to create indexes

Add primary key index

ALTER TABLE table_name ADD PRIMARY KEY (column_name)

Add a unique index

ALTER TABLE table_name ADD UNIQUE (column_name)

Add full text index

ALTER TABLE table_name ADD FULLTEXT (column_name)

Add common index

ALTER TABLE table_name ADD INDEX index_name (column_name)

Add composite index

ALTER TABLE table_name ADD INDEX index_name (column_name_1, column_name_2, column_name_3)

Delete index

DROP INDEX index_name ON table_name

View index

SHOW INDEX FROM table_name

Need to build an index

  • The primary key automatically creates a unique index
  • As a frequent query fields
  • Fields associated with other tables in the query: foreign key relationships are indexed
  • High concurrency tend to create lower composite index
  • Query sort of field. If the sort field is accessed by index, the sort speed will be greatly improved
  • Query packet fields

No need to index

  • Too few records in the table (too little data, MySQL can handle it by itself)
  • Frequently add, delete, and modify tables
  • Fields with duplicate and evenly distributed data, such as nationality, gender, etc.
  • Frequently updated fields are not suitable for indexing
  • The fields that are not used in the Where condition are also not suitable for indexing

Guess you like

Origin blog.51cto.com/14791564/2487352