[Solved] Why Does This SQL INNER JOIN Work and This Doesn\'t?

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. Avoid OFFSET In LIMIT Clause (query line: 66): OFFSET clauses can be very slow when used with high offsets (e.g. with high page numbers when implementing paging). Instead, use the following \u003ca target\u003d"_blank" href\u003d"http://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/"\u003eseek method\u003c/a\u003e, which provides better and more stable response rates.
  2. Avoid using SQL_CALC_FOUND_ROWS (query line: 2): Including SQL_CALC_FOUND_ROWS statements tend to slow down queries significantly as it doesn't scale well. It's recommended to split this query to two: a data selection query and a counting query.
  3. 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.
  4. Replace Join With Exists To Avoid Redundant Grouping (modified query below): When a joined table isn’t used anywhere other than in the WHERE clause, it's equivalent to an EXISTS subquery, which often performs better. In cases where the DISTINCT or GROUP BY clause contains only columns from the Primary key, they can be removed to further improve performance, as after this transformation, they are redundant.
Optimal indexes for this query:
ALTER TABLE `wp_postmeta` ADD INDEX `wp_postmeta_idx_meta_key_post_id` (`meta_key`,`post_id`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_post_type_post_statu_id` (`post_type`,`post_status`,`ID`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_id` (`ID`);
The optimized query:
SELECT
        SQL_CALC_FOUND_ROWS wp_posts.ID 
    FROM
        wp_posts 
    WHERE
        (
            1 = 1 
            AND wp_posts.post_type = 'product' 
            AND (
                wp_posts.post_status = 'publish'
            ) 
            AND (
                (
                    1 = 1 
                    AND 1 = 1
                ) 
                AND (
                    1 = 1 
                    AND 1 = 1
                )
            )
        ) 
        AND (
            EXISTS (
                SELECT
                    1 
                FROM
                    wp_postmeta 
                INNER JOIN
                    wp_postmeta AS mt1 
                WHERE
                    (
                        (
                            (
                                (
                                    (
                                        wp_posts.ID = wp_postmeta.post_id
                                    ) 
                                    AND (
                                        wp_postmeta.meta_key = '_visibility'
                                    )
                                ) 
                                AND (
                                    CAST(wp_postmeta.meta_value AS CHAR) IN (
                                        'visible', 'catalog'
                                    )
                                )
                            ) 
                            AND (
                                wp_posts.ID = mt1.post_id
                            )
                        ) 
                        AND (
                            mt1.meta_key = '_stock_status'
                        )
                    ) 
                    AND (
                        CAST(mt1.meta_value AS CHAR) = 'instock'
                    )
            )
        ) 
    GROUP BY
        wp_posts.ID 
    ORDER BY
        wp_posts.menu_order,
        wp_posts.post_title ASC LIMIT 0,
        10

Related Articles



* original question posted on StackOverflow here.