Quantcast
Channel: Long running SELECT query fails as deadlock victim to a DELETE on SQL SERVER - Why, and how to avoid this? - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 2

Long running SELECT query fails as deadlock victim to a DELETE on SQL SERVER - Why, and how to avoid this?

$
0
0

Firstly 1 thread selecting a huge amount of data from a table.

Then, before the SELECT query is not yet finished, another thread deletes data from that table.

It causes SELECT query to failed with error errorCode=1205 sqlState=40001 and message that deadlock happened.

I thought that second transaction must wait until first is finished, or first transaction must return data without the changes that were made by the second transaction. How can I avoid this?

I can't change the order of transactions or control them in some way (there is many users of app, and they can do whatever they want in any order at any time). Why does it happen?

There is how queries looks like (i obfuscated them a little):

SELECT distinct P.pID, (LastNAME+''+FirstName+''+SurName) as NAME, 'person' as Type, CA.OrgName, PC.personemail as EmailFROM Persons PINNER JOIN WORK PW ON (P.mainwork = PW.pwId)INNER JOIN CONTACT PC ON (PC.pID = P.pID)LEFT OUTER JOIN ORG CA ON (CA.orgId = PW.orgId)WHERE (P.Status = ?)

and

DELETE FROM persons WHERE id = 1234

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images