这是一个大约需要 5-6 秒的查询。内部只需要大约 50 毫秒。
SELECT id,messages.to,messages.from,message,datetime,messages.read
FROM messages WHERE id IN( //inside brackets grabs a list of IDs
SELECT max(messages.id) FROM
(SELECT id,messages.from FROM messages
WHERE messages.to = 'username' AND messages.from != 'username'
UNION
SELECT id,messages.to FROM messages WHERE
messages.from = 'username' AND messages.to != 'username')
AS x
LEFT JOIN messages ON messages.id = x.id
GROUP BY x.from)
ORDER BY id DESC
LIMIT 15
这是一个查询,它会提取某人最近发过消息的用户列表,以及两者之间的最后一条消息。我怎样才能改变它以使其更快?也许没有 id IN。也许多个查询?