Oracle 中的家谱查询

发布于 2024-07-18 02:13:15 字数 440 浏览 9 评论 0原文

我正在尝试从 Oracle 数据库中获取动物谱系树。

这是表格:

Animal
------------------------
Animal_ID
Parent_Male_ID
Parent_Female_ID
....
....
------------------------

如果我指定一种动物,我可以使用类似这样的方法获得它的所有后代(在雄性方面):

SELECT *
FROM animal
START WITH animal_id = 123
CONNECT BY PRIOR animal_id = parent_male_id

我正在尝试找到一种方法来扩展它,如果我指定一种动物,它将获取父母双方,然后获取他们的所有后代。

有什么想法吗? (这是Oracle 9.2)

I'm trying to fetch a genealogy tree of animals from my Oracle database.

Here's the table:

Animal
------------------------
Animal_ID
Parent_Male_ID
Parent_Female_ID
....
....
------------------------

If I specify an animal, I can get all of its descendants (on the male side) using something like this:

SELECT *
FROM animal
START WITH animal_id = 123
CONNECT BY PRIOR animal_id = parent_male_id

I'm trying to find a way to extend this in such a way that if I specify an animal, it will fetch both parents and then will fetch all of their descendants.

Any thoughts? (this is Oracle 9.2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-07-25 02:13:15
SELECT  *
FROM    animal
START WITH
        animal_id IN
        (
        SELECT  parent_male_id
        FROM    animal
        WHERE   animal_id = 123
        UNION ALL 
        SELECT  parent_female_id
        FROM    animal
        WHERE   animal_id = 123
        )
CONNECT BY
        PRIOR animal_id IN (parent_male_id, parent_female_id)

然而,这个查询会非常慢。

最好使用这个:

SELECT  DISTINCT(animal_id) AS animal_id
FROM    (
        SELECT  0 AS gender, animal_id, father AS parent
        FROM    animal
        UNION ALL
        SELECT  1, animal_id, mother
        FROM    animal
        )
START WITH
        animal_id IN
        (
        SELECT  father
        FROM    animal
        WHERE   animal_id = 9500
        UNION ALL 
        SELECT  mother
        FROM    animal
        WHERE   animal_id = 9500
        )
CONNECT BY
        parent = PRIOR animal_id
ORDER BY
        animal_id

,它将使用 HASH JOIN 并且速度更快。

有关性能详细信息,请参阅我的博客中的此条目:

SELECT  *
FROM    animal
START WITH
        animal_id IN
        (
        SELECT  parent_male_id
        FROM    animal
        WHERE   animal_id = 123
        UNION ALL 
        SELECT  parent_female_id
        FROM    animal
        WHERE   animal_id = 123
        )
CONNECT BY
        PRIOR animal_id IN (parent_male_id, parent_female_id)

This query, however, will be quite slow.

Better to use this one:

SELECT  DISTINCT(animal_id) AS animal_id
FROM    (
        SELECT  0 AS gender, animal_id, father AS parent
        FROM    animal
        UNION ALL
        SELECT  1, animal_id, mother
        FROM    animal
        )
START WITH
        animal_id IN
        (
        SELECT  father
        FROM    animal
        WHERE   animal_id = 9500
        UNION ALL 
        SELECT  mother
        FROM    animal
        WHERE   animal_id = 9500
        )
CONNECT BY
        parent = PRIOR animal_id
ORDER BY
        animal_id

, which will use HASH JOIN and is much faster.

See this entry in my blog for performance details:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文