Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrientDB how to get a result set of vertices and its edges in one query

I have been playing around with OrientDB sql queries to get a result set that contains not only vertices but also the internal edges that exists between them.

The query could be expressed as:

  • I want all the vertices that are related to project (without project itself) and all the edges between the vertices that are included in the results

enter image description here

Here is how I have achieved it but I think it is not the proper way to do it.

enter image description here

select expand($union) let $vertices = ( select from ( traverse both() from (select from V where label = 'project') ) skip 1 ), $edges = ( select from ( select from E where @rid in ( select bothE() from ( select from ( traverse both() from (select from V where label = 'project') ) skip 1 ) ) ) where out in ( select from ( traverse both() from (select from V where label = 'project') ) skip 1 ) and in in ( select from ( traverse both() from (select from V where label = 'project') ) skip 1 ) ), $union = unionall($vertices, $edges)

And the expected results:

enter image description here

Problems with this solution:

  • I have to traverse the graph multiple times (first to get the vertices and then to get the edges to finally merge the results)
  • The base query select from V where label = 'project' is also executed several times.

Is there a better way to solve this use case?

Thanks.

like image 764
aschiavoni Avatar asked Dec 03 '15 11:12

aschiavoni


1 Answers

Try this query:

select expand($c)
let $a=(traverse both(),bothE() from (select from V where label="project")),
$b=(traverse bothE() from (select from V where label="project")),
$c=difference($a,$b)
like image 56
Michela Bonizzi Avatar answered Oct 12 '22 23:10

Michela Bonizzi