[Solved] optimize 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. Avoid Calling Functions With Indexed Columns (query line: 32): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `value` is indexed, the index won’t be used as it’s wrapped with the function `isnumber`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 33): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `range_bottom` is indexed, the index won’t be used as it’s wrapped with the function `isnumber`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid Calling Functions With Indexed Columns (query line: 34): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `range_top` is indexed, the index won’t be used as it’s wrapped with the function `isnumber`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  4. Avoid Calling Functions With Indexed Columns (query line: 67): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `value` is indexed, the index won’t be used as it’s wrapped with the function `isnumber`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  5. Avoid Calling Functions With Indexed Columns (query line: 68): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `range_bottom` is indexed, the index won’t be used as it’s wrapped with the function `isnumber`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  6. Avoid Calling Functions With Indexed Columns (query line: 69): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `range_top` is indexed, the index won’t be used as it’s wrapped with the function `isnumber`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  7. Avoid OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  8. Avoid Subqueries (query line: 95): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  9. 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.
  10. Use Numeric Column Types For Numeric Values (query line: 53): Referencing a numeric value (e.g. 1) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  11. Use Numeric Column Types For Numeric Values (query line: 79): Referencing a numeric value (e.g. 1) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  12. Use Numeric Column Types For Numeric Values (query line: 123): Referencing a numeric value (e.g. 1) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  13. Use UNION ALL instead of UNION (query line: 88): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
  14. Use UNION ALL instead of UNION (query line: 56): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
CREATE INDEX product_idx_visible_sku ON PRODUCT (visible,SKU);
CREATE INDEX product_category_idx_sku ON PRODUCT_CATEGORY (sku);
CREATE INDEX product_properties_idx_property_id_value ON product_properties (property_id,value);
The optimized query:
SELECT
        * 
    FROM
        (SELECT
            COUNT(*) AS countme,
            a_string_value,
            a_name,
            a_property_id,
            a_category_id 
        FROM
            (SELECT
                a_string_value,
                a_name,
                a_property_id,
                b_product_id,
                a_category_id 
            FROM
                ((SELECT
                    DISTINCT a.string_value AS a_string_value,
                    a.name AS a_name,
                    a.property_id AS a_property_id,
                    b.product_id AS b_product_id,
                    a.category_id AS a_category_id 
                FROM
                    filter_criterias a 
                JOIN
                    product_properties b 
                        ON (
                            a.property_id = b.property_id 
                            AND (
                                (
                                    isnumber(b.value) IS NOT NULL 
                                    AND isnumber(a.range_bottom) IS NOT NULL 
                                    AND isnumber(a.range_top) IS NOT NULL 
                                    AND (
                                        a.range_bottom <= a.range_top 
                                        AND b.value >= a.range_bottom 
                                        AND b.value <= a.range_top
                                    )
                                )
                            )
                        ) 
                JOIN
                    PRODUCT_CATEGORY prc 
                        ON (
                            prc.sku = b.product_id 
                            AND prc.category_id = a.category_id
                        ) 
                JOIN
                    PRODUCT pr 
                        ON (
                            b.product_id = pr.SKU 
                            AND pr.visible = '1'
                        )) 
            UNION
            DISTINCT (SELECT
                DISTINCT a.string_value AS a_string_value,
                a.name AS a_name,
                a.property_id AS a_property_id,
                b.product_id AS b_product_id,
                a.category_id AS a_category_id 
            FROM
                filter_criterias a 
            JOIN
                product_properties b 
                    ON (a.property_id = b.property_id 
                    AND ((isnumber(b.value) IS NOT NULL 
                    AND isnumber(a.range_bottom) IS NOT NULL 
                    AND isnumber(a.range_top) IS NOT NULL 
                    AND (a.range_bottom > a.range_top 
                    AND b.value >= a.range_bottom)))) 
            JOIN
                PRODUCT_CATEGORY prc 
                    ON (prc.sku = b.product_id 
                    AND prc.category_id = a.category_id) 
            JOIN
                PRODUCT pr 
                    ON (b.product_id = pr.SKU 
                    AND pr.visible = '1'))
            ) AS union1
        ) 
GROUP BY
    (a_string_value,
    a_name,
    a_property_id,
    a_category_id) 
UNION
SELECT
    COUNT(*) AS countme,
    string_value,
    name,
    property_id,
    category_id 
FROM
    (SELECT
        DISTINCT a.string_value,
        a.name,
        a.property_id,
        b.product_id,
        a.category_id 
    FROM
        filter_criterias a 
    JOIN
        product_properties b 
            ON (
                a.property_id = b.property_id 
                AND (
                    (
                        a.name = b.value
                    )
                )
            ) 
    JOIN
        PRODUCT_CATEGORY prc 
            ON (
                prc.sku = b.product_id 
                AND prc.category_id = a.category_id
            ) 
    JOIN
        PRODUCT pr 
            ON (
                b.product_id = pr.SKU 
                AND pr.visible = '1'
            )) 
GROUP BY
    (string_value,
    name,
    property_id,
    category_id)) 
ORDER BY
    5,
    4,
    3,
    2

Related Articles



* original question posted on StackOverflow here.