When will create a composite index? Leftmost matching principle?

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

  1. CREATE TABLE `student`(
  2. `id`int(11) NOT NULL,
  3. `name` varchar(10) NOT NULL,
  4. `age`int(11) NOT NULL,
  5. PRIMARY KEY (`id`),
  6. KEY `idx_id_name_age`(`id`,`name`,`age`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8

100w filling test data

  1. DROP PROCEDURE pro10;
  2. CREATE PROCEDURE pro10()
  3. BEGIN
  4. DECLARE i INT;
  5. DECLARE char_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  6. DECLARE return_str varchar(255) DEFAULT '';
  7. DECLARE age INT;
  8. SET i =1;
  9. WHILE i <5000000do
  10. SET return_str = substring(char_str, FLOOR(1+ RAND()*62),8);
  11. SET i = i+1;
  12. SET age = FLOOR(RAND()*100);
  13. INSERT INTO student(id, name, age) values(i, return_str, age);
  14. END WHILE;
  15. END;
  16.  
  17. CALL pro10();

Scenario testing

  1. EXPLAIN SELECT * FROM student WHERE id =2;

We can see that the query uses to index

  1. EXPLAIN SELECT * FROM student WHERE id =2 AND name ='defghijk';

We can see that the query uses to index

  1. EXPLAIN SELECT * FROM student WHERE id =2 AND name ='defghijk'and age =8;

We can see that the query uses to index

  1. EXPLAIN SELECT * FROM student WHERE id =2 AND age =8;

We can see that the query uses to index

  1. 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

  1. EXPLAIN SELECT * FROM student WHERE name ='defghijk' AND id =2;
  2.  
  3. EXPLAIN SELECT * FROM student WHERE age =8 AND id =2;
  4.  
  5. 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.

This article Title: MySQL composite index with the leftmost matching principle Detailed
This article addresses: http://www.cppcns.com/shujuku/mysql/254483.html

Guess you like

Origin www.cnblogs.com/AbnerLc/p/11924933.html