score:16

Accepted answer

sorry for the late reply, we were busy building v2.8 ;) i would suggest to do as many things as possible on the database side, as copying and serializing/deserializing json over the network is typically expensive, so transferring as little data as possible should be a good aim.

first of all i have used your query and executed it on a sample dataset i created (~ 800 vertices and 800 edges are hit in my dataset) as a baseline i used the execution time of your query which in my case was ~5.0s

so i tried to create the exact same result as you need in aql only. i have found some improvements in your query: 1. graph_neighbors is a bit faster than graph_edges. 2. if possible avoid {includedata: true} if you do not need the data especially if you need to/from vertices._id only graph_neighbors with {includedata: false} outperforms graph_edges by an order of magnitude. 3. graph_neighbors is deduplicated, graph_edges is not. which in your case seems to be desired. 3. you can get rid of a couple of subqueries there.

so here is the pure aql query i could come up with:

let docid = "exampledocclass/1234567"
let edges = graph_edges('edgeclass',docid,{direction:'any',maxdepth:1,includedata:true})
let verticestmp = (for v in graph_neighbors('edgeclass', docid, {direction: 'any', maxdepth: 1, includedata: true})
  return {
    vertexdata: v,
    outedges: graph_neighbors('edgeclass', v, {direction: 'outbound', maxdepth: 1, includedata: false}),
    inedges: graph_neighbors('edgeclass', v, {direction: 'inbound', maxdepth: 1, includedata: false})
  })
let vertices = push(verticestmp, {
  vertexdata: document(docid),
  outedges: graph_neighbors('edgeclass', docid, {direction: 'outbound', maxdepth: 1, includedata: false}),
  inedges: graph_neighbors('edgeclass', docid, {direction: 'inbound', maxdepth: 1, includedata: false})
})
return { edges, vertices }

this yields the same result format as your query and has the advantage that every vertex connected to docid is stored exactly once in vertices. also docid itself is stored exactly once in vertices. no deduplication required on client side. but, in outedges / inedges of each vertices all connected vertices are also exactly once, i do not know if you need to know if there are multiple edges between vertices in this list as well.

this query uses ~0.06s on my dataset.

however if you put some more effort into it you could also consider to use a hand-crafted traversal inside a foxx application. this is a bit more complicated but might be faster in your case, as you do less subqueries. the code for this could look like the following:

var traversal = require("org/arangodb/graph/traversal");
var result = {
  edges: [],
  vertices: {}
}
var myvisitor = function (config, result, vertex, path, connected) {
  switch (path.edges.length) {
    case 0:
      if (! result.vertices.hasownproperty(vertex._id)) {
        // if we visit a vertex, we store it's data and prepare out/in
        result.vertices[vertex._id] = {
          vertexdata: vertex,
          outedges: [],
          inedges: []
        };
      }

      // no further action
      break;
    case 1:
      if (! result.vertices.hasownproperty(vertex._id)) {
        // if we visit a vertex, we store it's data and prepare out/in
        result.vertices[vertex._id] = {
          vertexdata: vertex,
          outedges: [],
          inedges: []
        };
      }
      // first depth, we need edgedata
      var e = path.edges[0];
      result.edges.push(e);
      // we fill from / to for both vertices
      result.vertices[e._from].outedges.push(e._to);
      result.vertices[e._to].inedges.push(e._from);
      break;
    case 2:
      // second depth, we do not need edgedata
      var e = path.edges[1];
      // we fill from / to for all vertices that exist
      if (result.vertices.hasownproperty(e._from)) {
        result.vertices[e._from].outedges.push(e._to);
      }
      if (result.vertices.hasownproperty(e._to)) {
        result.vertices[e._to].inedges.push(e._from);
      }
      break;
  }
};
var config = {
  datasource: traversal.generalgraphdatasourcefactory("edgeclass"),
  strategy: "depthfirst",
  order: "preorder",
  visitor: myvisitor,
  expander: traversal.anyexpander,
  mindepth: 0,
  maxdepth: 2
};
var traverser = new traversal.traverser(config);
traverser.traverse(result, {_id: "exampledocclass/1234567"});
return {
  edges: result.edges,
  vertices: object.keys(result.vertices).map(function (key) {
              return result.vertices[key];
            })
};

the idea of this traversal is to visit all vertices from the start vertex to up to two edges away. all vertices in 0 - 1 depth will be added with data into the vertices object. all edges originating from the start vertex will be added with data into the edges list. all vertices in depth 2 will only set the outedges / inedges in the result.

this has the advantage that, vertices is deduplicated. and outedges/inedges contain all connected vertices multiple times, if there are multiple edges between them.

this traversal executes on my dataset in ~0.025s so it is twice as fast as the aql only solution.

hope this still helps ;)


Related Query

More Query from same tag