[Solved] How to improve the performance of a SQL query even after adding indexes?

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. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `batchactionhistory`) 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.
  3. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `lookup_cities`) 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.
Optimal indexes for this query:
ALTER TABLE `batchactionhistory` ADD INDEX `batchactionhistory_idx_batchactionid_batchid` (`batchactionid`,`batchid`);
ALTER TABLE `batches` ADD INDEX `batches_idx_id_statusid` (`id`,`statusid`);
ALTER TABLE `bylines` ADD INDEX `bylines_idx_id` (`id`);
ALTER TABLE `itembylines` ADD INDEX `itembylines_idx_itemid` (`itemid`);
ALTER TABLE `itemorganisationissues` ADD INDEX `itemorganisationis_idx_itemorganisationid` (`itemorganisationid`);
ALTER TABLE `itemorganisationmessages` ADD INDEX `itemorganisationme_idx_itemorganisationid` (`itemorganisationid`);
ALTER TABLE `itemorganisations` ADD INDEX `itemorganisations_idx_itemid` (`itemid`);
ALTER TABLE `itemorganisationsources` ADD INDEX `itemorganisationso_idx_itemorganisationid` (`itemorganisationid`);
ALTER TABLE `items` ADD INDEX `items_idx_statusid_isrelevant` (`statusid`,`isrelevant`);
ALTER TABLE `lookup_countries` ADD INDEX `lookup_countries_idx_id` (`id`);
ALTER TABLE `lookup_mediachannels` ADD INDEX `lookup_mediachanne_idx_id` (`id`);
ALTER TABLE `lookup_messages` ADD INDEX `lookup_messages_idx_id` (`id`);
ALTER TABLE `lookup_messagetypes` ADD INDEX `lookup_messagetype_idx_id` (`id`);
ALTER TABLE `lookup_sourcetypes` ADD INDEX `lookup_sourcetypes_idx_id` (`id`);
ALTER TABLE `profileresults` ADD INDEX `profileresults_idx_itemid` (`itemid`);
ALTER TABLE `profiles` ADD INDEX `profiles_idx_id` (`id`);
ALTER TABLE `projectissues` ADD INDEX `projectissues_idx_id` (`id`);
The optimized query:
SELECT
        mc.name AS MediaName,
        lcc.name AS Country,
        i.overridedate AS Date,
        oi.rating,
        bl1.firstname + ' ' + bl1.surname AS Byline,
        b.id BatchNo,
        i.numinbatch ItemNumberInBatch,
        bah.changedatutc AS BatchDate,
        pri.code AS IssueNo,
        pri.name AS Issue,
        lm.neptunemessageid AS MessageNo,
        lmt.name AS MessageType,
        bl2.firstname + ' ' + bl2.surname AS SourceFullName,
        lst.name AS SourceTypeDesc 
    FROM
        profiles P 
    INNER JOIN
        profileresults PR 
            ON P.id = PR.profileid 
    INNER JOIN
        items i 
            ON PR.itemid = I.id 
    INNER JOIN
        batches b 
            ON b.id = i.batchid 
    INNER JOIN
        itemorganisations oi 
            ON i.id = oi.itemid 
    INNER JOIN
        lookup_mediachannels mc 
            ON i.mediachannelid = mc.id 
    LEFT OUTER JOIN
        lookup_countries lcc 
            ON lcc.id = mc.countryid 
    LEFT OUTER JOIN
        itembylines ib 
            ON ib.itemid = i.id 
    LEFT OUTER JOIN
        bylines bl1 
            ON bl1.id = ib.bylineid 
    INNER JOIN
        batchactionhistory bah 
            ON b.id = bah.batchid 
    INNER JOIN
        itemorganisationissues ioi 
            ON ioi.itemorganisationid = oi.id 
    INNER JOIN
        projectissues pri 
            ON pri.id = ioi.issueid 
    LEFT OUTER JOIN
        itemorganisationmessages iom 
            ON iom.itemorganisationid = oi.id 
    LEFT OUTER JOIN
        lookup_messages lm 
            ON iom.messageid = lm.id 
    LEFT OUTER JOIN
        lookup_messagetypes lmt 
            ON lmt.id = lm.messagetypeid 
    LEFT OUTER JOIN
        itemorganisationsources ios 
            ON ios.itemorganisationid = oi.id 
    LEFT OUTER JOIN
        bylines bl2 
            ON bl2.id = ios.bylineid 
    LEFT OUTER JOIN
        lookup_sourcetypes lst 
            ON lst.id = ios.sourcetypeid 
    WHERE
        p.id = @profileID 
        AND b.statusid IN (
            6, 7
        ) 
        AND bah.batchactionid = 6 
        AND i.statusid = 2 
        AND i.isrelevant = 1

Related Articles



* original question posted on StackOverflow here.