Conditional Target vertex for syntax v2

I have a network of vertices Transaction connected undirectly to one another via other vertices. I would like to calculate the shortest path length from a Source Transaction vertex to all other Transaction vertices having the same attribute id. This is the query:

    CREATE QUERY tg_shortest_ss_no_wt (VERTEX source, SET<STRING> v_type, SET<STRING> e_type) RETURNS (ListAccum<INT>) {


      MinAccum<INT> @min_dis;
      OrAccum @or_visited;
      ListAccum<INT> @@result;

      ##### Initialization  #####
      Source = {source};
      Source = SELECT s 
               FROM Source:s
                      ACCUM s.@or_visited += true, 
                            s.@min_dis = 0; 
      ResultSet = {};


      ##### Calculate distances #####
      WHILE(Source.size()>0) DO
          Source = SELECT t
                          FROM Source:s -(e_type:e)-> v_type:t
                          WHERE t.@or_visited == false AND t.id == s.id
                          ACCUM t.@min_dis += s.@min_dis + 1,
                                t.@or_visited += true
          ORDER BY getvid(t);
          ResultSet = ResultSet UNION Source;
      END;

    	
      ##### Show result only for Transaction nodes and modify shortest lengths #####
      ResultSet = SELECT s
                  FROM ResultSet:s 
                  WHERE s.type == "Transaction"
                  ACCUM @@result += s.@min_dis/2;

      RETURN @@result;
    }

The problem is that the query calculates the shortest path length from the Source Transaction vertex to not only other Transaction vertices but also all other vertices, which makes the query very slow. Does anyone know how to make the query not calculate the shortest path length to vertices which are not Transaction? Thank you very much.

Seems like you could modify this general purpose query that suits all vertex and edge types, to one that is more suited to your schema. For instance change this line

SELECT t
FROM Source:s -(e_type:e)-> v_type:t

to

SELECT t
FROM Source:s -(e_type:e)-> Transaction:t

Hi there! I would second what Mark said, on the condition that you might Transactions to be directly connected. If the Transactions are indirectly connected via some intermediate vertices, then there’s not much to improve here, except that you could split out the select statement in the WHILE loop to something like the following

neighbors = 
  SELECT t FROM Source:s -(e_type:e)-> v_type:t
  WHERE ...
  ACCUM ...
Source =
  SELECT t FROM neighbors:s -(e_type:e)-> Transaction:t
  WHERE ...
  ACCUM ...
ResultSet = ResultSet UNION Source;

This optimization is assuming that the reason for your slowdown is because those intermediate vertices have neighbors of a type other than Transaction that are being needlessly included in the select statement.
Note that you can also remove that ORDER BY clause, since I’m not sure what it’s adding.

1 Like

Thank you but as @Wyatt_Joyner mentioned, this only worked when Transactions are directly connected, but they are indirectly connected in my case.

Thank you for your detailed response. The intermediate vertices only connect to Transactions in my case so it seems like I can’t optimize the query, but still your explanation gives me better understanding.

1 Like