查询Appengine数据存储上的N个随机记录 [英] Querying for N random records on Appengine datastore

查看:103
本文介绍了查询Appengine数据存储上的N个随机记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个返回N个特定种类随机记录的GQL查询。我当前的实现工作正常,但需要N次调用数据存储。如果可能的话,我想让它调用数据存储。

我目前为每种放入数据存储区的类型分配一个随机数。当我查询一个随机记录时,我生成另一个随机数并查询记录> rand ORDER BY asc LIMIT 1。



这有效,但它只返回1条记录我需要做N个查询。任何想法如何使这一个查询?谢谢。

解决方案

底层一个搜索查询调用只能从某个索引返回一组连续的行。这就是为什么一些GQL查询(包括任何对!=的使用)扩展到多个数据存储区调用。

在任何索引中,N个独立的统一随机选择不是。

QED。

您可以使用memcache来存储实体,并降低抓取N其中。或者,如果您不介意索引中的随机选项紧密结合,请在一个查询中选择一个随机选择的(比如说)100块,然后从这些块中随机选取N.既然你有一个已经随机化的领域,那么外部人就不会立即明白N个项目是相关的。至少,直到他们看了大量的样本,并注意到项目A和Z从不出现在同一组中,因为它们在随机索引中相距超过100。如果性能允许,您可以不时重新随机化您的实体。


I'm trying to write a GQL query that returns N random records of a specific kind. My current implementation works but requires N calls to the datastore. I'd like to make it 1 call to the datastore if possible.

I currently assign a random number to every kind that I put into the datastore. When I query for a random record I generate another random number and query for records > rand ORDER BY asc LIMIT 1.

This works, however, it only returns 1 record so I need to do N queries. Any ideas on how to make this one query? Thanks.

解决方案

"Under the hood" a single search query call can only return a set of consecutive rows from some index. This is why some GQL queries, including any use of !=, expand to multiple datastore calls.

N independent uniform random selections are not (in general) consecutive in any index.

QED.

You could probably use memcache to store the entities, and reduce the cost of grabbing N of them. Or if you don't mind the "random" selections being close together in the index, select a randomly-chosen block of (say) 100 in one query, then pick N at random from those. Since you have a field that's already randomised, it won't be immediately obvious to an outsider that the N items are related. At least, not until they look at a lot of samples and notice that items A and Z never appear in the same group, because they're more than 100 apart in the randomised index. And if performance permits, you can re-randomise your entities from time to time.

这篇关于查询Appengine数据存储上的N个随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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