Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

InnoDB locks whole table when doing an update

$
0
0
My understanding was that InnoDB is doing row-level locking for updates. However this does not seem to always be the case when updating a range of rows.

Assume the following setup:

Code:

create table foo
(
  id integer not null primary key,
  c1 integer not null
) engine=innodb;

insert into foo
(id, c1)
values
( 1, 10),
( 2, 11),
( 3, 12),
( 4, 13),
( 5, 14),
( 6, 15),
( 7, 16),
( 8, 17),
( 9, 18),
(10, 19);

commit;

Now I start two different sessions by invoking the mysql commandline client.

In the first window I do the following:
Code:

mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> set transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> update foo
    -> set c1 = c1 + 1
    -> where id between 1 and 4;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql>

Then in the second window, I run a similar statement, only updating the other rows of the table:
Code:

mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> set transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> update foo
    ->  set c1 = c1 + 2
    -> where id between 5 and 10;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql>
mysql>

The second statement will not be successful until I commit the first transaction. Does this mean that InnoDB is using a table level lock when I update a range of rows?

Note that the column that is being updated is not indexed, so this can't be a problem with gap locking or something similar (that's one reason I changed the isolation level to "read committed" as apparently the default leve "repeatable read" is more "vulnerable" against index locking)

The two updates run fine if I only update a single row.

What am I missing here?

Viewing all articles
Browse latest Browse all 13329

Trending Articles