我有一张表(汽车),它保存了一些特征汽车,如 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 万),所以查询很慢。你有什么意见?有没有更好的方法来获得这些车?