English 中文(简体)
Selecting a random result from Simple Database (SDB) on AWS
原标题:

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.





相关问题
Best logging option on Amazon Cloud. RDS or SimpleDB?

My site s architecture includes ASP.Net & MySQL. I am planning to deploy it on Amazon Cloud. This would mean EC2 instance(s) and RDS. My query is regarding logging. I m ensuring that my ...

How does Amazon.com function with a key-value datastore?

I have heard that Amazon uses a key-value data store - that it does not use a traditional relational normalized db. Speaking as someone who only has used the traditional approach, how does this work? ...

Cassandra Vs Amazon SimpleDB

I m working on an application where data size and SQL queries are going to be heavy. I am thinking between Cassandra or Amazon SimpleDB. Can you please suggest which is more suitable in this kind of ...

SimpleDB vs Tokyo Cabinet

Has anybody compared SimpleDB and Tokyo Cabinet for performance and scalability? I m coding my project against SimpleDB at the moment and considering benchmarking TC, be nice if somebody had already ...

How to do paging with simpledb?

I know how to page forward with SimpleDB data by using NextToken. However, how exactly does one handle previous pages? I m on .NET, but I don t think that matters. I m more interested in the ...

热门标签