MySQL indexing and optimization of SQL statements

The concept
1. Ordinary Index: basic index, it does not have any restrictions
2. unique index: the index column value must be unique, and the combination can not be empty, if it is a composite index, the column value must be unique.
3. The primary key index: special index, identify a unique record, it can not be empty, generally used to restrain primary key.
4. Combination Index: index based on a plurality of fields, queries can be accelerated to speed
5.Union: two result sets and set operations, not including duplicate rows, simultaneously default rule ordering
6.Union all: for two result set and set operations, including duplicates, do not sort

optimization

1, leading fuzzy queries can not use the index,

  The name like '% static'

2, Union, in, or you can hit the index, it is recommended to use in

3, the negative conditions of the query can not use an index, you can be optimized in the query,

  Wherein the negative conditions! =, <>, Not in, not exists, not like other

4, the most left-prefix principle of joint index, also known as the left-most queries,

  If the establishment of a joint index on (a, b, c) three fields, it is possible to speed up a | (a, b) | (a, b, c) three groups of query speed.

5, the establishment of the joint inquiry, the highest distinction of the field on the far left

6, if the joint index established (a, b), will not have to build a separate index.

  Similarly, if the establishment of (a, b, c) index will not have to establish a, (a, b) Index

7, there is an equal sign and a non-mixing equal sign determination condition, when indexing, should Equation Condition column predecoder

8, the scope of the column can use the index, but the scope of the column behind the column can not use the index.

  Up to a range of columns for the index, if the query has two full-range of columns you can not use the index. Range conditions: <, <=,>,> =, between like.

9, the calculated business layer into the layer rather than a database.

  Index calculation can not hit on the field,

10, will cast a full table scan,

  If the phone is varcher field type, the following SQL index can not hit. Select * fromuser where phone = 13800001234

11, updated very frequently, not to index data on discrimination is not high fields.

  Updates will change B + tree, frequently updated field indexing can greatly reduce database performance.

  "Gender" This discrimination is not too large properties, indexing is meaningless, can not effectively filter the data, and performance similar to a full table scan.

  Discrimination in general more than 80% can be indexed. Discrimination may be calculated using the count (distinct (column names)) / count (*).

12, using the index to cover the query, to avoid back to the table.

  Columns being queried, the data acquired from the index, instead of getting that through row-locator locator then the row "is a query column built to be covered by the index," which can query acceleration.

13, indexed column can not be null, not null constraints and use the default values

14, or using the delay associated with the sub-query optimization over many paging scene,

  MySQL not Skip offset rows, but rather, offset + N rows, and the row offset before giving up, return N rows, and that particularly when the offset time, the efficiency is very low, the total number of returned either control, or in excess of a certain threshold SQL rewrite pages.

15, the only field operational characteristic, even when a combination of a plurality of fields, a unique index must be completed.

16, best not to use more than three tables join,

  Need to join the field, data types must be consistent, multi-table associated with the query, ensure that fields are associated with the need to have an index.

17, if the query results as long as a clear know, limit 1 can improve efficiency, such as validation at login.

18, Select statement is sure to specify the name of the field

19, if the sort field is not used in the index, as little as possible to sort

20, as far as possible in place of union with union all.

  Union required after performing the combined set of unique filter operation, which involves sorting a large number of cpu operation, resource consumption and increase the delay, of course, use the union of all proviso that no duplicate data two result sets.

21, the use of reasonable paging improve efficiency.

  select id,name from product limit 866613, 20

When using the SQL statement to do paging, some people may find that, with the increasing amount of table data, use direct query page limit will become slower.

Optimization method is as follows:

It can be taken before a maximum number of rows id, then to limit the starting point of the next page according to the biggest id.

So the column than the largest previous id is 866612.

SQL can be written as follows: select id, name from product where id> 866612 limit 20.

 

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159799.htm