Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Insert new values in dimension avoiding deadlock

By Federico Iori,

Sql Server DBMS is build such that selecting from a table requires a share lock on the table ( except if hint NOLOCK  is used) , and in some situations this can lead to deadlocks .

On the other side, the hint NOLOCK can lead to dirty reads.

Typical case , if we have in DWH a dimension that can filled simultaneosly by more than one ETL process .

The solution is to take a table share lock before inserting, this way

insert into dim_IMSI
select distinct a.SERVED_IMSI from Source_table_A a
left join dim_IMSI exi with(tablock) on
a.SERVED_IMSI= exi.imsi where 1=1 and
exi.imsi_id is null

This insert statement is deadlock safe and is not blocked by long running selects from dim_imsi, because a select takes a share page lock while the insert takes a table exclusive lock , which does not require all the pages of the table to be locked

Total article views: 1160 | Views in the last 30 days: 2
 
Related Articles
FORUM

Insert - Exec Select statements

Insert - Exec Select statements

FORUM

Blocking Issue:Insert blocking select statements

Insert blocking select statements

FORUM

DYNAMIC SELECT & INSERT QUERY HELP

DYNAMIC SELECT & INSERT QUERY HELP

SCRIPT

Huge Insert .... Select that not blocking using snapshot isolation

a technique that allows large insert .... select but blocks no other queries

ARTICLE

Knowledge sharing

This article talks about the sharing of knowledge in forums such as SQLServerCentral.

Tags
deadlock    
dimension    
dwh    
exclusive table lock    
normalization    
tablockx    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones