0

此查询当前运行大约需要 30 秒。

我想要一些帮助加快它,但不知道如何去做。任何帮助都会很棒。请注意:我目前无法对数据库或其设计进行任何实际更改。

SELECT top 1 v.[address], v.[address2], v.[city], v.[state_prov_id],v.[postcode],
        v.[first_name], v.[last_name], v.[client_specific_id], v.[name], v.phone,
        v.Location_id, v.Contact_id, v.Event_ID, v.client_id, v.program_id,
        v.source_code_id, v.file_source_code_id, v.URL, v.ScriptFrame,
        v.calldatemark, v.NumCallMark + 1 as Numcallmark,
        v.NumCallMarkDay + 1 as NumCallMarkDay,
        v.NumCallMarkWeek + 1 as NumCallMarkWeek, v.autoaudio,
        v.autohumantext, v.automachinetext 
   FROM vw_locationcontactdialer v 
  WHERE v.program_id = 10001565
    and v.numcallmark < 3 
    and (dateadd(hh,72,v.calldatemark) < getdate()
        or v.calldatemark = '01/01/1900' or v.calldatemark is null) 
    and  source_code_id = 10015311 
    and v.contact_id not in ( select contact_ID from CALL_HISTORY with (NOLOCK) 
                               where program_ID = 10001565 
                                 and result_id not in ('8','U','N')
                               group by contact_ID)
  order by calldatemark

我得到了一个提示,可以尝试进行 JOIN,但我不确定我将如何完成此操作。我不确定如何进行 JOIN 并确保所有不在子查询中的 contact_id 都已完成。

我想出了这个:

SELECT TOP 1 v.[address], v.[address2], v.[city], v.[state_prov_id],v.[postcode],
            v.[first_name], v.[last_name], v.[client_specific_id], v.[name],
            v.phone, v.Location_id, v.Contact_id, v.Event_ID, v.client_id,
            v.program_id, v.source_code_id, v.file_source_code_id, v.URL,
            v.ScriptFrame, v.calldatemark,
            v.NumCallMark + 1 as Numcallmark,
            v.NumCallMarkDay + 1 as NumCallMarkDay,
            v.NumCallMarkWeek + 1 as NumCallMarkWeek, v.autoaudio,
            v.autohumantext, v.automachinetext 
  from vw_locationcontactdialer v 
  JOIN CALL_HISTORY ch on v.Contact_ID = ch.contact_ID
 where v.Program_ID = 10001565
   and v.NumCallMark < 3
   and (DATEADD(hh,72,v.calldatemark) < GETDATE() 
        or v.CallDateMark = '01/01/1900' or v.CallDateMark is null)
   and v.Source_Code_ID = 10015311
   and ch.result_ID not in ('8','U','N')
 group by ch.contact_id
 order by v.CallDateMark

但由于此错误而无法正常工作,我不完全理解:Msg 8120, Level 16, State 1, Line 1 Column 'vw_locationcontactdialer.address' 在选择列表中无效,因为它不包含在任一聚合函数中或 GROUP BY 子句。

任何帮助都是极好的。

4

3 回答 3

1

我可以给你一个小提示:子句 "(DATEADD(hh,72,v.calldatemark) < GETDATE()" 可以防止调用 calldatemark 字段上的索引被使用。它可能有助于将其替换为 "v.calldatemark <日期添加(hh,-72,GETDATE())“。

另外,忽略“分组依据”。您只对某些contact_Id 感兴趣。如果子选择的结果仅包含几个条目,则将其转换为 JOIN 时不会获得任何性能。

正如评论已经说过的那样,您应该检查并最终发布底层视图的架构,因为它可能是性能不佳的根本原因。

于 2012-11-12T21:53:09.190 回答
1

您可以将其转换为连接,如下所示:

from vw_locationcontactdialer v left outer join
     (select contact_ID
      from CALL_HISTORY with (NOLOCK) 
      where program_ID = 10001565 and 
            result_id not in ('8','U','N')
      group by contact_ID
     ) ch
     on v.contact_id = vh.contact_id
where . . .
      ch.contact_id is null
order by calldatemark

(抱歉,如果我尝试发布整个查询,我会收到错误消息。)

但是,为了使这项工作更好,您可能希望返回原始查询并使用相关子查询:

where
      not exists (select 1
                  from CALL_HISTORY ch with (NOLOCK) 
                  where program_ID = 10001565 and 
                        result_id not in ('8','U','N') and
                        ch.contact_id = v.contact_id
                 )

如果您在 CALL_HISTORY.Contact_Id 上有一个索引,它会有所帮助。更好的是,Call_History 上的多部分索引包含contact_id、result_id 和program_id。

于 2012-11-12T21:54:48.573 回答
0

我确实想出了一种方法来让它正常工作,这是我的代码:

Select top 1 v.[address], v.[address2], v.[city], v.[state_prov_id], v.[postcode], 
v.[first_name], v.[last_name], v.[client_specific_id], v.[name], 
v.phone, v.Location_id, v.Contact_id, v.Event_ID, v.client_id, v.program_id,
v.source_code_id, v.file_source_code_id, v.URL, v.ScriptFrame, v.calldatemark,
v.NumCallMark + 1 as Numcallmark, 
v.NumCallMarkDay + 1 as NumCallMarkDay, 
v.NumCallMarkWeek + 1 as NumCallMarkWeek, v.autoaudio, 
v.autohumantext, v.automachinetext 
from vw_locationcontactdialer v 
left join (select contact_id from CALL_HISTORY 
where program_ID = 10001565 and result_ID not in ('8','U','N') 
group by contact_ID) h on v.Contact_ID = h.contact_ID 
where v.program_id = 10001565 
and v.numcallmark < 3 and (dateadd(hh,72,v.calldatemark) < getdate() 
or v.calldatemark = '01/01/1900' or v.calldatemark is null) 
and  source_code_id = 10015311  
and h.contact_ID is null order by calldatemark
于 2012-11-19T15:37:57.293 回答