I am attempting to find persons in a data set with multiple rows containing mismatched birth dates.
Even limiting the select to 40 rows, this code will run for hours.
If I take out the check for month, the select will complete in a few minutes.
Of course, there are additional statement not shown that check for matching names and other limiting criteria, but I think the problem is shown in this fragment.
Is there any way I can improve the performance of this query?
SELECT distinct top 40
a.state_id, a.name_first + ' ' +a.name_middle + ' ' + a.name_last + ' ' + a.name_suffix as [a_name], a.birth_date, 'KI'as [a.cnty],
a.voter_id, a.status, a.comment_id, a.timestamp, a.drivers_license, a.ssn, ltrim (rtrim (rtrim
(rtrim (rtrim (rtrim (rtrim (isnull( rtrim(ltrim
(a.house_number)),'') ) + ' ' + isnull (a.house_fraction, '') ) + ' ' + isnull(a.pre_dir,'') ) + ' '
+ isnull(a.street,'') ) + ' ' + isnull(a.type, '') ) +' ' + isnull(a.post_dir, '') )
) as [residence], a.apartment_number, a.city, a.zip, a.phone_1, a.birth_date as [sortDOB], a.reg_date , a.name_first
FROM voter
AS a
INNER JOIN voter
AS b
ON a.voter_id <> b.voter_id and a.birth_date > '1/1/1900' and b.birth_date > '1/1/1900'
and
(
(a.status in ('A', 'P') and b.status in ('A','I','P'))
or
(a.status = 'I' and b.status in ('A', 'P'))
)
and dateadd (mm,0,a.birth_date) >= dateadd (MM, -2, b.birth_date) and dateadd (MM,0,a.birth_date) <= dateadd(MM, 2, b.birth_date)
and dateadd (yy,0,a.birth_date) >= dateadd (yy, -2, b.birth_date) and dateadd (yy,0,a.birth_date) <= dateadd(yy, 2, b.birth_date)