sql: SQL optimization knowledge point records (9)

(1) Small table drives large table

Analysis of sql tuning:

Sort optimization: 

The database connection method, try to connect the data in this way, try to choose the first method, because the connection between the two tables is established 5 times in total, and the second connection is established 1000 times, choose the first method for the small table to drive the large table kind

B is equivalent to a department, A is an employee, and the department id is in the department table. There are a small number of departments and a large number of employees. First, get the id data in the small table B, and then get the data with id=department id

Use exist Use exist when the data in A is less than B

 (2)in和exist

The data in the department table is multi-employee table, so there is no problem in writing the following 

(2) Use index OrderBy optimization for sorting

The previous analysis of our index is after Where and before Order By. Now we are looking at whether fileSort will be generated after Order By.

The following does not produce fileSort

The fileSort is generated below because the index used after Order By is not in the order in which it was established 

An index is a sorted data structure for quick search, indicating that the index focuses on two functions of searching and sorting, and the order used when sorting must be the same as the order in which the index was built, otherwise the file will be sorted

The following generates the sorting in the file. Order by uses the same order as the index created, but the default is ascending, but birth uses descending order.

The sorting operation is completed on the index column, because the index has already been sorted for us once the index is established. It is best that the Order by is consistent with the index, which will prevent MySql from sorting again. If the order of the Order by is reversed, it is not on the index column, or The big brother who takes the lead hangs up, filesort is generated, and two algorithms will be generated after generation: two-way sorting and one-way sorting

One-way sorting: 

Optimization Strategy:

It is easy to use up sort_Buffer when using select *, so it is not recommended to use * when querying 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/132645724