[Solved] MYSQL query on products within variant price range without duplicates

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 Correlated Subqueries (query line: 32): A correlated subquery is a subquery that contains a reference (column: product_id) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
  2. Avoid Correlated Subqueries (query line: 42): A correlated subquery is a subquery that contains a reference (column: product_id) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
  3. Avoid Selecting Unnecessary Columns (query line: 3): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  4. 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.
  5. Prefer IN Clause Over OR Conditions (modified query below): Using an IN clause is far more efficient than OR conditions, when comparing a column to more than one optional values. When using an IN clause, the database sorts the list of values and uses a quick binary search.
Optimal indexes for this query:
ALTER TABLE `variants` ADD INDEX `variants_idx_product_id_price` (`product_id`,`price`);
ALTER TABLE `wp_wps_collects` ADD INDEX `wp_collects_idx_collection_id_product_id` (`collection_id`,`product_id`);
ALTER TABLE `wp_wps_tags` ADD INDEX `wp_tags_idx_tag_product_id` (`tag`,`product_id`);
The optimized query:
SELECT
        colortags.tag,
        products.*,
        (SELECT
            variants.price 
        FROM
            variants 
        WHERE
            variants.product_id = products.product_id LIMIT 1) price,
        collects.position 
    FROM
        products 
    INNER JOIN
        wp_wps_collects collects 
            ON products.product_id = collects.product_id 
            AND collects.collection_id = 123456788 
    INNER JOIN
        wp_wps_tags colortags 
            ON colortags.product_id = collects.product_id 
            AND (
                colortags.tag IN (
                    'black',
                'nav')) 
            INNER JOIN
                wp_wps_tags styletags 
                    ON styletags.product_id = collects.product_id 
                    AND (
                        styletags.tag = 'purses'
                    ) 
            WHERE
                (
                    SELECT
                        variants.price 
                    FROM
                        variants 
                    WHERE
                        variants.product_id = products.product_id 
                        AND variants.price >= 200 
                        AND variants.price < 300 LIMIT 1
                ) > 0 
                AND (
                    SELECT
                        variants.price 
                    FROM
                        variants 
                    WHERE
                        variants.product_id = products.product_id 
                        AND variants.price >= 200 
                        AND variants.price < 300 LIMIT 1
                ) <> 0

Related Articles



* original question posted on StackOverflow here.