gpt4 book ai didi

mysql - 如何管理分页中的mysql偏移量和限制

转载 作者:行者123 更新时间:2023-11-29 02:43:07 24 4
gpt4 key购买 nike

包含大量数据的表,数据插入率几乎是每秒 5 行。我正在使用 limit 和 offset 以及左连接按 created_date 的降序从该表中获取无限分页的数据(它存储插入时间戳)。

因此,考虑到时间,它恰好从表中获取了重复的数据。

假设,目前我有 1000 条数据,用于:

预期输出:

总记录数:1000

  1. 第一次获取:限制:10,偏移量:0(预期:1000、999、998,....第991章

  2. 第二次获取:限制:10,偏移量:10(预期:990,...,981)

  3. 第三获取:限制:10,偏移量:20(预期:980,...,971)

实际数据:

  1. 总记录数:1000

    第一次获取:限制:10,偏移量:0(实际:1000、999、998、.... 991)

  2. 总记录数:1005

    第二次获取:限制:10,偏移量:10(实际:995,...,986)

    重复记录:995,994,993,992,991

  3. 总记录数:1012

    第三次获取:限制:10,偏移量:20(实际:992,...,983)

    重复记录:992,991,990,989,988,987,986

mysql 中的当前请求或过程是否有任何锁定以正确获取数据而不添加另一个 where 子句,如记录 id 大于第一次这样获取的?

如果解决方案/查询需要任何进一步的信息,请发表评论。

我的查询是:

SELECT `tab_a`.*, `tab_b`.`likes`, `tab_b`.`comment`, `tab_b`.`share` 
FROM `tab_a`
LEFT JOIN `tab_b` ON `tab_a`.`id` = `tab_b`.`post_id`
WHERE post_position IN (?) AND (post_date BETWEEN ? AND ?)
GROUP BY `tab_a`.`id` ORDER BY `tab_a`.`id` DESC, `tab_b`.`created_date` DESC
LIMIT 9 OFFSET 0

最佳答案

尝试将列 tab_a.id 添加到 where 子句中。每次请求查询时,尝试添加最后一个 tab_a.id 的值(假设默认值为最大 tab_a.id=1000)。

第一次查询:

select `tab_a`.*, `tab_b`.`likes`, `tab_b`.`comment`, `tab_b`.`share` from `tab_a` 
left join `tab_b` on `tab_a`.`id` = `tab_b`.`post_id`
where `tab_a`.`id` <= 1000 and post_position in (?) and (post_date between ? and ?)
group by `tab_a`.`id` order by `tab_a`.`id` desc, `tab_b`.`created_date` desc
limit 9 offset 0

第二次查询,第一次查询结果的最后一个tab_a.id是990,那么查询应该是

select `tab_a`.*, `tab_b`.`likes`, `tab_b`.`comment`, `tab_b`.`share` from `tab_a` 
left join `tab_b` on `tab_a`.`id` = `tab_b`.`post_id`
where `tab_a`.`id` <= 990 and post_position in (?) and (post_date between ? and ?)
group by `tab_a`.`id` order by `tab_a`.`id` desc, `tab_b`.`created_date` desc
limit 9 offset 0

关于mysql - 如何管理分页中的mysql偏移量和限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com