[Solved] Optimise Custom Wordpress SQL query

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.
Optimal indexes for this query:
ALTER TABLE `wp_postmeta` ADD INDEX `wp_postmeta_idx_meta_key_post_id_meta_value` (`meta_key`,`post_id`,`meta_value`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_post_type_post_status` (`post_type`,`post_status`);
ALTER TABLE `wp_term_relationships` ADD INDEX `wp_relationships_idx_object_id` (`object_id`);
ALTER TABLE `wp_term_taxonomy` ADD INDEX `wp_taxonomy_idx_taxonomy_term_id` (`taxonomy`,`term_taxonomy_id`);
ALTER TABLE `wp_terms` ADD INDEX `wp_terms_idx_term_id` (`term_id`);
The optimized query:
SELECT
        p.ID AS 'id',
        p.post_title AS 'title',
        t.name AS 'property_type',
        c.name AS 'listing_type',
        pm.meta_value AS 'address',
        pm2.meta_value AS 'latitude',
        pm3.meta_value AS 'longitude',
        pm4.meta_value AS 'price',
        pm5.meta_value AS 'bedrooms',
        pm6.meta_value AS 'baths',
        pm7.meta_value AS 'show_date',
        p.guid,
        wm2.meta_value AS 'image' 
    FROM
        wp_posts p 
    INNER JOIN
        wp_postmeta AS pm 
            ON pm.post_id = p.ID 
    INNER JOIN
        wp_postmeta AS pm2 
            ON pm2.post_id = p.ID 
    INNER JOIN
        wp_postmeta AS pm3 
            ON pm3.post_id = p.ID 
    INNER JOIN
        wp_postmeta AS pm4 
            ON pm4.post_id = p.ID 
    INNER JOIN
        wp_postmeta AS pm5 
            ON pm5.post_id = p.ID 
    INNER JOIN
        wp_postmeta AS pm6 
            ON pm6.post_id = p.ID 
    INNER JOIN
        wp_postmeta AS pm7 
            ON pm7.post_id = p.ID 
    LEFT JOIN
        wp_term_relationships AS r 
            ON (
                p.ID = r.object_id
            ) 
    INNER JOIN
        wp_term_taxonomy AS x 
            ON (
                r.term_taxonomy_id = x.term_taxonomy_id
            ) 
    INNER JOIN
        wp_terms AS t 
            ON (
                r.term_taxonomy_id = t.term_id
            ) 
    LEFT JOIN
        wp_term_relationships AS v 
            ON (
                p.ID = v.object_id
            ) 
    INNER JOIN
        wp_term_taxonomy AS z 
            ON (
                v.term_taxonomy_id = z.term_taxonomy_id
            ) 
    INNER JOIN
        wp_terms AS c 
            ON (
                v.term_taxonomy_id = c.term_id
            ) 
    LEFT JOIN
        wp_postmeta wm1 
            ON (
                wm1.post_id = p.id 
                AND wm1.meta_value IS NOT NULL 
                AND wm1.meta_key = '_thumbnail_id'
            ) 
    LEFT JOIN
        wp_postmeta wm2 
            ON (
                wm1.meta_value = wm2.post_id 
                AND wm2.meta_key = '_wp_attached_file' 
                AND wm2.meta_value IS NOT NULL
            ) 
    WHERE
        pm.meta_key = 'property_address' 
        AND pm2.meta_key = 'property_lat' 
        AND pm3.meta_key = 'property_lng' 
        AND pm4.meta_key = 'property_price' 
        AND pm5.meta_key = 'property_beds' 
        AND pm6.meta_key = 'property_baths' 
        AND pm7.meta_key = 'property_show_date' 
        AND x.taxonomy = 'property-type' 
        AND z.taxonomy = 'listing-type' 
        AND p.post_type = 'property' 
        AND p.post_status = 'publish'

Related Articles



* original question posted on StackOverflow here.