[MySQL] Query trekt teveel load

Pagina: 1
Acties:

  • BierPul
  • Registratie: Juni 2001
  • Laatst online: 00:12

BierPul

2 koffie graag

Topicstarter
IK trek met deze query de nieuwste topics en replies uit mn DB ze worden gesorteerd op tijd

ik voeg de 2 tijden van aanmaken samen en sorteer daarop.

Echter trekt deze query mn hele dbase op hol :(
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$latest_topics = mysql_query("SELECT 
    tbl_topics.topic_id, 
    tbl_topics.titel, 
    tbl_topics.forum_id, 
    tbl_topics.text, 
    tbl_topics.active,
    tbl_topics.added,
    count(tbl_topic_replies.id) as aantal,
    Ifnull(max(tbl_topic_replies.added), tbl_topics.added) as Sorter
FROM
    tbl_topics
LEFT OUTER JOIN 
    tbl_topic_replies ON tbl_topics.topic_id = tbl_topic_replies.topic_id 
    AND tbl_topic_replies.active = 0
WHERE tbl_topics.active = 0
GROUP BY 
    tbl_topics.topic_id DESC

ORDER BY
    Sorter DESC 
    LIMIT 10") or die (mysql_error());

iemand een andere optie :)

Ja man


  • Orphix
  • Registratie: Februari 2000
  • Niet online
Check of een index op tbl_topics.active zetten veel winst oplevert.

  • brammetje
  • Registratie: Oktober 2000
  • Laatst online: 12-01-2025
hmmz.. lijkt mij dat dit count en die Sorter het echt traag maken..

Je zou kunnen overwegen deze waarden gewoon in een kolom op te slaan, soms gaat snelheid boven design.

Verwijderd

Volgens mij die count()... Ik "had" ongeveer dezelfde query als jou: zonder die count, en dus die count in een aparte query ging het veel sneller.

Waarom weet ik niet, daar ben ik niet pro genoeg voor :7

Verwijderd

gebruik explain eens:

EXPLAIN
SELECT
tbl_topics.topic_id,
tbl_topics.titel,
tbl_topics.forum_id,
tbl_topics.text,
tbl_topics.active,
tbl_topics.added,
count(tbl_topic_replies.id) as aantal,
Ifnull(max(tbl_topic_replies.added), tbl_topics.added) as Sorter
FROM
tbl_topics
LEFT OUTER JOIN
tbl_topic_replies ON tbl_topics.topic_id = tbl_topic_replies.topic_id
AND tbl_topic_replies.active = 0
WHERE tbl_topics.active = 0
GROUP BY
tbl_topics.topic_id DESC
ORDER BY
Sorter DESC
LIMIT 10

zie je vanzelf wel wat ie doet :)
hij zal voor sorteren enzo wel TEMP table aanmaken en filesort gebruiken.