Forum Moderators: open
$query = sprintf("SELECT ref_id FROM tableB WHERE message = '%s' LIMIT 1"...);
$sth = mysqli_query($dbh, $query); if ($sth && mysqli_num_rows($sth)) > 0) {
list($ref_id) = mysqli_fetch_row($sth);
// insert in to tableA
}
else {
// insert in to tableB with AutoIncrement, and then
// insert in to tableA with LAST_INSERT_ID()
} id
username
sender
status
folder
postdate
subject
message SELECT * FROM private_messages WHERE username = 'example' AND folder = 'inbox' ORDER BY postdate DESC Each time a message is sent it saves 2 rows: the first row would be saved with the recipient's username under "username", the sender's username under "sender", folder as "inbox", and status as "New". Then the second row would be saved with the sender's username as "username", the recipient's username as "sender", folder as "sent", and status as NULL.
It uses an index that contains username, status, and folder. Using Explain shows that "key_len" is 104, "rows" is 5706, and "extra" says "Using where; Using filesort". I'm guessing that a new index that didn't include "status" might load faster, but last time I tried to add an index it took too long and ended up crashing the database...
Assuming that I should move forward with a hash, this is a new one for me... I find people storing passwords in a hash, is it the same concept?
Wouldn't the first row be sufficient to put the same message into the proper folders of both sender and recipient, if you disregard the "folder" column?
It'll get more complicated if you have more types of folders, perhaps even custom ones...
Ideally you would also store user IDs along with each message, rather than repeating the full username every time.
Note that the order of columns in combined indexes is important. If you put 1 index on the 3 columns "username", "status" and "folder" (in that specific order), then this will only help with queries that select first by "username", then (optionally) by "status" and then (optionally) by "folder".
Sort of, but with passwords it's about encryption so that you don't have to store the actual password. In your case it would be creating a hash string that uniquely identifies each message, the same way that a CRC checksum is often used to verify that a file is unchanged
Question: when you say "optionally" status and "optionally" folder, does it work the same if I don't use status in the query? It's been complicated to get a side-by-side comparison and I'm not sure how to do a query without using the cache, but I THINK that by adding status to the query it got faster...
Do you mean that I would have a 3rd column (eg, "id", "hash", and "message"), then use MD5 on "message" to create a 128-bit hash? Then just query for a matching hash instead of a matching message?
message_id.........message_sender.........message_text
3544...............52363.................."Hello world" message_id.........message_recipient
3544...............79237
3544...............63723
3544...............97427 message_id.........message_text.........message_hash
3544..............."Hello world"........8bd69e52 message_id.........message_sender
3544...............52363
3544...............78373