MySQL Advanced - Indexes

Thanks: https://www.cnblogs.com/zhaobingqing/p/7066112.html

1. What is an index

  Index (Index) is a data structure that helps MySQL obtain data efficiently. The essence of the index can be obtained: the index is a data structure.

  Can be understood as "sorted fast lookup data structure"

  In addition to data, database systems also maintain data structures that satisfy specific lookup algorithms that reference (point to) data in some way,

  This enables advanced search algorithms to be implemented on these data structures, which are indexes.

2. Advantages

  Similar to the establishment of a bibliographic index in a university library, it improves the efficiency of data retrieval and reduces the IO cost of the database.

  Sorting data by index reduces the cost of data sorting and CPU consumption.

3. Disadvantages

  In fact, the index is also a table, which saves the primary key and index fields, and points to the records of the entity table, so the index column also takes up space.

  Although the index greatly improves the query speed, it will indeed reduce the speed of updating the table, such as INSERT, UPDATE, DELETE on the table.

  Because when updating the table, MySQL not only saves the data, but also saves the index file every time the fields where the index column is added are updated.

  The index information after the key value changes caused by the update will be adjusted. 

4. Classification of indexes

  single-valued index

    That is, an index contains only a single column, and a table can have multiple single-column indexes.

  unique index

    Values ​​for indexed columns must be unique, but null values ​​are allowed.

  compound index

    That is, an index contains multiple columns.

5. Basic grammar

  

    

6. MySQL index structure

    BTree index

    Hash index

    full-text full-text index

    R-Tree Index

7. What situations need to create an index

    ①The primary key automatically establishes a unique index

    ② Fields that are frequently used as query conditions should be indexed

    ③ The fields associated with other tables in the query, the foreign key relationship is indexed

    ④ Frequently updated fields are not suitable for indexing, because each update not only updates the record but also updates the index

    ⑤Do not create indexes for fields that are not used in the WHERE condition

    ⑥Single key/combination index selection problem, who? (Tendency to create a composite index under high concurrency)

    ⑦ The sorted fields in the query, if the sorted fields are accessed through the index, the sorting speed will be greatly improved

    ⑧ Statistics or grouping fields in the query

8. Under what circumstances do not create an index

  ①The table records are too few

  ② Tables that are frequently added, deleted, and modified

    Improve the query speed, but will reduce the speed of updating the table, such as INSERT, UPDATE, and DELETE on the table.

    Because when updating the table, MySQL not only saves the data, but also saves the index file.

    Table fields where data is repeated and evenly distributed, so only the most frequently queried and most frequently sorted data should be indexed.

  ③ Note that if a data column contains many repeated content, indexing it will not have much practical effect.

Guess you like

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