[Solved] Sending data taking too long but indexes aready create

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 Calling Functions With Indexed Columns (query line: 71): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `name` is indexed, the index won’t be used as it’s wrapped with the function `LTRIM`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 128): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `name` is indexed, the index won’t be used as it’s wrapped with the function `LTRIM`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. 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.
  4. 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.
  5. 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.
  6. Use UNION ALL instead of UNION (query line: 78): 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 `accounts` ADD INDEX `accounts_idx_deleted_id` (`deleted`,`id`);
ALTER TABLE `cases` ADD INDEX `cases_idx_deleted_date_entered` (`deleted`,`date_entered`);
ALTER TABLE `cases` ADD INDEX `cases_idx_date_entered` (`date_entered`);
ALTER TABLE `users` ADD INDEX `users_idx_deleted_id` (`deleted`,`id`);
The optimized query:
SELECT
        cases_id,
        cases_cstm_assunto_c,
        cases_name,
        cases_case_number,
        cases_priority,
        account_name,
        account_name_owner,
        account_name_mod,
        cases_account_id,
        assigned_user_name,
        assigned_user_name_owner,
        assigned_user_name_mod,
        cases_status,
        cases_date_entered,
        cases_assigned_user_id 
    FROM
        ((SELECT
            cases_id AS cases_id,
            cases_cstm.assunto_c AS cases_cstm_assunto_c,
            cases_name AS cases_name,
            cases_case_number AS cases_case_number,
            cases_priority AS cases_priority,
            accounts.name account_name,
            accounts.assigned_user_id account_name_owner,
            'Accounts' account_name_mod,
            cases_account_id AS cases_account_id,
            LTRIM(RTRIM(CONCAT(IFNULL(jt1.first_name,
            ''),
            ' ',
            IFNULL(jt1.last_name,
            '')))) assigned_user_name,
            jt1.created_by assigned_user_name_owner,
            'Users' assigned_user_name_mod,
            cases_status AS cases_status,
            cases_date_entered AS cases_date_entered,
            cases_assigned_user_id AS cases_assigned_user_id 
        FROM
            (SELECT
                cases.id AS cases_id,
                cases.name AS cases_name,
                cases.case_number AS cases_case_number,
                cases.priority AS cases_priority,
                cases.account_id AS cases_account_id,
                cases.status AS cases_status,
                cases.date_entered AS cases_date_entered,
                cases.assigned_user_id AS cases_assigned_user_id 
            FROM
                cases 
            WHERE
                cases.deleted = 0 
            ORDER BY
                cases.date_entered DESC LIMIT 11) AS cases 
        LEFT JOIN
            cases_cstm 
                ON cases.cases_id = cases_cstm.id_c 
        LEFT JOIN
            accounts accounts 
                ON cases.cases_account_id = accounts.id 
                AND accounts.deleted = 0 
                AND accounts.deleted = 0 
        LEFT JOIN
            users jt1 
                ON cases.cases_assigned_user_id = jt1.id 
                AND jt1.deleted = 0 
                AND jt1.deleted = 0 
        WHERE
            (
                (
                    (
                        LTRIM(RTRIM(CONCAT(IFNULL(accounts.name, '')))) LIKE 'rodrigo fernando%'
                    )
                )
            ) 
            AND 1 = 1 LIMIT 11
        ) 
    UNION
    DISTINCT (SELECT
        cases_id AS cases_id,
        cases_cstm.assunto_c AS cases_cstm_assunto_c,
        cases_name AS cases_name,
        cases_case_number AS cases_case_number,
        cases_priority AS cases_priority,
        accounts.name account_name,
        accounts.assigned_user_id account_name_owner,
        'Accounts' account_name_mod,
        cases_account_id AS cases_account_id,
        LTRIM(RTRIM(CONCAT(IFNULL(jt1.first_name,
        ''),
        ' ',
        IFNULL(jt1.last_name,
        '')))) assigned_user_name,
        jt1.created_by assigned_user_name_owner,
        'Users' assigned_user_name_mod,
        cases_status AS cases_status,
        cases_date_entered AS cases_date_entered,
        cases_assigned_user_id AS cases_assigned_user_id 
    FROM
        (SELECT
            cases.id AS cases_id,
            cases.name AS cases_name,
            cases.case_number AS cases_case_number,
            cases.priority AS cases_priority,
            cases.account_id AS cases_account_id,
            cases.status AS cases_status,
            cases.date_entered AS cases_date_entered,
            cases.assigned_user_id AS cases_assigned_user_id 
        FROM
            cases 
        WHERE
            cases.deleted = 0 
        ORDER BY
            cases.date_entered DESC LIMIT 11) AS cases 
    LEFT JOIN
        cases_cstm 
            ON cases.cases_id = cases_cstm.id_c 
    LEFT JOIN
        accounts accounts 
            ON cases.cases_account_id = accounts.id 
            AND accounts.deleted = 0 
            AND accounts.deleted = 0 
    LEFT JOIN
        users jt1 
            ON cases.cases_assigned_user_id = jt1.id 
            AND jt1.deleted = 0 
            AND jt1.deleted = 0 
    WHERE
        (((LTRIM(RTRIM(CONCAT(IFNULL(accounts.name, '')))) LIKE 'rodrigo fernando%'))) 
        AND 1 = 1 LIMIT 11)
) AS union1 
ORDER BY
union1.cases_date_entered DESC LIMIT 11

Related Articles



* original question posted on StackOverflow here.