外键索引

[复制链接]
查看11 | 回复4 | 2005-2-28 12:57:00 | 显示全部楼层 |阅读模式
外键:
一张表有20多个字段,近有10个字段有外键,听别人说如果外键上不加索引速引会很慢,但如果要建的话,需要给每个外键建索引吗?
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
"外键上不加索引速引会很慢" 好像没有这方面的概念,我只是觉得没有外键的话,对DML的并发有影响,因为如果修改主表的话,从表整个表都将被 Lock 起来。
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
In addition to the table lock issue that might hit you, an unindexed foreign key
is bad in the following cases 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 query:
select * from dept, emp
where emp.deptno = dept.deptno and dept.deptno = :X;
to generate a report or something, you'll find not having the index in place
will slow down the queries.
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
哦,谢谢给出这段说明。
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
我也是新发现的。@_@
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行