I am trying to write a database view that selects from 2 tables. Here is an example:
USER_TABLE
- ID
- USER_NAME
FAMILY_TABLE
- ID
- FAMILY_NAME
- FATHER_USER_NAME
- MOTHER_USER_NAME
- GRANDFATHER_1_USER_NAME
... (multiple user name columns)
(The table writers didn’t associate via ID for some reason)
I essentially need to make a view that has each user listed, along with the names of the families they belong to. I have tried a number of things, but each seems to fail.
I first tried to union SELECT statements from the FAMILY_TABLE, but that took forever (see my previous post).
Then I tried to do an “or join”:
SELECT ut.USER_NAME, ft.FAMILY_NAME
FROM USER_TABLE ut
LEFT OUTER JOIN FAMILY_TABLE ft ON
(
UPPER(ut.USER_NAME) = UPPER(ft.FATHER_USER_NAME)
OR
UPPER(ut.USER_NAME) = UPPER(ft.MOTHER_USER_NAME)
... (etc)
)
But that also takes a long time (took 22 seconds with just FATHER_USER_NAME and MOTHER_USER_NAME).
Can anyone suggest the most efficient way to write the query for the view I need? I am not sure how to make it quick.
FYI: Using Oracle 10g.
This apparently works quickly (about 500 ms):