当前位置:网站首页>MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
2022-07-27 12:04:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
LIMIT Clauses can be used to enforce SELECT Statement returns the specified number of records .LIMIT Accept one or two numeric parameters . Argument must be an integer constant . If two parameters are given , The first parameter specifies the offset of the first return record line , The second parameter specifies the maximum number of rows to return records . The offset of the initial record line is 0( instead of 1). below , For special cases, we are right mysql Paging query to summarize .
mysql Provide paging function :SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
The simplest use is :select * from table limit ?,?
This is the simplest limit Paging query . coordination where Conditional use :select * from table where column > ? order by id limit ?,?
This is the case above , It is suggested that column and id It is better to establish a composite index .
In both cases , For paging queries with small amounts of data , In this way sql It's enough . But for data sheets above the million level , If you use the above sql Words , The later limit The offset of the statement is getting larger and larger , Queries will become slower and slower . Be similar to :select * from `user` where `cate`=’ shaanxi ’ order by id limit 100000,10
To avoid such queries , We can improve query efficiency by sub query .select * from `user` where id >=(select * from `user` where `cate`=’ shaanxi ’ order by id limit 100000,1) and `cate`=’ shaanxi ’ limit 10
adopt explain We can see the direct limit The gap between query and sub query : direct limit Inquire about :typepossible_keyskeykey_lenrefrowsExtraALL(NULL)(NULL)(NULL)(NULL)4076607 Sub query paging query :typepossible_keyskeykey_lenrefrowsExtraPRIMARYrangePRIMARYPRIMARY42038331Using where
SUBQUERYindex(NULL)PRIMARY44076663Using index
It can be seen that , Through subqueries , Subqueries are performed on the index , While ordinary queries are carried out on data files . Generally speaking , The index file is much smaller than the data file , Therefore, the operation of index files is more direct and efficient .
Besides , You can also use join Pagination SELECT * FROM `user` AS t1
JOIN (SELECT id FROM `user` ORDER BY id LIMIT 100000, 1) AS t2
WHERE t1.id <= t2.id ORDER BY t1.id LIMIT 10;
join The efficiency of paging and subquery paging is basically at one level .( however , Subqueries need to create temporary tables in memory , When the query is finished ,MySQL Need to undo these temporary tables . adopt join This can be avoided ) Before paging query , Can be judged , If it is within the limited number of pages , Just use basic paging query , Greater than, use sub query paging .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/128200.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

shell编程之免交互

TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值

go入门篇 (4)

Principle, concept and construction process of MySQL database master-slave replication cluster

【机器学习-白板推导系列】学习笔记---条件随机场

LNMP architecture setup (deploy discuz Forum)

MySQL数据库主从复制集群原理概念以及搭建流程
![[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution](/img/cc/a27db6c5a380102029b85255df79c9.png)
[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution

STS download tutorial (the solution cannot be downloaded on the include official website)

图像分割 vs Adobephotoshop(PS)
随机推荐
CH340模块无法识别/烧写不进的一种可能性
广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
kazoo使用教程
Sword finger offer notes: t58 - ii Rotate string left
Finding the finite zero point of transfer function under different sampling periods
Weibo comment crawler + visualization
Guangdong: fire safety supervision is no longer "absent" in new industries and new formats such as script killing
A possibility that ch340 module cannot be recognized / burned
Packet transmission: application layer - kernel - hardware
V-show failure
【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布
CLS 监控告警:实时保障线上服务高可用性
求不同采样周期下的传递函数有限零点
Common power supply problems and solutions of Arduino
Some commonly used shortcut keys for MathType
Difference between verification and calibration
基于bolt数据库实现简单的区块链 day(2)
Greek alphabet reading
torch‘ has no attribute ‘inference_ mode‘