Hi,
I see a strange locking behaviour in MySQL 5.1.41. Not sure if this is a bug or a feature. Delete statement seems to lock the select tables but only if the condition for the select does NOT find any rows.
I have boiled down my production queries to somewhat silly example here but it shows the issue:
First, create these two tables:
CREATE TABLE T1 (tmp VARCHAR(40) PRIMARY KEY)
ENGINE = InnoDB;
CREATE TABLE T2 (id int(40) PRIMARY KEY)
ENGINE = InnoDB;
Then insert a record into T2:
insert into T2 values(100);
Now run this in the transaction w/o commit:
DELETE T1
FROM T1,
T2
WHERE T2.id = 100;
In a separate transaction try to insert into T2:
INSERT INTO T2 VALUES (123);
This will run just fine. So far so good.
Now rollback the delete transaction and try this:
DELETE T1
FROM T1,
T2
WHERE T2.id = 10;
Now try to insert into T2. The insert statement will block waiting on T2.primary key lock.
Conclusion: if you use T2.id values NOT found in the table the whole key is being locked. If you use existing values then the lock is not placed to the key.
Could someone explain that?
So far the only workaround we came up with is to copy relevant portion from T2 to a temp table and then use that table in delete joins.
Thanks for the help,
Dima.
I see a strange locking behaviour in MySQL 5.1.41. Not sure if this is a bug or a feature. Delete statement seems to lock the select tables but only if the condition for the select does NOT find any rows.
I have boiled down my production queries to somewhat silly example here but it shows the issue:
First, create these two tables:
CREATE TABLE T1 (tmp VARCHAR(40) PRIMARY KEY)
ENGINE = InnoDB;
CREATE TABLE T2 (id int(40) PRIMARY KEY)
ENGINE = InnoDB;
Then insert a record into T2:
insert into T2 values(100);
Now run this in the transaction w/o commit:
DELETE T1
FROM T1,
T2
WHERE T2.id = 100;
In a separate transaction try to insert into T2:
INSERT INTO T2 VALUES (123);
This will run just fine. So far so good.
Now rollback the delete transaction and try this:
DELETE T1
FROM T1,
T2
WHERE T2.id = 10;
Now try to insert into T2. The insert statement will block waiting on T2.primary key lock.
Conclusion: if you use T2.id values NOT found in the table the whole key is being locked. If you use existing values then the lock is not placed to the key.
Could someone explain that?
So far the only workaround we came up with is to copy relevant portion from T2 to a temp table and then use that table in delete joins.
Thanks for the help,
Dima.