当前位置:网站首页>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
边栏推荐
- Practical project: boost search engine
- 2019 biometric forum successfully ended: these ten highlights should not be missed!
- A13处理器成iPhone11系列最大亮点:性能是麒麟980两倍!
- 面试官问:JS的this指向
- 华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
- JSON formatting gadget -- pyqt5 instance
- 实战项目:Boost搜索引擎
- [paper reading] logan:membership influence attacks against generative models
- 证监会:同意传音控股科创板IPO注册
- Science | 华盛顿大学利用AI和结构预测设计全新蛋白质
猜你喜欢

Arduino experiment I: two color lamp experiment

Incremental secure file system SFS based on C language design

Do you know the common core types of magnetic ring inductors?

Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education

比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算

科研太忙无法顾家?陈婷:人生不能只有一个支点

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

基本的SELECT语句

PostgreSQL and Navicat: the backbone of the database industry
![[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version](/img/70/5638080a2d2eabf6ae1f2a8db3abe6.png)
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version
随机推荐
After working for one year, I have some insights (written in 2017)
MySQL 数据的导入
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ )
STM32-如何使用串口
Dao:op token and non transferable NFT are committed to building a new digital democracy
Eureka基本使用
Reinforcement learning weekly 55: lb-sgd, msp-drl & robust reinforcement learning against
P5469 [NOI2019] 机器人(拉格朗日插值、区间dp)
Interview: your most impressive bug, for example
The JSON string is converted into a JSON object, the value of a key is obtained, and whether a key exists is judged
Counter attack dark horse: devdbops training, give you the best courses!
Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP
App information reconnaissance & night God simulator burp packet capture configuration
Cloud native microservices Chapter 1 server environment description
8-其他编程语言--记录
【HCIP】OSPF 外部路由引入
PostgreSQL and Navicat: the backbone of the database industry
关于 StatefulWidget,你不得不知道的原理和要点!
推动ARM服务器芯片替代X86,华为、飞腾扛起国产化大旗!
【HCIP】OSPF 关系建立