mysql study notes - index

1, the role of the index

  • Build MySQL MySQL index for efficient operation is very important, the index can greatly improve the retrieval speed of MySQL.

    - an analogy, take Chinese dictionary catalog page (index) analogy, we can press Pinyin, strokes, and other radical sort of table of contents (index) to quickly find the required word.

  • The disadvantage index is to create and maintain indexes takes time, slow down the write speed

2, increasing the index (general index, the only index, multi-column index)

General index seat Note the two methods table, column names, index names -

The CREATE  INDEX t_emptest_inx ON t_emptest (empno); - creating an index; 
the ALTER  TABLE t_emptest the ADD  INDEX   t_emptest_inx (empno); - creating an index;

Unique index UNIQUE index

- unique index, the general index of similar, is different: the value of the index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique. 
The CREATE  UNIQUE  INDEX   t_emptest_inx ON t_emptest (empno); - creating an index; 
the ALTER  TABLE t_emptest the ADD  UNIQUE  INDEX   t_emptest_inx (empno); - creating an index;

Multi-column index t_emptest ( empno, ename )

- a composite index, when you create a composite index should be the most commonly used as a constraint on the leftmost column, in descending order. 
The CREATE  UNIQUE  the INDEX   t_emptest_inxm ON t_emptest (EMPNO, ename);

3, view the index - the index of the fields displayed in meaning

SHOW INDEX  the FROM t_emptest; - see the index

View of the fields have the following meanings

4, delete the index

The ALTER  TABLE t_emptest DROP  INDEX t_emptest_inx;   - remove the index; 
DROP  INDEX t_emptest_inx ON t_emptest;            - remove the index;

5, for a scene

  • Primary key, foreign key must have an index; A foreign key is unique and often inquiry
  • The amount of data of more than 300 tables should be indexed;
  • Often connected with other tables table, in connection field on should be indexed; often join queries, the need for an index
  • Often appear in the Where clause field, accelerating the speed of judgment, especially in the field of large tables, should be indexed, indexing is generally used in select ...... where f1 and f2, we build an index on f1 or f2 is useless of. Use only two joint index can be useful
  • Often used to sort the column, because the index has been sorted.
  • Often used in the columns of the search range to create an index, because the index has been sorted, its specified range is continuous

6, is not suitable scene

  • For those columns rarely used or referenced in the query should not create the index. This is because, since these columns rarely used, so there is no index or index does not improve query speed. On the contrary, due to the increase in the index, but reduces maintenance and increases the speed of the system space requirements.
  • For those with only few data values in a column should not increase the index. Because originally the result set is the equivalent of a full table queries, so there is no need. This is because the values of these columns is small, such as gender column personnel table, in the results of the query, the result set of data rows in a large proportion of rows of data tables that need to search for data in the table a large proportion of the line. Increase in the index, did not significantly speed up the retrieval speed.
  • For those defined as text, image and bit data types of columns should not increase the index. This is because the amount of data in those columns either a large or small value.
  • When modifying performance is far greater than the retrieval performance , it should not create the index. This is because, retrieval performance and performance modifications are conflicting. When the increase in the index will increase the retrieval performance, but reduces performance modifications. When the reduction of the index will be modified to improve performance and reduce retrieval performance. Therefore, when modifying the performance is far greater than the retrieval performance, should not create the index.
  • Where conditions do not appear in the field should not be indexed.


Applicable scene Original: https: //blog.csdn.net/u012954706/article/details/81241049

Guess you like

Origin www.cnblogs.com/shishibuwan/p/11165585.html