MySQL indexes are there?

What indexes are?

Build MySQL MySQL index for efficient operation is very important, the index can greatly improve the retrieval speed of MySQL.

For example, if a reasonable design and use MySQL index is a Lamborghini, then there is no MySQL design and use of the index is a tricycle.

Sub-index separate index and combination index. Separate index, i.e. only contains a single column index, a table can have multiple separate index, but this is not a composite index. Combination index, i.e. an index containing a plurality of columns.

When you create an index, you need to make sure that the index is a condition in SQL query applications (usually as a condition of the WHERE clause).

In fact, the index is also a table that holds the primary key and index fields, and point to record the entity table.

The above are talking about the benefits of using the index, but too much use of the index will lead to abuse. Therefore, the index also has its disadvantages: Although the index greatly increased query speed, while it will reduce the speed of updating the table, such as table INSERT, UPDATE, and DELETE. Because when you update table, MySQL is not only to save the data, but also save about index file.

Indexing files index takes up disk space.

Category Index

First, according to the classification listed property

A separate index

The index to a single column field in the table created

Joint index

To index multiple columns field combination table created, use the index in the query sequence from left field to take effect following the leftmost matching principle.

Joint index and a separate index also includes:

General index

Non-primary key, non-unique index of the column

Primary key index

Based on the table's primary key generated automatically into the index, if not to the table defines the primary key, looks for whether there is non-empty, shaping the table, a unique index as its primary key (which can be viewed through select _rowid from table name), if not satisfied will rowid implicitly generated as a primary key (not directly found)

The only index

Generated based on the unique index column table, allowing a null value

Full-text index

Will be stored in the database of the entire book or any of the content of the whole article to find out information, such as a large class of text, such as like% keyword%, the general index of efficiency compared to full-text index is very low.

Second, according to the data structure classification

B + tree index

b + tree is based on a balanced binary tree of multiplexers balanced search trees, all records are stored in the order of leaf nodes, each leaf node are directly connected by a linked list. And b tree it is different:

Non-leaf nodes only store key information.

It has a chain of pointers between all leaf nodes.

Data records are stored in the leaf node.

hash index

Based on the index hash table structure to achieve, mysql only MEMORY / HEAP and NDB storage engine support;

InnoDB engine supports adaptive hash index, but is used to create the database itself, and can not be arbitrarily defined. When the secondary index is accessed frequently, it will automatically create adaptive hash index;

SHOW ENGINE INNODB STATUS command to view the use of adaptive hash index;

Command SHOW VARIABLES LIKE '% ap% hash_index' to see if the open adaptive hash index.

Compared:

Since the hash index is to compare the hash value, hash index can only be equivalent lookup can not find the range

hash index can not be sorted: for the same reason

Does not support the leftmost matching principle, calculate the hash value merge together when the composite index

A high retrieval efficiency hash index can be positioned once, but when a large number of hash collision occurs, the list becomes longer, the efficiency is not as good hash index b + tree of

Because of hash collision issue, when you need to get the total number, hash index at any time can not avoid a table scan

T-tree index

R-tree index

Third, according to the storage structure classification

Clustered index (clustered index)

InnoDB clustered index is in fact the same structure BTree simultaneously storing the index and the entire row of data, query data query may be acquired directly through the row index.

Clustered index is not a separate index type, but a way of storing data, Clustered indexes, that is, the physical order of the data on the hard disk.

In mysql clustered index is usually synonymous with the primary key, each table contains only one clustered index (not necessarily the other databases).

Secondary index (non-clustered index, secondary index, secondary index)

Save the non-clustered index and primary key index columns in the leaf nodes of BTree. If the query is not in the index column, can only be found in its primary key, you also need an action query clustered index back to the table query.

Clustered indexes advantages:

Can be saved together relevant data, such as: implementing e-mail, you can gather data based on user ID, so only a small amount of data read from disk pages a user will be able to get all the mail, if not using a clustered index, each message may cause a disk IO.

Faster data access, clustered index and the index data stored in the same btree, so get data faster than usually find in a non-clustered index from a clustered index.

Use a covering index scan query can be used directly in the primary key node page.

Clustered index drawbacks:

Clustered data to maximize the performance of the IO-intensive applications, but if all the data in memory, the order of the visit was not so important, clustered index also no advantage

Insertion speed is heavily dependent on the order of insertion, inserted in the order of the primary key is to load data into innodb table fastest way, but if it is not loaded data in the primary key order, it is preferable to use optimize table command after loading is complete reorganize table .

Update clustered index column costly, because it will force the innodb will be updated each row to move to a new location.

Clustered index on the table insert a new row, or when the primary key is updated based on results in the need to move the line, may face the problem page split, when the primary key must be inserted into the line requires a full page in this line, storage engine will split the page into two pages to accommodate the bank, this is a page split operation, the split will result page table takes up more disk space.

Clustered index may result in a full table scan slow down, especially in the line sparse, or because the page split causes data storage discontinuous time.

Secondary index may be greater than previously thought, because in the secondary index leaf node contains the primary key column references row.

Secondary index access requires two index lookups, instead of one.

Published 109 original articles · won praise 101 · views 360 000 +

Guess you like

Origin blog.csdn.net/Alen_xiaoxin/article/details/105146610