Understand the principle and optimization skills of mysql_ index

Index of innodb:
  ● Innodb uses the form of index to organize data, which is implemented at the storage engine level and has nothing to do with the back-end server layer. Different storage engines have different implementation algorithms.
 
  ● Principle of
           index The index is stored in the form of a b tree (balanced tree) tree, so it is ordered.
           The data of the index is stored in the tablespace, the
           clustered index (primary key index) stores the data of the entire row, and the auxiliary index only stores the pointer to the primary key, so the query efficiency of the auxiliary index is 1 times lower than that of the primary key index .
             The performance is relatively low during insertion and deletion, and a balanced tree needs to be maintained.
              
  ● Classification of indexes:
  1. Clustered index (primary key index), a table only allows one primary key index, stored in a b+ tree structure, and data has been stored on the index. The time complexity of the query is the height of the tree
  2. Auxiliary index, which stores the pointer of the primary key. When querying, you need to find the auxiliary index first, then find the primary key index according to the value of the auxiliary index, and then get the data
  3. Joint index, the leftmost principle
             Because the index on the right is not in order when the index is built, it will perform a full table scan.
             mysql is currently unable to do loose index scanning
  4. Inverted index, segment the text to get many small fragments of phrases, and index these phrases
  5. Adaptive hash index.
  6. Covering Index
             The fields in the select are in the index, you don't need to go to the clustered index again to get the value
             and use the explain query, you can see the extra annotation: using index

  ● Optimization point
  1. The index is a string, but there is no single quotation mark when querying' '
  2. A sql can only use one index. If multiple indexes are used, it is recommended to use a joint index
  . 3. In the optimization process, priority should be given to the use of clustered indexes and covering indexes, which can greatly improve efficiency
  . 4. It is recommended to use less in, Especially nested subqueries, because in that case the external table will do a full table scan, which can be well removed by using the table association exists and in, no distinct and group are needed, and the creation of temporary tables is avoided, and the efficiency            is
            sometimes higher.
It is not necessary to prohibit the use of subqueries (such as exists, in), it depends on the scene, because the use of inner join is prone to duplicate records, and it is necessary to use distcint to remove weights, which will affect the efficiency.
          Under what circumstances will a temporary table be generated?
       5. min\max uses primary key index
       skillfully 6. skillfully uses subquery to update table records
            update t inner join (select id, count(1) count from t2 group by id) t3 set t.max_value = t3.count
      

        The smaller the index type, the better. For big data processing, this is not a trivial matter. Replacing a string type with a number type can greatly save memory, disk storage and network bandwidth, reduce the cost of IO, and many data structures and algorithms. Using numeric types is faster than strings

Guess you like

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