I'm experiencing very poor performance in a query I've written for Oracle 12c. It's probably related to my inefficient use of joins and was hoping someone could help me out with where I'm going wrong. The query I have is currently taking over a minute to run.
I'm am trying to return the table and column names where:
- The column belongs to a Primary Key
- The column type is Number
- The owner of the table is MY_OWNER
- The Primary Key is a single column constraint
Currently my query looks as follows
SELECT consCols.table_name, consCols.column_name
FROM all_cons_columns consCols
INNER JOIN all_constraints cons
ON cons.constraint_name = consCols.constraint_name
INNER JOIN all_tab_columns cols
ON consCols.table_name = cols.table_name AND consCols.column_name = cols.column_name
WHERE cons.constraint_type = 'P'
AND cons.owner = 'MY_OWNER'
AND cols.data_type = 'NUMBER'
AND consCols.table_name IN(
SELECT consCols2.table_name
FROM all_cons_columns consCols2
INNER JOIN all_constraints cons2
ON cons2.constraint_name = consCols2.constraint_name
WHERE cons2.constraint_type = 'P'
AND cons2.owner = 'MY_OWNER'
GROUP BY consCols2.table_name
HAVING COUNT(consCols2.table_name) = 1
);
Thank you for any help you can give me.