문제

i have been reading some articles about "in statement" for sql. i learned "IN" creates some bad performance. i would like to make better performance above my codes. Can you give me some advises to achieve this, please?

select 
    v.Id,  
    v.MissingVehicleFlag,  
    v.Make,  
    v.OatsModelId,  
    v.Model,  
    v.PersonalisedName,  
    v.RegistrationPlate, 
    v.ImageUrl,  
    v.ImageCropStartingX,  
    v.ImageCropStartingY, 
    v.LastModified, 
    v.Account_Id, 
    v.Deleted
from RobbinsuatLatest.dbo.Vehicles v  
    where v.Id in(
        select v1.Id
        from RobbinsuatLatest.dbo.Vehicles v1 
        except
        select v2.Id from RobbinsuatLatest.dbo.VehiclesV2 v2
        )
도움이 되었습니까?

해결책 2

Use NOT EXISTS

select 
    v.Id,  
    v.MissingVehicleFlag,  
    v.Make,  
    v.OatsModelId,  
    v.Model,  
    v.PersonalisedName,  
    v.RegistrationPlate, 
    v.ImageUrl,  
    v.ImageCropStartingX,  
    v.ImageCropStartingY, 
    v.LastModified, 
    v.Account_Id, 
    v.Deleted
from RobbinsuatLatest.dbo.Vehicles v  
    where NOT EXISTS (
        select v2.Id 
        from RobbinsuatLatest.dbo.VehiclesV2 v2
        WHERE v.Id = v2.Id
        )

And make sure there is an index on Id in both RobbinsuatLatest.dbo.Vehicles and RobbinsuatLatest.dbo.VehiclesV2

다른 팁

SELECT 
    v.Id,  
    v.MissingVehicleFlag,  
    v.Make,  
    v.OatsModelId,  
    v.Model,  
    v.PersonalisedName,  
    v.RegistrationPlate, 
    v.ImageUrl,  
    v.ImageCropStartingX,  
    v.ImageCropStartingY, 
    v.LastModified, 
    v.Account_Id, 
    v.Deleted
FROM RobbinsuatLatest.dbo.Vehicles v  
LEFT JOIN RobbinsuatLatest.dbo.VehiclesV2 v2 on v.Id = v2.Id
WHERE v2.Id IS NULL
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top