SELECT
DISTINCT *
FROM
posts s
JOIN
budy f ON s.userid = f.fid AND s.time >= f.f_since OR s.userid='$thisid'
WHERE
f.myid='$thisid'
GROUP BY
s.pid DESC
LIMIT 20
Looking at your query, I'd think your JOIN is the culprit. I've never seen a join syntax with boolean conditions attached, so I am not exactly sure how it is going to behave without testing it on mysql directly.
Obvious thoughts:
- is posts.userid indexed?
- is f.fid indexed?
- what about posts.time and budy.f_since?
- do your AND and OR options on the join statement require parenthesis?
Have you tried rewriting the query to see if your time can be improved? Perhaps this might make a difference:
SELECT
DISTINCT *
FROM
posts s
JOIN
budy f ON s.userid = f.fid
WHERE
s.userid='$thisid'
s.time >= f.f_since
GROUP BY
s.pid DESC
LIMIT 20
Since I am not sure what data is in your tables and what you expect to see as a result of your joins, I cannot be sure that my query will match yours. You'd need to check them first. Also, don't forget to make use of the EXPLAIN command to find out what mysql wants to do with your query.