当前位置:网站首页>MySQL random paging to get non duplicate data
MySQL random paging to get non duplicate data
2022-07-26 23:12:00 【tiwayDeng】
demand
From the list page of post bar 

Click into the video details, and you can slide up and down to brush the video
It is required that each time you enter the same video details, the video display that slides up and down should not be repeated
analysis
The difficulty of this requirement is that the video list given by the back end is different from the details and the repetition is minimized
programme :
- If according to id The default sorting is random id then >id Number paging return , There will be a large number of continuous duplicate data
- according to order by rand() return , The probability of recurrence meets the demand, but the amount of data in post bar is relatively large , use rand() Return efficiency is worrying
When used for a large table , This may slow down use ORDER BY RAND() The execution of the function . This is because MySQL You need to sort the entire table to find random values , It's a time-consuming process
Today we are going to talk about the third of the above two schemes , namely MD5( Primary key id) Generating uniqueness 32 Bit character field , When requesting a list from the details page ,, adopt mysql Of substr Function randomly from 32 A part of bit data is intercepted and sorted , On the next page, the data of the next page is still returned according to the intercepted parameters . When entering this detail again, randomly generate the intercepted digits
Realization
SELECT
`posts`.`post_id`,
MD5( `posts_id` ) AS `uid`,
`posts`.`user_id`,
`posts`.`post_content`,
`posts`.`created_at`
FROM
(
SELECT
`post_id`,
`user_id`,
`post_content`,
`created_at`
FROM
`posts`
WHERE
( `posts`.`deleted_at` = 0 )
) `posts`
ORDER BY
SUBSTR(uid, 23, 6 )
offset 0
LIMIT 30

As above sql Shown , But the first page of the request list can be randomly generated 0-32 The number of as SUBSTR(uid, { KaTeX parse error: Expected 'EOF', got '}' at position 6: start}̲, { len} ) Of s t a r t Parameters , If you want to start the second step, you need the front end to pass the previous interception parameters , The back end obtains the data of the next page according to the interception parameters . start Parameters , If you want to start the second step, you need the front end to pass the previous interception parameters , The back end obtains the data of the next page according to the interception parameters . start Parameters , If you want to start the second step, you need the front end to pass the previous interception parameters , The back end obtains the data of the next page according to the interception parameters .len The parameters of can be written directly 4-6 position
// If the intercepted parameters are not transmitted, they will be regenerated
$subKey = $params['sub_key'] ?? 0;
if (empty($subKey)) {
$subKey = rand(0, 32);
}
Next through page Replace offset Parameters can be paged perfectly
When exiting the list and requesting again, re intercepting the character sorting will obtain different data and achieve perfect paging
Our test data is 3 Ten thousand
use rand() Pagination llimit 10 offset 10000 need 0.122~148s
use substr Paged in the form of llimit 10 offset 10000 need 0.082-0.084
边栏推荐
- Restful接口规范
- Parameter analysis and stone jumping board
- Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
- HCIA-R&S自用笔记(20)VLAN综合实验、GVRP
- Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education
- Will the approval in advance affect the formal approval?
- With a total investment of 10billion US dollars, Huahong Wuxi 12 inch wafer factory was officially put into operation
- STM32 how to use serial port
- Hcia-r & s self use notes (19) VLAN configuration and experiment, routing between VLANs
- Apifox -- a better API testing tool than postman
猜你喜欢

利用Go制作微信机器人(一)发送消息

华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应

Monte Carlo search tree (UCT) based on confidence upper bound to realize four sub chess

ESMFold: AlphaFold2之后蛋白质结构预测的新突破

Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP

The JSON string is converted into a JSON object, the value of a key is obtained, and whether a key exists is judged

SQL Basics

PostgreSQL 与 Navicat:数据库行业的中坚力量

Hcia-r & s self use notes (18) campus network architecture foundation, switch working principle, VLAN principle

Programmer growth chapter 29: how to motivate employees?
随机推荐
思立微的反击:汇顶涉案屏下光学指纹专利被宣告无效
Hcia-r & s self use notes (18) campus network architecture foundation, switch working principle, VLAN principle
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version
Eureka basic use
数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款
MySQL 数据的导入
Learn various details and thoughts of chatroom implementation in Muduo
Apifox--比 Postman 还好用的 API 测试工具
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
基于gRPC编写golang简单C2远控
One fingerprint patent has been declared invalid! Huiding and siliwei both officially responded!
黑马瑞吉外卖之新增员工
What if redis memory is full? This is the right way to deal with it
Making wechat robot with go (I) sending messages
Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!
比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
Novice online interview [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
ZTE: more than 50000 5g base stations have been shipped worldwide!
Why am I still writing articles on CSDN? A journey of accompanying learning.
Do you know the common core types of magnetic ring inductors?