【问题标题】:Create edges between nodes of the same class using SQL使用 SQL 在同一类的节点之间创建边
【发布时间】:2016-05-16 08:05:19
【问题描述】:

我已经插入了一些“人”类的顶点,我想在居住在同一个城市的人之间创建边缘。这些顶点尚未通过任何类型的边相互连接。

我相信一种方法是使用(标准)SQL,就像这样:

CREATE EDGE SameCity FROM (SELECT From People) A TO (SELECT FROM People) B WHERE A.@rid<>B.@rid AND A.city=B.city;

不幸的是,这不起作用,因为子查询名称不适用于 OrientDB SQL。 你能帮帮我吗?

【问题讨论】:

    标签: sql orientdb


    【解决方案1】:

    如果您需要让所有人住在同一个城市,您可以稍微修改您的数据结构以支持此操作:

    ----------    livesIn      --------
    | People |  ----------->   | City |
    ----------                 --------
    

    我创建了一个小数据示例:

    create class people extends v
    create property people.name string
    create class city extends v
    create property city.name string
    
    create class livesIn extends e
    
    
    create vertex people set name="pippo"
    create vertex people set name="gigi"
    create vertex people set name="pluto"
    
    create vertex city set name="bergamo"
    create vertex city set name="grassobbio"
    
    create edge livesIn from (select from people where name = "pippo") to (select from city where name = "bergamo")
    create edge livesIn from (select from people where name = "gigi") to (select from city where name = "grassobbio")
    create edge livesIn from (select from people where name = "pluto") to (select from city where name = "bergamo")
    

    然后你可以得到所有人,例如,住在贝加莫你可以做到这一点

    select in("livesIn") from city where name = "bergamo"
    

    select in("livesIn").name from city where name = "bergamo" unwind in
    

    希望对您有所帮助。 伊万

    【讨论】:

    • 谢谢Ivan,但我宁愿不创建一个新类;这个属性(城市)不是那么重要。
    【解决方案2】:

    在我阅读了这篇文章后,我最终做到了: OrientDB - Create Edge using kind of self join

    但是我不确定这是否是最快的方法(与 SQL 相比),但它确实有效,所以我现在就这样吧。

    编辑: 这个解决方案虽然需要将所有节点都放入内存,但效率不高(我将拥有数百万个节点,而边缘将更多)。使用 SQL 也会因为索引而更有效。 因此,如果有其他建议,我将不胜感激!

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      相关资源
      最近更新 更多