mysql composite index, abc index hit

mysql joint index, abc controversial practice

Reason :

There was a controversy when discussing the mysql joint index interview questions with my colleagues. The main question is: The three fields of a, b, and c are used as a joint index, b, c; and will the cases of a and c hit the index?

I checked the relevant blogs online and found that many answers were different, so I simply experimented with my hands. The mysql version I used is 5.6

One: create a table

In order to fit the interview questions more directly, the fields are directly represented by AA, BB, CC

create table IF NOT EXISTS TEST_COMPOSITE_INDEX 
(
`TID` BIGINT  NOT NULL AUTO_INCREMENT,
`AA` VARCHAR(50) NOT NULL DEFAULT '' ,
`BB` VARCHAR(50) NOT NULL DEFAULT '',
`CC` VARCHAR(50) NOT NULL DEFAULT '',
`DD` VARCHAR(50) NOT NULL DEFAULT '',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`TID`),
KEY `index_comp` (`AA`,`BB`,`CC`)
)ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;

In sql, we have two indexes, one: the primary key TID, two: the combined index composed of AA, BB, and CC: index_comp

Two: insert data

insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A1','B1','C1','D1');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A2','B2','C2','D2');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A3','B3','C3','D3');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A4','B4','C4','D4');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A5','B5','C5','D5');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A6','B6','C6','D6');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A7','B7','C7','D7');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A8','B8','C8','D8');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A9','B9','C9','D9');
insert into TEST_COMPOSITE_INDEX (AA,BB,CC,DD)VALUE('A10','B10','C10','D10');

Three: View the execution plan
We respectively view the following execution plans:

EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.tid='3' ;
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA='A1';
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.BB='B1';
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.CC='C1' ;
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA='A1' AND tci.CC='C1';
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.BB='B1' AND tci.CC='C1';
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA='A1' AND tci.BB='B1';

The following results are shown in order, let’s look at them one by one:

The first one: the
Implementation plan one
first query using the primary key, you can see that the primary key is used in the execution plan, and the number of scanned rows is 1, which is very efficient

the second:

EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA='A1';

Implementation plan two
You can see that the index is also used, this time the joint index index we created: index_comp. Note that key_len: the number of bytes used by the index, this will be used for comparison later

The third and fourth:

EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.BB='B1';
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.CC='C1' ;

Implementation plan three
Implementation plan four
Only use the BB or CC field, and will not use the index, the number of scan lines is 10

the fifth:

EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA='A1' AND tci.CC='C1';

Insert picture description here

Here is a point we have a dispute. You can see that the index is indeed used, but the number of bytes used by the key_len index is 152 and the second statement only uses A to query, the number of bytes used is the same, so this sentence The conclusion is: Although the index is used when querying A and C, only A is actually used, not AC

sixth:

EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.BB='B1' AND tci.CC='C1';

Insert picture description here
Obviously, indexes are not used when querying B and C

the seventh

EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA='A1' AND tci.BB='B1';

Insert picture description here

The last is the normal joint index, which follows the leftmost match. The number of bytes used by the index: 304, which is exactly double the previous hit of a single field

Finally, explain the problems that may be encountered in the interview:

If it is a composite index, and the leftmost match is followed, if one of the fields is a range query, then: the hit field will only go to the range query that field, for example:
EXPLAIN select * FROM TEST_COMPOSITE_INDEX tci where tci.AA>'A1' AND tci.BB='B1';
then the index field used in this query is only: AA, but not used BB

Guess you like

Origin blog.csdn.net/bgs_fly/article/details/108753335