in

SG SQL Server Usergroup

The Premier web platform for Microsoft SQL Server professionals in Singapore

Should i use timestamp in every tables??

Last post 06-18-2008 10:08 AM by ayztun. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 06-12-2008 1:57 PM

    • ayztun
    • Top 10 Contributor
    • Joined on 03-28-2008
    • Jurong West
    • Posts 4

    Should i use timestamp in every tables??

    Hi All,

             I am very new to DBA and I want to know should I use timestamp in every tables for concurrency control?

    Database Administrator
  • 06-16-2008 8:44 PM In reply to

    Re: Should i use timestamp in every tables??

    Hi,

    TIMESTAMP is one of the best ways to manage concurrency. Let me explain this in detail:

    Pessimistic and Optimistic Concurrency

    In a multiuser environment, there are two models for updating data in a database: optimistic concurrency and pessimistic concurrency. Pessismistic concurrency involves locking the data at the database when you read it.  You essentially lock the database record and don't allow anyone to touch it until you are done modifying and saving it back to the database.  Here you have 100% assurance that nobody will modify the record while you have it checked out.  Another person will have to wait until you have made your changes.Optimistic concurrency means you read the database record, but don't lock it.  Anyone can read and modify the record at anytime and you will take your chances that the record is not modified by someone else before you have a chance to modify and save it.  As a developer, the burden is on you to check for changes in the original data ( collisions ) and act accordingly based on any errors that may occur during the update.Depending on the application and the number of users, pessimistic concurrency can cause delays in your application.  Other users may have to wait while another user has locked a database record.  Worst case, you get an actual dead lock, because there is a cyclical dependency on database resources and nobody can complete his/her intended task.  A timeout occurs in the application and nobody is happy, including the guy responsible for cutting you a check.With optimistic concurrency, the application has to check for changes to the original record to avoid overwriting changes.  There is no guarantee that the original record has not been changed, because no lock has been placed on that data at its source - the database.  Hence, there is the real possibility of losing changes made by another person.

    Simplest Thing Possible, But No Simpler

    The key is to know your application and to pick a solution that best meets its needs.If your application is a portal for your INETA .NET Developer Group Website, I dare say that there is absolutely no chance that two people will be updating content at any time. ;)  You are lucky if you get one person to update the website.  Hence, I would say this is very much a single-user environment and the heck with concurrency issues - they won't happen.  The chance of overwriting someone else's data is slim to none as well as has very little repercussions if it happens.If your application is a banking application and the repercussions of overwriting data is severe, concurrency issues will be a big concern and need to be judged accordingly.  Although the chance of 2 users / processes modifying the same data may be slim to none in the application, the risk of it happening once may be important enough to use pessimistic concurrency and the heck with performance on the off chance that two processes may want to access the same data.

    Optimistic Concurrency Strategies

    If you are in a performance state-of-mind, chances are you will go with optimistic concurrency.  Optimistic concurrency frees up database resources as quickly as possible so that other users and processes can act upon that data as soon as possible.To the best of my knowledge, there are four popular strategies to dealing with optimistic concurrency:
    1. Do Nothing.
    2. Check for changes to all fields during update.
    3. Check for changes to modified fields during update.
    4. Check for changes to timestamp ( rowversion ) during update.
    All of these strategies have to deal with the shaping of the Update T-SQL Command sent to the database during the updating of the data.  The examples below are not very detailed on purpose and assume a basic understanding of ADO.NET.  Below shows the strategies from a view point of 30,000 ft high.

    Optimistic Concurrency on Update Strategy #1 - Do Nothing

    The simplest strategy for dealing with concurrency issues during the updating of data is to do nothing.The update command will not check for any changes in the data, only specify the primary key of the record to be changed.  If someone else changed the data, those changes will more than likely be overwritten:
    Update Product
        Set
            Name = @Name
        Where
            ID = @ID
    One would hope that this means either 1) the application is a single-user application, or 2) the chance of multi-user update collisions is very unlikely and the repercussions of overwriting data is negligible.

    Optimistic Concurrency on Update Strategy #2 - Check All Fields

    With this strategy, the update command will check that all fields in the row ( usually minus BLOB fields ) are equal to their original values when peforming the update to assure no changes have been made to the original record.  A check of the return value of the ExecuteNonQuery Command will tell you if the update actually took place.  The return value of the ExecuteNonQuery Command is typically the number of rows affected by the query.
    Update Product
        Set
            Name = @Name,
        Where
            ID = @ID
          AND
            Name = @OriginalName
          AND
            Price = @OriginalPrice
    This is essentially what CommandBuilder creates when using DataSets and is a strategy that doesn't want to see any changes to the data.

    Optimistic Concurrency on Update Strategy #3 - Check Only Changed Fields

    Rather than checking all fields in the row to make sure they match their original value, this strategy checks only those fields that are being updated in the command.
    Update Product
        Set
            Name = @Name
        Where
            ID = @ID
          AND
            Name = @OriginalName
    This strategy only cares that it is not overwriting any data and could care less that other fields in the record may have been changed. This could create an interesting combination of data in the row.

    Optimistic Concurrency on Update Strategy #4 - Implement Timestamp

    SQL Server has a timestamp ( alias rowversion ) field that is modified everytime a change is made to a record that contains such a field.  Therefore, if you add such a field to a table you only have to verify the timestamp record contains the same original value to be assured none of the fields have been changed in the record.
    Update Product
        Set
            Name = @Name
        Where
            ID = @ID
          AND
            TimestampID = @TimestampID
    This is the same as Strategy #2 above without the need for checking all fields.
    Hope this article explains you.
    Regards,

    Sudhir Chawla
  • 06-17-2008 5:43 PM In reply to

    • ayztun
    • Top 10 Contributor
    • Joined on 03-28-2008
    • Jurong West
    • Posts 4

    Re: Should i use timestamp in every tables??

    Thanks you for your explanation.... I will try my best.

    Database Administrator
  • 06-18-2008 10:08 AM In reply to

    • ayztun
    • Top 10 Contributor
    • Joined on 03-28-2008
    • Jurong West
    • Posts 4

    Re: Should i use timestamp in every tables??

    Thank you so much for your explanation Sudhir Chawla!

    Now I think I know what sholud I do. :)

    have a nice day!

    Database Administrator
Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems