0

我有一张表(汽车),它保存了一些特征汽车,如 EngineNo、LastProductionStepId、NodyNo、...

此外,我还有另一个表 (CarSteps),它保存了特定汽车在其制造过程中应通过的所有步骤,例如 Engine Assigning(Id = 2)、Engraving(3)、PrePaint(4)、Paint(5)、AfterPaint(6 )、确认(7)、交付(8)

我现在想获得所有介于 PrePaint 和 Confirmation 之间汽车:

select    cr.Id, cr.BodyNo, cr.LastStepId 
from      Cars cr WITH (NOLOCK)
inner join CarSteps steps WITH (NOLOCK) on cr.Id = trace.CarId and    cr.LastStepId=trace.StepId 
where       
     cr.LastStepId >= 4
    AND cr.[Status] = 1
    AND steps.[Status] = 1
    AND not exists (    select  * 
            from    CarSteps steps1 WITH (NOLOCK) 
            where   steps1.CarId = cr.Id 
                AND steps1.StepId >= 7                                  AND steps1.Status = 1
             )

因为 CarSteps 有很多记录(4400 万),所以查询很慢。你有什么意见?有没有更好的方法来获得这些车?

4

1 回答 1

0

查看您的查询,我看到从 Cars 到 CarSteps 的连接,我看到您加入到 trace.CarId 和 trace.StepId 是。您的查询中未定义跟踪。

from      
 Cars cr WITH (NOLOCK) inner join 
 CarSteps steps WITH (NOLOCK) on 
  cr.Id = trace.CarId and    
  cr.LastStepId=trace.StepId

如果我能更好地理解完整的查询,我可能会提供帮助。执行计划揭示了什么?

于 2011-12-13T00:19:35.000 回答