[erlang-questions] Efficient way to store message lists in mnesia

Dmitry Belyaev be.dmitry@REDACTED
Wed Jun 8 01:26:30 CEST 2016


Alexander, thanks for a great example of a double-linked list implementation in a database.

As we can see one insert operation requires 3 write operations in the database in this case. Anyway this is still a constant factor applied to a normal write operation.

Wear we never heard is how the messages are read and if they are removed from the database. If as in most chat applications they need to be read and deleted in bulk, this approach might be tedious as you need to read the messages one by one to find out the next id.

I suggest to try using table of type bag and see if performance it provides is enough for all the task use cases. I believe that implementation would be easiest to read and maintain. 

Another approach might be to use ordered set with records of following structure: {{UserId, Timestamp}, Message}.

On 8 June 2016 5:32:14 AM AEST, Aleksander Nycz <Aleksander.Nycz@REDACTED> wrote:
>Hello,
>
>Try this:
>
>Users table:
>
>{userId, firstMessageId, lastMessageId}
>
>Messages table:
>
>{MessageId, prevMessageId, nextMessageId, Message}
>
>
>Insert 1st message for user 1000:
>
>Users table:
>
>{1000, 1, 1}
>
>Message table:
>
>{1, undefined, undefined, "message1"}
>
>
>Insert 2nd message for user 1000:
>
>Users table:
>
>{1000, 1, 2}
>
>Message table:
>
>{1, undefined, 2, "message1"}
>
>{2, 1, undefined, "message2"}
>
>And so on...
>
>You can also agregate messages (yearly/monthly/daily):
>
>{{UserId, Year}, firstMessageId, lastMessageId}
>
>
>Regards
>
>Aleksander
>
>
>W dniu 2016-06-03 o 19:08, Khitai Pang pisze:
>> Hi,
>>
>> I need to store a list of unread messages for every user id.  The
>order
>> of the messages needs to be preserved.  Currently I store the
>messages
>> in a list as the value of the user id key in a table of type set:
>>
>> {user_id, [message]}
>>
>> The table is a disc_copies table.
>>
>> Whenever someone sends a message to a user, the message is prepended
>to
>> the head of the message list of the user.
>>
>> Now my concern is, adding a new message to the list requires reading
>and
>> writing the whole list, which can be pretty slow if the list is very
>> long, i.e. thousands of messages.
>>
>> There is another way to do this, which uses a table of type bag and
>> composite keys:
>>
>> {user_id, timestamp1, message1}
>> {user_id, timestamp2, message2}
>> {user_id, timestamp3, message3}
>> ...
>>
>> But I don't think this is any better because I heard that mnesia bag
>> tables performance is very bad:
>> http://erlang.org/pipermail/erlang-questions/2011-March/057044.html
>>
>> Are there other ways to store lists of messages in mnesia where
>> reading/updating the message lists can be very efficient?
>>
>>
>> -Khitai
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>erlang-questions mailing list
>erlang-questions@REDACTED
>http://erlang.org/mailman/listinfo/erlang-questions

-- 
Best wishes, 
Dmitry Belyaev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160608/f19bcc54/attachment.htm>


More information about the erlang-questions mailing list