Mysql detailed explanation Index

Detailed MySQL index (advantages and disadvantages, when you need / do not need to create an index, and optimize the sql statement)

 First, what is the index?

  Index is the value or more columns of a database table for a configuration of ranking, the index can quickly access specific information in the database table.

 

Second, the role of the index?

  Index equivalent to a directory on the books, you can quickly find content based on the catalog page number, to improve the performance (speed up the search)

 

Third, the advantages:

  1. By creating a unique index, you can guarantee the uniqueness of each row in the database table data.
  2. You can accelerate the speed of data retrieval
  3. You can accelerate the connection between the table and the table
  4. When using grouping and sorting to search, you can reduce query time grouping and sorting

 

Fourth, the shortcomings

  1. Creating indexes and index maintenance takes time, this time with the increase in the amount of data increases.
  2. Index requires additional physical space, the greater the amount of data, the greater the space
  3. It will reduce the efficiency of the additions and deletions to the list of additions and deletions to the index because every time, need to be dynamically maintained

 

Fifth, when you need to create an index

  1.  Primary key automatically create a unique index
  2.  Frequently as a query field should create an index
  3.  Sort of query field to create the index will greatly enhance the speed of sorting (sorting index is plus Quick Find
  4.  Query statistics or packet field;

 

 Sixth, when they are not creating an index

  1. Frequently updated fields are not suitable for creating an index, because each update is not just update record, will update the index, the index file saved
  2. where conditions are less than in the field, do not create an index;
  3. Table records too, do not need to create an index;
  4. Table frequent additions and deletions;
  5. Data duplication and an average of field distribution, indexing and therefore often sort of field frequently queried. Note that some data contains a lot of duplicate data, indexing so he would not have much effect, such as gender field, only men and women, not suitable for indexing.

 

Seven of the index Category:

  1. Ordinary Index: basic index, it does not have any restrictions
  2. The only 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. Primary key index: special index, identify a unique record, can not be empty, generally used to restrain primary key.
  4. Joint index: index on multiple fields, able to accelerate query speed

 

 Eight, indexing and optimization sql statement

 

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.

 First, what is the index?

  Index is the value or more columns of a database table for a configuration of ranking, the index can quickly access specific information in the database table.

 

Second, the role of the index?

  Index equivalent to a directory on the books, you can quickly find content based on the catalog page number, to improve the performance (speed up the search)

 

Third, the advantages:

  1. By creating a unique index, you can guarantee the uniqueness of each row in the database table data.
  2. You can accelerate the speed of data retrieval
  3. You can accelerate the connection between the table and the table
  4. When using grouping and sorting to search, you can reduce query time grouping and sorting

 

Fourth, the shortcomings

  1. Creating indexes and index maintenance takes time, this time with the increase in the amount of data increases.
  2. Index requires additional physical space, the greater the amount of data, the greater the space
  3. It will reduce the efficiency of the additions and deletions to the list of additions and deletions to the index because every time, need to be dynamically maintained

 

Fifth, when you need to create an index

  1.  Primary key automatically create a unique index
  2.  Frequently as a query field should create an index
  3.  Sort of query field to create the index will greatly enhance the speed of sorting (sorting index is plus Quick Find
  4.  Query statistics or packet field;

 

 Sixth, when they are not creating an index

  1. Frequently updated fields are not suitable for creating an index, because each update is not just update record, will update the index, the index file saved
  2. where conditions are less than in the field, do not create an index;
  3. Table records too, do not need to create an index;
  4. Table frequent additions and deletions;
  5. Data duplication and an average of field distribution, indexing and therefore often sort of field frequently queried. Note that some data contains a lot of duplicate data, indexing so he would not have much effect, such as gender field, only men and women, not suitable for indexing.

 

Seven of the index Category:

  1. Ordinary Index: basic index, it does not have any restrictions
  2. The only 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. Primary key index: special index, identify a unique record, can not be empty, generally used to restrain primary key.
  4. Joint index: index on multiple fields, able to accelerate query speed

 

 Eight, indexing and optimization sql statement

 

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.cnblogs.com/ibos/p/12077072.html
Recommended