当前位置:网站首页>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
边栏推荐
- 测试开发是开发吗?
- [paper reading] logan:membership influence attacks against generative models
- Mate30 series release: how far can Huawei go in reconstructing images?
- 实战项目:Boost搜索引擎
- After closing the Suzhou plant, Omron Dongguan plant announced its dissolution, and more than 2000 people are facing unemployment!
- Eureka基本使用
- 基于C语言设计的增量型安全文件系统 SFS
- 逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
- Ribbon负载均衡
- gateway基本使用
猜你喜欢

HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理

STM32-如何使用串口
The memory occupation of the computer is too high after it is turned on (more than 50%)

How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged

研究阿尔茨海默病最经典的Nature论文涉嫌造假

杰理下载器强制下载工具的使用介绍_AC695N696NAD14AD15全系列支持

HCIA-R&S自用笔记(23)DHCP

Regular expressions and bypass case recurrence

Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!

KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新
随机推荐
SQL 基础知识
摩尔定律的新推力,英特尔先进封装技术详解!
Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!
Incremental secure file system SFS based on C language design
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
Eureka basic use
Restful接口规范
[hcip] OSPF external route introduction
SQL multi table query exercise
Interview: your most impressive bug, for example
Mate30 series release: how far can Huawei go in reconstructing images?
ZTE: more than 50000 5g base stations have been shipped worldwide!
New employees of black maredge takeout
Eureka基本使用
A13处理器成iPhone11系列最大亮点:性能是麒麟980两倍!
Network and VPC hands-on experiment
【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版
Reinforcement learning weekly 55: lb-sgd, msp-drl & robust reinforcement learning against
Monte Carlo search tree (UCT) based on confidence upper bound to realize four sub chess
With a total investment of 10billion US dollars, Huahong Wuxi 12 inch wafer factory was officially put into operation