0

Here's the situation:

MS SQL 2008 database with table that is updated approximately once a minute. The table structure is similar to following:

[docID], [warehouseID], [docDate], [docNum], [partID], [partQty]

Typical working cycle:

User starts data exchange from in-house developed system:

BEGIN TRANSACTION

SELECT * FROM t1 
WHERE [docDate] BETWEEN &DateStart AND &DateEnd 
AND [warehouseID] IN ('w1','w2','w3')

...then system performs rather long processing of the data selected, generates the list of [docID]s to delete from t1, then goes

DELETE FROM t1 WHERE [docID] IN ('d1','d2','d3',...,'dN')

COMMIT TRANSACTION

Here, the problem is that while 1st transaction processes selected the data, another reads it too and then they together populate the same data in in-house system.

At first, I inserted (TABLOCKX) table hint into SELECT query. And it worked pretty well until users started to complain about system's performance.

Then I changed hints to (ROWLOCK, XLOCK, HOLDLOCK), assuming that it would:

  • exclusively lock...
  • selected rows (instead of whole table)...
  • until the end of transaction

But this seems making a whole table lock anyway. I have no access to database itself, so I can't just analyze these locks (actually, I have no idea yet how to do it, even if I had access)

What I would like to have as a result:

  1. users are able to process data related with different warehouses and dates in parallel

  2. as a result of 1., avoid duplication of downloaded data

Except locks, other solutions I have are (although they both seem clumsy):

  1. Implement a flag in t1, showing that the data is under processing (and then do 'SELECT ... WHERE NOT [flag]')

  2. Divide t1 into two parts: header and details, and apply locks separately.

I beleive that I might misunderstood some concepts with regards to transaction isolation levels and/or table hints and there is another (better) way. Please, advise!

1 Answer 1

0

You may change a concept of workflow.

Instead of deleting records update them with setting extra field Deprecated from 0 to 1.

And read data not from the table but from the view where Deprecated = 0.

BEGIN TRANSACTION

SELECT * FROM vT1 
WHERE [docDate] BETWEEN &DateStart AND &DateEnd 
AND [warehouseID] IN ('w1','w2','w3')

where vT1 view looks like this:

select *
from t1
where Deprecated = 0

And the deletion will look like this:

UPDATE t1 SET Deprecated = 1 WHERE [docID] IN ('d1','d2','d3',...,'dN')

COMMIT TRANSACTION

Using such a concept you will achieve two goals:

  • decrease probability of locks
  • get history of movings on warehouses
3
  • How can this workflow help us to ensure that data is processed only once? If two or more users start exchange at the same time, we will get two or more identical sets of data processed by target system. Kinda newbie question, but can view be presented exclusively for single user? If yes, how can we achieve it? Mar 5, 2014 at 9:43
  • 1
    You can add WITH(READPAST) hint into the view: select * from t1 with(readpast) where Deprecated = 0. In this case every user reading some data will wait until this data will be changed.
    – GriGrim
    Mar 5, 2014 at 10:29
  • Indeed! Thank you for such simple yet elegant solution! Mar 5, 2014 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.