Anyone know how to do this - just what would be the equivalent to this: "select * from YOUR_TABLE order by rand() limit 1" in mysql??
Maybe not possible in SDB?
Anyone know how to do this - just what would be the equivalent to this: "select * from YOUR_TABLE order by rand() limit 1" in mysql??
Maybe not possible in SDB?
我实际上与亚马逊代表谈过这个问题。您应该做的是将随机值与您的数据一起存储在 SDB 中。当您想要返回一行时,您会生成另一个随机值并选择小于该值的第一个结果。所以你需要存储更多的数据,但它只需要一个查询。
我意识到这个问题发布已经超过 18 个月了,但如果其他人需要这个,我还是会发布我的答案。如果选择的一致性不重要,那么贾斯汀描述的方法是正确的。但是,如果一致性很重要,那么您可以使用贾斯汀方法的改编版(在非常粗糙的伪代码中):
Generate a random value
Generate a random boolean
If the boolean is true {
Select the first item with a randomizer less than or equal to the random value
}
otherwise {
Select the first item with a randomizer greater than or equal to the random value
}
Generate a new random value
Set the selected items randomizer to the new random value
我在我的博客上对此进行了更深入的撰写,并举例说明了概率分布被破坏的原因。
不,SimpleDB 中没有随机函数类型。您必须自己实现随机部分。
您可以执行以下操作:
count = sdb.select("select count(*) from YOUR_TABLE")
random = (rand() * count) + 1
nextToken = sdb.select("select count(*) from YOUR_TABLE limit " + random)
item = sdb.select("select * from YOUR_TABLE limit 1" , nextToken)
但这至少需要三个查询。必须重复第一个,直到获得完整计数(没有 NextToken)。除非您从第一组查询中保存了一些 NextToken,否则需要重复第二个直到您达到随机计数(每个请求最多 2500 个)。
总之不是很方便。