Home Database Mysql Tutorial MySQL index left prefix matching rules

MySQL index left prefix matching rules

Feb 24, 2024 am 10:42 AM
principle mysql index leftmost principle

MySQL index left prefix matching rules

MySQL index leftmost principle principle and code examples

In MySQL, indexing is one of the important means to improve query efficiency. Among them, the index leftmost principle is an important principle that we need to follow when using indexes to optimize queries. This article will introduce the principle of the leftmost principle of MySQL index and give some specific code examples.

1. The principle of index leftmost principle

The index leftmost principle means that in an index, if the query condition is composed of multiple columns, then only the leftmost index in the index Only by querying side columns can you make full use of the index. Specifically, when querying, only by matching according to the leftmost column in the index, can the orderliness in the index be used. If the non-leftmost column in the index is used for query, the orderliness of the index cannot be used. , the query efficiency will be affected.

2. Example of the index leftmost principle

In order to better understand the index leftmost principle, let’s make an example. Assume that there is the following table student:

+----+-------+--------+----------+
| id | name  | gender | birthday |
+----+-------+--------+----------+
|  1 | John  | Male   | 2001-02-01 |
|  2 | Mary  | Female | 2002-03-02 |
|  3 | Tom   | Male   | 2003-04-03 |
|  4 | Alice | Female | 2004-05-04 |
+----+-------+--------+----------+
Copy after login

Create an index:

CREATE INDEX idx_student ON student(name, gender, birthday);
Copy after login

According to the index leftmost principle, we can use the following method to query:

  1. Use Query the leftmost column in the index:
SELECT * FROM student WHERE name = 'John';
Copy after login

This query can make full use of the index, and the query efficiency will be relatively high.

  1. Use the two leftmost columns in the index to query:
SELECT * FROM student WHERE name = 'John' AND gender = 'Male';
Copy after login

This query can also make full use of the index, because the column order in the query condition and the The column order is consistent.

  1. Use the three leftmost columns in the index for query:
SELECT * FROM student WHERE name = 'John' AND gender = 'Male' AND birthday = '2001-02-01';
Copy after login

This query can also make full use of the index and conforms to the leftmost index principle.

However, if we use a query method that does not comply with the leftmost principle of the index, such as:

SELECT * FROM student WHERE gender = 'Male';
Copy after login

This query cannot make full use of the index, because the columns in the query conditions are not in accordance with the index. Querying in the leftmost column will reduce query efficiency.

3. Summary

The index leftmost principle is a principle we need to follow when using MySQL indexes to optimize queries. By querying in the order of the leftmost column in the index, we can make full use of the orderliness of the index and improve query efficiency. If you use a query method that does not comply with the leftmost index principle, the query efficiency will be reduced. Therefore, when designing the database, it is necessary to create appropriate indexes according to specific query requirements, and when writing query statements, query according to the leftmost index principle, thereby improving query efficiency.

The above is an introduction to the principles and code examples of the leftmost principle of MySQL indexes. I hope it will be helpful to everyone.

The above is the detailed content of MySQL index left prefix matching rules. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

In-depth discussion of the principles and practices of the Struts framework In-depth discussion of the principles and practices of the Struts framework Feb 18, 2024 pm 06:10 PM

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

An in-depth discussion of the functions and principles of Linux RPM tools An in-depth discussion of the functions and principles of Linux RPM tools Feb 23, 2024 pm 03:00 PM

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

An in-depth analysis of the functions and working principles of the Linux chage command An in-depth analysis of the functions and working principles of the Linux chage command Feb 24, 2024 pm 03:48 PM

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

Several situations of mysql index failure Several situations of mysql index failure Feb 21, 2024 pm 04:23 PM

Common situations: 1. Use functions or operations; 2. Implicit type conversion; 3. Use not equal to (!= or <>); 4. Use the LIKE operator and start with a wildcard; 5. OR conditions; 6. NULL Value; 7. Low index selectivity; 8. Leftmost prefix principle of composite index; 9. Optimizer decision; 10. FORCE INDEX and IGNORE INDEX.

The basic principles and methods of implementing inheritance methods in Golang The basic principles and methods of implementing inheritance methods in Golang Jan 20, 2024 am 09:11 AM

The basic principles and implementation methods of Golang inheritance methods In Golang, inheritance is one of the important features of object-oriented programming. Through inheritance, we can use the properties and methods of the parent class to achieve code reuse and extensibility. This article will introduce the basic principles and implementation methods of Golang inheritance methods, and provide specific code examples. The basic principle of inheritance methods In Golang, inheritance is implemented by embedding structures. When a structure is embedded in another structure, the embedded structure has embedded

See all articles