[Solved] Optimizing MySQL Left join query between 3 tables to reduce execution time

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. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `region_cuboid`) 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.
The optimized query:
SELECT
        region.id,
        region.world_id,
        min_x,
        min_y,
        min_z,
        max_x,
        max_y,
        max_z,
        version,
        mint_version 
    FROM
        minecraft_worldguard.region 
    INNER JOIN
        minecraft_worldguard.region_cuboid 
            ON region.id = region_cuboid.region_id 
            AND region.world_id = region_cuboid.world_id 
    LEFT JOIN
        minecraft_srvr.lot_version 
            ON region.id = lot 
    WHERE
        region.world_id = 10 
        AND region_cuboid.world_id = 10

Related Articles



* original question posted on StackOverflow here.