4

Subquestioning SQL - when should you use “with (nolock)”

In one local financial institution I was rebuked by their programmers for expressing them my opinion that (their programmers' obsession with) (b)locking issues in their MS SQL Server 2005 database(s) did not make much sense to me.

What are the possible issues with possible locking, blocking, deadlocking if financial operations are never updated, deleted and even incorrect operations (part of "transactions"?) are corrected by inserting (adding) new correcting records into database(s)?
What is the term for this in English? In other languages it is called storno, stornoed (?) operations/records.

So, as I understand, the "transactions" are really never rolled back and there are never incorrect/non-existent records, only non-actualized ones.

Update:
I googled for storno and could not find any results with its definition in English or its use in English texts.
I found definition for storno (in latin letters) only in Italian.
But accounting was invented in Italia and many Italian accounting terms are used in other languages, for ex., in Russian accountance (banking).
I also thought that it was internationally accepted practice in financial operations accountance, isn't it?

Update2:
S.Lott gave me link to The way that transactions are reversed in an ERP application is a big deal! telling that storn is reversal transaction.

Well, this is not correct. Storno is not only transation, it is any operation (part of transaction) correcting incorrect operation though 2 operations combined might seem to reverse tranaction (consisting of 2 operations - of crediting and debiting on target and source accounts).

So, storno is not common financial accountancy practice through the world?

Anyway, I'd like to avoid discussion of accountancy details/techniques/terms and to restrict the question to context when records are never deleted or updated.

What are the possible problems with locking, blocking, deadlocking, performance in this context?

3

1 Answer 1

2

"Storno Transactions" or "Reversing Transactions" are summarized nicely. In lots of places.

http://richardatopenbravo.blogspot.com/2010/02/way-that-transactions-are-reversed-in.html

http://help.sap.com/saphelp_46b/helpdata/en/d2/6f921f415e11d182b10000e829fbfe/content.htm

http://forum.wordreference.com/showthread.php?t=1875166

Don't conflate software implementation with accounting. A reasonable implementation can get by with minimal locking. That doesn't mean anything, however. You may have earned a rebuke because the software is (a) badly designed and (b) requires careful locking because of poor design.

What are the possible issues...?

Since we don't know how well or how poorly the software is written, it's impossible to guess. They may know something about their system that you didn't know.

A simple storno transaction system should be easy to implement. Indeed, it should be trivial.

A pair of "insert-only" tables can still encounter deadlocks if page-level locking is used during the inserts.

Table A, page 1 has an insert in transaction X.

Table B, page 2 has an insert in transaction Y.

Table B, page 2 has an insert in transaction X.

Table A, page 1 has an insert in transaction Y.

The only way to avoid deadlocks is to have the entire system use a single table. Or have all transactions limited to a single table. Or use a single database-wide lock.

If you have multiple-table operations (and page-level locking) then you will still have potential deadlocks even with insert-only operations. Clearly it's rare, but still possible.

12
  • "Or use a single database-wide lock." Er, that's called a single user system. Sure, you've eleiminated deadlocks, but you've serialised all transactions. Nov 2, 2010 at 15:28
  • ... even with insert-only operations. Clearly it's rare, but still possible. It is not "rare". Inserts cause locks at various points: the heap; index pages; when rows need to be moved. When num of users increases, blocking locks and thus deadlocks increase. It is ordinary. Nov 2, 2010 at 15:31
  • @PerformanceDBA: "SQL server will lock" is not the same as a deadlock. With a single lockable resource, deadlocks are logically impossible.
    – S.Lott
    Nov 2, 2010 at 18:25
  • @PerformanceDBA: "Inserts cause locks at various points". True, but they rarely cause deadlock because insert transactions -- typically -- involve few resources and little contention for resources.
    – S.Lott
    Nov 2, 2010 at 18:26
  • @S.Lott: I will restate. The only way to avoid deadlocks is to have the entire system use a single table. That is grossly incorrect, SQL server will lock (and therefore block, and therefore deadlock) just as much within on table. On a multi-user system, you've just ensured that the locks are close to gether (instead of spread across many tables) and thus guaranteed more blocking locks and deadlocks. Since you do not know anything about SQL Server locking, I suggest you (a) do not post re the subject and (b) avoid challenging those who do know. Nov 2, 2010 at 21:56

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.