There is a business that queries the 5 most recently reviewed data. SELECT `id`, `title` FROM `th_content` WHERE `audit_time` < 1541984478 AND `status` = 'ONLINE' ORDER BY `audit_time` DESC, `id` DESC LIMIT 5; Checking the monitoring status at that time, the CPU usage was over 100%. View the structure of the table CREATE TABLE `th_content` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(500) CHARACTER SET utf8 NOT NULL DEFAULT '' COMMENT 'Content title', `content` mediumtext CHARACTER SET utf8 NOT NULL COMMENT 'Main content', `audit_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Audit time', `last_edit_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last edit time', `status` enum('CREATED','CHECKING','IGNORED','ONLINE','OFFLINE') CHARACTER SET utf8 NOT NULL DEFAULT 'CREATED' COMMENT 'Information status', PRIMARY KEY (`id`), KEY `idx_at_let` (`audit_time`,`last_edit_time`) )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; The index has a joint index with Analyze the logic of the above sql execution:
Finally, because the amount of data is large, although only 5 rows are taken, according to the extreme example we just gave, 1 million rows of data are actually queried, and finally a memory sort of 500,000 rows of the database is performed in memory. So it is very inefficient. A schematic diagram is drawn to illustrate the query process in the first step. The pink part represents the data rows that need to be queried in the table in the end. In the picture, I forged and filled in some data according to the index storage rules. If there is anything wrong, please leave a message to point it out. I hope that through this picture, you can see the way of joint index storage and index query. Improvement idea 1 Range searches are not easy to use with good indexes. What improvements will there be if we add a joint index of ALTER TABLE `th_content` ADD INDEX `idx_audit_status` (`audit_time`, `status`); mysql> explain select `id`, `title` from `th_content` where `audit_time` < 1541984478 and `status` = 'ONLINE' order by `audit_time` desc, `id` desc limit 5; +----+-------------+------------+-------+------------------------------------------+------------------+--------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+-------+------------------------------------------+------------------+--------+------+--------+-------------+ | 1 | SIMPLE | th_content | range | idx_at_ft_pt_let,idx_audit_status | idx_audit_status | 4 | NULL | 209754 | Using where | +----+-------------+------------+-------+------------------------------------------+------------------+--------+------+--------+-------------+ Details: Because Let's analyze the execution process after adding the index:
In the diagram above, the pink color indicates the rows that meet the index requirements of the first column. If we query forward one by one, we will find three records on this leaf node. Then we need to continue to query to the left at the previous leaf node. Until 5 rows that meet the records are found, and finally return to the table. Improvements Because Disadvantages of this index If If only 4 rows in the first 1 million rows scanned in Improvement idea 2 ALTER TABLE `th_content` DROP INDEX `idx_audit_status`; ALTER TABLE `th_content` ADD INDEX `idx_status_audit` (`status`, `audit_time`); This way, there is no pressure whether it is sorting or returning to the table. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links You may also be interested in:
|
<<: Graphic tutorial on installing CentOS7 on VMware 15.5
>>: Detailed explanation of JavaScript stack and copy
1. Download, install and configure mysql-8.0.15 1...
Application software generally has such business ...
introduction With the widespread popularity of In...
After the docker installation is completed on the...
[Looking at all the migration files on the Intern...
1. When inserting, updating, or removing DOM elem...
1. Introduction to docker-maven-plugin In our con...
1. Filter Example: <!DOCTYPE html> <html...
Written in front A database is essentially a shar...
This article example shares the specific code of ...
The development of Docker technology provides a m...
Development environment windows Development Tools...
When setting the text in the search text box, the...
Everyone is familiar with the meta tag in desktop...
Pseudo-arrays and arrays In JavaScript, except fo...