简体   繁体   English

MySQL查询结果偏移量与新结果可能的限制?

[英]MySQL Query Result Offset With Limit With New Result Possibility?

I'm working on an application that uses a scroll load system of 20 or so results loading into a feed at a time as you scroll. 我正在开发一个使用20左右滚动加载系统的应用程序,结果在您滚动时一次加载到Feed中。 This feed consists of constantly added user generated content. 此提要包含不断添加的用户生成的内容。 Which means the result set can change per query that is offset by X. 这意味着每个查询的结果集可以更改,且偏移X。

So let's say we load 20 results, then scroll, another 20, and then before scrolling more to load the next 20, another user has uploaded a new piece of content, which effectively would present a duplicate in the next set of 20 results for the feed because we're using OFFSET to get additional results, and the total result set is getting shifted by 1 with this addition of new content that falls into the conditions of the query. 假设我们先加载20个结果,然后再滚动20个,然后再滚动以加载下一个20个结果,则另一个用户上传了新内容,这实际上将在下一组20个结果中显示一个重复项feed,因为我们正在使用OFFSET来获取其他结果,并且由于添加了新内容(属于查询条件),因此总结果集已偏移1。

What is the best and most efficient way around this? 最好的和最有效的方法是什么? We've dabbled with using the id of a row in a where condition to prevent duplicate results, and only using limit without offset for new results fetched.. so we could do WHERE id < 170 LIMIT 20, WHERE id < 150 LIMIT 20, WHERE id < 130 LIMIT 20, etc.. to control and prevent duplciates... HOWEVER, this does not work in every possible scenario as our result sets aren't always ordered with the id column ordered by DESC.. 我们已经尝试在where条件中使用行的id来防止重复结果,并且仅对获取的新结果使用没有偏移的限制..因此我们可以执行WHERE id <170 LIMIT 20,WHERE id <150 LIMIT 20, id <130 LIMIT 20等等,以控制和防止重复出现...但是,这并非在所有可能的情况下都有效,因为我们的结果集并非总是按DESC排序的id列排序。

Soo.. what other options are there?.. o ..还有什么其他选择?..

You might want to try a database solution. 您可能想尝试数据库解决方案。 On the initial request, create and populate a table. 根据初始请求,创建并填充一个表。 Use that table for the feed. 使用该表作为提要。

Make sure the tablename starts with something consistent, like TableToFeedMyApp, and ends with something guaranteed to make it unique. 确保表名以一致的开头(例如TableToFeedMyApp),并以保证使其唯一的结尾。 Then set up a scheduled job to find and drop all these tables that were created earlier than whatever you deem to be a certain interval. 然后设置一个计划作业,以查找和删除所有早于您认为一定间隔的表。

Why are you using the where clause instead of limit with the offset option? 为什么在偏移量选项中使用where子句而不是limit Limit can take two arguments. Limit可以采用两个参数。 The offset argument seems to do exactly what you want. offset参数似乎完全可以满足您的要求。 For instance: 例如:

limit 100, 20

Takes 20 rows starting at the 101st row. 从第101行开始进行20行。 Then: 然后:

limit 120, 20

Takes 20 rows starting at the 121st row. 从第121行开始进行20行。 (The offsets start at 0 rather than 1 in MySQL counting.) (偏移量从0开始而不是MySQL计数中的1。)

The one enhancement that you need to make is to ensure that the sort order for the records is stable . 您需要做的一项增强功能是确保记录的排序顺序是稳定的 A stable sort is, essentially, one where there are no sort keys with the same value. 本质上,稳定排序是没有排序键具有相同值的排序。 To make this happen, just make the id column the last column in the sort. 为此,只需将id列作为排序的最后一列即可。 It is unique, so if there are any duplicate sort keys, then the addition of the id makes the sort stable. 它是唯一的,因此,如果有任何重复的排序键,则添加id会使排序稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM