I need some help with the optimization of a query. I have a query that's taking too long to run 12s and I would love if I could get some help in trying to optimize it as I'm not a sql guru. Here it is :
SELECT ID ,
user_login ,
user_nicename ,
user_registered ,
user_status ,
display_name ,
t1.meta_value AS account_type ,
1 t2.meta_value AS views ,
GROUP_CONCAT(t4.term_id) AS interests_skills,
GROUP_CONCAT(t4.taxonomy) AS taxonomyComb ,
t4.term_id ,
t4.taxonomy
FROM wp_users
INNER JOIN wp_usermeta AS t1
ON (
t1.user_id = wp_users.ID
AND
(
t1.meta_key = 'account_type'
AND t1.meta_value = 'individual'
)
)
LEFT JOIN wp_usermeta AS t2
ON (
t2.user_id = wp_users.ID
AND t2.meta_key = 'views'
)
LEFT JOIN wp_term_relationships AS t3
ON (
t3.object_id = (1000000+wp_users.ID)
)
INNER JOIN wp_term_taxonomy AS t4
ON (
(
t3.term_taxonomy_id = t4.`term_taxonomy_id`
AND t4.taxonomy = 'category'
AND t4.term_id IN (396,410,411,416,142,417)
)
OR
(
t3.term_taxonomy_id = t4.`term_taxonomy_id`
AND t4.taxonomy = 'skill'
AND t4.term_id IN (461,463,464,466,490,468,470,491,473,474,475)
)
)
WHERE t4.term_id IS NOT NULL
GROUP BY ID LIMIT 0,10
Here is the explain
1 SIMPLE t4 range PRIMARY,term_id_taxonomy,taxonomy term_id_taxonomy 106 NULL 17 Using where; Using temporary; Using filesort
1 SIMPLE t1 ref user_id,meta_key meta_key 768 const 3773 Using where
1 SIMPLE wp_users eq_ref PRIMARY PRIMARY 8 jasper_gi.t1.user_id 1
1 SIMPLE t2 ref user_id,meta_key meta_key 768 const 2
1 SIMPLE t3 eq_ref PRIMARY,term_taxonomy_id PRIMARY 16 func,jasper_gi.t4.term_taxonomy_id 1 Using where; Using index
I like the layout of your SQL - very easy to read.
Whenever I have a problem like this I try to break it down. Start with your base table without all the joins and see how it performs - look at the query plan, validate results, etc.
Then add in each join one at a time until you see the culprit. Might be a mixture of a few, might be some missing indexes, etc. But by systematically working thru the joins like this you can find the trouble-spots and get a better idea of what to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With