查询SQL Server的性能调优

时间:2016-03-02 07:42:11

标签: sql sql-server sql-server-2008-r2

我有两张桌子:

SELECT [DealerdistributionId]
      ,[DealerCode]
      ,[BarCode]
      ,[BarCode2]
      ,[Model]
      ,[DONumber]
      ,[DistributionDate]
  FROM [tblDealerDistributionDetails]

产生这些结果

enter image description here

和另一张表:

SELECT [ProductRegID]
      ,[ProductType]
      ,[ProductModelID]
      ,[SalerID]
      ,[ProductID]
      ,[PhoneNumber]
      ,[RegistrationDate]
  FROM [tblProductRegistration]

结果

enter image description here

第二个表tblProductRegistration从第一个表ProductID BarCodeBarCode2开始tblDealerDistributionDetails,但只有一个BarCode或{{ 1}}。

我想找到哪个经销商在tblProductRegistration中有多少型号的模型。

输出: DelarCode,Model,tblProductRegistration中的TotalEntry。

我试过这个

BarCode2

对于特定模型'Olvio L12'但它会导致性能问题。我需要更快的查询性能

具有更快的查询性能。请帮助。

1 个答案:

答案 0 :(得分:1)

CREATE NONCLUSTERED INDEX ix_p
    ON tblProductRegistration (RegistrationDate, ProductID)
GO
CREATE NONCLUSTERED INDEX ix_d
    ON tblDealerDistributionDetails (model, DealerCode) INCLUDE (BarCode, BarCode2)
GO

SELECT DealerCode, COUNT_BIG(*) AS [Activated]
FROM tblDealerDistributionDetails d
WHERE model = 'Olvio L12'
    AND EXISTS(
        SELECT *
        FROM tblProductRegistration r
        WHERE r.RegistrationDate BETWEEN '20160301' AND '20160302'
            AND r.ProductID IN (d.BarCode, d.BarCode2)
    )
GROUP BY DealerCode
--OPTION(RECOMPILE)