Jump to content

Recommended Posts

Hi all,

Ok...I've recently done a bit of speed-testing on my site, mainly my forum.

 

It turns out, that a join that counts the number of posts for a thread takes about 0.2 seconds...which is crazy. If I take out the join, it takes about 0.05 seconds (but obviously, I can't display how many posts the thread has).

 

I then tried an inner join and it was about 0.09 seconds, but didn't display child forums, as it wouldn't return any posts for them, only threads. I then changed it to use an IF statement. Hard to explain, I'll give you examples:

 

This is the original query:

SELECT F.*, COUNT(P.`ID`) AS `PostCount`
FROM `$tb_Forum` F
LEFT OUTER JOIN `$tb_Forum` P ON (F.`ID`=P.`Thread` OR F.`ID`=P.`ID`) AND P.`Type` IN('thread', 'post')
#blah....and so on

 

And this is the new query:

SELECT F.*, COUNT(P.`ID`) AS `PostCount`
FROM `$tb_Forum` F
INNER JOIN `$tb_Forum` P ON IF(F.`Type`='forum', F.`ID`=P.`Forum` OR F.`ID`=P.`ID`, (F.`ID`=P.`Thread` OR F.`ID`=P.`ID`) AND P.`Type` IN('thread', 'post'))

 

And the new one is about 0.2 seconds faster. Why is this? Are left joins really that slow?

 

I'm just wondering why this is so much faster...and returns the exact same results.

 

Thanks for any info :)

Link to comment
https://forums.phpfreaks.com/topic/162257-major-join-optimisation/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.