I am trying to select all submissions which don't exist in Quote or Policy table.
select
COUNT (*)
from Submission S
where
(S.Submission_Status='New' OR S.Submission_Status='Quoted to Agent' OR S.Submission_Status='Cleared')
and YEAR(S.Submission_Date) >= YEAR( GETDATE())
and S.Submission not in (select distinct Submission from Policy where Submission is not null)
and S.Submission not in (select distinct Submission from Quote where Submission is not null)
This query returns good results but query is extremely slow (since there are lot of records in Policy and Quote tables). Is it possible to make faster version of this query (I guess with JOIN)?
Thanks,
Ilija
EDIT:
I have tried this but results don't match first query:
I'd try replacing
not in
withnot exist
, this way you won'tdistinct
. Ultimate solution - if you are sure that performance degrades because of large number of records inpolicy
andquote
, create indexed view on each of the table (select distinct …
). In some sqlserver editions you even don't have to change the query, it will use view automatically