我需要选择表上的所有外键引用。我提出了一个有效的查询,但需要9秒才能执行。如果有人可以帮助优化它,那将是非常好的。非常感谢你!
以下是查询:
SELECT A.TABLE_NAME REFENCING_TABLE,
A.COLUMN_NAME REFENCING_COLUMN,
B.TABLE_NAME REFERENCED_TABLE,
B.COLUMN_NAME REFERENCED_COLUMN
from (select TABLE_NAME,
COLUMN_NAME,
REFERENCED_CONSTRAINT_NAME,
POSITION
from all_cons_columns C
join (select CONSTRAINT_NAME,
PK.REFERENCED_CONSTRAINT_NAME
from all_constraints FK
JOIN (select CONSTRAINT_NAME REFERENCED_CONSTRAINT_NAME
from all_constraints
where constraint_type in ('P','U') and
table_name = :tablename) PK
on FK.R_CONSTRAINT_NAME = PK.REFERENCED_CONSTRAINT_NAME
where constraint_type = 'R') FK
on C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME) A
join (select TABLE_NAME,
COLUMN_NAME,
CONSTRAINT_NAME,
POSITION
from all_cons_columns) B
on REFERENCED_CONSTRAINT_NAME = B.CONSTRAINT_NAME AND
A.POSITION = B.POSITION;
答案 0 :(得分:0)
您可以使用此查询查找表中的所有外键引用:
SELECT
fk.owner fk_schema_owner,fk.table_name fk_table,
fk.column_name fk_column, fk.constraint_name fk_constraint_name,
pk.r_owner pk_schema_owner,
c_pk.table_name pk_table_name, c_pk.constraint_name pk_constraint_name
FROM
all_cons_columns fk
JOIN all_constraints pk
ON fk.owner = pk.owner AND fk.constraint_name = pk.constraint_name
JOIN all_constraints c_pk
ON pk.r_owner = c_pk.owner AND pk.r_constraint_name = c_pk.constraint_name
WHERE pk.constraint_type = 'R' AND fk.table_name = :InputTableName