[Slow query optimization] When creating an index, pay attention to the field selectivity. for range query, pay attention to the field order of the combined index. bitsCN.com
Preface:
- I have previously said that "everyone is not required to understand the mysql computation process during join/left join/inner join", but what does the field selectivity mean, what is the order of combined index fields? Everyone must understand them;
- Repeat the last time: put the mysql client (such as SQLyog, such as HeidiSQL) on the desktop and explain it from time to time. This is a virtue!
- Make sure that you have checked the SQL execution plan by yourself. check the possible_keys, key, and rows values in the execution plan to minimize the number of affected rows and ensure correct indexes are used, reduce unnecessary Using temporary/Using filesort;
- Do not index fields with very low selectivity. for the reason, see optimization policy;
- When A range query (for example, A> 7, A in (2, 3) appears in the query condition, be cautious. if you do not create A composite index, you cannot use it. for the reason, see optimization policy B;
Let's first review the basic knowledge of field selectivity.
-- Basic knowledge of field selectivity --
Introduction: Can I create indexes for all fields?
As shown in the following table, the sort field has very poor selectivity. you can run the show index from ads command to see that sort's Cardinality (hash level) is only 9, and this field should not be indexed:
|
Table |
Non_unique |
Key_name |
Seq_in_index |
Column_name |
Collation |
Cardinality |
Sub_part |
Packed |
Null |
Index_type |
Comment |
|
Ads |
1 |
Sort |
1 |
Sort |
A |
9 |
/N |
/N |
|
BTREE |
|
Optimization policy A: field selectivity
- Low selectivity index may cause performance problems
- Index selectivity = unique value of the index column/number of table records;
- The higher the selectivity, the higher the index retrieval value, and the less system resources are consumed. The lower the selectivity, the lower the index retrieval value, and the more system resources are consumed;
- When a query condition contains multiple fields, do not create an index on a field with low selectivity.
- You can create a composite index to enhance the low field selectivity and avoid the side effects of creating an index with low selectivity;
- Minimize possible_keys. correct indexes will increase the SQL query speed. excessive indexes will increase the cost of selecting indexes by the optimizer. do not abuse indexes;
Review the business scenarios of combined index and range query.
-- Relationship between the order of combined index fields and range query --
Reference: Can I use the combined index (ads_id, city_id) to query city_id in (, 10) in a range?
For example,
The ac table has a composite index (ads_id, city_id ).
Can the following ac. city_id IN (0, 8005) query conditions use the combined index (ads_id, city_id) of the ac table?
EXPLAIN
SELECT ac. ads_id
FROM ads, ac
WHERE
Ads. id = ac. ads_id
ANDAc. city_id IN (0, 8005)
AND ads. status = 'online'
AND ac. start_time < p><>
AND ac. end_time> UNIX_TIMESTAMP ()
Optimization policy B:
Because mysql indexes are based on B-Tree, composite indexes have the concept of "field order.
Therefore, the query conditions includeAc. city_id IN (0, 8005)And the composite index is(Ads_id, city_id)The composite index cannot be used for this query.
DBA concluded:
Various scenarios of combined index queryIndex (A, B, C) -- Composite Index multi-field is ordered, and is A complete BTree Index.
- The following conditions can be queried using the combined index:
- A> 5
- A = 5 and B> 6
- A = 5 and B = 6 AND C = 7
- A = 5 and B IN (2, 3) AND C> 5
- The following conditions cannot be queried using the composite index:
- B> 5 -- the query condition does not contain the fields in the first column of the composite index.
- B = 6 AND C = 7 -- the query condition does not contain the fields in the first column of the composite index.
- The following conditions can be queried using the combination of the preceding indexes:
- A> 5 and B = 2 -- when the range query uses the first column, the query condition can only use the first column
- A = 5 and B> 6 AND C = 2 -- use the second column for range query. the query condition can only use the first two columns.
Various scenarios of composite index sortingThere is A composite Index (A, B ).
- The following conditions can be sorted by composite indexes:
- Order by a -- sort the first column
- A = 5 order by B -- sort BY the second column after filtering in the first column
- ORDER BYDESC, BDESC-- Note that the two columns are sorted in the same order.
- A> 5 order by a -- data retrieval and sorting are in the first column
- The following conditions cannot be sorted by composite indexes:
- Order by B -- sort in the second column of the index
- A> 5 order by B -- the range query is in the first column, and the sorting is in the second column.
- A in (1, 2) order by B -- same reason as above
- ORDER BYASC, BDESC-- Note that the two columns are sorted in different order.
Follow the steps of creating a composite index and continue to extend. please pay attention to the concept of "index merge:
- For MySQL versions earlier than, only one index (use at most only one index for each referenced table) can be used for a table during SQL query ),
- Starting from MySQL 5.0Index mergeConcept, including Index Merge Union Access Algorithm (multiple indexes are accessed in parallel), including Index Merge Intersection Access Algorithm (multiple indexes are accessed at Intersection ), multiple indexes in a table can be used in an SQL query.
- Before MySQL 5.6.7, using index merge has an important precondition: no range is available. [Reference Resource 2]
A brief description of index merge:
- MySQL index combination can use multiple indexes
- SELECT * from tb where a = 5ANDB = 6
- Indexes (A), (B), and indexes can be merged respectively;
- It is better to create A composite index (A, B;
- SELECT * from tb where a = 5ORB = 6
- Indexes (A), (B), and indexes can be merged respectively;
- Composite indexes (A, B) cannot be used in this query. it is better to create indexes (A) and (B) respectively;
Summary:It is still emphasized again:
Remember, it is a virtue to explain and try again!
Reference Resources:1) Chinese translation, 2013, MySQL index best practice feedback; 2) orczhou, 2013, MySQL Optimizer: index merge introduction; 3) orczhou, 2013, supplementary description of index merge; 4) zhengyun, 2013, [slow query optimization] pay attention to who is the driving table for table query & you don't know who is better to join, please let mysql determine it by yourself; 5) nocode, 2013, mySQL Internals-Index Merge optimization; Gift: turn: I have meed a mi, and the boss urged me to refactor the legacy Python code ...... BitsCN.com