获得与Doctrine的随机记录 [英] Get random records with Doctrine

查看:128
本文介绍了获得与Doctrine的随机记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从一个小组中随机抽取一些成员,但是我不知道最好的方法是什么,而我认为 ORDER BY RAND()不是最好的选择,因为一个组可以有超过100,000个成员,执行这种类型的查询可能会很慢。

I wonder how to get a random number of Members from a Group, but I do not know what is the best way to do this, and I think ORDER BY RAND() is not the best alternative, as a Group can have more than 100,000 Members, performing this type of query could be very slow.

我发现这样做使SQL,但我不知道如何在DQL中做同样的事情:如何优化MySQL的ORDER BY RAND()函数?

I found this way to make using SQL, but I do not know how to do the same thing in DQL: How can i optimize MySQL's ORDER BY RAND() function?

推荐答案

没有意识到任何方式可以有效地从教条中获得。在你的情况下,最好的事情可能是首先获得主键,洗牌这些键,然后在IN语句中使用它们。

I'm not aware of any way to ORDER BY RAND() "efficiently" from Doctrine. In your situation, the best thing is probably to get primary keys first, shuffle these keys and then make use of them within an IN statement.

您还可以添加一个缓存图层,放置第一个查询中的键的一个子集(特别是如果您有很多记录),以避免重复查询每次按键。

You could also add a caching layer where to put (a subset of) the keys from the first query, especially if you have many records, so to avoid repeating the query on keys each time.

这篇关于获得与Doctrine的随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆