I have a table of messages
Key - UserId, MsgIndex
C1...Cn - some data columns
Cn+1 - Date, when message has been added to the table.
Question is the best practice to do with client…
Client ask server for new messages…
There are 2 options to do it:
- check for messages with
index > lastRxMsgIndex(client will save the last msg index received) - check for messages with
date > lastRxMsgDate(client will save the last rx msg date- server will give it to him when getting msg results)
Which is better and faster…
Keeping date/TS or index is the same, common sense says to keep date/TS but it is same for msg index.
MsgIndex is in the table primary key so should it be faster that searching on dates (when user will have many messages…)
Which is the best way?
Thanks
Yoav
First regarding performance: you could add an index to your date column to improve the performance of searches by date. You will most likely want to also include the
user_idin the index. You could for example use a combined index on(user_id, id)or(user_id, date)so that individual users can quickly find the messages they own without the server having to scan through other users’ messages too.Regarding functionality: One potential issue with using the datetime as a key is that timestamps are not in general unique. It’s possible (but unlikely) that if you search based on date you’ll miss a message. Here’s an example scenario demonstrating the problem:
At 16:01:04.312 the table contains two messages:
The client already has received row 1 previously and now requests and receives the latest row:
Then at 16:01:04.420 a new row comes into the database with the same timestamp:
Client requests latest row but doesn’t get it:
Another issue is if the server’s time is adjusted backwards. This could cause later messages to be inserted with an earlier timestamp. These messages will also be missed if you use the date to find the newest messages. It may be better to use the
idinstead to avoid these potential problems.