Jaxel
Well-known member
So in my code, I am using the following query:
Its pretty simple; looking at the code from the inside->out, I can explain what it does...
So what needs to be optimized? Well I recently added the ability to not only tag keywords, but to tag users to media... and naturally, I want to be able to add a *booru-style menu for users underneath the menu for keywords. So I've changed the code to do the same queries, but for users, instead of keywords:
But this code... wow, it runs SOOOOO much slower than the keyword version. Taking sometimes as long as 6 seconds to process. I assume its taking so long because of the sheer size of the xf_user table? What can I do to optimize this code?
Code:
SELECT * FROM
(
SELECT EWRmedio_keywords.*, COUNT(EWRmedio_keylinks.keylink_id) AS count
FROM EWRmedio_keywords
LEFT JOIN EWRmedio_keylinks ON (EWRmedio_keylinks.keyword_id = EWRmedio_keywords.keyword_id)
WHERE EWRmedio_keywords.keyword_id IN (
SELECT keyword_id FROM EWRmedio_keylinks WHERE media_id IN (6878, 6877, 6876, 6875)
)
GROUP BY EWRmedio_keywords.keyword_id
ORDER BY count DESC
LIMIT 10
) t
ORDER BY keyword_text ASC
-
Code:
SELECT keyword_id FROM EWRmedio_keylinks WHERE media_id IN (6878, 6877, 6876, 6875)
-
Code:
SELECT EWRmedio_keywords.*, COUNT(EWRmedio_keylinks.keylink_id) AS count FROM EWRmedio_keywords LEFT JOIN EWRmedio_keylinks ON (EWRmedio_keylinks.keyword_id = EWRmedio_keywords.keyword_id) WHERE EWRmedio_keywords.keyword_id IN ( --- step 1 --- ) GROUP BY EWRmedio_keywords.keyword_id ORDER BY count DESC LIMIT 10
-
Code:
SELECT * FROM ( --- step 2 --- ) t ORDER BY keyword_text ASC
So what needs to be optimized? Well I recently added the ability to not only tag keywords, but to tag users to media... and naturally, I want to be able to add a *booru-style menu for users underneath the menu for keywords. So I've changed the code to do the same queries, but for users, instead of keywords:
Code:
SELECT *
FROM
(
SELECT xf_user.*, COUNT(EWRmedio_userlinks.userlink_id) AS count
FROM xf_user
LEFT JOIN EWRmedio_userlinks ON (EWRmedio_userlinks.user_id = xf_user.user_id)
WHERE xf_user.user_id IN (
SELECT user_id FROM EWRmedio_userlinks WHERE media_id IN (6878, 6877, 6876, 6875)
)
GROUP BY xf_user.user_id
ORDER BY count DESC
LIMIT 10
) t
ORDER BY username ASC
But this code... wow, it runs SOOOOO much slower than the keyword version. Taking sometimes as long as 6 seconds to process. I assume its taking so long because of the sheer size of the xf_user table? What can I do to optimize this code?