random entry from mensia

James Churchman jameschurchman@REDACTED
Thu Nov 18 18:00:24 CET 2010


Someone i know brought up an interesting question that is usually very easy to solve in sql but i could not see an obvious efficient solution in Mnesia

If say you want to display on a webpage random entries. A bit like Facebook shows random friends when you view your profile, how do you go about doing it.

You can obviously :
1) Retrieve or cache all the keys and pick a key at random
2) Traverse the entire table and stop at a random point (probably with a rough knowledge of the length of the table first)
3) Add an integer increment to every row, and pick a random number, index it and look that up (which is roughly what you would do in sql, though in sql having an auto increment is almost a prerequisite, unlike Mnesia)

All of the above are fine solutions, but seem a bit wasteful in several different ways

There seemed to be an obvious solution that is fast and requires no additions to the table, but unless i miss understand the function, it just does not work.

There is the option in both ETS and Mnesia called dirty_slot, which i thought gets an item by its numeric order. So if i do mnesia:dirty_slot(table_name,50) as long as there are more than 50 items it will return item 50. I realise that it warns you that in some cases due to updates it will fail, but for me this and the ETS equivalent always return an empty list. I am using r13b02. Is this just a bug as its an under-used feature and never been fixed, or is there a specific table type it has to be used with? (it does not say it does in the docs) or is there a far better solution i missed?

James



More information about the erlang-questions mailing list