Index usage rules

1. Rules for the establishment and use of indexes

 

1: The leftmost prefix matching principle

mysql will match from left to right until it encounters a range query ( >,<,between,like ) and stop matching

For example, create an index in the order of (a,b,c,d)

Use a query like this a = 1 and b=2 and c>3 and d =4 ; d is not indexed

But if the index of (a,b,d,c) is established, it can be used, and the order of a,b,d can be adjusted

 

2: = and in can be out of order, such as a =1 and b = 2 and c =3 

The order of indexing can be arbitrary: (a,b,c) or (b,a,c)

 

3: Use high discrimination (high selectivity) for indexing

 

4: Index columns cannot participate in calculations

 

5: Try to use joint indexes instead of separate indexes

 

2. Execution plan

Rows is the core flag, and most statements with small rows must be executed very quickly.

 

Basic steps for slow query optimization

0. Run it first to see if it is really slow, pay attention to setting SQL_NO_CACHE
1. Where condition single table query, lock the minimum return record table . The meaning of this sentence is to apply the where of the query statement to the table with the smallest number of records returned in the table to start the search, query each field of the single table separately, and see which field has the highest degree of discrimination
2.explain to check the execution plan, whether Consistent with the expectation of 1 (start the query from the table with fewer locked records)
3. The sql statement in the form of order by limit allows the sorted table to be checked first
4. Understand the usage scenarios of the business side
5. Refer to several principles for index building when adding indexes

6. Observation results that do not meet expectations continue to analyze from 0 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058663&siteId=291194637