سؤال

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