读书人

外键跟锁

发布时间: 2013-09-10 13:42:18 作者: rapoo

外键和锁
使用外键是需要付出代价的,也就是,即时检查,逐行进行,当然Oracle有个延时检查
要求每次修改时都要对另外一张表多一次select操作,使用select lock in share mode方式
意味着需要额外的锁,来确保该记录不会在事务未完成前被删除
这将导致更多的锁等待,甚至是死锁,因为关联到其他表,死锁很难被爬出

小测试

会话A:mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> select * from t where id=1 for update;+----+| id |+----+|  1 |+----+1 row in set (0.00 sec)会话B:mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> select * from t where id=2 for update;+----+| id |+----+|  2 |+----+1 row in set (0.00 sec)会话A:mysql> select * from t where id=2 for update;--被阻塞会话B:mysql> select * from t where id=1 for update;ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction会话A:+----+| id |+----+|  2 |+----+1 row in set (16.69 sec)



By DBA_WaterBin

2013-09-08

good luck

读书人网 >其他数据库

热点推荐