Types of indexes and common indexes

Types of indexes (there are many types of indexes. In mysql, there is no uniform index standard, indexes of different storage engines work differently, and not all storage engines support all types of indexes)

       1. B-Tree index Most MySQL engines support this kind of index, and the index columns are organized and stored sequentially, which is very suitable for searching range data. Such as: lookup like "find all names starting with I to K" will be very efficient. Suitable for full key value, key value range or key prefix (leftmost prefix lookup) lookup.

        2. Hash index Hash index only contains hash values ​​and row pointers, and does not store field values.

                                             Cannot be used for sorting, not stored in index value order

                                              Partial index column matching lookup is not supported (a hash index uses the entire contents of the index column to calculate the hash value)

                                               Only supports equality comparison queries, including =, IN(), <=>

                                                Does not support any range queries

       3. Spatial data index (R-Tree)

              MyISAM indexes support spatial indexes. No prefix query is required, data will be indexed from all dimensions

        4. Full-text index

         5. Other index categories

               (Third-party storage engine to store indexes, such as: TokuDB Fractal Tree Index)




Common indexes are:

      1.  Ordinary index (unlimited)

        2. Unique Index

                  The primary key is the unique index, but the unique index is not necessarily the primary key. The unique index can be empty, but there can only be one empty value, and the primary key cannot be empty.
Ordinary unique index: To establish a unique index on a single field, it is required that the column where this field is located cannot have duplicate values, which belongs to the secondary index.

Composite unique index: A unique index is jointly established on multiple fields, which is a secondary index. (Reference: https://www.jianshu.com/p/e1dce41a6b2b

         3. Single column index

         4. Multi-column index (prefix)

         5. Covering index:  An index contains (or covers) the values ​​of all fields that need to be queried 

         6. Clustered Index

              InnoDB uses a clustered index for the primary key, and MyISAM uses a non-clustered index whether it is a primary key index or a secondary index.

              A clustered index is not a separate index type, but a way of storing data. InnoDB's clustered indexes store B-Tree indexes and data rows in the same structure. Data rows are stored in the leaf pages of the index, and the term "cluster" means that data rows and adjacent key values ​​are stored compactly together. The storage engine is responsible for implementing the index (not all storage engines support clustered indexes).

              InnoDB will aggregate data by primary key, if no primary key is defined, InnoDB will choose a unique non-null index instead. If there is no such index, InnoDB will implicitly define a primary key as a clustered index.

               Advantages: 1. Data access is faster, clustered index saves index and data in the same B-Tree, so it is faster to get data than in non-clustered index.

                       2. The query using the covering index scan can directly use the primary key value of the page node.

                Disadvantages: 1. Insertion speed is heavily dependent on insertion order. Inserting in primary key order is the fastest way to load data into an InnoDB table. But if you are not inserting by primary key, it is better to use the optimize table command to reorganize the table

                           2. Updating clustered index columns is expensive. Because it forces InnoDB to move each updated row to a new location.

                           3. When a table based on a clustered index is inserted into a new row, or the primary key is updated and the row needs to be moved, it may face the problem of ""page paging".

When the primary key value of a row requires that the row must be inserted into a full page, the storage engine will paginate the page into two pages to accommodate the row. This is a page paging operation, which will cause the table to occupy more of disk space. Clustered indexes may cause slow full table scans, especially when rows are sparse or page splits are not stored contiguously.

                           The secondary index leaf node does not store a pointer to the physical location of the row, but the primary key value of the row.

        

         7. Full-text index

                    InnoDB uses a clustered index for the primary key, and MyISAM uses a non-clustered index whether it is a primary key index or a secondary index.




Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

Guess you like

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