我有一个日志表,其中包含作业状态。其中一列是作业名称。有时作业根本不会运行,因此没有记录。我的SQL确定哪些作业在日志中“丢失”。
这个查询大约需要17分钟,非常漫长。我有很多其他更复杂的查询(至少看起来更复杂),但没有这么慢。
如何进行优化?
-- Display missing jobs. Thats jobs that are not in job log but should be
declare @startDate datetime, @endDate datetime
declare @rangeInHours int
set @rangeInHours = -24
set @endDate = '2012-01-17 12:00:01'
set @startDate = dateadd(hour, @rangeInHours, @endDate)
declare @myTable table( name nvarchar(50))
insert into @myTable values('Activity work')
-- There are another 100 entries like this one above to add all the expected jobs
-- this is my sql to find missing jobs
select distinct i.name from @myTable i
where not exists
( select 1 from job_log j
where j.name = i.name
and j.start_date > @startDate and j.start_date < @endDate
)
order by i.name asc
我浏览了很多帖子,但没有找到合适的答案,至少是我能理解并在我的有限SQL中实现的答案。
job_log
中有适当的索引吗?特别是(name,start_date)
的覆盖索引。你能发布查询计划吗? - Lieven Keersmaekers