all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

Preventing Deadlock



Preventing Deadlock morphius
9/5/2007 9:28:01 PM
sql server programming: Trace 1204 has narrowed the cause of deadlock to these delete statements.
There are a total of 10 delete statements in this SP.

create procedure del_emp @empid int

Delete from tbl_employees
where empid in (Select empid from tbl_location where empid = @empid)

Delete from tbl_management
where mge_ID in (Select mge_ID from tbl_sunmanagement where empid = @empid)

.........
.........

Questions:
1. should i add (nolock) within the select statement? ex: Select empid from
tbl_location WITH (NOLOCK)
2. should i add (updlock) within the delete statement? ex: Delete from
tbl_employees WITH (UPDLOCK)

Any suggestions highly appreciated.


Re: Preventing Deadlock Uri Dimant
9/6/2007 12:00:00 AM
Hi
To prevent deadlocks first of all you have to access table in the same
order. That means if Connection1 runs
DELETE tbl_employees and Delete from tbl_management so the Connection2 has
to touch tbl_employees and tbl_management in the same order. I don't know
your business requirements so design your application properly

Regarding NOLOCK hint I'd say you can get dirty data and again it may or
may not accepted by your requirements



[quoted text, click to view]

Re: Preventing Deadlock Dan Guzman
9/6/2007 12:00:00 AM
UPDLOCK can reduce the chance of deadlocks but at the expense of concurrent
operations. Although you might view fast response time as performance, I
think it's better to distinguish between performance (query/index tuning)
and concurrency (blocking, deadlocking) so you can address the underlying
causes. Performance and concurrency are closely related, though; the faster
queries run, the less likely you will run into concurrency problems . Of
course, you'll ultimately need to address both for the best response time.

[quoted text, click to view]

Did you examine the execution plans for scans? Are the delete statements
run as part of a transaction? UPDLOCK is ok but I would be wary of using
READPAST except in special situations.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Preventing Deadlock morphius
9/6/2007 5:50:02 AM
Hi Dan,
I've used the UPDLOCK and it prevented the deadlocks, however it slowed down
the SP it takes a while to finish but it works. Do you recommend UPDLOCK,
READPAST to improve performance?
Thank you!

[quoted text, click to view]
Re: Preventing Deadlock morphius
9/6/2007 6:02:03 AM
Dan,
A transaction is explicitly started outside this procedure and between
100-300 users could be using the transaction simultaneously.


[quoted text, click to view]
Re: Preventing Deadlock Dan Guzman
9/6/2007 6:42:51 AM
[quoted text, click to view]

Only if it's acceptable to accidentally delete the wrong data or skip data
that should be deleted.

[quoted text, click to view]

Since you have no explicit transaction (at least in your post), each delete
statement is in an independent atomic transaction. The PADLOCK hint has
little value in that case. A UPDLOCK, HOLDLOCK hint might be appropriate in
your SELECT subqueries if you have an explicit transaction and the row is
subsequently deleted.

In addition to Uri's suggestion about the delete order, I suggest you review
the execution plans to avoid scans and tune indexes/queries accordingly.
Scans greatly increase deadlock likelihood because more rows are accessed
than needed and the performance hit results in more concurrent queries.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Preventing Deadlock Dan Guzman
9/6/2007 6:47:46 AM
[quoted text, click to view]

Sorry, should be UPDLOCK. Damn spell checker :-)

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
AddThis Social Bookmark Button