Mysql index design principles

The design of the index can follow some existing principles. When creating an index, you should try to comply with these principles, so as to improve the efficiency of the index and use the index more efficiently. This section will introduce some index design principles.

1. Choose a unique index

The value of a unique index is unique, and a record can be determined more quickly through the index. For example, the middle school 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.

2. Establish indexes for fields that often require sorting, grouping, and joint operations

Often require ORDER BY, GROUP BY, DISTINCT, and UNION fields, sorting operations will waste a lot of time. If you build an index for it, you can effectively avoid sorting operations.

3. Create an index for the fields that are often used as query conditions

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

Note: The field of the common query condition is not necessarily the column to be selected. In other words, the column that is most suitable for indexing is the column that appears in the WHERE clause, or the column specified in the join clause, instead of appearing in the SELECT keyword After the select list of columns.

4. 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. When modifying the contents of a table, the index must be updated, and sometimes it may need to be reconstructed. Therefore, the more indexes, the longer it takes to update the table.

If there is an index that is rarely used or never used, it will unnecessarily slow down the modification speed of the table. In addition, MySQL must consider each index when generating an execution plan, which also takes time. Creating redundant indexes brings more work to query optimization. Too many indexes may prevent MySQL from choosing the best index to use.

5. Try to use indexes with less data

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

6. It is best not to use indexes for tables with small amounts of data

Due to the small size of the data, the query time may be shorter than the time to traverse the index, and the index may not produce optimization effects.

7. Try to use prefixes to index

If the value of the index field is very long, it is best 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. If only the first few characters of the field are retrieved, the retrieval speed can be improved in this way.

8. Delete indexes that are no longer used or rarely used

After the data in the table has been updated a lot, or the way the data is used is changed, some of the original indexes may no longer be needed. These indexes should be found regularly and deleted to reduce the impact of indexes on update operations.

to sum up

The ultimate goal of choosing an index is to make the query faster. The principles given above are the most basic criteria, but you can't just stick to the above criteria. It should be practiced continuously in study and work, analyze and judge according to the actual situation of the application, and choose the most suitable indexing method.

Guess you like

Origin blog.csdn.net/weixin_43452467/article/details/114126716