sql - Oracle Statement performance: Retrieve primary keys exluding combined foreign keys -


for application need retrieve primary key columns of tables of database schema, excluding columns foreign key.
in other words, if m:n relation table doesn't use own primary key rather uses combination of 2 foreign keys primary key, columns shall excluded.

now first part can done using following statement:

select   ac.table_name,          acc.column_name     all_constraints ac,          all_cons_columns acc    ac.constraint_name = acc.constraint_name   ,    ac.constraint_type = 'p'   ,    ac.owner = upper('my_schema') order ac.table_name, acc.position 

executing takes around 0.2 seconds case. i've tried excluding combined foreign keys using following addition:

  ,    not exists(    select 1      all_constraints ac1, all_cons_columns acc1     ac1.constraint_name = acc1.constraint_name      ,  ac1.owner = ac.owner      ,  ac1.table_name = ac.table_name      ,  acc1.column_name = acc.column_name      ,  ac1.constraint_type = 'r'   ) 

executing whole statement takes around 2.5 seconds.

is there more efficient way same result?

i execute 2 separate queries, put results in lists , remove entries of list 2 list 1 in code, prefer having single statement solution.

to make question less subjective have these rules:

  • i upvote answer consider helpful (i do)
  • i accept first answer brings execution time below 1 second

ps: i'm using oracle 10g , statement executed .net application using oracle.dataaccess.dll, same execution times sqldeveloper.


solution:

based on don bracuk's answer, i've managed execution time down around 120ms using following statement:

select   table_name, column_name (   select ac.table_name, acc.column_name     all_constraints ac, all_cons_columns acc    ac.constraint_type = 'p'     ,  ac.owner = upper('my_schema')     -- if wondered, "upper" used 'my_schema' inserted @ runtime   minus     select ac1.table_name, acc1.column_name     all_constraints ac1, all_cons_columns acc1    ac1.constraint_type = 'p'     ,  ac1.owner = upper('my_schema') ) order table_name; 

you try structure:

select yourfields yourtables whatever , somefield in  (select somefield  fromyourtables  conditions same above  minus  select same field  whereever  want exclude it) 

if problem, i'd consider worth trying.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -