Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Question about Microsoft SQL Server 2000, deletion, and lo..

 
   Database Help (Home) -> Programming RSS
Next:  Opinion on query approach  
Author Message
theRat

External


Since: Jan 11, 2008
Posts: 3



(Msg. 1) Posted: Fri Jan 11, 2008 2:21 pm
Post subject: Question about Microsoft SQL Server 2000, deletion, and locks
Archived from groups: microsoft>public>sqlserver>programming (more info?)

All,

I've been racking my brains trying to solve a deadlocking issue I've
encountered and trying to find the simplest way of stating my problem
so that others can help - and I think I've figured out the best way to
ask. I have it all narrowed down to 2 questions:

1. If I have a database with only ONE table (no foreign keys) and NON-
CLUSTERED indexes on that table, is it possible for me to get in a
deadlock situation when 2 (or more) threads are deleting multiple rows
within their transactions (no other selects or updates)?

2. If I have a database with only ONE table (no foreign keys) and
CLUSTERED indexes on that table, is it possible for me to get in a
deadlock situation where 2 (or more) threads are deleting multiple
rows within their transactions (no other selects or updates)?

I'm all a-tingle awaiting your answer. Many thanks!

-john

 >> Stay informed about: Question about Microsoft SQL Server 2000, deletion, and lo.. 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 2) Posted: Fri Jan 11, 2008 2:44 pm
Post subject: Re: Question about Microsoft SQL Server 2000, deletion, and locks [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 11, 4:21 pm, theRat wrote:
> All,
>
> I've been racking my brains trying to solve a deadlocking issue I've
> encountered and trying to find the simplest way of stating my problem
> so that others can help - and I think I've figured out the best way to
> ask. I have it all narrowed down to 2 questions:
>
> 1. If I have a database with only ONE table (no foreign keys) and NON-
> CLUSTERED indexes on that table, is it possible for me to get in a
> deadlock situation when 2 (or more) threads are deleting multiple rows
> within their transactions (no other selects or updates)?
>
> 2. If I have a database with only ONE table (no foreign keys) and
> CLUSTERED indexes on that table, is it possible for me to get in a
> deadlock situation where 2 (or more) threads are deleting multiple
> rows within their transactions (no other selects or updates)?
>
> I'm all a-tingle awaiting your answer. Many thanks!
>
> -john

Yes, it is possible, and it is very easy to reproduce. See for
yourself:
-- set up data
CREATE TABLE a(i INT, j INT)
GO
SET NOCOUNT ON
GO
DECLARE @i INT, @j INT
SET @i = 0
WHILE(@i < 100000) BEGIN
SELECT @i = @i + 1, @j = rand()*100000
INSERT a(i,j) VALUES(@i, @j)
END
GO
CREATE NONCLUSTERED INDEX a_i ON a(i)
GO
CREATE NONCLUSTERED INDEX a_j ON a(j)
GO

--from one connection, run this:

DECLARE @i INT
SET NOCOUNT ON
SET @i = 0
WHILE(@i < 100000) BEGIN
SELECT @i = @i + 1
DELETE FROM a WHERE i = @i
END
GO

-- from another connection, run this:
DECLARE @j INT
SET NOCOUNT ON
SET @j = 0
WHILE(@j < 100000) BEGIN
SELECT @j = @j + 1
DELETE FROM a WHERE j = @j
END
GO

Msg 1205, Level 13, State 45, Line 6
Transaction (Process ID 52) was deadlocked on lock resources with
another process and has been chosen as the deadlock victim. Rerun the
transaction.


in my experience 100K is enough to reproduce a deadlock on my server.
You might want to play with that value in your enviropnment.

 >> Stay informed about: Question about Microsoft SQL Server 2000, deletion, and lo.. 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 3) Posted: Fri Jan 11, 2008 3:25 pm
Post subject: Re: Question about Microsoft SQL Server 2000, deletion, and locks [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

theRat ( ) writes:
> 1. If I have a database with only ONE table (no foreign keys) and NON-
> CLUSTERED indexes on that table, is it possible for me to get in a
> deadlock situation when 2 (or more) threads are deleting multiple rows
> within their transactions (no other selects or updates)?

If the DELETE statements works with different conditions, for instance

DELETE tbl WHERE indexcol1 = value
DELETE tbl WHERE indexcol2 = value

it could certainly happen. If all deletes are on the same column, it
is maybe less likely. Then again, if you have partially overlapping
ranges, who knows.

> 2. If I have a database with only ONE table (no foreign keys) and
> CLUSTERED indexes on that table, is it possible for me to get in a
> deadlock situation where 2 (or more) threads are deleting multiple
> rows within their transactions (no other selects or updates)?

You cannot have more than one clustered index on a table. Assuming
that that is all you have, maybe partially overlapping ranges could
give you a deadlock.

The key on both cases is a DELETE first locates all rows to delete
with an UPDATE lock and then converts to an exclusive to delete.
(At least I think it does.) If access is in different order, you can
get a deadlock. In some cases, when all there is is a single
clustered index, you will need quite funny queries to get a deadlock,
but it could still happen.


--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Question about Microsoft SQL Server 2000, deletion, and lo.. 
Back to top
Login to vote
Tom Moreau

External


Since: Apr 21, 2004
Posts: 502



(Msg. 4) Posted: Fri Jan 11, 2008 5:25 pm
Post subject: Re: Question about Microsoft SQL Server 2000, deletion, and locks [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yes to both.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"theRat" wrote in message


All,

I've been racking my brains trying to solve a deadlocking issue I've
encountered and trying to find the simplest way of stating my problem
so that others can help - and I think I've figured out the best way to
ask. I have it all narrowed down to 2 questions:

1. If I have a database with only ONE table (no foreign keys) and NON-
CLUSTERED indexes on that table, is it possible for me to get in a
deadlock situation when 2 (or more) threads are deleting multiple rows
within their transactions (no other selects or updates)?

2. If I have a database with only ONE table (no foreign keys) and
CLUSTERED indexes on that table, is it possible for me to get in a
deadlock situation where 2 (or more) threads are deleting multiple
rows within their transactions (no other selects or updates)?

I'm all a-tingle awaiting your answer. Many thanks!

-john
 >> Stay informed about: Question about Microsoft SQL Server 2000, deletion, and lo.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
SQL Server 2000 master6.ldf question - Hi, In my SQL Server 2000, I have a master6.ldf. It's size is huge, and it's also growing. I am wondering why it is master6.ldf and not master.ldf. Since it's growing, I am wondering how to make it smaller. Thanks for help. Jason

A question about keyword search strategy against SQL Serve.. - We have a small web application using Sql Server 2000. We would like to provide a keyword search functionality. The key word can be a location, a string in the description of certain topic or a combination of both. What I have currently is to take the...

Microsoft Reporting Services deisgn question - I thought I'd ask here in case there is a sql alternative to this problem, or should should somebody happen to know. I've got these two tables FileLog: ProcessKey Filename EventLog: ProcessKey Eventtype Date Both tables can be..

deletion cause transacation log to grow - SQL Express 2005 I delete the 31st day of data from my customers Production data to keep the database at a manageble size. I run the query below. Delete From ProductionData Where ProdWorkDate < '6/19/2008' I noticed that he transactioni log wa...

Monitoring Creation/Deletion of Database - Hi, I have the need to be notified when databases are created/dropped on a particular SQL Server instance. I have created a trigger as follows: --Create the trigger to send the mail each time a DB is created or dropped CREATE TRIGGER DDLTRIGGER ...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]