[Solved] Can I make this multiple join query faster?

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. Mixed Order By Directions Prevents Index Use (query line: 77): The database will not use a sorting index (if exists) in cases where the query mixes ASC (the default if not specified) and DESC order. To avoid filesort, you may consider using the same order type for all columns. Another option that will allow you to switch one direction to another is to create a new reversed "sort" column (max_sort - sort) and index it instead.
  3. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `DDate`) 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.
Optimal indexes for this query:
ALTER TABLE `DBrand` ADD INDEX `dbrand_idx_dbrandid` (`DBrandID`);
ALTER TABLE `DCSaleItem` ADD INDEX `dcsaleitem_idx_dcsaleid` (`DCSaleID`);
ALTER TABLE `DCStore` ADD INDEX `dcstore_idx_dcstoreid` (`DCStoreID`);
ALTER TABLE `DCurrVenr` ADD INDEX `dcurrvenr_idx_dcurrveid` (`DCurrVeID`);
ALTER TABLE `DDate` ADD INDEX `ddate_idx_fyqtr` (`FYQtr`);
ALTER TABLE `ReportProF` ADD INDEX `reportprof_idx_dimprflgid` (`DimPrFlgID`);
ALTER TABLE `ReportSales` ADD INDEX `reportsales_idx_ddateid` (`DDateID`);
ALTER TABLE `dimitem` ADD INDEX `dimitem_idx_itemidnumber` (`ItemIDNumber`);
The optimized query:
SELECT
        csi.ItemIDNumber,
        csi.UnOMe,
        csi.SellItemID,
        csi.ItemBc,
        ISNULL(pr.PrFlg,
        'PromoYes') Promo,
        br.BrandID Brand,
        cs.AddSte State,
        csi.ItemIDNumber,
        csi.UnOMe UOM,
        csi.SellItemID,
        csi.ItemBc,
        sum(f.TLSale) sales,
        sum(f.TotalLineCost) COGS,
        sum(f.ItemQS) Quantity,
        sum(f."1stM") FirstM,
        sum(f."1stM") B_Mar,
        sum(f."1stM") T_Mar,
        sum(f."1stM") T_Mar,
        csi.ItemCategory,
        cv.Name,
        f.ReportSalesID,
        it.ItemName,
        it.ItemCategory,
        it.ItemSubCategory,
        it.itemclass,
        it.GList,
        it.UMlit,
        it.ItemShortName,
        it.Clssficatn 
    FROM
        dbo.ReportSales F WITH (NOLOCK) 
    INNER JOIN
        dbo.DDate dd WITH (NOLOCK) 
            ON f.DDateID = dd.DDateID 
    LEFT OUTER JOIN
        dbo.DCSaleItem csi WITH (NOLOCK) 
            ON f.DCSaleID = csi.DCSaleID 
    LEFT OUTER JOIN
        dbo.DCurrVenr cv WITH (NOLOCK) 
            ON f.DCurrVeID = cv.DCurrVeID 
    LEFT OUTER JOIN
        dbo.DBrand br WITH (NOLOCK) 
            ON f.DBrandID = br.DBrandID 
    LEFT OUTER JOIN
        dbo.DCStore cs WITH (NOLOCK) 
            ON f.DCStoreID = cs.DCStoreID 
    LEFT OUTER JOIN
        dbo.ReportProF pr WITH (NOLOCK) 
            ON f.DPromotionMID = pr.DimPrFlgID 
    LEFT OUTER JOIN
        dbo.dimitem it WITH (NOLOCK) 
            ON csi.ItemIDNumber = it.ItemIDNumber 
    WHERE
        dd.FYQtr = '2015-1' 
    GROUP BY
        csi.ItemIDNumber,
        cs.AddSte,
        csi.ItemCategory,
        cv.Name,
        csi.SellItemID,
        csi.ItemBc,
        br.BrandID,
        pr.PrFlg,
        csi.UnOMe,
        f.ReportSalesID,
        it.ItemName,
        it.ItemCategory,
        it.ItemSubCategory,
        it.ItemClass,
        it.GList,
        it.UMlit,
        it.ItemShortName,
        it.Clssficatn 
    ORDER BY
        br.BrandID,
        cv.Name,
        csi.ItemIDNumber,
        pr.PrFlg DESC,
        cs.AddSte,
        csi.ItemCategory

Related Articles



* original question posted on StackOverflow here.