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