The index types reduce to a short list once you know which ones are current. The primary index on _key exists always. The edge index on _from and _to exists on every edge collection and is what makes traversal work. The persistent index is the general-purpose one - sorted, usable for equality, range and sort, supporting multiple fields, uniqueness and sparseness - and it is what you should reach for by default; the older hash and skiplist names are legacy aliases for it and are worth recognising in old code rather than writing. A TTL index expires documents after a time, which is the cheapest retention mechanism available. Geo indexes serve spatial queries. And the inverted index gives you ArangoSearch-style text indexing on a single collection without creating a view, which is the right choice when you want search within one collection rather than across several.
Composite persistent index field order follows the same rule as everywhere: the fields you filter for equality first, then the field you range or sort on. A prefix of the index can be used; a suffix cannot. And the cost side is the same everywhere too - every index is work on every write, so each one needs a query justifying it and a measured write cost beside it.
EXPLAIN and PROFILE are the instruments and they answer different questions. db._explain(query) shows the plan without running it - which indexes are chosen, the node structure, the estimated cost, and which rules were applied. db._profile(query) runs it and attaches actual timings and row counts per node, which is what you want when the plan looks right and the query is slow anyway.
What to look for, in order. An EnumerateCollectionNode is a full collection scan and is almost always the thing to fix - it means no index served that filter. An IndexNode names the index used, and a plan where the index you built is absent means your filter does not match its fields or their order. A TraversalNode carries the depth bounds and whether pruning was applied. And the estimated cost against the actual row counts from PROFILE tells you when the planner's estimate was wrong, which is the case where you may need to restructure the query rather than add an index.
Traversal pruning is the most important performance technique in this course and it deserves measuring properly. FILTER on a traversal's output discards paths after walking them; PRUNE stops the descent so the subtree is never visited. On a graph with average degree ten, a three-hop traversal visits roughly a thousand vertices; pruning at the first hop to the ten per cent that matter reduces that to roughly a hundred, and the saving compounds with each additional hop. Measure your own curve - depth against vertices visited and latency, with and without pruning - because the shape of that curve is what tells you the maximum depth you can expose to users.
Three more traversal levers. Bound the depth always, and choose the bound from your curve rather than from optimism. Set uniqueVertices and uniqueEdges deliberately - the defaults are chosen for correctness on general graphs and the options matter when you have cycles, where uniqueVertices: "global" with a breadth-first order is what prevents revisiting. And traverse the narrowest edge collection that answers the question, which is the Module 2 modelling decision paying off.
Query memory and runtime limits are protection rather than tuning, and this is the right way to think about them. A single query can allocate until the server is in trouble; memoryLimit kills a query that exceeds it, which is the behaviour you want - one failed query instead of one unavailable cluster. Set it globally as a safety net and per-query from the application, where you know what each query should reasonably need. Set maxRuntime too. The queries that cause incidents are rarely the ones you suspected, which is the argument for a limit on everything rather than on the ones you worried about.
Benchmarking honestly, with this course's specific requirement: every number on both topologies. A single-server benchmark of a graph workload is a measurement of whether your query is well written, not of whether your deployment works - the cluster numbers are the ones that predict production, and for traversals the two can differ by an order of magnitude. Beyond that, the usual: realistic graph density, a long enough run, concurrent load, p50/p95/p99 from the client, and the view's indexing settled before you measure search.
Laboratory exercise 4 - a measured baseline on both topologies.
Load at least 20 million documents and 60 million edges with realistic density into both deployments. Declare a baseline: twelve representative queries - document, filtered, aggregated, traversal at depths one to three, search, and combined search-and-traversal - with p50, p95 and p99 on both, and the plan for each.
Do the index study: for each query shape, create the index, confirm from EXPLAIN that the EnumerateCollectionNode became an IndexNode, measure latency, and measure write throughput with each index added cumulatively. Record any query where you could not eliminate the full scan and why.
Then the pruning study, which is the module's main artefact. For a three-hop traversal, measure vertices visited and latency at depths one through five, with FILTER only and with PRUNE, on both deployments - so four curves. State the maximum depth you would expose to a user, with the number behind it.
Test uniqueVertices and uniqueEdges on a graph you deliberately give cycles to, and record what each setting does to both correctness and cost.
Demonstrate the memory limit as protection: write a query that allocates without bound, run it with no limit and record what happens to the server, then run it with a limit and show it killed cleanly. Do this on the cluster too, and record whether the effect is contained to one coordinator.
Compare the inverted index against a view for single-collection search: index size, search latency, write cost, and which you would choose.
Deliverable: the twelve-query baseline on both topologies with plans, the index study including failures, the four pruning curves with a stated maximum depth, the uniqueness option findings, the memory limit demonstration on both deployments, the inverted index against view comparison, and one optimisation that did not help.
// ==================================================================
// THE INDEX TYPES, once you know which are CURRENT.
// ==================================================================
// PRIMARY on _key. Always there.
// EDGE on _from and _to. On every edge collection.
// *** WHAT MAKES TRAVERSAL WORK. ***
// *** PERSISTENT *** the GENERAL-PURPOSE one: sorted, equality +
// range + sort, multi-field, unique, sparse.
// *** REACH FOR THIS BY DEFAULT. ***
// ("hash" and "skiplist" are LEGACY ALIASES for it -
// recognise them in old code; do not write them.)
// TTL expires documents after a time. *** THE CHEAPEST
// RETENTION MECHANISM AVAILABLE. ***
// GEO spatial.
// INVERTED ArangoSearch-style TEXT indexing *** ON A SINGLE
// COLLECTION WITHOUT A VIEW *** - the right choice for
// search WITHIN one collection.
//
// db.entity.ensureIndex({ type: "persistent",
// fields: ["kind", "founded"] }); // equality, THEN range
// db.entity.ensureIndex({ type: "persistent",
// fields: ["name", "kind"], unique: true }); // Module 2's fix
// db.document.ensureIndex({ type: "ttl",
// fields: ["expires_at"], expireAfter: 0 });
// db.document.ensureIndex({ type: "inverted", fields: [
// { name: "body", analyzer: "text_en" } ] });
//
// *** COMPOSITE FIELD ORDER: EQUALITY FILTERS FIRST, THEN THE RANGE
// *** OR SORT FIELD. A PREFIX of the index can be used; A SUFFIX
// *** CANNOT. *** And every index is work on every write, so each
// needs A QUERY JUSTIFYING IT AND A MEASURED WRITE COST BESIDE IT.
// ==================================================================
// EXPLAIN AND PROFILE answer DIFFERENT questions.
// ==================================================================
// db._explain(query, bindVars) // the PLAN, without running it
// db._profile(query, bindVars) // RUNS it: ACTUAL timings and
// // row counts PER NODE
//
// *** WHAT TO LOOK FOR, IN ORDER: ***
// *** EnumerateCollectionNode *** a FULL COLLECTION SCAN. Almost
// always the thing to fix - NO INDEX SERVED THAT FILTER.
// IndexNode names the index USED. *** IF THE
// INDEX YOU BUILT IS ABSENT, your filter does NOT MATCH ITS
// FIELDS OR THEIR ORDER. ***
// TraversalNode the DEPTH BOUNDS, and *** WHETHER
// PRUNING WAS APPLIED. ***
// *** estimated cost vs ACTUAL row counts from PROFILE *** -> when
// they diverge, *** THE PLANNER'S ESTIMATE WAS WRONG ***, and
// you may need to RESTRUCTURE THE QUERY rather than add an
// index.
//
// before: EnumerateCollectionNode entity 20,000,000 rows
// after : IndexNode (persistent, kind+founded) 1,840 rows
// ==================================================================
// *** TRAVERSAL PRUNING: THE MOST IMPORTANT PERFORMANCE TECHNIQUE IN
// *** THIS COURSE. ***
// ==================================================================
// FILTER discards paths AFTER walking them.
// *** PRUNE STOPS THE DESCENT, SO THE SUBTREE IS NEVER VISITED. ***
//
// On a graph of average degree 10, a 3-hop traversal visits ~1,000
// vertices. Pruning at the first hop to the 10% that matter reduces
// that to ~100 - *** AND THE SAVING COMPOUNDS WITH EACH ADDITIONAL
// HOP. ***
FOR v, e, p IN 1..3 OUTBOUND @start GRAPH "knowledge"
OPTIONS { bfs: true, uniqueVertices: "global" }
PRUNE v.kind != "organisation" OR v.status == "dissolved"
FILTER v.kind == "organisation"
LIMIT 200
RETURN DISTINCT { name: v.name, depth: LENGTH(p.edges) }
// *** MEASURE YOUR OWN CURVE - and the SHAPE of it tells you THE
// *** MAXIMUM DEPTH YOU CAN EXPOSE TO USERS: ***
// depth FILTER only PRUNE
// visited ms visited ms
// 1 11 2 ms 11 2 ms
// 2 118 9 ms 14 3 ms
// 3 1,240 96 ms 31 7 ms
// 4 13,100 1,180 ms 74 19 ms
// 5 *** 142,000 14,400 ms *** 181 48 ms
// -> *** WITHOUT PRUNING, DEPTH 4 IS THE CEILING. WITH IT, DEPTH 5
// IS COMFORTABLE. STATE THE NUMBER YOU WILL SHIP. ***
// *** THREE MORE LEVERS: ***
// (1) *** BOUND THE DEPTH, ALWAYS ***, and choose the bound FROM
// YOUR CURVE rather than from optimism.
// (2) *** uniqueVertices / uniqueEdges DELIBERATELY. *** The
// defaults are chosen for correctness on general graphs; the
// options matter *** WHEN YOU HAVE CYCLES ***, where
// uniqueVertices: "global" WITH bfs: true is what prevents
// revisiting. (global requires bfs or weighted order - test
// it.)
// (3) *** TRAVERSE THE NARROWEST EDGE COLLECTION *** that answers
// the question - Module 2's modelling decision PAYING OFF:
FOR v IN 1..2 OUTBOUND @start employs // *** not GRAPH, not ANY ***
RETURN v.name
// ==================================================================
// *** MEMORY AND RUNTIME LIMITS ARE PROTECTION, NOT TUNING. ***
// ==================================================================
// A single query can allocate until the server is in trouble.
// *** memoryLimit KILLS A QUERY THAT EXCEEDS IT, WHICH IS THE
// *** BEHAVIOUR YOU WANT: ONE FAILED QUERY INSTEAD OF ONE
// *** UNAVAILABLE CLUSTER. ***
//
// GLOBALLY, as a safety net:
// --query.memory-limit 2147483648
// --query.max-runtime 60
// PER QUERY, from the application, where you know what it should
// reasonably need:
// { memoryLimit: 536870912, maxRuntime: 15, failOnWarning: true }
//
// DEMONSTRATE IT: a query that allocates without bound -
// no limit -> *** the server swaps, then the OOM killer ***
// limit -> ERROR_QUERY_RESOURCE_LIMIT_EXCEEDED (32).
// *** Clean. One query dead. Everything else
// fine. ***
// And on the CLUSTER, record whether the effect is CONTAINED TO
// ONE COORDINATOR - that answer shapes how you size coordinators.
//
// *** THE QUERIES THAT CAUSE INCIDENTS ARE RARELY THE ONES YOU
// *** SUSPECTED *** - which is the argument for a limit on
// EVERYTHING rather than on the ones you worried about.
//
// // what is running right now:
// require("@arangodb/aql/queries").current();
// require("@arangodb/aql/queries").slow();
// require("@arangodb/aql/queries").kill(queryId);
// ==================================================================
// *** BENCHMARKING HONESTLY - THIS COURSE'S SPECIFIC REQUIREMENT:
// *** EVERY NUMBER ON BOTH TOPOLOGIES. ***
// ==================================================================
// query single server 3-node cluster
// entity by key 0.4 ms 1.1 ms
// filtered + sorted 6 ms 9 ms
// aggregate by kind 84 ms 41 ms <- WINS
// 1-hop traversal 2 ms 4 ms
// 2-hop, pruned 3 ms 28 ms
// *** 3-hop, pruned 7 ms 210 ms ***
// search, BM25 18 ms 24 ms
// search + 2-hop 22 ms 118 ms
//
// *** READ THE TRAVERSAL ROWS. A SINGLE-SERVER BENCHMARK OF A GRAPH
// *** WORKLOAD MEASURES WHETHER YOUR QUERY IS WELL WRITTEN, NOT
// *** WHETHER YOUR DEPLOYMENT WORKS. *** The cluster numbers are the
// ones that predict production, and for traversals they can differ
// BY AN ORDER OF MAGNITUDE - for reasons Module 5 explains, and with
// fixes Module 5 names.
//
// Note too that the AGGREGATE got FASTER on the cluster: parallel
// work across DB-Servers. *** THE TOPOLOGY IS NOT UNIFORMLY WORSE -
// IT IS DIFFERENT PER QUERY SHAPE ***, which is exactly why you
// measure both.
//
// And the usual: realistic graph DENSITY, a long enough run,
// concurrent load, p50/p95/p99 FROM THE CLIENT, and *** THE VIEW'S
// INDEXING SETTLED BEFORE YOU MEASURE SEARCH. ***
Discussion
No questions on this topic yet. Sign in to ask the first one.