[Solved] mysql counting with subquery rewriting problem

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. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  3. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `Task` ADD INDEX `task_idx_id` (`id`);
ALTER TABLE `Task_Department_Combi` ADD INDEX `task_combi_idx_task_id_department_id` (`Task_id`,`Department_id`);
ALTER TABLE `Task_Location_Combi` ADD INDEX `task_combi_idx_task_id_location_id` (`Task_id`,`Location_id`);
ALTER TABLE `Task_Worker_Combi` ADD INDEX `task_combi_idx_worker_id` (`Worker_id`);
ALTER TABLE `Worker` ADD INDEX `worker_idx_id` (`id`);
The optimized query:
SELECT
        W.id,
        W.Name,
        COUNT(TWC.Task_id) AS Count 
    FROM
        Worker AS W 
    LEFT JOIN
        Task_Worker_Combi AS TWC 
            ON (
                W.id = TWC.Worker_id
            ) 
    WHERE
        W.id > 0 
        AND EXISTS (
            SELECT
                1 
            FROM
                Task AS T 
            LEFT JOIN
                (
                    Task_Location_Combi AS TLC, Task_Department_Combi AS TDC
                ) 
                    ON (
                        T.id = TLC.Task_id 
                        AND T.id = TDC.Task_id
                    ) 
            WHERE
                (
                    1 
                    AND TLC.Location_id IN (
                        1, 2
                    ) 
                    AND TDC.Department_id IN (
                        3, 4
                    )
                ) 
                AND (
                    TWC.Task_id = T.id
                ) 
            GROUP BY
                T.id 
            ORDER BY
                NULL
        ) 
    GROUP BY
        W.id 
    ORDER BY
        W.Name

Related Articles



* original question posted on StackOverflow here.