The common misunderstanding of btree index
is to add an index to the columns commonly used in the where condition. For
example: where cat_id=3 and price>100 #Query the third column,
only the cat_id or price index can be used for products with more than 100, because the independent index Only 1 can be used at a time.
Multi-column index validation rules
For multi-column indexes to function, they need to meet the left prefix requirements.
Take index(a,b,c) as an example:
语句 | 索引是否发挥作用
- - - - - - - - - - - - - - -
where a=3 | 是
where a=3 and b=5 | 是
where a=3 and b=5 and c=4 | 是
where b=3 | 否
where c=4 | 否
where a=3 and c=4 | a列能用到索引,c不能
where a=3 and b>10 and c=7 | a能,b能,c不能
where a=3 and b like 'xxx%' and c=7 | a能,b能,c不能