Hi All, I have a main table called Stock for example. One process ( a service ) inserts data into this table , as well as updates certain fields of the table periodically. Another process ( SQL Job ) updates the table with certain defaults and rules that are unknown to the service - to deal with some calculations and removal of null values where we can estimate the values etc. These 2 processes have started to deadlock each other horribly. The SQL Job calls one stored procedure that has around 10 statements in it. This stored proc runs every minute. Most of them are of the form below - the idea being that once this has corrected the data - the update will not affect these rows again. I guess there are read locks on the selecting part of this query - but usually it updates 0 rows - so I am wondering if there are still locks taken ? UPDATE s SET equivQty = Qty * ISNULL(p.Factor,4.5) / 4.5FROM Stock s LEFT OUTER JOIN Pack p on s.Product = p.ProductId AND s.Pack = p.PackIdWHERE ISNULL(equivQty,0) <> Qty * ISNULL(p.Factor,4.5) / 4.5 The deadlocks are always between these statements from the stored procedure - and the service updating rows. I can't really see how the deadlocks occur but they do. Some suggestions from developers have been to put use nolock or to use snapshot isolation etc. My gut is that these are the wrong options and we need to find the real cause. Another suggestion has been to try and use an exists before the update as below IF EXISTS( SELECT based on above criteria ) ( UPDATE as before ) Does this reduce the locking at all ? I don't know how to test the theory - i added this code to some of the statements, and it didn't seem to make much difference ? Is there a way to make a process ( in my case the stored procedure ) - give up if it can't aquire the locks rather than being deadlocked - which leads to job failures and emails etc ? We are currently trying to filter down the data that is updated to be only the last few months - to reduce the amount of rows even analyzed - as the deadlocking does seem to be impacted by the number of rows in the tables. Any more advise etc would be great. I would love to post some code snippets, but its not something that I can easilly mock up really..Thanks all, Steve
↧