joint index

Naming rules: table name_field name
1. Fields that need to be indexed should be included in the where condition.
2. Fields with a small amount of data do not need to be indexed
. 3. If the where condition is an OR relationship, adding an index will not work
. 4. leftmost principle _

https://segmentfault.com/q/1010000003984016/a-1020000003984281

A joint index is also called a compound index. For compound indexes: Mysql uses the fields in the index from left to right. A query can only use a part of the index, but only the leftmost part. For example, the index is key index (a,b,c). It can support three combinations of a  |  a,b | a,b,c for search, but does not support b,c for search. When the leftmost field is a constant reference , the index is very efficient.  


An index on two or more columns is called a compound index.
Additional columns in the index allow you to narrow your search, but using one index with two columns is not the same as using two separate indexes. The structure of a compound index is similar to that of a phone book, where a person's name consists of a first and last name, and the phone book is first sorted by last name pairs, and then by first name for people with the same last name. A phone book is very useful if you know your last name; it is even more useful if you know your first and last name, but it is useless if you only know your first name and no last name.
So when creating a composite index, you should carefully consider the order of the columns. Compound indexes are useful when performing searches on all columns in the index or only the first few columns; they are not useful when performing searches on only any of the following columns.

http://blog.csdn.net/lmh12506/article/details/8879916

 

 

When a table has multiple indexes to go, Mysql chooses which index to go according to the cost of the query statement. If it is a joint index, it often calculates the first field (the leftmost one), which often leads to the wrong index. Such as : 
Index Index_1(Create_Time, Category_ID), Index_2(Category_ID) 

If there is a lot of data every day, and there are many categories, but there will not be many records for each category.

When the query SQL condition is select …where create_time ….and category_id=.., it is very likely that the index Index_1 is not used, but the index Index_2 is used, resulting in a slower query.

The solution is to reverse the order of the index fields.

Guess you like

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