当前位置:网站首页>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
边栏推荐
- After Party A's hard work, 49.08 million orders of China Mobile were scrapped
- go入门篇 (4)
- JS parasitic combinatorial inheritance
- Matlab S-function详解
- Principle of PWM and generation of PWM wave
- Idea: can't use subversion command line client: SVN solution
- Common power supply problems and solutions of Arduino
- 解决方案:idea project没有显示树状图
- The difference between microcomputer and single chip microcomputer
- 【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
猜你喜欢

基于反馈率的控制系统原理
新版数据仓库的同步使用参考(新手向)

Could not load dynamic library ‘libcudnn.so.8‘;

解决方案:idea project没有显示树状图

Newton-Raphson迭代法
![[untitled] multimodal model clip](/img/f0/8ae72ae0845372b6fe2866fae83f52.png)
[untitled] multimodal model clip

Finding the finite zero point of transfer function under different sampling periods

MATLAB画带延时系统的伯德图

Vscode removes style / syntax highlighting / code highlighting / black background when copying code

Source code compilation and installation lamp
随机推荐
Docker Mysql的使用note
Detailed explanation of MATLAB S-function
go入门篇 (5)
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
剑指 Offer 笔记: T39. 数组中出现次数超过一半的数字
Firewall firewall
求不同采样周期下的传递函数有限零点
快抖抢救“失意人”
Shell script text three swordsmen sed
基于反馈率的控制系统原理
NPM step pit
V-show failure
系统临时文件的写和读:createTempFile和tempFileContent[通俗易懂]
matlab二分法例题(用二分法求零点例题)
Can you really write binary search - variant binary search
检定和校准的区别
MATLAB画带延时系统的伯德图
Weibo comment crawler + visualization
Difference between verification and calibration
How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)