I am writing a query to find missing numbers. The query works, but is very very slow due to the number of records it is currently pulling.
I want to limit the records to only search where "records.event_year = 2007" but when I try to insert that after the "from dbo.records" it gives me an error.
Where exactly do I need to add this and is there anything else I can do to make the query run faster? The event_year, state_file_number, and isactive are all indexed.
================================================== =======
ALTER procedure [Migrate].[Chk_Missing_SFN]
@beg as int,
@end as int
AS
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
select BeforeSkip+1 as gapStart, NextValue-1 as gapEnd from (
select
a.state_file_number as BeforeSkip,
min(b.state_file_number) as NextValue
from dbo.records A join dbo.records b
on a.state_file_number < b.state_file_number
where a.state_file_number between @beg and @end and a.isactive = 'T' and b.isactive = 'T'
group by a.state_file_number
having min(b.state_file_number) > a.state_file_number + 1
) X