[Solved] Database view performance issue versus underlying query

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. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `IOHD_REQUEST_HELPDESK`) were detected in the query. Removing them will result in a performance improvement. In some cases, JOINs become redundant after an optimization is applied, such as when converting OR conditions to a UNION clause.
  4. Replace Left Join With Subquery (modified query below): The pattern of inflating the amount of data (using joins) and deflating (using GROUP BY) usually slows down queries. In this case, it can be avoided by moving some of the logic to the SELECT clause, and therefore removing some of the LEFT JOINs. In some cases, this transformation can lead to an obsolete GROUP BY clause, which can also be removed.
Optimal indexes for this query:
ALTER TABLE `IOHD_REF_KEYVALUE` ADD INDEX `iohd_keyvalue_idx_type_key` (`TYPE`,`KEY`);
ALTER TABLE `IOHD_REQUEST` ADD INDEX `iohd_request_idx_id` (`ID`);
ALTER TABLE `IOHD_REQUEST_CLIENT` ADD INDEX `iohd_client_idx_request_id` (`REQUEST_ID`);
ALTER TABLE `IOHD_REQUEST_IDENTIFIER` ADD INDEX `iohd_identifier_idx_request_id` (`REQUEST_ID`);
ALTER TABLE `IOHD_REQUEST_PLATFORM` ADD INDEX `iohd_platform_idx_request_id` (`REQUEST_ID`);
ALTER TABLE `IOHD_REQUEST_VENDOR` ADD INDEX `iohd_vendor_idx_request_id` (`REQUEST_ID`);
The optimized query:
SELECT
        r.ID AS requestIdMultiple,
        CAST(GROUP_CONCAT(ri.IDENTIFIER) AS CHAR (255)) AS IDENTIFIER,
        (SELECT
            CAST(GROUP_CONCAT(kv6.VALUE) AS CHAR (255)) AS CLIENT_ID 
        FROM
            IOHD_REF_KEYVALUE kv6 
        WHERE
            (
                rc.CLIENT_ID = kv6.KEY 
                AND kv6.TYPE = 'CLIENT'
            ) LIMIT 1) AS CLIENT_ID,
        (SELECT
            CAST(GROUP_CONCAT(kv1.VALUE) AS CHAR (255)) AS IDENTIFIER_ID 
        FROM
            IOHD_REF_KEYVALUE kv1 
        WHERE
            (
                ri.IDENTIFIER_ID = kv1.KEY 
                AND kv1.TYPE = 'IDENTIFIERTYPE'
            ) LIMIT 1) AS IDENTIFIER_ID,
        (SELECT
            CAST(GROUP_CONCAT(kv7.VALUE) AS CHAR (255)) AS PLATFORM_ID 
        FROM
            IOHD_REF_KEYVALUE kv7 
        WHERE
            (
                rp.PLATFORM_ID = kv7.KEY 
                AND kv7.TYPE = 'PLATFORM'
            ) LIMIT 1) AS PLATFORM_ID,
        (SELECT
            CAST(GROUP_CONCAT(kv8.VALUE) AS CHAR (255)) AS VENDOR_ID 
        FROM
            IOHD_REF_KEYVALUE kv8 
        WHERE
            (
                rv.VENDOR_ID = kv8.KEY 
                AND kv8.TYPE = 'VENDOR'
            ) LIMIT 1) AS VENDOR_ID 
    FROM
        IOHD_REQUEST r 
    LEFT JOIN
        IOHD_REQUEST_CLIENT rc 
            ON r.ID = rc.REQUEST_ID 
    LEFT JOIN
        IOHD_REQUEST_IDENTIFIER ri 
            ON r.ID = ri.REQUEST_ID 
    LEFT JOIN
        IOHD_REQUEST_PLATFORM rp 
            ON r.ID = rp.REQUEST_ID 
    LEFT JOIN
        IOHD_REQUEST_VENDOR rv 
            ON r.ID = rv.REQUEST_ID 
    GROUP BY
        r.ID 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.