在条款冲突和性能问题之间交叉应用/之间

时间:2014-09-22 22:04:09

标签: sql sql-server

DECLARE @Contract AS NVARCHAR(MAX) 
DECLARE @STARTDATE AS DATETIME; 
DECLARE @ENDDATE AS DATETIME; 

SET @STARTDATE = '2014-07-01'; 
SET @ENDDATE = '2014-09-21'; 

WITH CTE (Cntr) 
     AS (SELECT CONVERT(NVARCHAR(MAX), ContractId) + ',' 
         FROM   contract 
         WHERE  clientid = 20739 
         FOR XML PATH('')) 
SELECT @Contract = Cntr 
FROM   CTE; 

SELECT DISTINCT so.ServiceOrderId, 
                C.ContractId, 
                so.CreateDate, 
                CP.AccountNumber    AS 'Customer #', 
                CT.ContractName, 
                CP.NAME             AS 'Customer Name', 
                bm.BucketName       AS 'BucketCode', 
                LISTOFNOTES, 
                C.ContractName      AS 'Contract', 
                PP.PartnerProfileId AS 'PartnerId', 
                PP.PartnerName      AS 'PartnerName', 
                stm.ServiceTypename 
FROM   ServiceOrder AS SO 
       CROSS APPLY (SELECT T.BasCode + ',' 
                    FROM   TECHNICIANTRIPDETAIL TTD 
                           INNER JOIN TECHNICIAN T 
                                   ON TTD.TECHNICIANID = T.TECHNICIANID 
                    WHERE  TTD.SERVICEORDERID = SO.SERVICEORDERID 
                    FOR XML PATH('')) TEMP(LISTOFNOTES) 
       INNER JOIN Contract AS C 
               ON C.ContractId = so.ContractId 
       INNER JOIN ClientProfile AS CP 
               ON CP.ClientId = C.ClientId 
       INNER JOIN BucketMaster AS bm 
               ON bm.BucketId = so.Status 
       INNER JOIN Servicetypemaster AS stm 
               ON stm.servicetypeid = C.servicetypeid 
       INNER JOIN ContractType CT 
               ON CT.ContractTypeId = C.ContractTypeId 
       LEFT OUTER JOIN PartnerProfile AS PP 
                    ON PP.PartnerProfileId = CP.PartnerId 
WHERE  SO.Deleted = 'false' 
       AND so.CreateDate BETWEEN @STARTDATE AND @ENDDATE 
       AND C.ContractId IN (SELECT * 
                            FROM   dbo.Split(@CONTRACT, ',')) 

我使用上面的查询来获取报告中的数据。

此查询需要4到5分钟的执行时间,这太多了。

这个查询发生的奇怪的事情是,如果我将删除交叉应用查询将在10秒内运行,如果我保持交叉应用并删除条件之间,那么也在10到12秒的时间内执行查询

我想了解为什么这种奇怪的行为以及交叉应用和相互之间是如何相关的。

此外,是否有人可以帮助我使用Cross Apply和where where where where condition来优化此查询?

0 个答案:

没有答案