![]() ![]() Yes, it is true that inserting a row into a table will normally get an exclusive lock on just that row. Make sure you know what you are doing if using If I am not mistaken, nolock is also deprecated and should be replaced by the read uncommited or read past hint depending on wether you want to skip or read uncommited transactions. I advise reading the documentation before simply adding nolock reads uncommited transactions which means that you can get corrupt results when using it. I personally do not use nolock, It implies more than the name implies. If this is the case, maybe you willįind out your deadlock can be rid of by adding an index, a rewriting the query something or else which would improve the performance of your query. You might be scanning another table on insertion due to a constraint, a trigger or some other thing you do not think of. You isolate your statements, look at the execution plan and the profiler as well. the deadlock graph is your friend to figure what are the statements which cause issue. it is possible your query does not aquire row locks but rather page or table locks. You say it happens when you run your statements concurently. The solution is usually to create a covering index and the blocking should go away. This means the optimizer has to perform index maintainence and needs exclusive access, but cant because our session1 has a shared lock on the nonclustered index key. Perform an insert into the table, which also has the columns in a nonclustered index. This means you have shared locks on the nonclustered index and the clustered index. Index and not all the select list columns exist in the index, the optimizer has to go to the clustered index/heap to get the remaining columns. You have to remember that if you select query uses a nonclustered These types of deadlocks are usually caused not by the table (clustered index) itself, but are usually brought about by the use of nonclustered indexes being used with lookups. On the other hand, the second session's insert is waiting for some exclusive lock for the insert, but session1 has a shared lock preventing Basically, you have a select waiting to get its hands onĪ resource with a shared lock in one session but is blocked by the X lock of the insert in another session. ![]() This type of deadlock exists becuase a select statement is trying to take a shared lock on the table in question but an exclusive lock has already been taken on the table by another session. <<as far as I understand, when you do an insertion, only that specific row is locked, and a select just does a shared lock, so where is the wait condition? Transaction (Process ID 56) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. One of the connections should get a message like this: Msg 1205, Level 13, State 51, Line 1 Now go back to the first connection and run this select statement: Insert into dbo.test_Stuff values(newId(),'Second') Next, establish a second connection and run this: - Do this with a different connection second. Insert into dbo.test_Stuff values(newId(),'First C') Insert into dbo.test_Stuff values(newId(),'First B') Insert into dbo.test_Stuff values(newId(),'First A ') ( some_Guid uniqueidentifier not null primary key, Why do you have the SELECT statement that trails the update as part of the transaction?Ĭonnect to a database and run this query (as connection #1): -drop table dbo.test_Stuff Arbi might be correct about the nolock optimizer hint. I suspect that if you print out the deadlock graph for the deadlocks you will see the ring of the deadlock.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |