[Solved] Very slow mysql query: left join multiple table and where clause for each table

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. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `jjtable_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.
  3. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `jjtable_map_fea`) 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.
  4. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `jjtable_map`) 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. `jjtable_map_per`) 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. `jjtable_map_ag`) 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 Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `jjtable_map_fa`) 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.
  8. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `jjtable_tags`) 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.
  9. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `jjtable_map_ge`) 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.
  10. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `jjtable_map_ban`) 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.
  11. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `jjtable_map_ev`) 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.
  12. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `jjtable_map_im`) 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 `jjtable` ADD INDEX `jjtable_idx_published_approved_id` (`published`,`approved`,`id`);
ALTER TABLE `jjtable_catg` ADD INDEX `jjtable_catg_idx_cid` (`cid`);
ALTER TABLE `jjtable_map` ADD INDEX `jjtable_map_idx_delete_flag_picid` (`delete_flag`,`picid`);
ALTER TABLE `jjtable_map_ag` ADD INDEX `jjtable_ag_idx_delete_flag_picid` (`delete_flag`,`picid`);
ALTER TABLE `jjtable_map_fa` ADD INDEX `jjtable_fa_idx_delete_flag_picid` (`delete_flag`,`picid`);
ALTER TABLE `jjtable_map_fea` ADD INDEX `jjtable_fea_idx_delete_flag_picid` (`delete_flag`,`picid`);
ALTER TABLE `jjtable_map_per` ADD INDEX `jjtable_per_idx_delete_flag_picid` (`delete_flag`,`picid`);
ALTER TABLE `jjtable_tags` ADD INDEX `jjtable_tags_idx_delete_flag_tid` (`delete_flag`,`tid`);
The optimized query:
SELECT
        jj.id,
        jj.imgtitle,
        jj.alias 
    FROM
        jjtable jj 
    INNER JOIN
        jjtable_catg jjc 
            ON jj.catid = jjc.cid 
    INNER JOIN
        jjtable_map jjtm_name 
            ON jj.id = jjtm_name.picid 
    INNER JOIN
        jjtable_tags jjts_name 
            ON jjtm_name.tid = jjts_name.tid 
    INNER JOIN
        jjtable_map_per jjtm_per 
            ON jj.id = jjtm_per.picid 
    INNER JOIN
        jjtable_tags jjts_per 
            ON jjtm_per.tid = jjts_per.tid 
    INNER JOIN
        jjtable_map_fea jjtm_fea 
            ON jj.id = jjtm_fea.picid 
    INNER JOIN
        jjtable_tags jjts_fea 
            ON jjtm_fea.tid = jjts_fea.tid 
    INNER JOIN
        jjtable_map_ag jjtm_ag 
            ON jj.id = jjtm_ag.picid 
    INNER JOIN
        jjtable_tags jjts_ag 
            ON jjtm_ag.tid = jjts_ag.tid 
    INNER JOIN
        jjtable_map_fa jjtm_fa 
            ON jj.id = jjtm_fa.picid 
    INNER JOIN
        jjtable_tags jjts_fa 
            ON jjtm_fa.tid = jjts_fa.tid 
    WHERE
        jj.published = 1 
        AND jj.approved = 1 
        AND jjts_fea.tid IN (
            87, 90
        ) 
        AND jjts_fea.delete_flag = 0 
        AND jjtm_fea.delete_flag = 0 
        AND jjc.cid IN (
            4, 10
        ) 
        AND jjts_name.tid IN (
            77
        ) 
        AND jjts_name.delete_flag = 0 
        AND jjtm_name.delete_flag = 0 
        AND jjts_per.tid IN (
            28, 36
        ) 
        AND jjts_per.delete_flag = 0 
        AND jjtm_per.delete_flag = 0 
        AND jjts_ag.tid IN (
            98, 99
        ) 
        AND jjts_ag.delete_flag = 0 
        AND jjtm_ag.delete_flag = 0 
        AND jjts_fa.tid IN (
            104, 107
        ) 
        AND jjts_fa.delete_flag = 0 
        AND jjtm_fa.delete_flag = 0 
    GROUP BY
        jj.id 
    ORDER BY
        CASE 
            WHEN date(jj.date) > date_add(date(now()),
            INTERVAL -14 day) THEN jj.view END DESC,
jj.id DESC

Related Articles



* original question posted on StackOverflow here.