Unfortunately, this select is not so good after some posts...
Today, with a total of about 900 records on the table, when i'm filtering threads by my user id (only 45 distinct threads!), MySQL needs 0.30 sec (at the begin, there were needed only 0.04/0.05 sec... the deterioration i think is proportional to how many threads i subscribed)
This could means that when i will have 300 distinct threads subscribed i should wait about 2 seconds for this query... it's too much.
The strange thing is if i "limit 0,10" this query or get full query, the execution speed does not change! This is why i think it's not good... because i suppose it have to select the whole data even if i limit.
No way to solve. This is how i setup query. There is a new notications table, because i want to use it as notification query (to show ONLY replies not made by $USERID)
NOTIFICATIONS TABLE FIELDS: id,idcontent,userid
SELECT
CASE WHEN (re.id is NULL) THEN fa.id ELSE re.id END AS id,
fa.id as idtopic,
CASE WHEN (re.userid is NULL) THEN fa.userid ELSE re.userid END AS userid,
CASE WHEN (re.content is NULL) THEN fa.content ELSE re.content END AS content,
n.notify,
u.id as reuserid, u.name, u.surname, u.photo,
CASE WHEN (re.date is NULL) THEN unix_timestamp(fa.date) ELSE unix_timestamp(re.date) END AS LASTUPDATE
FROM notifications AS n
LEFT JOIN post AS fa
ON fa.id = n.idcontent
LEFT JOIN post AS re
ON re.id=(SELECT ID FROM post WHERE IDTOPIC = fa.ID AND userid <> $USERID ORDER BY DATE DESC LIMIT 1)
LEFT JOIN users AS u
ON u.id = ( CASE WHEN (re.userid is NULL) THEN fa.userid ELSE re.userid END )
WHERE
n.userid = $USERID
AND NOT (fa.userid = $USERID AND re.userid = $USERID)