1

我需要优化以下查询,有人可以帮忙吗?我知道是 Not Exists 部分导致了问题,因为它正在进行大规模的表扫描,但我对此并不陌生,任何人都可以提供任何建议吗?

select count(*)
from Job j
where company = 'A'
and branch = 'Branch123'
and engineerNumber = '000123'
and ID > 60473
and not exists(
select JobNumber, Company, Branch
from OutboundEvents o
where o.JobNumber = j.JobNumber
    and o.branch = j.branch
    and o.company = j.company
    and o.Formtype = 'CompleteJob')
4

2 回答 2

7
create index [<indexname>] on [Job] (
    [company], [branch], [engineerNumber], [ID]) include ([JobNumber]);
create index [<indexname>] on [OutboundEvents] (
    [company], [branch], [JobNumber], [Formtype]);

不是您优化的查询,是您优化的数据模型。从阅读设计索引开始。

于 2012-09-26T13:18:34.337 回答
0

感谢大家的有益见解。我有很多东西要学 :) 我设法使用这个查询将执行时间从 1 分 7 秒缩短到不到 1 秒:

select count(*)
from job
where company = 'A'
and branch = 'Branch123'
and EngineerNumber = '000123'
AND id> 60473
AND JobNumber not in(
    select Jobnumber from outboundevents b
    where b.company = 'A'
    AND b.Branch = 'Branch123'  
    and b.Formtype = 'CompleteJob'
    and jobnumber in (
        select jobnumber from Job
        where company = 'A'
        and branch = 'Branch123'
        and engineerNumber = '000123'
        and ID > 60473)
)
于 2012-09-26T15:35:27.947 回答