This query currently takes about 30 seconds to run.
I’d like some help speeding it up, but don’t know how to go about doing it. Any help would be fantastic. Please Note: I’m unable to make any actual changes to the database or it’s design at this time.
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
I was given a tip to try and do a JOIN, but I’m unsure how I would accomplish this. I’m not sure how to do a JOIN and make sure that all contact_id’s not in the subquery is accomplished.
I came up with this:
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
but it’s not working because of this error, which I don’t fully understand: Msg 8120, Level 16, State 1, Line 1
Column ‘vw_locationcontactdialer.address’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Any help would be awesome.
You can turn this into a join as follows:
(Apologies, I get an error if I try to post the entire query.)
To make this work better, though, you probably want to go back to your original query and use a correlated subquery:
It woudl then help if you had an index on CALL_HISTORY.Contact_Id. Even better, a multipart index on Call_History encompassing contact_id, result_id, and program_id.