SQL查询优化不存在

Lou*_*ise 1 sql optimization sql-server-2005

我需要优化以下查询,有人可以帮忙吗?我知道这是导致问题的不存在部分,因为它正在进行大规模的表扫描,但我是新手,有人可以给出任何建议吗?

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')
Run Code Online (Sandbox Code Playgroud)

Rem*_*anu 7

create index [<indexname>] on [Job] (
    [company], [branch], [engineerNumber], [ID]) include ([JobNumber]);
create index [<indexname>] on [OutboundEvents] (
    [company], [branch], [JobNumber], [Formtype]);
Run Code Online (Sandbox Code Playgroud)

您优化的查询不是您优化的数据模型.首先阅读设计索引.

  • 同意:工作量中的其他查询,提到的列的选择性,读/写比率都发挥作用.它的要点是解决方案不在查询的*text*中(很少是),而是在设计合适的数据模型.我只是不喜欢给无所不在的'它取决于'答案. (4认同)
  • 请注意,添加这些索引以优化此特定查询不会考虑其余的工作负载.如果您的写入:读取比率很高,您可能需要为维护这些索引付出高昂的代价.我不是说这使得这个答案错了​​,只是你需要考虑整个系统,而不仅仅是一个查询.这就是为什么我们不盲目遵循数据库引擎优化顾问的建议(或执行计划或缺失的索引DMV),告诉我们根据有限和隔离的信息创建索引. (3认同)