Review the index of the database

I. Overview

MySQL is officially defined as an index (Index) is a data structure that helps MySQL obtain data efficiently, so the essence of an index is a data structure. Further analysis, we can understand that the index is a sorted data structure for quickly finding data.

Two Index Index

The main function of the index is to sort and quickly locate and search. In the process of our SQL search, the conditional judgment in the Where statement will affect our query efficiency. In addition, according to the function of the index, the index will also affect the SQL Sorting, that is, the Order by statement.

In addition to the data, the database system also maintains a data structure that satisfies a specific search algorithm. These data refer to the data in a certain way (such as pointers). These can implement advanced search algorithms on these data structures. The data structure is the index.

As shown in the figure, in order to speed up the search for col2, a binary search tree as shown on the right can be maintained. Each node contains an index key value and a pointer to the physical address of the corresponding data record, so that the two can be used. Fork search obtains the corresponding data within a certain complexity, so as to quickly retrieve the records that meet the conditions.

Generally speaking, the index generally refers to the index of the B-tree (multiple search tree, but not necessarily the binary tree) structure, which is clustered index, secondary index, composite index, prefix index, The unique index uses B+ tree by default, in addition to Hash index and so on.

Index fields should not be frequently deleted. The index itself is large and cannot be stored in memory. Therefore, the index is often stored on the disk in the form of an index file.

Classification of three indexes

Single-value index: An index refers to a single column, and a table can have multiple single-column indexes.

Unique index: The index of the index column must be unique, when null values ​​are allowed.

Composite index: An index contains multiple columns.

In general, the number of indexes on a table should not exceed 5.

Create index

create [unique] index indexname on table(clumnname(name));

Add index

alter table [tablename] add [unique] index [indexname] on (clumnname(name)); //The value of the index created by this statement must be unique. In addition to NULL, NULL may appear multiple times.

alter table [tablename] add primary key (clumnname(name)) ;//This statement adds a primary key, which means that the index value must be unique and cannot be NULL.

alter table [tablename] add index [indexname] on (clumnname);//Add a common index, the index value may appear multiple times.

alter table [tablename] add FULLTEXT [indexname] on (clumnname);//This statement specifies that the index is FULLTEXT, which is used for full-text indexing.

Delete index

drop index [indexname] on table;

View index

show index from table name\G;

The structure of four indexes

B Tree index

Hash index

full-text index

R-Tree index

The 3-layer B+ tree can represent millions of data. If millions of data lookups only require three IOs, the performance improvement is very large. If there is no index, each data item will have one IO, then a total of millions of times are required IO, the cost is very large

Five index usage/non-applicable conditions

Conditions under which indexes should be used

1. The primary key automatically creates a unique index

2. Frequently queried fields should be indexed

3. Query the fields associated with other tables in the query, and establish an index for the foreign key relationship

4. The sorted field in the query, if the sorted field can be accessed through the index, the sorting speed will be greatly improved

5. Statistics or grouping fields in the query

Conditions under which indexes should not be used

1. Frequently updated fields are not suitable for index creation, because each update not only updates the record but also updates the index

2. Fields that are not used in the Where condition are not indexed

3. Single-key/combined index, more inclined to composite index in the case of high concurrency

4. Too few table records

5. Fields with repeated and evenly distributed data should only be indexed for incoming queries and frequently sorted data columns. When a data column contains many duplicate content, indexing it will not have much practical effect.

If there are 100,000 records in a table, a field A has only two values ​​of True and False, and the distribution probability of each value is about 50%, then building an index on the field of the table A will generally not improve the query efficiency of the database .

Index selectivity refers to the ratio of the number of different values ​​in the index column to the records in the table. If there are 2000 records in a table and the table index column has 1980 different values, then the selectivity of this index is 1980/2000=0.99 , The closer the selectivity of an index is to 1, the higher the efficiency of the index.

Advantages and disadvantages of five indexes

In fact, the index is also a table, which stores the primary key and index fields, and points to the records of the entity table, so the index column needs to take up storage space.

Although the index greatly improves the query speed, it will also reduce the speed of updating the table, such as insert, update and delete operations on the table, because when updating the table, MySQL must not only save the data, but also save the index file every time it is updated and added The fields of the index column will be adjusted to update the index information after the key value changes brought about by the update.

Guess you like

Origin blog.csdn.net/calm_encode/article/details/113915072