如何通过

时间:2016-05-06 10:18:09

标签: mysql sql

这里我们使用限制分页

这是我的情景

我的数据库中有一条喜欢的帖子。 我是通过订购最大数量的匹配来获取数据,其限制为0,5左

问题是

我们已经通过最大限度显示数据,第一页的限制为0,5,例如

1) post:hello,Likes:8
2) post:hello1,Likes:8
3) post:hello2,Likes:7
4) post:hello3,Likes:6
5) post:hello4,Likes:5

现在第二页呼叫5,5

6) post:hello5,Likes:5
7) post:hello6,Likes:5
8) post:hello7,Likes:4
9) post:hello8,Likes:3
10) post:hello9,Likes:2

但是如果以防邮件:hello5得到喜欢:在第二页的时候8意味着我得到副本,

因为第一页只有两个喜欢8,但是当走向secong页面时它有三个喜欢8,所以当限制数据时 在page1和page2中已经获取的帖子将是相同的 像这样的出局

运行首页时

1) post:hello,Likes:8
2) post:hello1,Likes:8
3) post:hello2,Likes:7
4) post:hello3,Likes:6
5) post:hello4,Likes:5

第二页

6) post:hello4,Likes:5
7) post:hello5,Likes:5
8) post:hello6,Likes:5
9) post:hello7,Likes:4
10) post:hello8,Likes:3

在这里看到第1页和第2页有相同的数据(帖子:hello4,喜欢:5),因为当调用第二页like8时有三个数据,当获得限制为5的mysql查询时,它会复制已经获取的数据< BR />

这是帖子将到达第三页(帖子:hello9,喜欢:2)

1 个答案:

答案 0 :(得分:0)

你应该意识到一件事:

这可能就是你想要的。

考虑在时间X加载page1。在转到第2页之前,您将花多长时间查看第1页的结果?是几微秒?那么订单在那段时间内不会发生变化。几分钟?在查看下一页时,结果将会过时。您如何确定是时候更新搜索并显示新的更新结果?

如果你仍坚持解决这个问题,我建议制作新表

CREATE TABLE likes_search_orders(search_id varchar(255) not null, post_id bigint(20) not null, order_id biging(20) not null)

将唯一索引设置为search_id-post_id并继续执行以下算法:

  1. 创建随机search_id(如md5(rand(0,1000000)。&#34; - &#34; .time()))

  2. 运行:

    INSERT INTO likes_search_orders (search_id, post_id, order_id) SELECT "search_id", post_id, row_number() over (order by (select NULL)) FROM table_with_likes ORDER BY likes DESC

  3. 使用:

    SELECT l.* FROM table_with_likes l INNER JOIN likes_search_orders o ON o.post_id = l.id AND o.search_id = "search_id" ORDER BY o.order_id DESC LIMIT 5,5

  4. 每次创建新的搜索ID时,您都会冻结&#34;处于该给定时刻的状态中的数据库(至少是事物的顺序,你可能还包括喜欢的那些喜欢的搜索订阅者,然后你甚至可以在给定的时间内计算精确的喜欢数量)

    我还建议使用搜索创建表,在其中存储search_id和创建时的日期。然后,您可以设置一个cron来清理DB中的旧搜索,以便它们不会膨胀DB