2

I have a type 2 dimension in Azure SQL Data Warehouse.

Essentially I am creating an interim table with

CREATE TABLE myDimension_temp
AS 
SELECT
...
FROM myStagingTable;

etc

After the CTAS is complete I do a

RENAME OBJECT myDimension TO myDimension_old;
RENAME OBJECT myDimension_tmp TO myDimension;
DROP TABLE myDimension_old;

Is there a way to lock these tables so that any actions against myDimension are blocked until the renaming of both tables is complete?

If so, is this worth implementing or is the time it takes to swap out tables in this manner so negligible that the chances of this occurring are too small to worry about or am I looking at/going about this in completely the wrong way?

Tom
  • 21
  • 1

2 Answers2

1

There is generally no exclusive locking in Azure SQL Data Warehouse as the default isolation level is READ UNCOMMITTED as documented here.

Other approaches to this such as sp_get_applock are also unavailable. Therefore you manage this based on low concurrency and workflow, ie don't allow users to connect to your warehouse during the update, or use some kind of flag that has to be checked during operations.

wBob
  • 10,420
  • 2
  • 25
  • 44
0

RENAME is not transactional ie begin transaction .. rename .. will throw an error. So any attempt to gain an exclusive lock on a table before renaming it is futile as the transaction acquiring the lock must end (and release the lock) before RENAME can run.

Your best bet is to schedule no access during the swap. If access must happen it must be written allowing for failure during the brief interval where the dim table does not exist.

An alternative is to use whole-table partition switching alter table Source switch to Target. I do not know if this has any better locking characteristics in your context. It has the advantage that all tables exist all the time so queries will not fail. They will return zero rows, however, so that has to be considered.

Michael Green
  • 25,255
  • 13
  • 54
  • 100