MySQL uses covering index to avoid table return and optimize query

MySQL uses covering index to avoid table return and optimize query

Preface

Before talking about covering index, we must first understand its data structure: B+ tree.

First create a table for demonstration (for simplicity, id is created in order):

id name
1 aa
3 k
5 op
8 aa
10 kk
11 k
14 jk
16 ml
17 mn
18 k
19 k
twenty two hj
twenty four io
25 vg
29 jk
31 jk
33 rt
34 ty
35 yu
37 rt
39 rt
41 ty
45 qt
47 ty
53 qi
57 gh
61 dh

The B+ tree index constructed with column values ​​other than the primary key as key values ​​is called a non-clustered index.

The difference between a non-clustered index and a clustered index is that the leaf nodes of a non-clustered index do not store the data in the table, but store the primary key corresponding to the column. If we want to find data, we also need to search in the clustered index based on the primary key. This process of searching for data based on the clustered index is called table return.

B+ Tree

B+ tree and B tree are commonly used data structures for MySQL indexes. B+ tree is a further optimization of B tree. Let's convert the above table into a graph for analysis:

Characteristics of B+ tree:

1. Non-leaf nodes of the B+ tree do not store data, only key values

2. The data of leaf nodes are arranged in order

3. The pages in the B+ tree are connected by a bidirectional linked list

Clustered and non-clustered indexes

B+ tree indexes are divided into clustered indexes and non-clustered indexes according to different storage methods.

Clustered index:

For tables that use InnoDB as the storage engine, all data in the table will have a primary key. Even if you do not create a primary key, the system will create an implicit primary key for you.

This is because InnoDB stores data in a B+ tree, and the key value of the B+ tree is the primary key. All the data in the table is stored in the leaf nodes of the B+ tree.

This kind of B+ tree index that uses the primary key as the key value of the B+ tree index is called a clustered index.

Nonclustered index:

The B+ tree index constructed with column values ​​other than the primary key as key values ​​is called a non-clustered index.

The difference between a non-clustered index and a clustered index is that the leaf nodes of a non-clustered index do not store the data in the table, but store the primary key corresponding to the column. If we want to find data, we also need to search in the clustered index based on the primary key. This process of searching for data based on the clustered index is called table return.

How to use covering index to avoid table back

Why does the table return even though a non-primary key index is used? Simply put, the non-primary key index is a non-clustered index. Only the primary key and the non-primary key index are saved in the tree child nodes of the B+ tree. Only these two fields can be found in one query. If you want to find three fields, you must search the clustered index again. This is a table return.

For example, a new field age is added to the table, and we create an index (non-clustered index) using name.

id name age
10 z twenty three
7 ls 54
13 ww 12
5 zl 76
8 x twenty three
12 xm 43
17 dy twenty one

select id,name from user where name = 'zs';

The name index can be hit. The index leaf node stores the primary key id. The id and name can be obtained through the name index tree without returning to the table. This meets the index coverage requirement and is highly efficient.

select id,name,age from user where name = 'zs';

The name index can be hit. The index leaf node stores the primary key id, but the age field can only be obtained by returning to the table for query, which does not meet the index coverage. It is necessary to scan the code through the id value again to obtain the age field, which will reduce efficiency.

Conclusion: So how can we avoid table return? It's simple, upgrade the single column index (name) to a joint index (name, age).

Summarize

This is the end of this article about MySQL using covering index to avoid table return to optimize query. For more relevant MySQL covering index to avoid table return to optimize query 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 table return and index coverage examples in MySQL
  • MySQL table return causes index invalidation case explanation
  • How to use MySQL covering index and table return
  • How much does MySQL table return performance damage

<<:  Beginners learn some HTML tags (1)

>>:  Docker Compose one-click ELK deployment method implementation

Recommend

Use of Linux cal command

1. Command Introduction The cal (calendar) comman...

Introduction to new features of MySQL 8.0.11

MySQL 8.0 for Windows v8.0.11 official free versi...

Tutorial on how to install and use Ceph distributed software under Linux

Table of contents Preface 1. Basic Environment 1....

How to use HTML 5 drag and drop API in Vue

The Drag and Drop API adds draggable elements to ...

Share some key interview questions about MySQL index

Preface An index is a data structure that sorts o...

How to quickly install RabbitMQ in Docker

1. Get the image #Specify the version that includ...

Modify file permissions (ownership) under Linux

Linux and Unix are multi-user operating systems, ...

Detailed explanation of html-webpack-plugin usage

Recently, I used html-webapck-plugin plug-in for ...

How to replace all tags in html text

(?i) means do not match case. Replace all uppercas...

MySQL select results to perform update example tutorial

1. Single table query -> update UPDATE table_n...

Summary of MySQL 8.0 Online DDL Quick Column Addition

Table of contents Problem Description Historical ...

How to create a web wireframe using Photoshop

This post introduces a set of free Photoshop wire...

How to install setup.py program in linux

First execute the command: [root@mini61 setuptool...

Example of using CSS3 to create Pikachu animated wallpaper

text OK, next it’s time to show the renderings. O...