当前位置:网站首页>Three schemes to improve the efficiency of MySQL deep paging query
Three schemes to improve the efficiency of MySQL deep paging query
2022-07-04 13:27:00 【1024 questions】
Development often meets the need of paging query , But when you turn too many pages , Deep paging will occur , This leads to a sharp decline in query efficiency . Is there any way , It can solve the problem of deep paging ? This paper summarizes three optimization schemes , Query efficiency is directly improved 10 times , Let's study together .
Development often meets the need of paging query , But when you turn too many pages , Deep paging will occur , This leads to a sharp decline in query efficiency .
Is there any way , It can solve the problem of deep paging ?
This paper summarizes three optimization schemes , Query efficiency is directly improved 10 times , Let's study together .
1. Prepare the dataFirst create a user table , Only in create_time Index the fields :
CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT COMMENT ' Primary key ', `name` varchar(255) DEFAULT NULL COMMENT ' full name ', `create_time` timestamp NULL DEFAULT NULL COMMENT ' Creation time ', PRIMARY KEY (`id`), KEY `idx_create_time` (`create_time`)) ENGINE=InnoDB COMMENT=' User table ';
Then insert 100 10000 test data , Here you can use stored procedures :
drop PROCEDURE IF EXISTS insertData;DELIMITER $$create procedure insertData()begin declare i int default 1; while i <= 100000 do INSERT into user (name,create_time) VALUES (CONCAT("name",i), now()); set i = i + 1; end while; end $$call insertData() $$
2. Verify the deep paging problem each page 10 strip , When we look up the first page , fast :
select * from user where create_time>'2022-07-03' limit 0,10;
In less than 0.01 It returns directly within seconds , So the execution time is not displayed .
When we turn to No 10000 Page time , Query efficiency drops sharply :
select * from user where create_time>'2022-07-03' limit 100000,10;
Execution time becomes 0.16 second , The performance has decreased at least dozens of times .
Where is the time spent ?
Need to scan before 10 Data , Large amount of data , More time-consuming
create_time It's a non clustered index , You need to query the primary key first ID, Go back to the table again , Through primary key ID Query out all fields
Draw the back table query process :
1. Through the first create_time Query the primary key ID2. Through the primary key ID Query all fields in the tableDon't ask why B+ The structure of the tree is like this ? Asking is the rule .
You can take a look at the first two articles .
Then we will optimize for these two time-consuming reasons .
3. Optimized query 3.1 Use subqueryFirst, use the sub query to find out the qualified primary keys , Then use the primary key ID Make conditions to find out all fields .
select * from user where id in ( select id from user where create_time>'2022-07-03' limit 100000,10);
However, such a query will report an error , It is said that the use of limit.
We add a layer of sub query nesting , That's all right. :
select * from user where id in ( select id from ( select id from user where create_time>'2022-07-03' limit 100000,10 ) as t);
The execution time is shortened to 0.05 second , Less 0.12 second , It is equivalent to that the query performance is improved 3 times .
Why use subquery to find qualified primary keys first ID, It can shorten the query time ?
We use it explain Just look at the implementation plan :
explain select * from user where id in ( select id from ( select id from user where create_time>'2022-07-03' limit 100000,10 ) as t);
You can see Extra Column displays the... Used in the subquery Using index, Indicates that the overlay index is used , Therefore, sub queries do not need to be queried back to the table , Speed up query efficiency .
3.2 Use inner join Relational queryTreat the result of subquery as a temporary table , Then perform an association query with the original table .
select * from user inner join ( select id from user where create_time>'2022-07-03' limit 100000,10) as t on user.id=t.id;
Query performance is the same as using subqueries .
3.3 Use paging cursor ( recommend )The implementation method is : When we look up the second page , Put the query results on the first page into the query conditions on the second page .
for example : First, check the first page
select * from user where create_time>'2022-07-03' limit 10;
Then check the second page , Put the query results on the first page into the query conditions on the second page :
select * from user where create_time>'2022-07-03' and id>10 limit 10;
This is equivalent to querying the first page every time , There is no deep paging problem , Recommended .
Execution time is 0 second , The query performance has been directly improved by dozens of times .
Although such a query method is easy to use , But it brings another problem , Is to jump to the specified number of pages , You can only turn down one page .
So this kind of query is only suitable for specific scenarios , For example, information APP Home page .
Internet APP Usually in the form of waterfall flow , For example, baidu home page 、 The front page of the headlines , All slide down and turn the page , There is no need to jump to the number of pages .
Don't believe it , You can look at it , This is the waterfall of headlines :
The query result of the previous page is carried in the transmission parameter .
Response data , Returned the query criteria on the next page .
Therefore, the application scenario of this query method is quite wide , Use it quickly .
Summary of knowledge points :
This is about solving MySQL This is the end of the article on deep paging inefficiency . I hope it will be helpful for your study , I also hope you can support the software development network .
边栏推荐
- 爬虫练习题(一)
- runc hang 导致 Kubernetes 节点 NotReady
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- Read the BGP agreement in 6 minutes.
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- Apache服务器访问日志access.log设置
- Dgraph: large scale dynamic graph dataset
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- 读《认知觉醒》
- Master the use of auto analyze in data warehouse
猜你喜欢
Practice: fabric user certificate revocation operation process
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
强化学习-学习笔记1 | 基础概念
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
实时云交互如何助力教育行业发展
Reinforcement learning - learning notes 1 | basic concepts
室外LED屏幕防水吗?
A data person understands and deepens the domain model
8个扩展子包!RecBole推出2.0!
随机推荐
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Dgraph: large scale dynamic graph dataset
Cors: standard scheme of cross domain resource request
PostgreSQL 9.1 soaring Road
Runc hang causes the kubernetes node notready
Read the BGP agreement in 6 minutes.
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Comprehensive evaluation of modular note taking software: craft, notation, flowus
上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
在 Apache 上配置 WebDAV 服务器
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
Reptile exercises (I)
runc hang 导致 Kubernetes 节点 NotReady
SQL statement syntax error in test SQL statement deletion in eclipse linked database
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Two dimensional code coding theory
8 expansion sub packages! Recbole launches 2.0!
7、 Software package management
Oracle 被 Ventana Research 评为数字创新奖总冠军