[Solved] Join Issue - How to bring back only 1 outer join per row

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. `sof_slot_game_images`) 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. Sort and Limit Before Joining (modified query below): In cases where the joins aren't filtering any rows, it's possible to sort and limit the amount of rows using a subquery in the FROM clause, before applying the joins to all other tables.
  4. Use Numeric Column Types For Numeric Values (query line: 39): Referencing a numeric value (e.g. 3) 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 `sof_developers` ADD INDEX `sof_developers_idx_developer_id` (`developer_id`);
ALTER TABLE `sof_slot_game_details` ADD INDEX `sof_game_idx_slot_id` (`slot_game_id`);
ALTER TABLE `sof_slot_game_images` ADD INDEX `sof_game_idx_image_id` (`image_type_id`);
ALTER TABLE `sof_slot_games` ADD INDEX `sof_games_idx_slot_id_launch_date` (`slot_game_id`,`launch_date`);
ALTER TABLE `sof_slot_games` ADD INDEX `sof_games_idx_launch_date` (`launch_date`);
The optimized query:
SELECT
        sof_slot_games_launch_date,
        sof_slot_games_game_name,
        sof_reviews_review_content,
        sof_reviews_slot_game_id,
        sof_slot_game_details.no_of_reels,
        sof_slot_game_details.paylines,
        sof_reviews_reg_timestamp,
        sof_developers.developer_name,
        sof_slot_games_game_slug,
        sof_slot_game_images.game_image 
    FROM
        (SELECT
            sof_reviews.review_content AS sof_reviews_review_content,
            sof_reviews.reg_timestamp AS sof_reviews_reg_timestamp,
            sof_reviews.slot_game_id AS sof_reviews_slot_game_id,
            sof_reviews.launch_date AS sof_slot_games_launch_date,
            sof_reviews.game_name AS sof_slot_games_game_name,
            sof_reviews.slot_game_id AS sof_slot_games_slot_game_id,
            sof_reviews.game_slug AS sof_slot_games_game_slug,
            sof_reviews.developer_id AS sof_slot_games_developer_id 
        FROM
            sof_reviews 
        INNER JOIN
            sof_slot_games 
                ON sof_slot_games.slot_game_id = sof_reviews.slot_game_id 
        ORDER BY
            sof_slot_games.launch_date DESC LIMIT 20) AS sof_reviews 
    INNER JOIN
        sof_slot_game_details 
            ON sof_reviews.sof_slot_games_slot_game_id = sof_slot_game_details.slot_game_id 
    INNER JOIN
        sof_developers 
            ON sof_reviews.sof_slot_games_developer_id = sof_developers.developer_id 
    INNER JOIN
        sof_slot_game_images 
            ON sof_reviews.sof_reviews_slot_game_id = sof_slot_game_images.slot_game_id 
    WHERE
        sof_slot_game_images.image_type_id = '3' LIMIT 20

Related Articles



* original question posted on StackOverflow here.