SELECT m.*, m.flags + 0 flags
FROM messages_test m
JOIN
(
(
SELECT id
FROM messages_test
WHERE (thread = 817549.001266066)
AND (id_user_s = 817549)
AND (del_s = 0)
ORDER BY id DESC LIMIT 3
)
UNION DISTINCT
( SELECT id
FROM messages_test
WHERE (thread = 817549.001266066)
AND (id_user_r = 817549)
AND (del_r = 0)
order by id desc LIMIT 3
)
) x ON x.id = m.id
order by id desc LIMIT 3;
Note how the the inner SELECTs and the UNION only haul around `id`. This is much less bulky. Then, the outer query does the heavy lifting of reaching into messages_test (again) to get the rest of the fields.
Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.