build index optimize sql

An introduction to the index

In a relational database, an index is a storage structure that separates and physically sorts the values ​​of one or more columns in a database table. A list of logical pointers to the data pages that physically identify these values.

The function of the index is equivalent to the directory of the book , which can quickly find the desired content according to the page number in the directory.

If we want to look up the word "mysql", we definitely need to locate the m letter, then find the y letter from the bottom down, and then find the rest of the sql. If there is no index, then you may need to go through all the words to find what you want.

The index is based on B+Tree as the index structure

2. Creation principles

An index is built on a specified column in a database table. When creating an index, you should carefully consider which columns can and cannot be indexed on.

Index creation principles:

1. The primary key and foreign key of

the table must have indexes; 2. Tables with more than 300 data should have indexes;

3. Tables that are often connected with other tables should have indexes on the connection fields;

4. Often appear in the Where subsection The fields in the sentence, especially the fields of large tables, should be indexed;

5. The index should be built on the field with high selectivity;

6. The index should be built on the small field. For large text fields or even super long fields, do not Build an index;

7. The establishment of a composite index needs to be carefully analyzed; try to consider using a single-field index instead:

      A. Correctly select the main column field in the composite index, which is generally a field with better selectivity;

      B. Several composite indexes Are fields often ANDed together in a Where clause? Are there few or no single-field queries? If yes, you can build a composite index; otherwise, consider a single-field index;

      C. If the fields contained in the composite index often appear in the Where clause alone, it will be decomposed into multiple single-field indexes;

      D. If the composite index contains If there are more than 3 fields, then carefully consider its necessity and consider reducing the composite fields;

      E. If there is both a single field index and a composite index on these fields, the composite index can generally be deleted;

8. Frequent data operations 9. Delete useless indexes

to avoid negative impact on the execution plan;

10. Fuzzy queries such as LIKE do not require indexes

3. Analysis of the pros and cons of indexing

Creating an index can greatly improve the performance of the system:
First, by creating a unique index, the uniqueness of each row of data in the database table can be guaranteed.
Second, it can greatly speed up data retrieval, which is the main reason for creating indexes.
Third, it can speed up table-to-table joins, especially in terms of achieving referential integrity of data.
Fourth, when using the grouping and sorting clauses for data retrieval, the time for grouping and sorting in the query can also be significantly reduced.
Fifth, by using the index, the optimization hider can be used in the query process to improve the performance of the system. 

Adding indexes also has a number of downsides:
First, it takes time to create and maintain indexes, and this time increases with the amount of data.
Second, the index needs to occupy physical space. In addition to the data table occupying the data space, each index also occupies a certain amount of physical space. If a clustered index is to be established, the required space will be larger.
Third, when adding, deleting and modifying the data in the table, the index should also be dynamically maintained, which reduces the speed of data maintenance. 

Guess you like

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