Examples of using MySQL covering indexes

Examples of using MySQL covering indexes

What is a covering index?

Creating an index that includes all the fields used in the query is called a covering index.

With a covering index, MySQL only needs to use the index to find and return the data required for the query, without having to perform a table return operation after processing the data using the index.

Covering indexes can complete the query work at one time, effectively reducing IO and improving query efficiency.

Usage Examples

Query statement:

SELECT col2, col3 FROM test_table WHERE col1 = xxx ORDER BY col2;

Create a single-column index:

ALTER TABLE `test_table` ADD INDEX `idx_col1`(`col1`) USING BTREE;

Analysis query:

EXPLAIN SELECT SQL_NO_CACHE col2, col3 FROM test_table WHERE col1 = xxx ORDER BY col2;

Result: After creating an index for col1, type is ref and the idx_col1 index is used.

Modify the index and create a joint index based on the query statement:

ALTER TABLE `test_table` DROP INDEX `idx_col1`;
ALTER TABLE `test_table` ADD INDEX `idx_col1_col2_col3`(`col1`, `col2`, `col3`) USING BTREE;

Analysis query:

EXPLAIN SELECT SQL_NO_CACHE col2, col3 FROM test_table WHERE col1 = xxx ORDER BY col2;

Result: After the joint index is created, type is ref, the idx_col1_col2_col3 index is used, and Extra is Using index, indicating that a covering index is used.

The difference between mysql covering index and joint index

A covering index means that the queried columns can be directly extracted through the index, such as only querying the columns of the primary key list! Or query all columns of the joint index or some columns starting from the left (note that they are in order)!

A joint index does not necessarily retrieve all data from the index; this depends on the column you are querying. For example, the method of select * from table where ××××××; is unlikely to be a covering index. Therefore, if the columns you query can use the joint index, and the columns you query can be obtained through the joint index, for example, you only query the column where the joint index is located or part of the columns starting from the left, this is equivalent to a covering index. Usually, in order to allow the query to use the covering index, the multiple columns of data to be queried are set as a joint index.

Summarize

This is the end of this article about the use of MySQL covering indexes. For more relevant MySQL covering 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:
  • MySQL uses covering index to avoid table return and optimize query
  • Summary of knowledge points about covering index in MySQL
  • How to use MySQL covering index and table return
  • Detailed explanation of MySQL covering index
  • Mysql performance optimization case - covering index sharing
  • Mysql performance optimization case study - covering index and SQL_NO_CACHE
  • Advantages of MySQL covering indexes

<<:  Detailed process of compiling and installing Storm on Kylin V10 server

>>:  How to display the border when td is empty

Recommend

Detailed explanation of the use of umask under Linux

I recently started learning Linux. After reading ...

How to use nginx to build a static resource server

Taking Windows as an example, Linux is actually t...

Detailed explanation of the use of MySQL sql_mode

Table of contents Preface sql_mode explained The ...

Example of Vue routing listening to dynamically load the same page

Table of contents Scenario Analysis Development S...

Linux 6 steps to change the default remote port number of ssh

The default ssh remote port in Linux is 22. Somet...

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

Docker uses root to enter the container

First run the docker container Run the command as...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...

UrlRewriter caching issues and a series of related explorations

When developing a website function, the session c...

Implementation of Docker Compose multi-container deployment

Table of contents 1. WordPress deployment 1. Prep...

Linux Operation and Maintenance Basic System Disk Management Tutorial

1. Disk partition: 2. fdisk partition If the disk...

Two types of tab applications in web design

Nowadays, tabs are widely used in web design, but...

Detailed tutorial on installing and configuring MySql5.7 on Ubuntu 20.04

Table of contents 1. Ubuntu source change 2. Inst...

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...