I need help performance tuning the query below. Obviously none of you have my DB but if anyone can look at it real quick and give me some direction that might help. I have to search through my leads by a ZipCode Radius Search. The zipcodes return from the function in less than a second so that isn't the slow down. I've tried both "where in" statements and joining on zipcode. Neither seems much faster. The query is fairly fast, actually until I add the part where I say ZipCode in x and ZipCode not in y. I need both of those though for more robust searching by my clients.
anyway, i'm pasting the code below. Any help would be appreciated.
Select Count(b.IndividualID) as Cnt
from dbo.IndividualOrigins a with(nolock) join
dbo.Individuals b with(nolock) on a.IndividualID = b.IndividualID join
--(Select ZipCode from dbo.ZipCodeRadiusSearch ('23692','180')) zi on b.ZipCode = zi.ZipCode join
dbo.Origins d with(nolock) on a.OriginID = d.OriginID join
dbo.Venues y with(nolock) on d.VenueID = y.VenueID join
(select IndividualID from Individuals with(nolock) where cycle = '1') qq on b.IndividualID = qq.IndividualID
b.DNC = 0 and
b.Dupe = 0 and
b.IndividualID not in
(Select max(IndividualExportsMasterID) as CampaignID,IndividualID
from IndividualExportsDetail with(nolock)
group by IndividualID) a
IndividualExportsDetail b with(nolock) on a.CampaignID = b.IndividualExportsMasterID and a.IndividualID = b.IndividualID
and datediff(day,b.DateExpiration,getdate()) < 0
) and b.IndividualCategoryID = '1'
and b.ZipCode in (Select ZipCode from dbo.ZipCodeRadiusSearch ('23692','180')) and b.ZipCode not in (Select ZipCode from dbo.ZipCodeRadiusSearch ('23692','60'))