Fetching a random record from the Google App Engine Datastore?

后端 未结 2 756
小蘑菇
小蘑菇 2020-12-10 01:50

I have a datastore with around 1,000,000 entities in a model. I want to fetch 10 random entities from this.

I am not sure how to do this? can someone help?

2条回答
  •  忘掉有多难
    2020-12-10 02:34

    Assign each entity a random number and store it in the entity. Then query for ten records whose random number is greater than (or less than) some other random number.

    This isn't totally random, however, since entities with nearby random numbers will tend to show up together. If you want to beat this, do ten queries based around ten random numbers, but this will be less efficient.

提交回复
热议问题