I'm trying to rewrite the following query to make it more efficient:
update c set c.cartype = (select Id from carTypes where carType = 'Coupe')
from cars c
inner join doorcount dc
on c.Id = dc.carId and dc.Value=2
and c.carType= (select Id from carTypes where carType = 'Sedan')
What I've come up with so far is:
update c set c.cartype = ct.id
from cars c
inner join cartypes ct on ct.carType='Coupe'
inner join doorcount dc on c.Id = dc.carId and dc.Value=2
inner join carTypes ct2 on c.carType = ct2.Id and ct2.carType = 'Sedan'
This looks better to me, but is performing almost exactly the same.
Is there a more efficient way to perform this update?
EXISTS
instead of direct joins (the only tables where you really need materialized values arecars
andcartypes
). You need to make sure that the indexes you're using efficiently support these operations. Could you share an actual, post-execution query plan somewhere?