[Solved] Brutal performance going from MySQL 5.6 to MySQL 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 OFFSET In LIMIT Clause (query line: 93): 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 Selecting Unnecessary Columns (query line: 19): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  3. Avoid Subqueries (query line: 18): 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. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `taxonomy_vocabulary`) are used in the 'where' clause, in a way that allows to replace it with an optimized inner join. Inner joins can be fully optimized by the database, while Left joins apply limitations on the database's optimizer.
  6. Use Numeric Column Types For Numeric Values (query line: 49): 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.
  7. Use Numeric Column Types For Numeric Values (query line: 37): Referencing a numeric value (e.g. 10) 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.
  8. Use Numeric Column Types For Numeric Values (query line: 68): Referencing a numeric value (e.g. 0) 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.
  9. Use Numeric Column Types For Numeric Values (query line: 76): 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.
  10. Use Numeric Column Types For Numeric Values (query line: 84): 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.
Optimal indexes for this query:
ALTER TABLE `node` ADD INDEX `node_idx_status_type` (`status`,`type`);
ALTER TABLE `node_access` ADD INDEX `node_access_idx_nid_grant_view` (`nid`,`grant_view`);
ALTER TABLE `taxonomy_index` ADD INDEX `taxonomy_index_idx_tid` (`tid`);
ALTER TABLE `taxonomy_term_data` ADD INDEX `taxonomy_data_idx_tid` (`tid`);
ALTER TABLE `taxonomy_vocabulary` ADD INDEX `taxonomy_vocabular_idx_vid_machine_name` (`vid`,`machine_name`);
The optimized query:
SELECT
        DISTINCT node.nid AS nid,
        node.title AS node_title,
        node.created AS node_created,
        ttdn.name AS ttdn_name,
        ttdn.vid AS ttdn_vid,
        ttdn.tid AS ttdn_tid,
        ttdn_tv.machine_name AS ttdn_tv_machine_name,
        node.sticky AS node_sticky,
        'node' AS field_data_field_top_image_node_entity_type,
        'node' AS field_data_field_summary_node_entity_type,
        'node' AS field_data_body_node_entity_type,
        'node' AS field_data_field_tags_node_entity_type 
    FROM
        node 
    LEFT JOIN
        (
            SELECT
                td.*,
                tn.nid AS nid 
            FROM
                taxonomy_term_data td 
            INNER JOIN
                taxonomy_vocabulary tv 
                    ON td.vid = tv.vid 
            LEFT JOIN
                taxonomy_index tn 
                    ON tn.tid = td.tid 
            WHERE
                (
                    tv.machine_name IN (
                        'news_categories'
                    )
                ) 
                AND (
                    td.tid IN (
                        '10', '21', '23', '24', '25', '26', '27', '28', '31', '32', '33'
                    )
                )
        ) ttdn 
            ON node.nid = taxonomy_term_data_node.nid 
    LEFT JOIN
        taxonomy_vocabulary ttdn_tv 
            ON ttdn.vid = ttdn_tv.vid 
    WHERE
        (
            (
                (
                    node.status = '1'
                ) 
                AND (
                    node.type IN (
                        'news_feed', 'www_news_releases_feed', 'article', 'www_rru_in_the_media_feed'
                    )
                )
            )
        ) 
        AND (
            EXISTS (
                SELECT
                    na.nid AS nid 
                FROM
                    node_access na 
                WHERE
                    (
                        (
                            (
                                na.gid = '0'
                            ) 
                            AND (
                                na.realm = 'all'
                            )
                        ) 
                        OR (
                            (
                                na.gid = '1'
                            ) 
                            AND (
                                na.realm = 'taxonomy_access_role'
                            )
                        )
                    ) 
                    AND (
                        na.grant_view >= '1'
                    ) 
                    AND (
                        node.nid = na.nid
                    )
            )
        ) 
    ORDER BY
        node_sticky DESC,
        node_created DESC LIMIT 1 OFFSET 0

Related Articles



* original question posted on StackOverflow here.