The following insert query is taking around 7 minutes to handle 4000 rows. The select runs in less than a second. At the end of the run it gets a foreign key constraint error.
I am assuming that the constraint error is what is taking so long. It created thousands of locks on the table. We will fix the constraint error, but is there anything we can do that would improve the time this takes? or a best practices to help with the locks?
Thanks.
INSERT INTO ServiceHistory(
ROID,
VehicleID,
ServiceAdvisorID,
CustomerID,
BatchHistoryID,
ServiceDate,
Mileage,
RONumber)
SELECT DISTINCT RO.ROID,RO.VehicleID,SA.ServiceAdvisorID,
RO.CustomerID,RO.BatchHistoryID,RO.CloseDate,RO.Vehicle_Mileage,
RO.RONumber
FROM ROs RO
JOIN ROLines RL ON RO.ROID = RL.ROID
JOIN ServiceAdvisors SA ON RO.DealerID = SA.DealerID
AND RL.AdvisorNbr = SA.ServiceAdvisorNumber
WHERE RO.Processed = 0