为什么外键最好加上索引?

3 篇文章 0 订阅

今天遇到一个数据库DeadLock的问题,与同事讨论,一个同事忽然问了一句:“为什么外键没有索引会导致deadlock“?

为了回答这个问题,我直接翻了Oracle文档,里面两张图画的非常直接。


文章地址:http://docs.oracle.com/cd/B19306_01/server.102/b14220/data_int.htm#sthref3108


Unindexed foreign keys cause DML on the primary key to get a share row exclusive table lock (also sometimes called a share-subexclusive table lock, SSX) on the foreign key table. This prevents DML on the table by other transactions. The SSX lock is released immediately after it is obtained. If multiple primary keys are updated or deleted, the lock is obtained and released once for each row.

Figure 21-8 Locking Mechanisms When No Index Is Defined on the Foreign Key

Description of Figure 21-8 follows


Indexed foreign keys cause a row share table lock (also sometimes called a subshare table lock, SS). This prevents other transactions from exclusive locking the whole table, but it does not block DML on the parent or the child table.

Figure 21-9 Locking Mechanisms When Index Is Defined on the Foreign Key

Description of Figure 21-9 follows



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值