Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my complete table is locked instead of rows?

I have run this SQL,

create table temp
(
     id int,
    name varchar(10)
)
insert into temp values(1,'a');

then I run,

select 1 from temp  where id = 1 

everything fine.

Then I run an uncommitted insert,

SET NOCOUNT ON;
    DECLARE @TranCount INT;
    SET @TranCount = @@TRANCOUNT;



        IF @TranCount = 0
            BEGIN TRANSACTION
        ELSE
            SAVE TRANSACTION Insertorupdatedevicecatalog;

insert into temp values(2,'b')

then I run,

select 1 from temp  where id = 1 

But this time nothing is returned. Why is my complete table locked instead of just second row?

like image 326
Imran Qadir Baksh - Baloch Avatar asked Dec 03 '13 18:12

Imran Qadir Baksh - Baloch


People also ask

How do you stop a table from locking?

Keep transactions that modify data as short as possible. The longer the transaction, the longer the exclusive or update locks are held. This blocks other activity and can lead to an increased number of deadlock situations. Keep transactions in one batch.

How do you unlock a table?

Use the UNLOCK TABLE statement in a database that does not support transaction logging to unlock a table that you previously locked with the LOCK TABLE statement. The UNLOCK TABLE statement is an extension to the ANSI/ISO standard for SQL.

What is the difference between row lock and table lock?

Table-level locking systems always lock entire tables. Row-level locking systems can lock entire tables if the WHERE clause of a statement cannot use an index. For example, UPDATES that cannot use an index lock the entire table.

How do you release a table lock in SQL Server?

ALL releases all table-level locks on all tables. The UNLOCK TABLE statement unlock tables that you have locked manually by using the LOCK TABLE command with the LONG option. The LONG option allows you to hold a lock past the end of the transaction in which the lock was placed.


2 Answers

SQL Server is not locking the entire table. I can see that a single row-id is locked by the writing transaction.

The reader has to scan the entire table because there are no indexes.

enter image description here

This means that it is blocked by the X-lock on the inserted row. Basically, the reader waits for the other transaction to decide whether it wants to actually commit this row or rollback.

enter image description here Session 51 has inserted id 2. Session 54 is the blocked select. No page or table locks here (apart from the intent-locks which do not matter here).

The fact that the table is a heap (no unique CI like usual) causes unexpected locking here. This issue will go away by creating a unique CI on id.

like image 83
usr Avatar answered Oct 11 '22 07:10

usr


My guess is that your table probably isn't locked per se (or perhaps it could be since you have so few rows). I think what's happening is that SQL Server, in order to know where to insert the new row, has to lock (write lock, which prevents reading) a range of rows around the value being inserted. Since you have so few rows in the table, the appearance is that the table is locked. As you add many more rows you should not see this behavior when inserting a single row.

By the way, your table should have a primary key and/or clustered index. This will help in the future, as you add more rows. Otherwise you're going to be doing scans, which will certainly lengthen the time it takes to do updates (and perhaps inserts).

like image 42
Randy Minder Avatar answered Oct 11 '22 07:10

Randy Minder