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:
|
<<: Using nginx + fastcgi to implement image recognition server
>>: How to use a game controller in CocosCreator
Achieve results html <div class="containe...
Negative distance refers to empathy. Preface (rai...
1. Design source code Copy code The code is as fol...
In this experiment, we configure MySQL standard a...
1. Multiple borders[1] Background: box-shadow, ou...
Preface In WeChat applet development (native wxml...
Installation Script Ubuntu / CentOS There seems t...
Install the unzipped version of MySql database un...
mysql returns Boolean type In the first case, ret...
As a software developer, you must have a complete...
1. Multiple calls to single arrow Once a single a...
When configuring web.xml for tomcat, servlet is a...
Hello everyone, today when I was looking at the H...
Table of contents 1. Scope 1. Global scope 2. Loc...
For example, to query yesterday's newly regis...