[Solved] Explanation of plain SQL to get user_meta from Wordpress

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  3. Replace Left Join With Subquery (modified query below): The pattern of inflating the amount of data (using joins) and deflating (using GROUP BY) usually slows down queries. In this case, it can be avoided by moving some of the logic to the SELECT clause, and therefore removing some of the LEFT JOINs. In some cases, this transformation can lead to an obsolete GROUP BY clause, which can also be removed.
Optimal indexes for this query:
ALTER TABLE `wp_usermeta` ADD INDEX `wp_usermeta_idx_user_id_meta_key` (`user_id`,`meta_key`);
The optimized query:
SELECT
        u.id,
        u.user_login,
        (SELECT
            MIN(CASE m.meta_key 
                WHEN 'title' THEN m.meta_value END) AS title 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS title,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'first_name' THEN m.meta_value END) AS first_name 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS first_name,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'last_name' THEN m.meta_value END) AS last_name 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS last_name,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'suburb' THEN m.meta_value END) AS phone 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS phone,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'state' THEN m.meta_value END) AS state 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS state,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'country' THEN m.meta_value END) AS country 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS country,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'postcode' THEN m.meta_value END) AS postcode 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS postcode,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'contact_no' THEN m.meta_value END) AS contact_no 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS contact_no,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'email' THEN m.meta_value END) AS email 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS email,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'occupation' THEN m.meta_value END) AS occupation 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS occupation,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'workplace' THEN m.meta_value END) AS workplace 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS workplace,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'maternitybg' THEN m.meta_value END) AS maternitybg 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS maternitybg,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'trainingdate' THEN m.meta_value END) AS trainingdate 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS trainingdate,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'traininglocation' THEN m.meta_value END) AS traininglocation 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS traininglocation,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'coltraining' THEN m.meta_value END) AS coltraining 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS coltraining,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'trainingyear' THEN m.meta_value END) AS trainingyear 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS trainingyear,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'coltraining' THEN m.meta_value END) AS coltraining 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS coltraining,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'isinstructor' THEN m.meta_value END) AS isinstructor 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS isinstructor,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'gender' THEN m.meta_value END) AS gender 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS gender,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'idf_indig_tsi' THEN m.meta_value END) AS idf_indig_tsi 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS idf_indig_tsi,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'idf_ct_ld' THEN m.meta_value END) AS idf_ct_ld 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS idf_ct_ld,
(SELECT
MIN(CASE m.meta_key 
    WHEN 'comments' THEN m.meta_value END) AS comments 
FROM
wp_usermeta m 
WHERE
u.ID = m.user_id 
AND m.meta_key IN (
    'title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments'
) LIMIT 1) AS comments 
FROM
wp_users u 
ORDER BY
NULL

Related Articles



* original question posted on StackOverflow here.