Efficient traverse with where condition

Hi all,

I built a tree in orientdb with a high amount of nodes and edges in bottom up direction. I would like to receive all leaves of that tree (each leaf has the @class LEAF) which match a certain range condition (e.g. val > 150 and val < 300):

Example:

       o
     /   \
    o     o 
   /    /  \ 
  o    o    o
 210  120   /\
          270 180 

Result: 210, 180, 270

What I tried so far is to use the following query:

SELECT FROM(TRAVERSE IN() FROM <root>) WHERE value > 150 and value < 300 

This is working well with a few elements but the performance drops with increasing amount of leafs. So my questions are:

  1. Is this the most efficient way to do this in OrientDB, especially if I have let’s say 300000 leaves under each node ?
  2. I know from the documentation, that using an index can help to improve the performance on range queries, but would the index be used in this case? My first assumption is no because orient is first traversing and then filtering the the traversed elements. An explain in orient studio gives me:
:+ FETCH FROM SUBQUERY 
+ FETCH FROM RIDs [#123:123] 
+ DEPTH-FIRST TRAVERSE [IN()] 
+ FILTER ITEMS WHERE value > 150 AND value < 300
  1. bonus question: what I really want is to query the last leaf for each node. Before I executed the query for each single node which was way slower than executing one single query. But still the performance is not really good and I wonder if there is way to do this in orient db.

Thanks and Regards

Jon

Hi,
what version are you using?

Thanks

Regards,
Michela

Hi,

I am using 3.0.x.

Regards
Jon

Hi,
can you try this?
SELECT expand(leaf) FROM (MATCH{CLASS: leaf, AS: leaf, WHERE: (val BETWEEN 150 and 300)} return leaf)
Is it still slow or a little bit faster?

Thanks

Regards,
Michela

Hi Michela,

definitely way faster - thanks so far! The thing is: the query should only fetch the leafs under root (or even any node) x and not ALL the leafs in the database. As far as I understand your query, it is searching over all vertices with @class=leaf or am I wrong? - I should have mentioned that sorry :smirk:.

Thanks
Jon