Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Posts
    205

    Unanswered: Optimizing Oracle Query : Please Help!

    I use the following oracle query to find out the parent table (or PK table) of the each of the table present in the database.

    select user_constraints.table_name, user_cons_columns.table_name from user_constraints, user_cons_columns where user_constraints.constraint_type='R' and user_cons_columns.constraint_name=user_constraints .r_constraint_name order by user_constraints.table_name

    This query takes approximately 8 minutes to return in a oracle db containing 300 tables! Anyhelp on optimizing this query ? or is there anyother way to find this (PK tables of all the tables) out ?

    Thanks in advance,
    Sam

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    I don't understand why you are using user_cons_columns when you are not interested in column names. The other issue is that you need to include OWNER in the joins to get better performance. So try this:
    Code:
    select parent.table_name, child.table_name
    from user_constraints parent, user_constraints child
    where child.constraint_type='R'
    and parent.constraint_name=child.r_constraint_name
    and parent.owner = child.owner
    order by parent.table_name, child.table_name

  3. #3
    Join Date
    Mar 2004
    Posts
    205
    What you mean by owner in the table ?
    Is it the PK of that table ?

  4. #4
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    Yes, I mean this line:

    and parent.owner = child.owner

    Constraints are uniquely identified by the combination (owner, constraint_name), and query performance suggests that the index is in that order, meaning you need to include owner in the query for it to use the index.

  5. #5
    Join Date
    Jul 2004
    Posts
    102
    Hi andrewst,

    Do you mean include owner(table owner name) in the query for use the index and better performance ?
    Working Together...

  6. #6
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    Yes, that makes a big difference to the performance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •