当前位置:网站首页>MySQL principle and optimization: Limit the query optimization
MySQL principle and optimization: Limit the query optimization
2022-08-09 16:03:00 【InfoQ】

Suppose there is a table tb_sku whose tableThe structure is as follows.

Approximately in the tableThere are 200w records, it takes about 4.36s to execute the following sql statement to return data
select count(*) from tb_sku;

Then we use to paginate it:
select * from tb_skulimit 0,10;
limit statement where 0 represents the starting position and 10 is the amount of data returned per page.

As shown abovedisplay, the query results are returned soon.
Then we use the SQL statement
select * from tb_sku limit 10,10;
The statement starts from the record position 10 and returns 10 items downrecord, that is, the information on the second page.The return time is also faster.
Then, we increase the starting position to 100w as follows,
select * from tb_sku limit 1000000,10;

At this time, the return time takes 0.74 s, which shows that the use of limit is not suitable for largeThe table with the amount of data is paginated, and the later the position is, the less efficient it is.Take the above example, limit will sort 100w data first, then return 10 data, and only return 100w to 100w zero 10 records, other query records will be discarded, this method of query sortingThe price is very high.
Therefore, we need to optimize the limit operation of the large data scale. The official solution is to use the method of
covering index and subquery
Optimization
According to this idea, first query the id
select id from tb_sku order by id limit 1000000,10;

The query result only takes 0.34s which is much faster than the previous 0.74s.The reason is that because the information of the id is directly returned, there is no return table operation, so the speed of select * is faster.
Because we need to obtain the information of select *, that is, the information of all fields of tb_user, soThe above query results need to be jioned with tb_user.
select s.* from tb_sku s,(select id from tb_sku order by id limit 1000000,10 ) t where s.id = t.id;

The query result is shortened here by query id and sub queryIt is 0.38s, which is twice the query time compared to the previous way of directly using select *.
边栏推荐
- 【Database】Sqlserver如何定时备份数据库和定时清除
- C语言程序设计笔记(浙大翁恺版) 第十二周:程序结构
- 极限挑战,如何做到分钟级搭建环境?
- spacedesk-notebook, tablet, extended screen-solve the problem that the tablet font is too small
- vivo手机上的系统级消息推送平台的架构设计实践
- Computational Imaging Technology
- 基于微信云开发的幼儿园招生报名小程序
- Item 37: Make std::threads unjoinable on all paths.
- Zero Time Technology | Nomad cross-chain bridge theft of 180 million US dollars incident analysis
- [Microservice] Detailed explanation of nacos registration center and configuration center
猜你喜欢
随机推荐
Mysql两个引擎对比
和月薪5W的测试聊过后,才知道自己一直在打杂...
FilenameFilter过滤文件名
Computer Graphics From Scratch - Chapter 5
项目小操作:form表单的校验,以及第二次显示提示校验内容等问题
回归测试:意义、挑战、最佳实践和工具
shell之函数和数组
【Message Center】Architecture Preparation
R7 6800H标压处理器+RTX 3050独显 无畏Pro15锐龙版高能开卖
二维数组实现八皇后问题
Selenium - 如何用xpath快速定位路径?
C语言程序设计笔记(浙大翁恺版) 第二周:计算
玩转云端 | 天翼云电脑的百变玩法
leetcode 剑指 Offer 07. 重建二叉树
Mongodb增加权限管理
[DevOps] jekins configuration (2)
DSPE-PEG-Hydrazide, DSPE-PEG-HZ, Phospholipid-Polyethylene Glycol-Hydrazide MW: 1000
Startup error: Caused by: org.apache.ibatis.binding.BindingException summary solution
spacedesk-notebook, tablet, extended screen-solve the problem that the tablet font is too small
VMWare不使用简易安装,手动安装ISO操作手册









