[Solved] Changes between Mysql version 5.1 TO 5.7

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: 4): A correlated subquery is a subquery that contains a reference (column: 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: 18): A correlated subquery is a subquery that contains a reference (column: 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 Subqueries (query line: 41): 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.
  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. 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'.
  6. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `ATTRIBUTE_VALUES` ADD INDEX `attribute_values_idx_attribute_code_id` (`ATTRIBUTE_CODE`,`ID`);
ALTER TABLE `PRODUCTS` ADD INDEX `products_idx_ean_is_draft` (`EAN`,`IS_DRAFT`);
The optimized query:
SELECT
        count(*) COUNT_PRODUCTS 
    FROM
        (SELECT
            d.ID,
            d.EAN,
            d.NAME,
            d.SAP_CATEGORY_ID,
            d.SAP_VENDOR_ID,
            d.RELEASE_DATE,
            d.REMOVED_DATE,
            d.IS_DRAFT,
            d.DATA_STATE,
            d.gc,
            ATTRIBUTE_CODE AS ATTRIB_NAME,
            ATTRIBUTE_VALUE AS APP_STATUS 
        FROM
            (SELECT
                b.ID,
                b.EAN,
                b.NAME,
                b.SAP_CATEGORY_ID,
                b.SAP_VENDOR_ID,
                b.RELEASE_DATE,
                b.REMOVED_DATE,
                b.IS_DRAFT,
                b.DATA_STATE,
                GROUP_CONCAT(pl.LIFECYCLE_VALUE) gc 
            FROM
                (SELECT
                    a.ID,
                    a.EAN,
                    a.NAME,
                    a.SAP_CATEGORY_ID,
                    a.SAP_VENDOR_ID,
                    a.RELEASE_DATE,
                    a.REMOVED_DATE,
                    a.IS_DRAFT,
                    a.DATA_STATE 
                FROM
                    (SELECT
                        p.ID,
                        p.EAN,
                        p.NAME,
                        p.SAP_CATEGORY_ID,
                        p.SAP_VENDOR_ID,
                        p.RELEASE_DATE,
                        p.REMOVED_DATE,
                        p.IS_DRAFT,
                        p.DATA_STATE 
                    FROM
                        PRODUCTS AS p 
                    ORDER BY
                        p.EAN,
                        p.IS_DRAFT) AS a 
                GROUP BY
                    a.EAN 
                ORDER BY
                    NULL) AS b 
                LEFT JOIN
                    PRODUCT_LIFECYCLE_STATES pl 
                        ON pl.PRODUCT_ID = b.ID 
                GROUP BY
                    b.ID 
                ORDER BY
                    NULL) AS d 
            LEFT JOIN
                PRODUCT_ATTRIBUTE_VALUES AS PRODAV 
                    ON d.ID = PRODAV.PRODUCT_ID 
                    AND EXISTS (
                        SELECT
                            1 
                    FROM
                        ATTRIBUTE_VALUES 
                    WHERE
                        (
                            ATTRIBUTE_VALUES.ATTRIBUTE_CODE = 'APPROVED_ATTRIBUTES'
                        ) 
                        AND (
                            PRODAV.ATTRIBUTE_VALUE_ID = ATTRIBUTE_VALUES.ID
                        )
                ) 
            LEFT JOIN
                ATTRIBUTE_VALUES AS ATVALS 
                    ON PRODAV.ATTRIBUTE_VALUE_ID = ATVALS.ID 
                    AND ATVALS.ATTRIBUTE_CODE = 'APPROVED_ATTRIBUTES' 
            ORDER BY
                d.SAP_CATEGORY_ID,
                d.NAME) f

Related Articles



* original question posted on StackOverflow here.