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

Detailed tutorial on installing MySQL 8.0.20 database on CentOS 7

Related reading: MySQL8.0.20 installation tutoria...

CSS to achieve the effect of rotating flip card animation

The css animation of the rotating flip effect, th...

Implementing Binary Search Tree in JavaScript

The search binary tree implementation in JavaScri...

Tutorial diagram of installing mysql8.0.18 under linux (Centos7)

1 Get the installation resource package mysql-8.0...

MySQL 8.0.20 installation tutorial and detailed tutorial on installation issues

Original address: https://blog.csdn.net/m0_465798...

Multiple methods to modify MySQL root password (recommended)

Method 1: Use the SET PASSWORD command MySQL -u r...

Detailed explanation of how to install PHP7 on Linux

How to install PHP7 on Linux? 1. Install dependen...

Summary of the dockerfile-maven-plugin usage guide

Table of contents pom configuration Setting.xml c...

js development plug-in to achieve tab effect

This article example shares the specific code of ...

Beginners learn some HTML tags (1)

Beginners can learn HTML by understanding some HT...

html base url tag

Its function is to set a global style. Then your s...

Detailed explanation of custom swiper component in JavaScript

Table of contents Effect display Component Settin...