[Solved] MySQL. Group matches in columns by a common id

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. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `attributes_one`) were detected in the query. Removing them will result in a performance improvement. In some cases, JOINs become redundant after an optimization is applied, such as when converting OR conditions to a UNION clause.
  3. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `attributes_two`) were detected in the query. Removing them will result in a performance improvement. In some cases, JOINs become redundant after an optimization is applied, such as when converting OR conditions to a UNION clause.
Optimal indexes for this query:
ALTER TABLE `attributes_one_match` ADD INDEX `attributes_match_idx_id_items` (`id_items`);
ALTER TABLE `attributes_one_translations` ADD INDEX `attributes_transla_idx_id_one` (`id_attributes_one`);
ALTER TABLE `attributes_two_match` ADD INDEX `attributes_match_idx_id_two` (`id_attributes_two`);
ALTER TABLE `attributes_two_translations` ADD INDEX `attributes_transla_idx_id_code` (`id_language_code`);
ALTER TABLE `languages` ADD INDEX `languages_idx_language_code_id_languages` (`language_code`,`id_languages`);
The optimized query:
SELECT
        i.id_items AS id,
        GROUP_CONCAT(DISTINCT aot.translation 
    ORDER BY
        aot.translation DESC SEPARATOR '!¡') AS attribute_one,
        GROUP_CONCAT(DISTINCT att.translation 
    ORDER BY
        att.translation DESC SEPARATOR '!¡') AS attribute_two 
    FROM
        items i 
    LEFT JOIN
        languages AS l 
            ON l.language_code = 'en' 
    LEFT JOIN
        attributes_one_match AS aom 
            ON aom.id_items = i.id_items 
    LEFT JOIN
        attributes_one_translations AS aot 
            ON aot.id_attributes_one = aom.id_attributes_one 
            AND l.id_languages = aot.id_language_code 
            AND (
                MATCH (attributes_one_translations.translation) AGAINST ('"Gold"' IN BOOLEAN MODE) 
                OR MATCH (attributes_one_translations.translation) AGAINST ('"Silver"' IN BOOLEAN MODE)
            ) 
    LEFT JOIN
        attributes_two_match AS atm 
            ON atm.id_items = i.id_items 
    LEFT JOIN
        attributes_two_translations AS att 
            ON att.id_attributes_two = atm.id_attributes_two 
            AND l.id_languages = att.id_language_code 
    GROUP BY
        id 
    ORDER BY
        2 ASC

Related Articles



* original question posted on StackOverflow here.