1

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?

Kevin Loney
  • 7,483
  • 3
  • 28
  • 33
Brig
  • 161
  • 1
  • 8

3 Answers3

2

I actually talked to an Amazon rep about this. What you're supposed to do is store a random value along with your data in SDB. When you want a row back, you generate another random value and pick the first result less than that. So you need to store a little more data, but it only takes one query.

Justin
  • 9,419
  • 7
  • 34
  • 41
  • While certainly a valid approach I can see one possible and one edge case that someone using this method should at least be aware of: 1. A second query would be required if the randomized selector is less than the lowest random attribute on an item in the domain. 2. Because the size of the gaps between the random attributes is not going to be nice and even (unless you have a lot of items) not all items will be selected with equal probability. – Kevin Loney Aug 05 '11 at 05:36
1

I realize that it's been over 18 months since this question was posted but in case someone else needs this I'll post my answer anyways. If the uniformity of the selection is not important then the approach Justin describes is correct. However if uniformity is important then you can use an adaptation of Justin's approach (in really rough psuedo-code):

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

I've done a more in-depth write-up on this on my blog with examples showing why the probability distribution is broken.

Community
  • 1
  • 1
Kevin Loney
  • 7,483
  • 3
  • 28
  • 33
0

No, there's no type of random function in SimpleDB. You'd have to implement the random part yourself.

You could do something like:

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)

But this takes a minimum of three queries. The first one would have to be repeated until you got the full count (no NextToken). The second one would need to be repeated until you reach a count of random (2500 per request max) unless you saved some NextTokens from the first set of queries.

All in all not very convenient.

Mocky
  • 7,768
  • 5
  • 28
  • 23
  • Hm - having trouble getting a nextToken result at all from SDB... have posted here about it: http://stackoverflow.com/questions/1795245/how-to-do-paging-with-simpledb any tips there? – Brig Jan 18 '10 at 18:10