

Now I decided to simplify things and just changed my deadlock priority then went straight to MULTI_USER.
Deadlock database series#
One of the answers suggested changing the deadlock priority before running a series of ALTERs to set the database OFFLINE, back ONLINE, and then finally back to MULTI_USER mode. From what they said, their problem, and quite possibly mine, was caused by the system trying to do an auto statistics update. Finally, though, Robert Davis ( b/ t) sent me to this link on dba.stackexchange. Now I don’t know about you but I hate rebooting a server because of a problem like this. After some discussion, it was starting to feel like we were going to have to reboot. I spent a little while trying various things and searching through forums before I went for help on twitter using the #SQLHELP hashtag. Interestingly when I tried to do the ALTER instead of just hanging I immediately got a deadlock error. Don’t ask me how.) I wasn’t able to get that exclusive access I needed. So because they were holding locks on the database (And somehow even though it was in single user there were multiple sessions with locks in the database. Why does it matter that they were system sessions? The important thing to remember here is that these sessions can not be KILLed. In my case the problem sessions were all TASK MANAGER sessions. These sessions include the LOG WRITER, RECOVERY WRITER, TASK MANAGER etc. The best way to tell is that the is_user_process flag in the sys.dm_exec_sessions DMO will be a 0. They typically (but not always) have session IDs under 50. System sessions are those created by SQL itself. Which meant I wasn’t able to get exclusive use of the database which is required to do an ALTER DATABASE to set it back into MULTI_USER.Īt this point you may have a couple of questions so let me try to answer (some of) them: How exactly was it stuck you ask? Well, 4-5 system sessions were holding locks on the database (and blocking each other). A database was stuck in single user mode. Thanks to all for the attention.I had an interesting problem recently.

Monday, Novem12:11:02 PM - Viacheslav Maliutin Plerase share If a Table Has More Than One Foreign Key? how did you fix all these. It is going to be published soon.įriday, Ma1:40:01 PM - Pradeep Kumar Giri Here is the second one explaining the issue with multiple foreing keys. Pradeem Kumar Giri, thank you for the reading my article. Saturday, Ma8:25:51 AM - Viacheslav Maliutin i.e reteriving data from more tables concurrently please go for a little bit more complex case. I would be interested how you resolved a table with multiple foreign keys.

The execution plan for both steps 7 and 8 will now look like this. To get only one city it is better to use a seek in the index for the row id and These dummy cities create statistics that tells SQL Server that if a client wants Script 10 below will show us all the locks have been acquired in the database. In order to understand the reason of the deadlock we need to take a look at what I will explain this in detail and how this solved this problem. To solve the deadlock you just need to cluster the foreign key “FK_Country”. Understanding and Solving the SQL Server Deadlock Issue Now let me solve the problem with a good problem explanation and a good solution. To tell our developers, but unfortunately they can’t change the way the SQL Why we have the problem I am talking about. Actually, it never writes the “*”, but it picks up everyĬolumn and that’s why my scripts 8 and 9 are written that way and that’s Yes, this will work inĪ perfect world, but when using an ORM it writes the SQL like “SELECT * FROM With the “CityName” column in scripts 8 and 9. So, to not have a deadlock just replace the “*” symbol in the SELECT queries On the “CountryID” column and included the “CityName” column.

You might say: “Hey, you created an index “ IX_City_Not_Covering_Index” In query window #1, select data from the City table for the USA records. This willġ9980000, -As Google just said and that's cool! Transaction, but not committing or rolling back the transaction. In query window #1 run the following code. The same T-SQL command with different data to show how the deadlock will be reproduced. In the scope of each transaction I will run Wanted to show that our system had many useful indexes and even with useful indexesĪs usual, to demonstrate a deadlock we need two connections to be opened. You might have noticed that I created an index on the “CountryID”Ĭolumn and included the “CityName” column. script 1 - Creation of County and City tables
