If you are interested, these look like the culprits. In fact, it's always just the first on executed - if they are both run, the second one is fast, maybe MySQL builds some sort of temporary index? I don't know.
I'll have to sit and think about it for a bit, SQL is not my strong point, so if anyone feels they can put me to shame with some clever coding, feel free 
They basically select the posters that appear, depending upon keyword relationships. The keywords are in a nested sets though, so posters need to be retrieved that are related to any offspring of the chosen keyword, as well as the keyword itself.
The first query just totals the number of results, and the second fetches the data, returning only a limited random selection.
Code:
SELECT
COUNT(*) as total
FROM
keywords_map
WHERE
keywords_map.keyword_id IN
(
SELECT
node.id
FROM
keywords AS node,
keywords AS parent
WHERE
node.lft
BETWEEN
parent.lft
AND
parent.rgt
AND (
keywords_map.keyword_id
IN
(
SELECT
node.id
FROM
keywords AS node,
keywords AS parent
WHERE
node.lft
BETWEEN
parent.lft
AND
parent.rgt
AND
parent.id IN (91)
)
)
)
Code:
SELECT DISTINCT
ads.[...]
FROM
ads
WHERE
ads.id
IN
(
SELECT
ads.id
FROM
wall_keywords_map
INNER JOIN
ads
ON
wall_keywords_map.advert_id = ads.id
WHERE
wall_keywords_map.keyword_id
IN
(
SELECT
node.id
FROM
wall_keywords AS node,
wall_keywords AS parent
WHERE
node.lft
BETWEEN
parent.lft
AND
parent.rgt
AND (
ads.id
IN
(
SELECT
ads.id
FROM
wall_keywords_map
INNER JOIN
ads
ON
wall_keywords_map.advert_id = ads.id
WHERE
wall_keywords_map.keyword_id
IN
(
SELECT
node.id
FROM
wall_keywords AS node,
wall_keywords AS parent
WHERE
node.lft
BETWEEN
parent.lft
AND
parent.rgt
AND
parent.id IN (91)
)
)
)
)
)
ORDER BY RAND();
Bookmarks