I am using the following query which is causing issues. Note following points:I am using 4 tables in this query:message_sharemessagesmyusertablefriendsWhat I am trying to do in this query is to display messages, and share messages of users who are friend with each other.This is similar to Facebook wall where they display friends' wall data.The problem is only myusertable is causing the issue. It increase the load too much,  even to where the query can timeout.When I remove myusertable from this query then it works fine, the use of myusertable is to fetch user full name. Everything else we are doing with userid and does not need  myusertable. For the sole purpose of getting user full name are we using myusertable.The query is(SELECT DISTINCT M.msg_id, M.uid_fk, M.message, S.created, M.like_count,M.comment_count,M.share_count, U.username,M.uploads, S.uid_fk AS share_uid,S.ouid_fk AS share_ouidFROM friends FLEFT JOIN message_share S ON S.ouid_fk <> F.friend_twoLEFT JOIN messages M ON M.msg_id = S.msg_id_fk AND M.uid_fk = S.ouid_fkLEFT JOIN myusertable U ON U.uid = M.uid_fk AND U.status1='1'WHERE F.friend_one='199095' AND F.role='fri'GROUP BY msg_idORDER BY created DESC LIMIT 10)UNION(SELECT DISTINCT M.msg_id, M.uid_fk, M.message, M.created, M.like_count,M.comment_count,M.share_count, U.username,M.uploads, '0' AS share_uid, '0' AS share_ouidFROM friends FLEFT JOIN messages M ON M.uid_fk = F.friend_twoLEFT JOIN myusertable U ON U.uid = M.uid_fk AND U.status1='1'WHERE F.friend_one='199095'GROUP BY msg_idORDER BY created DESC LIMIT 10)Is there anything that I can do with myusertable to make this query faster?
Optimizing nested Select Distinct query
optimization;sql;mysql