Solved

DeadLocks!

Posted on 2015-01-13
9
118 Views
Last Modified: 2015-02-05
I'm having problems with deadlocks and I don't think I should.

I fully understand how a deadlock is caused having a circle of resource requests, so no need to explain that.   Here is my situation..

I have a few parent-child tables that are used for reporting.   A user enters some criteria and a few hundred records are populated into the report tables based on the information entered, the user can then browse the report records created.   All reporting records have a unique report ID for a reporting session.  So each user is only touching those records for that session of reporting

Since every user is only touching records with his own unique report ID, there is no way that someone else's reporting form will access those same records.  They will insert/update records in the same table, but not those records.   So there should never be a dead lock.

So, why am I getting deadlocks and how do I prevent them?

The basic action of the reporting form is to (1) insert records using an insert-select statement (2) update those entered records and (3) delete those records if the user chooses to re-run the report

I was using SQL Server 2005 and now I have gone to 2012 and the problem is worse!
0
Comment
Question by:gdemaria
9 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 250 total points
ID: 40547689
Deadlocks are not just on a record. They could be on a page or the entire table.

Maybe you should use dirty reads perhaps. I note that in your 1-2-3 you don't have any actual SELECT statements, so your summary may not be complete.

Try using Extended Events to see if you can track this down - in 2012 EXtended Events is much better than in previous versions,
0
 
LVL 39

Author Comment

by:gdemaria
ID: 40547716
Thanks Phillip - I will Google, Extended Events, I am not familiar with that.

Number (1) indicates an insert based on an insert-select statement.   The updates are also using select statements to complete the updates.

Under what circumstance would SQL Server lock the entire table?  And wouldn't that only for for a short time and once unlocked, the other records and proceed?   Why would that cause a deadlock?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40547722
What are the clustering keys on all involved tables?

What are the join and WHERE conditions on those tables?

In the meantime, turn on trace flag 1222 so you can get more detailed on the deadlocking:

DBCC TRACEON ( 1222, -1 )
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 40548431
They will insert/update records in the same table, but not those records.   So there should never be a dead lock.
Inserts will cause a table lock and updates can cause at least a page lock so this may justify the dead locks.
As told above you can use dirty reads if that's not a problem. This can be accomplished by using locking hints. Ex.:
SELECT * FROM TableName WITH (NO_LOCK)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40548453
For Extended Events, have a look at:

http://msdn.microsoft.com/en-GB/library/bb630282.aspx
https://www.youtube.com/watch?v=2cbNXFUdAUg
(This is a video)

If you do Google Extended Events, make sure you are reading about the 2012 (or 2014) version - the 2008R2 version is quite different.
0
 
LVL 39

Author Comment

by:gdemaria
ID: 40548785
I tried to do extended events, but got an error saying I didn't have permission.  I am using Amazon RDS SQL Server Web Edition, it may not be part of that, I have to check with support.

Thanks for the tip on dirty reads,  I don't think that would be a problem because no other process could update the same records.   If I do an insert followed by an update of those just inserted records, the inserted records would be there even though it was a dirty read, right?   Same thing with multiple updates in a row by the same process?

I'm surprised dirty reads is my only route.   Is this a huge flaw of SQL Server?  I used Oracle for years and dead locks were very, very rare.    As I said, the different users/processes never touch the same records so there logically should not be any deadlocks, it seems to be a flaw to lock the entire table - can I tell SQL Server not to do that?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40548789
> I'm surprised dirty reads is my only route.

It's probably not the only route, but we are trying to diagnose from a distance. That's why I'm suggesting Extended Events or its equivalents to help you diagnose it.
0
 
LVL 39

Author Comment

by:gdemaria
ID: 40548822
Got it Phillip, I will try to get more information if Extended Events are allowed.
Thanks!
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40548828
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now