[Solved] mysql 7 + joins on a table, how to speed up?

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. 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.
  2. 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'.
  3. Reduce Impact Of Subqueries In Select Clause (modified query below): Subqueries in the SELECT clause will be executed once per each select row. Therefore, reducing the amount of selected rows in the FROM clause before getting to the SELECT clause will result in a performance improvement.
  4. Replace Left Join With Subquery (modified query below): The pattern of inflating the amount of data (using joins) and deflating (using GROUP BY) usually slows down queries. In this case, it can be avoided by moving some of the logic to the SELECT clause, and therefore removing some of the LEFT JOINs. In some cases, this transformation can lead to an obsolete GROUP BY clause, which can also be removed.
Optimal indexes for this query:
ALTER TABLE `table1` ADD INDEX `table1_idx_archived` (`archived`);
ALTER TABLE `table2` ADD INDEX `table2_idx_table1_id` (`table1_id`);
ALTER TABLE `table3` ADD INDEX `table3_idx_table1_id` (`table1_id`);
ALTER TABLE `table4` ADD INDEX `table4_idx_table1_id` (`table1_id`);
ALTER TABLE `table5` ADD INDEX `table5_idx_table1_id` (`table1_id`);
ALTER TABLE `table6` ADD INDEX `table6_idx_table1_id` (`table1_id`);
ALTER TABLE `table7` ADD INDEX `table7_idx_table1_id` (`table1_id`);
The optimized query:
SELECT
        optimizedSub1.*,
        (SELECT
            GROUP_CONCAT(DISTINCT table2.table2_id) AS table2Ids 
        FROM
            table2 
        WHERE
            optimizedSub1.table1Id = table2.table1_id LIMIT 1) AS table2Ids,
        (SELECT
            GROUP_CONCAT(DISTINCT table3.table3_id) AS table3Ids 
        FROM
            table3 
        WHERE
            optimizedSub1.table1Id = table3.table1_id LIMIT 1) AS table3Ids,
        (SELECT
            GROUP_CONCAT(DISTINCT table4.table4_id) AS table4Ids 
        FROM
            table4 
        WHERE
            optimizedSub1.table1Id = table4.table1_id LIMIT 1) AS table4Ids,
        (SELECT
            GROUP_CONCAT(DISTINCT table5.table5_id) AS table5Ids 
        FROM
            table5 
        WHERE
            optimizedSub1.table1Id = table5.table1_id LIMIT 1) AS table5Ids,
        (SELECT
            GROUP_CONCAT(DISTINCT table6.table6_id) AS table6Ids 
        FROM
            table6 
        WHERE
            optimizedSub1.table1Id = table6.table1_id LIMIT 1) AS table6Ids,
        (SELECT
            GROUP_CONCAT(DISTINCT table7.table7_id) AS table7Ids 
        FROM
            table7 
        WHERE
            optimizedSub1.table1Id = table7.table1_id LIMIT 1) AS table7Ids 
    FROM
        (SELECT
            table1.table1_id AS table1Id 
        FROM
            table1 
        WHERE
            table1.archived = false 
        ORDER BY
            NULL LIMIT 1000) AS optimizedSub1

Related Articles



* original question posted on StackOverflow here.