Implementation of MySQL joint index (composite index)

Implementation of MySQL joint index (composite index)

Joint Index

The definition of the joint index in this article is (MySQL):

ALTER TABLE `table_name` ADD INDEX (`col1`,`col2`,`col3`);

Advantages of joint indexes

If there is more than one SQL statement, two conditions are required.

SELECT * FROM `user_info` WHERE username='XX',password='XXXXXX';

When the index is searching the password field, the amount of data is greatly reduced, the index hit rate is reduced, and the efficiency of the index is increased.

The size of a matching index is smaller than that of a single index, and it is just an index tree, which saves more time and space complexity than a single column index.

The essence of joint index hits (understanding of the leftmost match)

definition

When creating a ( col1 , col2 , col3 ) joint index, it is equivalent to creating a ( col ) single-column index. For the ( clo1 , clo2 ) joint index and the ( col1 , col2 , col3 ) joint index to take effect, you can only use the three combinations of col1 and col1 , col2 and col1 , col2 , col3 ; of course, the combination of col1 and col3 is also possible, but in fact only the index of col1 is used, col3 is not used!

Diagram

Popular understanding

A joint index is like a phone book organized by姓氏——名字. You can only hit the index if you first determine the last name. The following statements can correctly hit the joint index (the fields directly in = and IN can be in random order, and the MySQL query optimizer can optimize it into an index-recognized form)

-- Only hit col1, col2
SELECT * FROM `table_name` WHERE `col1`='XX';
-- hit col1, col2. The order of col1 and col2 can be reversed SELECT * FROM `table_name` WHERE `clo1`='XX',`clo2`='XXX'; 
SELECT * FROM `table_name` WHERE `clo2`='XXX', `clo1`='XX';
-- hit col1,col2,col3. Similarly, the order of the three columns can be reversed. SELECT * FROM `table_name` WHERE `col1`='X',`col2`='XX',`col3`='XXX';
SELECT * FROM `table_name` WHERE `col1`='X',`col3`='XX',`col2`='XXX';
SELECT * FROM `table_name` WHERE `col2`='X',`col3`='XX',`col1`='XXX';

This is the end of this article about the implementation of MySQL joint index (compound index). For more relevant MySQL joint index content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of MySQL composite indexes
  • In-depth study of MySQL composite index
  • Optimize Mysql limit, reference the compound index of fast paging from one million to ten million and apply it to lightweight framework
  • Optimize Mysql limit, reference the compound index of fast paging from one million to ten million and apply it to lightweight framework
  • How does the composite index of MySQL take effect?

<<:  HTML tag ID can be a variable

>>:  Operations of communication between Docker containers and external network communication

Recommend

JS operation object array to achieve add, delete, modify and query example code

1. Introduction Recently, I helped a friend to ma...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...

vue-table implements adding and deleting

This article example shares the specific code for...

Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

Multi-way search tree Height of a complete binary...

Detailed installation process of nodejs management tool nvm

nvm nvm is responsible for managing multiple vers...

Connector configuration in Tomcat

JBoss uses Tomcat as the Web container, so the co...

js implements form validation function

This article example shares the specific code of ...

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

100-1% of the content on the website is navigation

Website, (100-1)% of the content is navigation 1....

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

HTML table only displays the outer border of the table

I would like to ask a question. In Dreamweaver, I...

Keepalived implements Nginx load balancing and high availability sample code

Chapter 1: Introduction to keepalived The purpose...

How to use negative margin technology to achieve average layout in CSS

We usually use float layout to solve the compatib...

Detailed explanation of the installation process of Jenkins on CentOS 7

Install Jenkins via Yum 1. Installation # yum sou...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...