Table locks while inserting into the table

  • komal145

    SSCrazy Eights

    Points: 9874

    Hi ,

    I am facing a deadlock while i am trying to insert data into a table. At the same time we have app users working on the application , which inserts data in our table.

    SO, in order to avoid deadlock , can i use table lock? if so , which table lock to use and how?

    I have insert statement within the transaction something like below. Can i use hold lock hint ? will that work? i am not familiar using this lock . Please suggest



    BEGIN TRANSACTION t_Transaction

    INSERT INTO LargeTable

    SELECT *

    FROM viewLargeView



    COMMIT TRANSACTION t_Transaction








    • This topic was modified 4 months, 3 weeks ago by  komal145.
  • Phil Parkin

    SSC Guru

    Points: 244578

    A table lock is different from a deadlock. Which one do you mean?

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See for details of how to post T-SQL code-related questions.

  • John Mitchell-245523

    SSC Guru

    Points: 148761

    So you are truncating a large table and then inserting a large amount of data into it, and all the time other users are trying to use the table?  Do the users who are inserting data into the table know that you are clearing that data out?

    Without understanding the architecture of your application, my best suggestions are:

    1. Tune the query in the definition of viewLargeView
    2. Schedule your work for out of hours, when there are no users


  • MVDBA (Mike Vessey)


    Points: 21757

    my 2 cents on this - why are you truncating a table and replacing it with the view data. - why not just reference the view?



  • Grant Fritchey

    SSC Guru

    Points: 396551

    If you lock it, no one can access it. That will stop the deadlocks. But then, the phone calls start "WHY CAN'T I GET MY DATA".

    I'm with Mike. What is this process satisfying?

    The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
    Theodore Roosevelt

    The Scary DBA
    Author of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd Edition
    Product Evangelist for Red Gate Software

  • MVDBA (Mike Vessey)


    Points: 21757

    an alternative (if you can't use the view because it points to a linked server) is to use transactional replication and update the table in small chunks, rather than a big one time hit

    but Grant hit the nail on the head... why are you doing this?


  • Jeff Moden

    SSC Guru

    Points: 996640

    You can do this with no chance of deadlocks nor even blocking AND the total "offline" time will be measured in milliseconds.

    You're replacing the entire table each time.  So... create two tables.  For example, if your table was originally created as "SomeTable", drop that and create two tables... one called "SomeTableA" and another called "SomeTableB".  Populate SomeTableA as you normally would and then create a SYNONYM called "SomeTable" (which was the name of the original table" and point it at the now populated "SomeTableA".

    When you need to update the table with new data, populate "SomeTableB".  You don't need a transaction for this.  If you do it right, it can even be a minimally logged evolution.  Once you've verified that "SomeTableB" has been correctly populated, drop the "SomeTable" synonym and immediately rebuild it but pointing to the most recent table, which is "SomeTableB".  You can then truncate the now unused "SomeTableA".  Total downtime will be measured in milliseconds.

    When it comes time for the next update, simply reverse the process.  Populate the currently empty "SomeTableA", repoint the synonym as previous described, then truncate "SomeTableB".

    Wash, rinse, repeat as time wears on.

    --Jeff Moden

    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "If "pre-optimization" is the root of all evil, then what does the resulting no optimization lead to?"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply