Mysql-explanation of leftmost prefix of index

When creating an n-column index, follow the "leftmost prefix" principle

Assume the following table: create table AAA (a varchar2 (32), b varchar2 (32), c date);

Create an ordinary index on columns a and c: create index iN_AAA_1 on AAA (a, c);
1. The first level of the left prefix of the index means: the first field of the index must be used. select * from AAA where b =: xxx and c = sysdate; the index will not be used, because a must appear in the where statement to use the index.
2. The second meaning of index prefix: For the first field of the index, the left side must be a fixed value when using like, and wildcards can only appear on the right side. select * from AAA where a like '1%'; will use the index; and select * from AAA where a like '% 1'; will not use the index.
3. The third meaning of index prefix: If a function is added in front of the field, the index will be suppressed. For example: select * from aaa where trim (a) = 1, the index will not be used.
With an expression embedded in front of the field, the index will also be suppressed. Assuming a is in date format, then where a + 7 <sysdate will not use the index, while where a <sysdate-7 will use the index.

 


There are also two special declarations:
1) .select * from AAA where a =: xxx and c = sysdate and select * from AAA where c = sysdate and a =: xxx; indexes are used, that is, fields appear in the where statement The order is irrelevant;

2) .select * from AAA where a =: xxx and b = 1; will use the index, at this time A appears, even if the other fields are not index fields will also use the index.

Guess you like

Origin www.cnblogs.com/liliuguang/p/12703744.html