简体   繁体   English

通过API对未排序和过滤的数据库数据进行分页的最佳方法是什么?

[英]What's the best way to paginate unsorted and filtered database data via API?

I have been stuck with this problem for a while where I have to fetch data for a home page, like doing multiple queries to filter data by popularity, by most viewed etc. and merging all into a single query using "union". 我一直被这个问题困扰了一段时间,在那里我必须获取主页的数据,例如执行多个查询以按受欢迎程度,浏览最多的人等过滤数据,并使用“联合”将所有内容合并到一个查询中。 So the query orders them automatically by importance, for example featured goes first then most popular records go next and then most viewed and so on. 因此,查询会按重要性自动对它们进行排序,例如,功能排在第一位,然后是最受欢迎的记录,然后是查看最多的等等。 The data changes from time to time if there is some new record, or some record becomes more popular than the other it might swap the order. 如果有一些新记录,或者某些记录变得比其他记录更受欢迎,则数据可能会不时更改,从而可能会交换顺序。 However, when I fetch more data via a pagination or "load more", and at the same time some record swapped places with another one in the background, then this record would be shown again in the next page, which makes it redundant since it showed also on the first. 但是,当我通过分页获取更多数据或“加载更多”数据,同时某些记录在后台与另一记录交换位置时,​​该记录将在下一页再次显示,这使其变得多余,因为也显示在第一位。

I checked out some twitter API algorithms with since_ID and max_ID, but in my case they don't help since I don't sort them by ID or any specific order, and this is where the complexity arises. 我检查了一些具有since_ID和max_ID的twitter API算法,但是在我的情况下,它们无济于事,因为我没有按ID或任何特定顺序对它们进行排序,这就是其中的复杂性所在。

So how exactly am I supposed to deal with redundant data in this case? 那么在这种情况下我应该如何处理冗余数据呢? Has anyone ever had similar experiences? 有没有人有过类似的经历?

Thanks in advance! 提前致谢!

When you "load more", you can send the set of ID that is already displayed and consequently exclude them with an additionnal condition in your sql queries " and id not in ([excluded set here]) ". 当“加载更多”时,您可以发送已经显示的ID集合,从而在sql查询中以“附加条件”将它们排除,而“ id not in([[excluded set here]]””中则不包含。

However with a pagination system, it gets too complex since you have to pass the set of all page visited and you don't control the order of visit, it would turn into a complete mess. 但是,对于分页系统,由于您必须传递所有已访问页面的集合并且您无法控制访问顺序,因此它变得太复杂了,这将变成一团糟。 So with pagination I would recommend you simply let your ranking be and eventually cache it for X minutes. 因此,通过分页,我建议您简单地保留您的排名,并最终将其缓存X分钟。 So all the users experience the same ranking for a few minutes, and pages never show duplicate content. 因此,所有用户在几分钟内都会经历相同的排名,并且页面永远不会显示重复的内容。 Also, to improve user experience you can add a visual feedback when the ranking is updated, which provide a sense of interactivity. 同样,为了改善用户体验,您可以在排名更新时添加视觉反馈,从而提供互动感。

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

相关问题 通过PHP使用Twitter API的最佳方法是什么? - What's the best way to use the Twitter API via PHP? PHP / MySQL:从登录用户输入的数据库中获取过滤数据的最佳方法是什么? - PHP/ MySQL: What is the best way to get filtered data from a database that has been inputted by a logged in user? 将html数据存储在mysql数据库中的最佳方法是什么? - What's the Best way to store html data in a mysql database? 重构数据库及其数据的最佳方法是什么? (Laravel 5.2) - What's the best way to refactor a database and its data? (Laravel 5.2) 从数据库检索数据并将其作为变量传递的最佳方法是什么 - What's the best way to retrieve data from a database and pass it as variables 在 php 中对结果进行分页的最佳方法是什么 - What is the best way to paginate results in php 在数据库中保存会话的最佳方法是什么? - what's the best way to save sessions in a database? 获取关系数据的最佳方法是什么? - What's the best way to get data with relations? 通过getJSON将变量发送到数据库查询的最佳方法是什么? - What is the best way to send variable via getJSON to database query? 将表单数据放入数组中,将它们传递给构造函数然后插入数据库的最佳方法是什么? - What's the best way to put form data in an array, pass them to a constructor, then insert into the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM