Java Coding Specification 13 (MySQL-Index Specification)

MySQL - Index Specification


Other related articles
Java Coding Specification 1 (Programming Specification - Naming Style)
Java Coding Specification 2 (Programming Specification - Constant Definition)
Java Coding Specification 3 (Programming Specification - Code Format)
Java Coding Specification 4 (Programming Specification - OOP Specification)
Java Coding Specification 5 (Programming Specification - Collection Processing)
Java Coding Specification 6 (Programming Specification - Concurrent Processing)
Java Coding Specification 7 (Programming Specification - Control Statement)
Java Coding Specification 8 (Programming Specification - Annotation Specification and Others)
Java Coding Specification 9 (Exception Log )
Java Coding Specification 10 (Unit Test)
Java Coding Specification 11 (Security Specification)
Java Coding Specification 12 (MySQL-Table Building Specification)
Java Coding Specification 13 (MySQL-Index Specification)
Java Coding Specification 14 (MySQL-SQL Statement and ORM Mapping )
Java Coding Specification 15 (Project Structure)


  1. [Mandatory] A field with unique characteristics in business, even if it is a combination of multiple fields, must be built into a unique index.

    • Note: Don't think that the unique index affects the insertspeed. This speed loss can be ignored, but it is obvious to improve the search speed. In addition, even if a very complete verification control is done at the application layer, as long as there is no unique index, according to Murphy's law, it must be Dirty data is generated.
  2. [Mandatory] Join is prohibited for more than three tables. The data types of the fields that need to be joined must be absolutely consistent; when multiple tables are associated with the query, ensure that the associated fields need to have indexes.

    • Note: Even with double-table joins, pay attention to table indexes and SQL performance.
  3. [Mandatory] When creating an index on a varchar field, the index length must be specified. It is not necessary to create an index on the entire field. The index length can be determined according to the actual text discrimination.

    • Note: The length of the index and the degree of discrimination are a pair of contradictions. Generally, for string type data, the index with a length of 20 has a degree of discrimination of more than 90%, which can count(distinct left(列名, 索引长度))/count(*)be determined by the degree of discrimination.
  4. [Mandatory] Left-blur or full-blur is strictly prohibited in page search. If necessary, please use the search engine to solve it.

    • Description: The index file has the leftmost prefix matching feature of B-Tree. If the left value is not determined, the index cannot be used.
  5. [Recommended] If there is an order by scenario, please pay attention to the orderliness of the index. The last field of order by is part of the composite index and is placed at the end of the index composite order to avoid file_sort and affect query performance.

    • Positive example: where a=? and b=? order by c;Index:a_b_c
    • Counter example: if there is a range search in the index, the ordering of the index cannot be used, for example, the WHERE a>10 ORDER BY b;index a_bcannot be sorted.
  6. [Recommended] Use a covering index for query operations to avoid returning tables.

    • Explanation: If a book needs to know what the title of chapter 11 is, will it turn to the page corresponding to chapter 11? Just browse the directory, this directory is to play the role of covering index.
    • Positive example: There are three types of indexes that can be established: primary key index, unique index, and ordinary index, and covering index is only an effect of a query. With the result of explain, the extra column will appear: using index.
  7. [Recommended] Use lazy association or subquery to optimize multi-page paging scenarios.

    • Note: MySQL does not skip offsetrows, but fetches offset+Nrows, then returns to give up the previous offsetrow, and returns N rows, then when offsetit is very large, the efficiency is very low, either control the total number of pages returned, or control the number of pages that exceed a certain threshold. The number of pages for SQL rewrite.
    • Positive example: first quickly locate the id segment to be obtained, and then associate:

      SELECT a.* FROM1 a, (select id from1 where 条件 LIMIT 100000,20 ) b where a.id=b.id
  8. [Recommendation] The goal of SQL performance optimization: at least to achieve the rangelevel , the requirement is the reflevel, if possible, it is the constsbest.

    • illustrate:
      • There is at most one matching row (primary key or unique index) in a consts single table, and the data can be read during the optimization phase.
      • ref refers to using a normal index.
      • range performs a range search on the index.
    • Counter example: the result of the explain table, type=index, the index physical file is fully scanned, and the speed is very slow. This index level is lower than the range, which is insignificant compared to the full table scan.
  9. [Recommended] When building a composite index, the one with the highest degree of discrimination is on the far left.

    • Positive example: If where a=? and b=?the column a is almost close to the unique value, then only a single idx_aindex is needed.
    • Note: When there is a mixed judgment condition of non-equal sign and equal sign, please prepend the column of the equal sign condition when creating an index. For example :where a>? and b=?, even if a is more discriminative, b must be placed at the forefront of the index.
  10. [Recommended] Prevent the implicit conversion caused by different field types, which will cause the index to fail.

  11. [Reference] Avoid the following extreme misunderstandings when creating indexes:

    • No shortage of abuse. Think that a query needs to build an index.
    • It is better not to overuse. It is believed that indexes consume space and severely slow down updates and new additions.
    • Resist unique indexes. It is believed that the uniqueness of the business needs to be solved by the method of "check first and then insert" at the application layer.

Guess you like

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