Foreword
Before the line to see a lot of articles about mysql joint index of the most left-prefix match, thinking on the understanding of its principles, and a recent interview the interviewer exchange, I found missing something here to sort out the contents of their own in this regard.
Creating composite index What time?
When there are multiple criteria query of where our inquiry, we need to create a composite index on the columns of the query
Why does not create an index
(1) reducing overhead: If for col1, col2, col3 create a combined index, created the equivalent of (col1), (col1, col2 ), (col1, col2, col3) 3 indices
covering indexes: If the query SELECT col1, col2 , col3 fROM table name, due to the presence of the index field query page, you can get directly from the index, without the need for back-table query
High (2) efficiency: to col1, col2, col3 three indexes are created, MySQL will only choose a high degree of recognition as an index. 100w is assumed that data of a selected 10% of the index data, the data can be selected 10w; for combination index, it can filter out 100w * 10% * 10% * 10% = 1000 pieces of data
Leftmost matching principle
Suppose we create (col1, col2, col3) such a composite index, the equivalent of the col1 column is sorted, that is, we create a combined index to the leftmost prevail, as long as the left-most column in the query with, then the query will be used to index
Create a test table
- CREATE TABLE `student`(
- `id`int(11) NOT NULL,
- `name` varchar(10) NOT NULL,
- `age`int(11) NOT NULL,
- PRIMARY KEY (`id`),
- KEY `idx_id_name_age`(`id`,`name`,`age`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
100w filling test data
- DROP PROCEDURE pro10;
- CREATE PROCEDURE pro10()
- BEGIN
- DECLARE i INT;
- DECLARE char_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- DECLARE return_str varchar(255) DEFAULT '';
- DECLARE age INT;
- SET i =1;
- WHILE i <5000000do
- SET return_str = substring(char_str, FLOOR(1+ RAND()*62),8);
- SET i = i+1;
- SET age = FLOOR(RAND()*100);
- INSERT INTO student(id, name, age) values(i, return_str, age);
- END WHILE;
- END;
- CALL pro10();
Scenario testing
- EXPLAIN SELECT * FROM student WHERE id =2;
We can see that the query uses to index
- EXPLAIN SELECT * FROM student WHERE id =2 AND name ='defghijk';
We can see that the query uses to index
- EXPLAIN SELECT * FROM student WHERE id =2 AND name ='defghijk'and age =8;
We can see that the query uses to index
- EXPLAIN SELECT * FROM student WHERE id =2 AND age =8;
We can see that the query uses to index
- EXPLAIN SELECT * FROM student WHERE name ='defghijk' AND age =8;
You can see that the query does not use the index, type of index, the number of queries the line is 4,989,449, it was almost full table scan, index only due to the combination of the rankings for the leftmost column for the name, age only scanned all
- EXPLAIN SELECT * FROM student WHERE name ='defghijk' AND id =2;
- EXPLAIN SELECT * FROM student WHERE age =8 AND id =2;
- EXPLAIN SELECT * FROM student WHERE name ='defghijk'and age =8 AND id =2;
As can be seen also be used to query the index, id put in front of and behind the queries put to the result is the same, MySQL will find the highest efficiencies of an executed query, the first query is based on id
to sum up
As a test, we can see that as long as the query column contains the combined index of the leftmost column that, regardless of the position of the column in the query will use the index to search.
Well, that's the entire contents of this article, I hope the contents of this paper has some reference value of learning for all of us to learn or work, thank you for supporting us.