SELECT
u.ID,
u.display_name as name,
u.user_email as email,
u.user_registered as registered,
(
select
meta_value
from
wp_usermeta
where
user_id = u.ID
and meta_key = 'mobileno'
limit
1
) as mobileno,
(
select
meta_value
from
wp_usermeta
where
user_id = u.ID
and meta_key = 'referral_id'
limit
1
) as referral_id,
(
SELECT
COUNT(meta_value) AS total_ref
FROM
wp_usermeta
WHERE
meta_key = 'ambassador_ref_id'
AND meta_value = referral_id
) as total_ref,
wc.task_no,
wc.status,
wc.uploaded_date,
wc.reject_reason
FROM
wp_users u,
wp_ca_tasks wc
WHERE
u.ID = wc.user_id
GROUP BY
wc.user_id,
wc.task_no;
In the above code, if we remove the block
(
SELECT
COUNT(meta_value) AS total_ref
FROM
wp_usermeta
WHERE
meta_key = 'ambassador_ref_id'
AND meta_value = referral_id
) as total_ref
the code executes a bit faster. But if we add that block, it basically gets stuck in Loading…
Currently using MySQL 5.7.
How can I optimize the above block of code to make the execution faster?
2
Answers
Ah, the notorious WordPress meta-table slowdown.
Change the comma-joins (
FROM a,b WHERE a.ID = b.user_id
) to proper JOINs.Eliminate your dependent subqueries and replace them with JOINed subqueries.
A quicker query might look like this.
The trick is to avoid repeating the queries buried in the SELECT statement over and over. LEFT JOINing them helps.
And, your WordPress tables need better indexes. Look at this. https://wordpress.org/plugins/index-wp-mysql-for-speed/
In addition to what O.Jones says,
wc
needsHowever, the
GROUP BY
probably violates "only_full_group_by". That is, for a givenuser_id
andtask_no
, you will get random values for other columns fetched fromwp_ca_tasks
.