The leftmost matching principle of MySQL database index

The leftmost matching principle of MySQL database index

1. Joint index description

Create a joint index of three fields

The joint index (a,b,c) is equivalent to creating the index: (a), (a,b), (a,b,c)

2. Can ac use indexes?

先給出結論:a可以命中聯合索引(a,b,c),c無法命中,所以ac組合無法命中聯合索引。

1. Create an abc joint index (province, city, district)

insert image description here

ac index query

SELECT * FROM user_address WHERE province = 'Guangdong' 
AND district = 'Nanxiong City'

insert image description here

Display query range is ALL

2. Create a joint index for the two fields ac directly

insert image description here

SELECT * FROM user_address WHERE province = 'Guangdong' 
AND district = 'Nanxiong City'

insert image description here

The query result is that ref uses the index, and the number of rows scanned has also changed from 21 to 13.

3.ab index query
insert image description here

The query scope is ref

in conclusion

abc joint index, c in ac cannot hit the joint index of these three fields, but a can hit it, so the possible_keys column will show that the joint index is used.

3. Thinking

abc index, will acb follow the index?

  • According to the leftmost prefix matching principle, MySQL will keep matching to the right until it encounters a range query (>, <, between, like) and stops matching.
  • For example, if a=3 and b=4 and c>5 and d=6, if you create an index in the order of (a, b, c, d), d will not be used. If you create an index in the order of (a, b, d, c), all of them can be used, and the order of a, b, d can be adjusted arbitrarily.
  • = and in can be in any order, for example, a=1 and b=2 and c=3. The (a, b, c) index can be created in any order. The MySQL query optimizer will help you optimize it into a form that the index can recognize.

4. The cause of the leftmost matching principle

  • Because the joint index, such as abc, can be understood as ordered and the basis of its formation is built on a, from a to build b, and from b to build c, so it must be in order
  • It can be simply understood as: first sort a and create an index, then sort b based on a, and then sort c.
  • Therefore, when a joint index encounters a range query, the subsequent indexes will become invalid.

This is the end of this article about the leftmost matching principle of MySQL database index. For more relevant MySQL index leftmost matching content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of MySQL database indexes and failure scenarios
  • Detailed introduction to MySQL database index
  • Detailed explanation of MySQL database index
  • MySQL Database Indexes and Transactions
  • MySQL database index order by sorting detailed explanation
  • Disadvantages and reasonable use of MySQL database index
  • Detailed explanation of transactions and indexes in MySQL database
  • Mysql database index interview questions (basic programmer skills)
  • Why does the index in the Mysql database table not improve the query speed?

<<:  Detailed explanation of vuex persistence in practical application of vue

>>:  A detailed introduction to Linux system operation levels

Recommend

Fixed a bug caused by scrollbar occupying space

background This bug was caused by滾動條占據空間. I check...

Vue+ssh framework to realize online chat

This article shares the specific code of Vue+ssh ...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...

6 interesting tips for setting CSS background images

Background-image is probably one of those CSS pro...

Pure CSS to achieve the list pull-down effect in the page

You may often see the following effect: That’s ri...

JavaScript implements large file upload processing

Many times when we process file uploads, such as ...

Solution to Incorrect string value in MySQL

Many friends will report the following error when...

MySQL scheduled backup solution (using Linux crontab)

Preface Although some love in this world has a pr...

202 Free High Quality XHTML Templates (1)

Here 123WORDPRESS.COM presents the first part of ...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

MySQL common statements for viewing transactions and locks

Some common statements for viewing transactions a...

MySQL 5.7.10 installation and configuration tutorial under Windows

MySQL provides two different versions for differe...