Analysis of MySQL joint index function and usage examples

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the functions and usage of MySQL joint indexes. Share with you for your reference, the details are as follows:

A joint index is also called a composite index. For composite indexes: MySQL uses the fields in the index from left to right. A query can use only part of the index, but only the leftmost part. For example, the index is key index (a,b,c). It can support the search of three combinations of a | a,b | a,b,c, but does not support the search of b,c. When the leftmost field is a constant reference, the index is very effective.

An index on two or more columns is called a composite index.

The additional columns in the index allow you to narrow the scope of your search, but using one index with two columns is different from using two separate indexes. The structure of a composite index is similar to a phone book, where a person's name consists of a last name and a first name. The phone book is sorted first by last name pairs, and then by first name pairs for people with the same last name. A phone book is very useful if you know the last name, even more useful if you know both the first and last name, but useless if you know only the first name.

So when creating a composite index, you should carefully consider the order of the columns. Composite indexes are useful when you perform searches on all columns in the index or on just the first few columns; they are not useful when you perform searches on just any of the later columns.

For example: create a composite index of name, age, and gender.

create table test(
a int,
b int,
c int,
KEY a(a,b,c)
);

 
Optimum: select * from test where a=10 and b>50
Bad: select * from test where a>50

Optimistic: select * from test order by a
Bad: select * from test order by b
Bad: select * from test order by c

Optimum: select * from test where a=10 order by a
Optimum: select * from test where a=10 order by b
Bad: select * from test where a=10 order by c

Optimum: select * from test where a>10 order by a
Bad: select * from test where a>10 order by b
Bad: select * from test where a>10 order by c

Optimum: select * from test where a=10 and b=10 order by a
Optimum: select * from test where a=10 and b=10 order by b
Optimum: select * from test where a=10 and b=10 order by c

Optimum: select * from test where a=10 and b=10 order by a
Optimum: select * from test where a=10 and b>10 order by b
Bad: select * from test where a=10 and b>10 order by c

 

Indexing principles

1. The fewer indexes, the better

Reason: Mainly when modifying data, every index needs to be updated, which reduces the writing speed.

2. The narrowest field is placed on the left side of the key

3. Avoid file sort, temporary tables and table scans.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Rules for using mysql joint indexes
  • MySQL independent index and joint index selection
  • Implementation of MySQL joint index (composite index)
  • In-depth analysis of the leftmost matching principle of MySQL joint index
  • MySQL joint index usage examples
  • Joint Index Learning Tutorial in MySQL
  • MySQL joint index effective conditions and index invalid conditions

<<:  Using nginx + fastcgi to implement image recognition server

>>:  How to use a game controller in CocosCreator

Recommend

Drop-down menu implemented by HTML+CSS3+JS

Achieve results html <div class="containe...

Negative distance (empathy) - iterative process of mutual influence

Negative distance refers to empathy. Preface (rai...

Form submission refresh page does not jump source code design

1. Design source code Copy code The code is as fol...

A brief discussion on three methods of asynchronous replication in MySQL 8.0

In this experiment, we configure MySQL standard a...

Tips for implementing multiple borders in CSS

1. Multiple borders[1] Background: box-shadow, ou...

WeChat applet example of using functions directly in {{ }}

Preface In WeChat applet development (native wxml...

Summary of Docker common commands and tips

Installation Script Ubuntu / CentOS There seems t...

A brief discussion on several situations where MySQL returns Boolean types

mysql returns Boolean type In the first case, ret...

When you enter a URL, what exactly happens in the background?

As a software developer, you must have a complete...

Example code for drawing double arrows in CSS common styles

1. Multiple calls to single arrow Once a single a...

Detailed explanation of the problem of configuring servlet url-pattern in tomcat

When configuring web.xml for tomcat, servlet is a...

JavaScript implements changing the color of a web page through a slider

Hello everyone, today when I was looking at the H...

A brief discussion on JavaScript scope

Table of contents 1. Scope 1. Global scope 2. Loc...

Mysql optimization techniques for querying dates based on time

For example, to query yesterday's newly regis...