2017-11-18 230 views
2

一个包含大量数据的表,其中数据插入速率几乎为每秒5行。 我正在使用限制和偏移量连同左连接按照created_date(它存储插入时间戳)降序的无限分页从该表中获取数据。如何管理mysql偏移和分页限制

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

假设,目前我有1000个数据,为:

预期输出:

总记录:1000

  1. 第一抓取:极限:10,偏移量:0(预期:1000,999,998,... 991)

  2. 第2次提取:限制:10,偏移量:10(Exp ected: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 
+0

您的查询是没有意义的,这让我在一个小的损失,从而进一步帮助。请参阅https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry

回答

1

尝试添加列tab_aid放入where子句。每次请求查询时,尝试添加最后一个值tab_aid(假设默认为最大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_aid从第一次查询的结果为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 
+0

我想你只理解这个问题。感谢您的回复。但我正在寻找除了添加where子句之外的其他解决方案。 – subhajit