Forum Moderators: open
SELECT private_messages.id, username, folder, status, postdate, sender, subject, message, pm_img.image
FROM private_messages
LEFT JOIN pm_img
ON
pm_img.id = private_messages.id AND
pm_img.sorter = 0
WHERE
username = '%s' AND
folder = '%s'
ORDER BY postdate DESC
LIMIT 21; SELECT * FROM private_messages WHERE id='%s' LIMIT 1 SELECT id, status, postdate, sender, message
FROM private_messages
WHERE
username = '%s' AND
(folder = '%s' or folder = 'sent')
GROUP BY sender
ORDER BY postdate DESC
LIMIT 21; # would have a UNIQUE on username, folder, and sender
SELECT id, status, postdate, sender, message
FROM pm_list
WHERE
username = '%s' AND
(folder = '%s' or folder = 'sent')
ORDER BY postdate DESC
LIMIT 21; Option 2. Create a second table that stores the info for the most recent message sent per user, and only query it for the list:
The second option should process faster, since "list" would have a much smaller table (and index) to query,
it is not recommended to use redis if the data you want to store in it exceeds the amount of ram in your server
Preliminary research makes me think that I should use MySQL to hold the messages themselves, and use Redis JUST in lieu of that new second table for the list
I don't think that this new table would be over 1G
The calculator shows that I need the "Flexible" plan of $0.881 /hour.
SELECT id, status, postdate, sender, message
FROM private_messages
WHERE
username = '%s' AND
(folder = '%s' or folder = 'sent')
GROUP BY sender
ORDER BY postdate DESC
LIMIT 21; # First modification, query speed 21.7974s
SELECT id, status, postdate, sender, message
FROM private_messages
WHERE id IN (
SELECT MAX(id) AS id
FROM private_messages
WHERE
username = '%s' AND
(folder = '%s' OR folder = 'sent')
GROUP BY sender
ORDER BY id DESC
)
ORDER BY postdate DESC
# Second modification, changed to INNER JOIN so that I could use LIMIT in the subquery
# This had no discernible impact on query speed, though
SELECT private_messages.id, status, postdate, sender, message
FROM private_messages
INNER JOIN (
SELECT MAX(id) AS id
FROM private_messages
WHERE
username = '%s' AND
(folder = '%s' OR folder = 'sent')
GROUP BY sender
ORDER BY id DESC
LIMIT 21
) AS two
ON private_messages.id = two.id
ORDER BY postdate DESC