[Solved] Mysql optimization based on explain

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 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.
  2. Avoid Subqueries (query line: 61): 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.
  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. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `attempts`) 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.
  5. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `reports`) 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. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `tags`) 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.
  7. Prefer Sorting/Grouping By The First Table In Join Order (modified query below): The database can use indexes more efficiently when sorting and grouping using columns from the first table in the join order. The first table is determined based on the prediction of the the optimal first table, and is not necessarily the first table shown in the FROM clause.
Optimal indexes for this query:
ALTER TABLE `attempts` ADD INDEX `attempts_idx_test_id_score` (`test_id`,`score`);
ALTER TABLE `attempts` ADD INDEX `attempts_idx_user_id` (`user_id`);
ALTER TABLE `reports` ADD INDEX `reports_idx_has_file_user_id` (`has_file`,`user_id`);
ALTER TABLE `tags` ADD INDEX `tags_idx_id` (`id`);
ALTER TABLE `tags_users` ADD INDEX `tags_users_idx_tag_id` (`tag_id`);
ALTER TABLE `users` ADD INDEX `users_idx_id` (`id`);
The optimized query:
SELECT
        `User`.`id`,
        `User`.`username`,
        `User`.`password`,
        `User`.`role`,
        `User`.`created`,
        `User`.`modified`,
        `User`.`email`,
        `User`.`other_user_id`,
        `User`.`first_name`,
        `User`.`last_name`,
        `User`.`place_id`,
        `Resume`.`id`,
        `Resume`.`user_id`,
        `Resume`.`other_resume_id`,
        `Resume`.`other_user_id`,
        `Resume`.`file_extension`,
        `Resume`.`created`,
        `Resume`.`modified`,
        `Resume`.`is_deleted`,
        `Resume`.`has_file`,
        `Resume`.`is_stamped`,
        `Resume`.`is_active` 
    FROM
        `streetofwalls`.`users` AS `User` 
    INNER JOIN
        `my_database`.`attempts` AS `Attempt` 
            ON (
                `Attempt`.`user_id` = `User`.`id` 
                AND `Attempt`.`test_id` != 5
            ) 
    INNER JOIN
        `my_database`.`reports` AS `Resume` 
            ON (
                `Resume`.`user_id` = `User`.`id`
            ) 
    WHERE
        `Attempt`.`test_id` = 8 
        AND `Attempt`.`score` > 60 
        AND `User`.`id` IN (
            SELECT
                `User1`.`id` 
            FROM
                `my_database`.`users` AS User1 
            LEFT JOIN
                `my_database`.`tags_users` AS TagUser 
                    ON (
                        `User1`.`id` = `TagUser`.`user_id`
                    ) 
            INNER JOIN
                `my_database`.`tags` AS Tag 
                    ON (
                        `TagUser`.`tag_id` = `Tag`.`id`
                    ) 
            WHERE
                `Tag`.`id` = (
                    8
                )
        ) 
        AND `User`.`id` NOT IN (
            SELECT
                `User1`.`id` 
            FROM
                `my_database`.`users` AS User1 
            LEFT JOIN
                `my_database`.`tags_users` AS TagUser 
                    ON (
                        `User1`.`id` = `TagUser`.`user_id`
                    ) 
            INNER JOIN
                `my_database`.`tags` AS Tag 
                    ON (
                        `TagUser`.`tag_id` = `Tag`.`id`
                    ) 
            WHERE
                `Tag`.`id` = (
                    3
                )
        ) 
        AND `Resume`.`has_file` = 1 
    GROUP BY
        `Attempt`.`user_id` 
    ORDER BY
        `Attempt`.`score` DESC

Related Articles



* original question posted on StackOverflow here.