Database Design -- Index Design

Database Design -- Index Design

 

what is an index

An index is a way to speed up the retrieval of data in a table, just so that you don't want to scan the entire table.

 

 

The advantages of indexing

 

  1. Greatly speed up finding data
  2. Create a unique index to ensure the uniqueness of each row of data in the data table
  3. Significantly reduces time when performing grouped and sorted searches

 

 

Disadvantages of Indexing

 

  1. Reduce the speed of additions, deletions and changes
  2. Creating and maintaining indexes requires our maintenance time
  3. The index still needs to occupy a certain amount of physical space

 

 

 

Index classification

 

By index method

 

  • B-Tree Index
  • Hash index

By index type

 

    Primary key index : Generally, if the database is set as the primary key, the index will be automatically added.

 

    Ordinary index : our commonly used index.

    CREATE INDEX indexName ON mytable(user_id(length));

    Naming example: field name_IDX

 

    Unique Index : Guarantees Uniqueness, but Allows Nulls

    CREATE UNIQUE INDEX indexName ON mytable(username(length));

    Naming example: field name _UNIQUE

 

    Full-text index : for full-text search, select Full Text as the index type

 

    Combined index : In order to further squeeze the efficiency of MySQL, it is necessary to consider building a combined index

    ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age);

 

 

 

 

Index Design Principles

 

Choose a unique index

 

       The value of the unique index is unique, and a record can be determined more quickly through the index. For example, the ID number in the student table is a unique field. Establishing a unique index for this field can quickly determine the information of a certain student. If the name is used, there may be the phenomenon of the same name, thereby reducing the query speed.

 

 

Index fields that frequently require sorting, grouping, and union operations

 

       For fields that often require operations such as ORDER BY, GROUP BY, DISTINCT, and UNION, sorting operations will waste a lot of time. If you build an index on it, you can effectively avoid sorting operations.

 

 

Build indexes for fields that are often used as query conditions

 

       If a field is often used as a query condition, the query speed of this field will affect the query speed of the entire table. Therefore, indexing such fields can improve the query speed of the entire table.

 

 

limit the number of indexes

 

       The number of indexes is not the more the better. Each index requires disk space, and the more indexes, the more disk space is required. Refactoring and updating indexes is cumbersome when modifying a table. The more indexes, the more time-consuming it is to update the table.

 

 

Use indexes with as little data as possible

 

       If the value of the index is very long, then the speed of the query will be affected. For example, a full-text search for a field of type CHAR(100) will definitely take more time than a field of type CHAR(10).

 

 

Use prefixes as much as possible to index

 

       If the value of the indexed field is very long, it is better to use the prefix of the value to index. For example, for fields of type TEXT and BLOG, full-text search is a waste of time. This can improve retrieval speed if only the first few characters of the field are retrieved.

 

 

Delete indexes that are no longer used or rarely used

 

       After the data in the table is updated a lot, or the way the data is used is changed, some of the original indexes may no longer be needed. Database administrators should regularly identify these indexes and delete them to reduce the impact of indexes on update operations.

 

 

Indexing is not recommended

 

  • Repeated and evenly distributed fields, fields like sex, only male and female, do not need to be indexed
  • There is too little data, if there are only a few rows of data, do not index
  • If you frequently insert and modify data, it is not recommended to add an index.

 

 

 

 

 

 

Guess you like

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