旧的SQL查询优化

时间:2014-11-17 06:35:21

标签: sql-server performance

我预先编写了SQL Query,它使45 minutes实际运行并显示数据。它返回总80000+条记录。但是运行和显示数据需要花费很多时间。

SELECT 
hq.QuoteHeaderId,
hq.HandsetQuoteId,
hq.QuoteDate,
t_PhoneAudit.PhoneModelId,
t_PhoneAudit.IMEI,
t_PhoneModel.ModelName,
t_PhoneBrand.Name As BrandName
, al.ActivityId
, par.Result
, al.CustomMessage,
al.[Description]
,thqai.Value AS AgentName
FROM [t_DynaGroupMissingRecordsProcessing] 
INNER JOIN t_HandsetQuote hq ON t_DynaGroupMissingRecordsProcessing.HandsetQuoteId = hq.HandsetQuoteId
INNER JOIN t_PhoneAudit ON t_PhoneAudit.PhoneAuditId = hq.QuotePhoneAuditId
INNER JOIN t_PhoneModel ON t_PhoneModel.PhoneModelId = t_PhoneAudit.PhoneModelId
INNER JOIN t_PhoneBrand ON t_PhoneBrand.PhoneBrandId = t_PhoneModel.PhoneBrandId
INNER JOIN 
(
    SELECT par.HandsetQuoteId, txt_Value AS ActivityId, 'OK' AS Result FROM t_PhoneAuditRetail par CROSS APPLY dbo.fn_ParseText2Table(par.Ok, ',') 
    UNION 
    SELECT par.HandsetQuoteId, txt_Value AS ActivityId, 'Fault' AS Result FROM t_PhoneAuditRetail par CROSS APPLY dbo.fn_ParseText2Table(par.Fault, ',') 
) par ON hq.HandsetQuoteId = par.HandsetQuoteId
INNER JOIN t_ActivityLocalization al ON par.ActivityId = al.ActivityId
INNER JOIN t_ContactChannel CC ON al.ContactChannelId = CC.ContactChannelId
INNER JOIN t_testingProfiledetails ON t_testingProfiledetails.ActivityId = al.ActivityId AND (    IsVisibleForRetail = '1' OR t_testingProfiledetails.ActivityId IN ('93','97')) AND  t_testingProfiledetails.ProfileId IN (SELECT DefailtRetailProfileId FROM t_ContactChannel WHERE Name IN ('Holland Retail', 'BelCompanyNPEY Retail', 'Belcompany Retail', 'HollandNPEY Retail'))
LEFT OUTER JOIN t_HandsetQuoteAdditionalInfo thqai on thqai.HandsetQuoteId = hq.HandsetQuoteId and thqai.KeyName = 'AgentName'
WHERE [t_DynaGroupMissingRecordsProcessing].IsProcessed = 0 
AND CC.Name IN ('Holland Retail', 'BelCompanyNPEY Retail', 'Belcompany Retail', 'HollandNPEY  Retail')
ORDER BY hq.HandsetQuoteId

主要问题在于CC.Name IN ('Holland Retail', 'BelCompanyNPEY Retail', 'Belcompany Retail', 'HollandNPEY Retail')这句话。

如果我继续在In Statement中添加名称,则查询会变得越来越慢。

以下是我正在使用的功能的定义:

ALTER FUNCTION [dbo].[fn_ParseText2Table]     
(    
@p_SourceText  VARCHAR(8000)  
,@p_Delimeter VARCHAR(100) = ',' --default to comma delimited.    
)    
RETURNS @retTable TABLE    
(    
 txt_value VARCHAR(2000)    
)    
AS    
BEGIN    
 DECLARE @w_Continue  int    
,@w_StartPos  int    
,@w_Length  int    
,@w_Delimeter_pos int    
,@w_tmp_txt   VARCHAR(2000)    
,@w_Delimeter_Len tinyint    

SET  @w_Continue = 1    
SET @w_StartPos = 1    
SET @p_SourceText = RTRIM( LTRIM( @p_SourceText))    
SET @w_Length   = DATALENGTH( RTRIM( LTRIM( @p_SourceText)))    
SET @w_Delimeter_Len = len(@p_Delimeter)    

WHILE @w_Continue = 1    
BEGIN    
 SET @w_Delimeter_pos = CHARINDEX(@p_Delimeter,(SUBSTRING( @p_SourceText, @w_StartPos,((@w_Length - @w_StartPos)+@w_Delimeter_Len))))    
 IF @w_Delimeter_pos > 0  -- delimeter(s) found, get the value    
 BEGIN    
  SET @w_tmp_txt = LTRIM(RTRIM( SUBSTRING( @p_SourceText, @w_StartPos     
 ,(@w_Delimeter_pos - 1)) ))    
  SET @w_StartPos = @w_Delimeter_pos + @w_StartPos + (@w_Delimeter_Len- 1)    
 END    
 ELSE      -- No more delimeters, get last value    
 BEGIN    
   SET @w_tmp_txt = LTRIM(RTRIM( SUBSTRING( @p_SourceText, @w_StartPos     
  ,((@w_Length - @w_StartPos) + @w_Delimeter_Len)) ))    
  SELECT @w_Continue = 0    
 END    
 INSERT INTO @retTable VALUES( @w_tmp_txt )    
 END    
RETURN  

END

请帮我优化此查询。

1 个答案:

答案 0 :(得分:0)

你可以试试这个

Insert the list of names to @table(name) and join it to cc
on cc.name=@table.name