Optimering af query med subqueries
HejJeg forsøger at lave et rssfeed til brug på et oss forum, men jeg er lidt usikker på performance.
Selvfølgelig er query cache aktiveret og feedet caches for hvert 5. min som default (man kan godt slå det fra, selvom det ikke anbefales) :)
DB-strukturen er ret enkel, kun 3 tabeller.
Jeg har pastet den her: http://pastebin.com/m7a52b482
Er der nogen der ud fra queryet her, kan spå flaskehalse eller (performance/potentielle) problemer?
(queriet virker som det skal)
$query = "SELECT
a.*,
b.*
FROM
#__fb_messages AS a
INNER JOIN
(
SELECT
b.id AS lastpost_id,
b.parent AS lastpost_parent,
b.thread AS lastpost_thread,
b.name AS lastpost_name,
b.userid AS lastpost_userid,
b.email AS lastpost_email,
b.subject AS lastpost_subject,
b.time AS lastpost_time,
b.ip AS lastpost_ip,
b.hits AS lastpost_hits,
b.modified_by AS lastpost_modified_by,
b.modified_time AS lastpost_modified_time,
b.modified_reason AS lastpost_modified_reason,
c.message as lastpost_message
FROM #__fb_messages b, #__fb_messages_text c
WHERE b.time > '{$querytime}' AND b.hold = '0' AND b.moved = '0' AND b.id=c.mesid
ORDER BY b.time DESC
)
AS b ON (b.lastpost_thread = a.thread)
JOIN #__fb_categories
AS d ON (d.id = a.catid)
WHERE
a.time > '{$querytime}'
AND a.parent = '0'
AND a.moved = '0'
AND a.hold = '0'
AND d.published = '1'
AND d.pub_access = '0'
";
// sorteringer
if ($sort == 'thread') {
// 1. Newest threads (ordered by newest threads)
$query .= "
GROUP BY a.thread
ORDER BY a.time DESC
";
}
else {
// 2. Recent activity (threads, but ordered by newest post in threads)
$query .= "
GROUP BY a.thread
ORDER BY b.lastpost_time DESC
";
}