Erlang/OTP Forums

Author Message

<  Advanced Erlang/OTP  ~  Select random record

datacompboy
Posted: Wed Sep 27, 2006 3:40 pm Reply with quote
User Joined: 21 Sep 2006 Posts: 69 Location: Novosibirsk, Russia
How effective select an random record from mnesia table?
I have table with user statuses:
{us, id, status}.
I have additional index on status field.
And I need to get any random record with status=X.

Any easy and fast way?
Or only have tables for every status, and then:
Table=list_to_atom("status"++"_"++X),
X=mnesia:table_size(Table),
record=mnesia_geti(Table, random:uniform(X))
?

_________________
--- suicide proc near\n call death\n suicide endp
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number
Mazen
Posted: Thu Sep 28, 2006 9:42 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
I think i would have done it similar to this:

Select all records with status = X, set the key as return value,
This gives you a list of keys, randomize a number between 1 and size
of the list. Take out that key and read it from the table.

MatchHead = #tablerecord{ id = '$1', status = 'online' , _ = '_' },
List = mnesia:dirty_select(table, [{MatchHead, [], ['$1']}]),
Record = mnesia:dirty_read(table, lists:nth(random:uniform(length(List)), List)).

Although I think having different tables might be somewhat faster, I think its a hassle to use different tables (well.... it depens on how many statuses you have?)

/Mazen
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum