SmartGraphs

SmartGraphs are only available in theEnterprise Edition,also available as managed service.

This chapter describes the smart-graph module, which enables you to managegraphs at scale. It will give a vast performance benefit for all graphs shardedin an ArangoDB Cluster. On a single server this feature is pointless, hence itis only available in cluster mode.

In terms of querying there is no difference between SmartGraphs andGeneral Graphs. The former is a transparent replacement for the latter.For graph querying please refer to AQL Graph Operationsand General Graph Functions sections.The optimizer is clever enough to identify whether it is a SmartGraph or not.

The difference is only in the management section: creating and modifying theunderlying collections of the graph. For a detailed API reference please referto SmartGraph Management.

Do the hands-onArangoDB SmartGraphs Tutorialto learn more.

What makes a graph smart?

Most graphs have one feature that divides the entire graph into several smallersubgraphs. These subgraphs have a large amount of edges that only connectvertices in the same subgraph and only have few edges connecting vertices fromother subgraphs.

Examples for these graphs are:

  • Social NetworksTypically the feature here is the region/country users live in.Every user typically has more contacts in the same region/country then shehas in other regions/countries

  • Transport SystemsFor those also the feature is the region/country. You have many localtransportation but only few across countries.

  • E-CommerceIn this case probably the category of products is a good feature.Often products of the same category are bought together.

If this feature is known, SmartGraphs can make use if it.

When creating a SmartGraph you have to define a smartAttribute, which is thename of an attribute stored in every vertex. The graph will than beautomatically sharded in such a way that all vertices with the same value arestored on the same physical machine, all edges connecting vertices withidentical smartAttribute values are stored on this machine as well.During query time the query optimizer and the query executor both know forevery document exactly where it is stored and can thereby minimize networkoverhead. Everything that can be computed locally will be computed locally.

Benefits of SmartGraphs

Because of the above described guaranteed sharding, the performance of queriesthat only cover one subgraph have a performance almost equal to an only localcomputation. Queries that cover more than one subgraph require some networkoverhead. The more subgraphs are touched the more network cost will apply.However the overall performance is never worse than the same query using aGeneral Graph.

Getting started

First of all SmartGraphs cannot use existing collections, when switching toSmartGraph from an existing data set you have to import the data into a freshSmartGraph. This switch can be easily achieved witharangodump andarangorestore.The only thing you have to change in this pipeline is that you create the newcollections with the SmartGraph before starting arangorestore.

  • Create a graph

In comparison to General Graph we have to add more options when creating thegraph. The two options smartGraphAttribute and numberOfShards arerequired and cannot be modified later.

@startDocuBlockInline smartGraphCreateGraphHowTo1 arangosh> var graph_module = require(“@arangodb/smart-graph”); arangosh> var graph = graph_module._create(“myGraph”, [], [], {smartGraphAttribute: “region”, numberOfShards: 9}); arangosh> graph; [ SmartGraph myGraph EdgeDefinitions: [ ] VertexCollections: [ ] ] @endDocuBlock smartGraphCreateGraphHowTo1

  • Add some vertex collections

This is again identical to General Graph. The module will setup correctsharding for all these collections. Note: The collections have to be new.

@startDocuBlockInline smartGraphCreateGraphHowTo2 arangosh> graph._addVertexCollection(“shop”); arangosh> graph._addVertexCollection(“customer”); arangosh> graph._addVertexCollection(“pet”); arangosh> graph; [ SmartGraph myGraph EdgeDefinitions: [ ] VertexCollections: [ “shop”, “customer”, “pet” ] ] @endDocuBlock smartGraphCreateGraphHowTo2

  • Define relations on the Graph

@startDocuBlockInline smartGraphCreateGraphHowTo3 arangosh> var rel = graph_module._relation(“isCustomer”, [“shop”], [“customer”]); arangosh> graph._extendEdgeDefinitions(rel); arangosh> graph; [ SmartGraph myGraph EdgeDefinitions: [ “isCustomer: [shop] -> [customer]” ] VertexCollections: [ “pet” ] ] @endDocuBlock smartGraphCreateGraphHowTo3