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?
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.
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.
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.