mysql index

1 Where to use the index

As long as the query operation is designed to use the index, if the index is not used, it is necessary to traverse the whole table.

For inquiries, please refer to the following blog:

https://www.cnblogs.com/whgk/p/6149009.html

2 Types of mysql indexes and their creation commands

2.1 Primary key index

A special kind of unique index that does not allow nulls because the primary key itself does not allow nulls.

ALTER TABLE <table_name> ADD PRIMARY KEY (<column_name>);

2.2 Unique Index

The values ​​of the columns being indexed must be distinct, that is, must be unique.

ALTER TABLE <table_name> ADD UNIQUE(<column_name>);

2.3 Ordinary index

The most basic index, with no restrictions.

ALTER TABLE <table_name> ADD INDEX <index_name>(<column_name>);

2.4 Full-text indexing

Full-text indexing of text data for a keyword search and then returning rows containing that keyword.

ALTER TABLE <table_name> ADD FULLTEXT (<column_name>);

2.5 Joint Index

To create an index on multiple columns, first create an index on the first column, then create an index on the second column of all rows with the same first column, and so on. On the basis of the previous column, the joint index is invalid when the latter column is used alone as the query condition.

ALTER TABLE <table_name> ADD INDEX <index_name>(<column1_name>, <column2_name>);

3 MySQL's two index structures

3.1 B+ tree index

It is not only suitable for single-record query, but also suitable for range query, because all records are located at the bottom node, and are stored in order using linked list connection, in this case, as long as the first record in the return is found, and then traversed to find All records that meet the conditions are displayed.

3.2 hash index

It is only suitable for single-record query, because each record is distributed by hash.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686925&siteId=291194637
Recommended