Oracle diary - Apr27 2011

Use PRowID to see table organization, LROWID not

 

The database is my Virtual Machine. If I can do it in the database, I will. --TomAS Kyte

 

Index the Fkey, Otherwise :

 

- If I update the parent tableʹs primary key (a very rare occurrence if you follow the
rules of relational databases that primary keys should be immutable), the child
table will be locked in the absence of an index.


- If I delete a parent table row, the entire child table will be locked (in the absence of
an index) as well.

 

- When you have an ON DELETE CASCADE and have not indexed the child table.
For example, EMP is child of DEPT. DELETE DEPTNO = 10 should CASCADE to
EMP. If DEPTNO in EMP is not indexed, you will get a full table scan of EMP. This
full scan is probably undesirable, and if you delete many rows from the parent table,
the child table will be scanned once for each parent row deleted.

- When you query from the parent to the child. Consider the EMP/DEPT example
again. It is very common to query the EMP table in the context of a DEPTNO. If you
frequently run the following query, say to generate a report, youʹll find that not
having the index in place will slow down the queries:
       select * from dept, emp where emp.deptno = dept.deptno and dept.deptno = :X;

 

So, when do you not need to index a foreign key? The answer is, in general, when the
following conditions are met:
- You do not delete from the parent table.
- You do not update the parent tableʹs unique/primary key value (watch for
unintended updates to the primary key by tools!
- You do not join from the parent to the child (like DEPT to EMP)
If you satisfy all three above, feel free to skip the index ‐ it is not needed. If you do any of
the above, be aware of the consequences. This is the one very rare time when Oracle tends
to ʹover‐lockʹ data.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值