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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
You’ll also need to sort on your random number column, otherwise, Google App Engine will pick 10 entries that are greater (or less than) your number, but it will pick them in a non random way. So, if you are picking records whose random number is greater than a random number, you’d sort asending on the column, otherwise you’d sort decending.
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.