开发者

SQL nolock and join

I am using a process that inserts data in 2 tables with rowlock, continuously. In the same time I want to use some queries on these tables. As I said the inserts are done with (rowlock) and I use for the queries the isolation level read uncomitted and nolock.

When I use the queries on a single table they work perfectly, but when I try to join the 2 tables I get this error:

Transaction (Process ID 88) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Meanwhile, if I use the sp_lock procedure I found that the Key lock becomes a tab lock when I perform my queries.

Does anyone know if there is a special relation between (nolock) and join? And if there is how can I avoid it.

UPDATE:

Insert 开发者_StackOverflow社区into tbl1 with (rowlock)
(
 col1,
 col2,
 col3
)
select * from #tbl_temp

( this is in an infinite loop and the data from #tbl_temp is always changed. Actualy this is a more complex process but this is the idea.)

Insert into tbl2 with (rowlock)
(
 col3,
 col4,
 col5
)

select * from #tbl_temp2 

In the same time I perform

set transaction isolation level read uncomitted

select col1,col2,col3 
from tbl1 with (nolock)  -- works fine


select col1,col2,a.col3 

from tbl1 with (nolock) join tbl2 with (nolock) on (tbl1.col3 = tbl2.col3)

-- deadlock


You might want to try turning on READ_COMMITTED_SHAPSHOT isolation level for your database.

(But be aware that this will put increased pressure on tempDB)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜