[Solved] mysql explain slow where on left joined 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. Avoid OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  2. 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.
  3. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `group_status`) 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 Sorting/Grouping By The First Table In Join Order (modified query below): The database can use indexes more efficiently when sorting and grouping using columns from the first table in the join order. The first table is determined based on the prediction of the the optimal first table, and is not necessarily the first table shown in the FROM clause.
  5. Use UNION ALL instead of UNION (query line: 22): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
ALTER TABLE `group_status` ADD INDEX `group_status_idx_group_id_id` (`group_id`,`id`);
ALTER TABLE `group_status` ADD INDEX `group_status_idx_id` (`id`);
ALTER TABLE `status` ADD INDEX `status_idx_id` (`id`);
ALTER TABLE `status` ADD INDEX `status_idx_user_id` (`user_id`);
The optimized query:
SELECT
        s_id,
        s_status,
        gs_group_id 
    FROM
        ((SELECT
            s.id AS s_id,
            s.status AS s_status,
            s.group_id AS gs_group_id 
        FROM
            status s 
        INNER JOIN
            group_status gs 
                ON s.id = gs.id 
        WHERE
            gs.group_id IN (
                78, 79, 79, 80, 80, 83, 84, 85, 86, 87, 88, 89, 89, 91, 92, 92, 94, 98
            ) 
        ORDER BY
            gs.id DESC LIMIT 15) 
    UNION
    DISTINCT (SELECT
        s.id AS s_id,
        s.status AS s_status,
        s.group_id AS gs_group_id 
    FROM
        status s 
    LEFT JOIN
        group_status gs 
            ON s.id = gs.id 
    WHERE
        s.user_id IN (55883, 122024, 442468, 846269, 903941, 980896, 192660, 20608, 525056, 563457) 
    ORDER BY
        gs.id DESC LIMIT 15)
) AS union1 
ORDER BY
union1.s_id DESC LIMIT 15

Related Articles



* original question posted on StackOverflow here.