Let's say I have two tables, t1
and t2
. t1
has a trigger upon it so that on every INSERT an INSERT then happens on t2
.
If I then acquire a write lock on 开发者_JS百科t2
, what happens when I INSERT into t1
? There are a number of things I'm curious about here:
Can I still INSERT into
t1
? Or will it lock?If I can INSERT into
t1
just fine, does that mean that the trigger will be queued up, just waiting until I UNLOCKt2
?If I'm INSERTing many things into
t1
while I still have the lock ont2
, are there any consequences to DB performance overall? Will this keep connections open or anything like that?
t1
will be not be locked implicitly, as the docs for implicit locking explain would be the case if you were doing the reverse (lockingt1
will lockt2
- A MySQL Error (ERROR 1100 (HY000): Table
t1
was not locked with LOCK TABLES) will be generated on attempting to insert intot1
because it has not been locked, but the destination of its trigger has. - N/A, the query to write to
t1
is not permitted by the MySQL engine.
Please consider your use case here however. If you are simply copying INSERTS into another table, and not calculating something, you might want to give a thought to using replication instead.
Verified on MySQL 5.1 with the following SQL of a toy action log:
DROP TABLE IF EXISTS acts, actions;
CREATE TABLE acts
(
act_id int unsigned not null auto_increment primary key,
user_id int unsigned not null
)
ENGINE=innodb;
CREATE TABLE actions
(
action_id int unsigned not null auto_increment primary key,
act_id int unsigned not null,
user_id int unsigned not null
)
ENGINE=innodb;
DELIMITER #
CREATE TRIGGER acts_post_ins_trigger AFTER INSERT ON acts
FOR EACH ROW
BEGIN
INSERT INTO actions(act_id, user_id) VALUES (new.act_id, new.user_id);
END#
DELIMITER ;
INSERT INTO acts VALUES(1,2);
SELECT * FROM acts;
SELECT * FROM actions;
LOCK TABLE actions WRITE;
SELECT('trying to SELECT acts w/ actions locked...') AS 'SELECT TEST';
SELECT * FROM acts;
SELECT('trying to INSERT into acts w/ actions locked...') AS 'INSERT TEST';
INSERT INTO acts VALUES(3,4);
UNLOCK TABLES;
精彩评论