Im using MySQL as Database and fetching messages via PHP, so my problem is i don't know much MySQL, and my query is taking to long to fetch the messages, my messages table has 731,847 Rows
My query:
SELECT DISTINCT
c.from_id,
c.to_id
FROM
messages c
JOIN(
SELECT CASE WHEN
`from_id` = '15' THEN `to_id` ELSE `from_id`
END AS other,
MAX(TIME) AS latest
FROM
messages
WHERE
`from_id` = '15' OR `to_id` = '15'
GROUP BY
other
) m
ON
(
c.from_id = '15' AND c.to_id = m.other OR c.to_id = '15' AND c.from_id = m.other
) AND c.time = m.latest
ORDER BY
TIME
DESC
Showing rows 0 - 24 (27289 total, Query took 1.8380 seconds.)
Is there any way to improve this query, and load it faster.