|
|
| Author |
Message |
|
| datacompboy |
Posted: Wed Sep 27, 2006 3:40 pm |
|
|
|
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 |
|
| Back to top |
|
| Mazen |
Posted: Thu Sep 28, 2006 9:42 am |
|
|
|
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 |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|