MySQL index optimization: paging exploration detailed introduction

MySQL index optimization: paging exploration detailed introduction

MySQL Index Optimization Paging Exploration

Table Structure

CREATE TABLE `demo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'Name',
  `age` int(11) NOT NULL DEFAULT '0' COMMENT 'Age',
  `position` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'Position',
  `card_num` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'Work card number',
  PRIMARY KEY (`id`),
  KEY `index_union` (`name`,`age`,`position`)
) ENGINE=InnoDB AUTO_INCREMENT=450003 DEFAULT CHARSET=utf8;

450003 records

Limit paging execution status

MySQL Index Optimization Paging Exploration_Subnode

Like select * from demo limit 90000,10; considering the table return, MySQL simply chooses to scan the entire table.

MySQL does not count 10 items directly from the 90000th row, but starts counting from the first leaf node and counts 90010 rows.

Case 1

MySQL Index Optimization: Paging Exploration_Paging_02

For the above figure, when the id is continuously incremented, the primary key can be used to filter out the data after id=90000. Because the primary key index is a B+ tree structure, it is ordered.

MySQL Index Optimization Paging Exploration_Data_03

Case 2

MySQL Index Optimization Paging Exploration_Data_04

First sort by name, and then find 10 rows starting from row 90000. Although name is an index, the selected column is not saved in the index_union index tree.

Therefore, it will involve returning to the table, so MySQL directly chooses to scan the leaf nodes of the primary key index tree, first sorts more than 400,000 data according to name, and then calculates 90,000 rows + 10 rows.

Optimization method: Use subqueries to solve the most time-consuming sorting and table return problems. The joint index tree stores the primary key id. If you order by name, you can make full use of the entire index of name, age, and position because the sorting of the leftmost column is determined, and the sorting of the other two columns, age and position, is actually

The sorting is done, and the index tree can also be used for sorting through the Extra field.

The outermost query is related by primary key, so it can be almost ignored. 10+10 Because id is the primary key, you can directly scan 10 records in the temporary table.

MySQL Index Optimization: Paging Exploration_Paging_05

This is the end of this article on the detailed introduction of MySQL index optimization and paging exploration. For more relevant MySQL paging exploration 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:
  • This article teaches you how to optimize MySQL joins without indexes
  • MySQL index principle and query optimization detailed explanation
  • MySQL Data Optimization - Multi-layer Index
  • Detailed explanation of MySQL index selection and optimization
  • MySQL performance optimization index pushdown
  • Optimizing query speed of MySQL with tens of millions of data using indexes
  • MySQL optimization and index analysis

<<:  Practice of implementing user login through front-end and back-end interaction of Node.js

>>:  Pure CSS custom multi-line ellipsis problem (from principle to implementation)

Recommend

HTML page jump and parameter transfer issues

HTML page jump: window.open(url, "", &q...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

Solution to the docker command exception "permission denied"

In Linux system, newly install docker and enter t...

Do you know the common MySQL design errors?

Thanks to the development of the Internet, we can...

Detailed tutorial for installing MySQL 8.0.11 compressed version under win10

After reinstalling my computer recently, I downlo...

Super detailed MySQL usage specification sharing

Recently, there have been many database-related o...

The 6 Most Effective Ways to Write HTML and CSS

This article shares the 6 most effective methods,...

How to ensure transaction characteristics of MySQL InnoDB?

Preface If someone asks you "What are the ch...

Defining the minimum height of the inline element span

The span tag is often used when making HTML web p...

Detailed tutorial on using cmake to compile and install mysql under linux

1. Install cmake 1. Unzip the cmake compressed pa...

The iframe frame sets the white background to transparent in IE browser

Recently, I need to frequently use iframe to draw ...

Setting up a proxy server using nginx

Nginx can use its reverse proxy function to imple...