When using Intellisense CTRL+. for something like a table / partial proc name in Toad 15.1.113.1379, it gets stuck on "Loading Constraints", this is particularly bad on instances with lots of schemas.
If you spool the SQL, the problem is obvious, the data dictionary query for FK constraints is NOT filtering for the current schema. So this can be 100000s of FKs on large instances.
-- Currently
WITH
ref_cons
AS
(Select c.owner,
c.constraint_name,
c.table_name,
c.r_owner,
c.r_constraint_name,
cc.position,
cc.column_name
FROM sys.DBA_CONSTRAINTS c, sys.DBA_CONS_COLUMNS cc
WHERE c.constraint_type = 'R'
AND c.owner = cc.owner
AND c.constraint_name = cc.constraint_name),
pk_cons
AS
(Select c.owner,
c.constraint_name,
c.table_name,
cc.position,
cc.column_name
FROM sys.DBA_CONSTRAINTS c, sys.DBA_CONS_COLUMNS cc
WHERE c.constraint_type IN ('P', 'U')
AND c.owner = cc.owner
AND c.constraint_name = cc.constraint_name)
SELECT 'REF_INTEGRITY' AS type,
ref_cons.owner,
ref_cons.table_name,
ref_cons.column_name,
ref_cons.constraint_name,
ref_cons.r_owner,
pk_cons.table_name AS r_table_name,
pk_cons.column_name AS r_column_name,
pk_cons.constraint_name AS r_constraint_name
FROM pk_cons, ref_cons
WHERE ref_cons.r_owner = pk_cons.owner
AND ref_cons.r_constraint_name = pk_cons.constraint_name
AND ref_cons.position = pk_cons.position
ORDER BY ref_cons.owner,
ref_cons.table_name,
ref_cons.constraint_name,
ref_cons.position;
Suggested fix :
/*
Query filters below don't use "USER" function for filtering, as it is slow to
evaluate for each row in where clause. It could be used if wrapped in subquery
to leverage cursor caching. E.g. AND c.owner = (select USER from dual)
*/
WITH
ref_cons
AS
(Select c.owner,
c.constraint_name,
c.table_name,
c.r_owner,
c.r_constraint_name,
cc.position,
cc.column_name
FROM sys.DBA_CONSTRAINTS c, sys.DBA_CONS_COLUMNS cc
WHERE c.constraint_type = 'R'
AND c.owner = cc.owner
AND c.constraint_name = cc.constraint_name
AND sys_context('USERENV', 'SESSION_USER') in (c.r_owner, c.owner) -- Constraint owner is USER or referenced owner is USER
),
pk_cons
AS
(Select c.owner,
c.constraint_name,
c.table_name,
cc.position,
cc.column_name
FROM sys.DBA_CONSTRAINTS c, sys.DBA_CONS_COLUMNS cc
WHERE c.constraint_type IN ('P', 'U')
AND c.owner = cc.owner
AND c.constraint_name = cc.constraint_name
AND c.owner = sys_context('USERENV', 'SESSION_USER')
)
SELECT 'REF_INTEGRITY' AS type,
ref_cons.owner,
ref_cons.table_name,
ref_cons.column_name,
ref_cons.constraint_name,
ref_cons.r_owner,
pk_cons.table_name AS r_table_name,
pk_cons.column_name AS r_column_name,
pk_cons.constraint_name AS r_constraint_name
FROM pk_cons, ref_cons
WHERE ref_cons.r_owner = pk_cons.owner
AND ref_cons.r_constraint_name = pk_cons.constraint_name
AND ref_cons.position = pk_cons.position
ORDER BY ref_cons.owner,
ref_cons.table_name,
ref_cons.constraint_name,
ref_cons.position;