我在 Oracle 上有这个声明,但是速度很慢。是否可以对其进行优化?
SELECT col2.column_name
FROM all_constraints cons, all_cons_columns col, all_cons_columns col2, all_constraints cons2
WHERE cons.owner = 'OWNER'
AND cons.table_name = 'TABLE'
AND col.constraint_name = cons.constraint_name
AND cons.constraint_type = 'P'
AND col2.column_name = col.column_name
AND col2.table_name = col.table_name
AND col2.owner = col.owner
AND cons2.constraint_name = col2.constraint_name
AND cons2.constraint_type = 'R'
AND cons2.r_constraint_name
IN (SELECT parentcol.constraint_name FROM all_cons_columns parentCol WHERE parentcol.owner = 'OWNER2' AND parentcol.table_name = 'TABLE2')
我试过这个(返回相同的结果),但它仍然很慢
SELECT *
FROM all_constraints cons, all_cons_columns col
WHERE col.table_name = 'TABLE2' AND col.Owner = 'OWNER2'
AND col.constraint_name = cons.constraint_name AND cons.constraint_type = 'P' AND
col.column_name IN
(
SELECT col2.column_name
FROM all_constraints cons2, all_cons_columns col2
WHERE col2.table_name = 'TABLE2' AND col2.Owner = 'OWNER2'
AND col2.constraint_name = cons2.constraint_name AND cons2.constraint_type = 'R'
AND cons2.r_constraint_name IN
(
SELECT cons3.constraint_name
FROM all_constraints cons3, all_cons_columns col3
WHERE col3.table_name = 'TABLE' AND col3.Owner = 'OWNER'
AND col3.constraint_name = cons3.constraint_name AND cons3.constraint_type = 'P'
)
)