Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12554

Can reading an FK cause a deadlock?

$
0
0

I'm a newbie on deadlocks, but I have one now!

Say you have two tables, myDimension and myFact.  There is an N:1 FK from myFact to myDimension.

SPID#1 begins a transaction, updates myDimension, and then tries to update myFact.

begin trans
update myDimension
set aField=1 where aDKey='abc';
update myFact
set aFlag=1 where aFKey=123;
commit trans

meanwhile, SPID#2 begins a transaction, updates myFact, and then tries to update (actually insert) myFact again.

begin trans
update myFact
set aFlag=2 where aFKey=@somevalue;
insert myFact
select a,b,c from #mytemp;
commit trans

I get a deadlock because (if I read this report correctly) SPID#1 wants a key lock on myFact and has to wait, while SPID#2 wants a shared key lock on myDimension.

But my question is, why does SPID#2 want a shared keylock on myDimension, since it is a simple insert into myFact from a temp table - myDimension is not even mentioned?  Can it be because the FK makes it want to read myDimension?

Thanks.

Josh


Viewing all articles
Browse latest Browse all 12554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>