Alibaba MySQL database index of the statute (b)

Index Statute

Mandatory part

[Force] fields with the unique operational characteristics, even when a combination of a plurality of fields, a unique index must be completed.
Description : Do not think that unique index affects the insertspeed, the speed loss is negligible, but to improve the search speed is obvious; In addition,
even in the application layer to do a very comprehensive check control, as long as there is no unique index, according to Murphy's Law, the inevitable dirty data generation.


[Mandatory] over three tables prohibited join. Required joinfields, data types must be absolutely consistent; multi-table associated with the query, ensure that fields are associated with the need to have an index.
Description : Even double table joinalso pay attention to the table index SQLperformance.


[Mandatory] in varcharindexing on the field, you must specify the length of the index, no need for indexing the whole field, you can determine the length of an index based on the actual text of discrimination.
Description : the length of the discrimination index is a contradiction, the general type of string data length of 20the index, the discrimination will be up to
90%the above, you may be used count(distinct left(列名, 索引长度))/count(*)to distinguish between degree determined.


[Mandatory] page search left vague or non-whole fuzzy, if necessary, please take the search engine to solve.
Description : Index file has B-Treethe most left-prefix matching characteristics, if the left value is not determined, it can not use this index.

Recommended section

[Recommended] If there is order bya scene, note the order of use of the index. order byThe final field is part of the composite index, and the index on a combination of final order to avoid file_sortthe situation, the impact query performance.
Positive examples : where a=? and b=? order by c; Index: a_b_c
counter-example : index range query if there is, then the index orderliness can not use, such as: WHERE a>10 ORDER BY b; the index a_bcan not be sorted.


[Recommended] use an index to cover the query, to avoid back to the table.
Description : If you need a book to know the first 11chapter title is what will turn on 11that page chapter corresponding to it? Directory look like, this directory is to play the role of covering indexes.
Positive examples : the type of points can be indexed primary key index, the only index, the general index of three, and a covering index is only one kind of an effect the query, with explainthe result, extrathe column will appear: using index.


[Recommended] using the delay associated or sub-query optimization over more scene paging.
Description : MySQLnot skip offsetthe line, but take offset+Nthe line, and then return before giving up offsetthe line, return Nline, when it offsetis particularly large, the efficiency is very low, or control the total number of pages returned or page in excess of a certain threshold number be SQLrewritten.
Positive examples : first need to get to quickly locate the idsegment and then associate: SELECT a.* FROMTable1 a, (select id from 表 1 where 条件 LIMIT 100000,20 ) b where a.id=b.id


[Recommended] SQL performance optimization goal: to reach at least rangelevel requirement is reflevel, if you can be conststhe best.

Description :

  • consts At most only a single table matching row (primary key or unique index), data can be read in the optimization stage.
  • refIt refers to the use of a common index (normal index).
  • range Of the index range search.

Counterexample : explainthe results table type=index, index files full scan physical, very slow, this indexlevel is relatively rangelower, and a full table scan is trivial.


[Recommended] build composite index of time, the highest distinction of the far left.
Positive example : if where a=? and b=?, if aalmost close to a unique value for the column, then you only need to build a single idx_aindex can be.
Description : When a non-equal sign equal sign and mixed, at the time of construction of the index, the column pre-condition of the equal sign. Such as: where c>? and d=?even if ca higher degree of differentiation must also be put din the forefront on the index, that index idx_d_c.


[Recommended] prevent implicit conversion caused because of the different field types, resulting in failure of the index.


Reference section

[Reference] when creating an index to avoid extreme misunderstanding follows:

  • Ninglanwuque. I think a query needs to build an index.
  • Ningquewulan. I think the index will consume space, a serious slow down the speed of the new record and update the line.
  • The only index boycott. The only think that all of the business need to be addressed in the application layer through the "check the plug after the first" approach.

Related reference

Guess you like

Origin www.cnblogs.com/yxhblogs/p/12537019.html