Working with Indexes

Learn how to use different indexes efficiently by going through theArangoDB Performance Course.

Index Identifiers and Handles

An index handle uniquely identifies an index in the database. It is a string and consists of the collection name and an index identifier separated by a /. The index identifier part is a numeric value that is auto-generated by ArangoDB.

A specific index of a collection can be accessed using its index handle orindex identifier as follows:

  1. db.collection.index("<index-handle>");
  2. db.collection.index("<index-identifier>");
  3. db._index("<index-handle>");

For example: Assume that the index handle, which is stored in the _idattribute of the index, is demo/362549736 and the index was created in a collectionnamed demo. Then this index can be accessed as:

  1. db.demo.index("demo/362549736");

Because the index handle is unique within the database, you can leave out thecollection and use the shortcut:

  1. db._index("demo/362549736");

Collection Methods

Listing all indexes of a collection

returns information about the indexesgetIndexes()

Returns an array of all indexes defined for the collection.Since ArangoDB 3.4, indexes() is an alias for getIndexes().

Note that _key implicitly has an index assigned to it.

  1. arangosh> db.test.ensureHashIndex("hashListAttribute",
  2. ........> "hashListSecondAttribute.subAttribute");
  3. arangosh> db.test.getIndexes();

Show execution results

  1. {
  2. "deduplicate" : true,
  3. "fields" : [
  4. "hashListAttribute",
  5. "hashListSecondAttribute.subAttribute"
  6. ],
  7. "id" : "test/73182",
  8. "isNewlyCreated" : true,
  9. "selectivityEstimate" : 1,
  10. "sparse" : false,
  11. "type" : "hash",
  12. "unique" : false,
  13. "code" : 201
  14. }
  15. [
  16. {
  17. "fields" : [
  18. "_key"
  19. ],
  20. "id" : "test/0",
  21. "selectivityEstimate" : 1,
  22. "sparse" : false,
  23. "type" : "primary",
  24. "unique" : true
  25. },
  26. {
  27. "deduplicate" : true,
  28. "fields" : [
  29. "skiplistAttribute"
  30. ],
  31. "id" : "test/73176",
  32. "selectivityEstimate" : 1,
  33. "sparse" : false,
  34. "type" : "skiplist",
  35. "unique" : true
  36. },
  37. {
  38. "deduplicate" : true,
  39. "fields" : [
  40. "skiplistUniqueAttribute"
  41. ],
  42. "id" : "test/73179",
  43. "selectivityEstimate" : 1,
  44. "sparse" : false,
  45. "type" : "skiplist",
  46. "unique" : true
  47. },
  48. {
  49. "deduplicate" : true,
  50. "fields" : [
  51. "hashListAttribute",
  52. "hashListSecondAttribute.subAttribute"
  53. ],
  54. "id" : "test/73182",
  55. "selectivityEstimate" : 1,
  56. "sparse" : false,
  57. "type" : "hash",
  58. "unique" : false
  59. }
  60. ]

Hide execution results

Creating an index

Indexes should be created using the general method ensureIndex. Thismethod obsoletes the specialized index-specific methods ensureHashIndex,ensureSkiplist, ensureUniqueConstraint etc.ensures that an index existscollection.ensureIndex(index-description)

Ensures that an index according to the index-description exists. Anew index will be created if none exists with the given description.

The index-description must contain at least a type attribute.Other attributes may be necessary, depending on the index type.

type can be one of the following values:

  • hash: hash index
  • skiplist: skiplist index
  • fulltext: fulltext index
  • geo: geo index, with one or two attributessparse can be true or false.

For hash, and skiplist the sparsity can be controlled, fulltext and _geo_are sparse by definition.

unique can be true or false and is supported by hash or skiplist

Calling this method returns an index object. Whether or not the indexobject existed before the call is indicated in the return attributeisNewlyCreated.

deduplicate can be true or false and is supported by array indexes oftype hash or skiplist. It controls whether inserting duplicate index values from the same document into a unique array index will lead to a unique constrainterror or not. The default value is true, so only a single instance of eachnon-unique index value will be inserted into the index per document. Trying toinsert a value into the index that already exists in the index will always fail,regardless of the value of this attribute.

Examples

  1. arangosh> db.test.ensureIndex({ type: "hash", fields: [ "a" ], sparse: true });
  2. arangosh> db.test.ensureIndex({ type: "hash", fields: [ "a", "b" ], unique: true });

Show execution results

  1. {
  2. "deduplicate" : true,
  3. "fields" : [
  4. "a"
  5. ],
  6. "id" : "test/73128",
  7. "isNewlyCreated" : true,
  8. "selectivityEstimate" : 1,
  9. "sparse" : true,
  10. "type" : "hash",
  11. "unique" : false,
  12. "code" : 201
  13. }
  14. {
  15. "deduplicate" : true,
  16. "fields" : [
  17. "a",
  18. "b"
  19. ],
  20. "id" : "test/73131",
  21. "isNewlyCreated" : true,
  22. "selectivityEstimate" : 1,
  23. "sparse" : false,
  24. "type" : "hash",
  25. "unique" : true,
  26. "code" : 201
  27. }

Hide execution results

Dropping an index via a collection handle

drops an indexcollection.dropIndex(index)

Drops the index. If the index does not exist, then false isreturned. If the index existed and was dropped, then true isreturned. Note that you cannot drop some special indexes (e.g. the primaryindex of a collection or the edge index of an edge collection).

collection.dropIndex(index-handle)

Same as above. Instead of an index an index handle can be given.

  1. arangosh> db.example.ensureSkiplist("a", "b");
  2. arangosh> var indexInfo = db.example.getIndexes();
  3. arangosh> indexInfo;
  4. arangosh> db.example.dropIndex(indexInfo[0])
  5. arangosh> db.example.dropIndex(indexInfo[1].id)
  6. arangosh> indexInfo = db.example.getIndexes();

Show execution results

  1. {
  2. "deduplicate" : true,
  3. "fields" : [
  4. "a",
  5. "b"
  6. ],
  7. "id" : "example/72974",
  8. "isNewlyCreated" : true,
  9. "selectivityEstimate" : 1,
  10. "sparse" : false,
  11. "type" : "skiplist",
  12. "unique" : false,
  13. "code" : 201
  14. }
  15. [
  16. {
  17. "fields" : [
  18. "_key"
  19. ],
  20. "id" : "example/0",
  21. "selectivityEstimate" : 1,
  22. "sparse" : false,
  23. "type" : "primary",
  24. "unique" : true
  25. },
  26. {
  27. "deduplicate" : true,
  28. "fields" : [
  29. "a",
  30. "b"
  31. ],
  32. "id" : "example/72974",
  33. "selectivityEstimate" : 1,
  34. "sparse" : false,
  35. "type" : "skiplist",
  36. "unique" : false
  37. }
  38. ]
  39. false
  40. true
  41. [
  42. {
  43. "fields" : [
  44. "_key"
  45. ],
  46. "id" : "example/0",
  47. "selectivityEstimate" : 1,
  48. "sparse" : false,
  49. "type" : "primary",
  50. "unique" : true
  51. }
  52. ]

Hide execution results

Load Indexes into Memory

Loads all indexes of this collection into Memory.collection.loadIndexesIntoMemory()

This function tries to cache all index entriesof this collection into the main memory.Therefore it iterates over all indexes of the collectionand stores the indexed values, not the entire document data,in memory.All lookups that could be found in the cache are much fasterthan lookups not stored in the cache so you get a nice performance boost.It is also guaranteed that the cache is consistent with the stored data.

For the time being this function is only useful on RocksDB storage engine,as in MMFiles engine all indexes are in memory anyways.

On RocksDB this function honors all memory limits, if the indexes you wantto load are smaller than your memory limit this function guarantees that mostindex values are cached.If the index is larger than your memory limit this function will fill up valuesup to this limit and for the time being there is no way to control which indexesof the collection should have priority over others.

  1. arangosh> db.example.loadIndexesIntoMemory();

Show execution results

  1. {
  2. "result" : true
  3. }

Hide execution results

Database Methods

Fetching an index by handle

finds an indexdb._index(index-handle)

Returns the index with index-handle or null if no such index exists.

  1. arangosh> db.example.ensureIndex({ type: "skiplist", fields: [ "a", "b" ] });
  2. arangosh> var indexInfo = db.example.getIndexes().map(function(x) { return x.id; });
  3. arangosh> indexInfo;
  4. arangosh> db._index(indexInfo[0])
  5. arangosh> db._index(indexInfo[1])

Show execution results

  1. {
  2. "deduplicate" : true,
  3. "fields" : [
  4. "a",
  5. "b"
  6. ],
  7. "id" : "example/68261",
  8. "isNewlyCreated" : true,
  9. "selectivityEstimate" : 1,
  10. "sparse" : false,
  11. "type" : "skiplist",
  12. "unique" : false,
  13. "code" : 201
  14. }
  15. [
  16. "example/0",
  17. "example/68261"
  18. ]
  19. {
  20. "fields" : [
  21. "_key"
  22. ],
  23. "id" : "example/0",
  24. "sparse" : false,
  25. "type" : "primary",
  26. "unique" : true,
  27. "code" : 200
  28. }
  29. {
  30. "deduplicate" : true,
  31. "fields" : [
  32. "a",
  33. "b"
  34. ],
  35. "id" : "example/68261",
  36. "sparse" : false,
  37. "type" : "skiplist",
  38. "unique" : false,
  39. "code" : 200
  40. }

Hide execution results

Dropping an index via a database handle

drops an indexdb._dropIndex(index)

Drops the index. If the index does not exist, then false isreturned. If the index existed and was dropped, then true isreturned.

db._dropIndex(index-handle)

Drops the index with index-handle.

  1. arangosh> db.example.ensureIndex({ type: "skiplist", fields: [ "a", "b" ] });
  2. arangosh> var indexInfo = db.example.getIndexes();
  3. arangosh> indexInfo;
  4. arangosh> db._dropIndex(indexInfo[0])
  5. arangosh> db._dropIndex(indexInfo[1].id)
  6. arangosh> indexInfo = db.example.getIndexes();

Show execution results

  1. {
  2. "deduplicate" : true,
  3. "fields" : [
  4. "a",
  5. "b"
  6. ],
  7. "id" : "example/73731",
  8. "isNewlyCreated" : true,
  9. "selectivityEstimate" : 1,
  10. "sparse" : false,
  11. "type" : "skiplist",
  12. "unique" : false,
  13. "code" : 201
  14. }
  15. [
  16. {
  17. "fields" : [
  18. "_key"
  19. ],
  20. "id" : "example/0",
  21. "selectivityEstimate" : 1,
  22. "sparse" : false,
  23. "type" : "primary",
  24. "unique" : true
  25. },
  26. {
  27. "deduplicate" : true,
  28. "fields" : [
  29. "a",
  30. "b"
  31. ],
  32. "id" : "example/73731",
  33. "selectivityEstimate" : 1,
  34. "sparse" : false,
  35. "type" : "skiplist",
  36. "unique" : false
  37. }
  38. ]
  39. false
  40. true
  41. [
  42. {
  43. "fields" : [
  44. "_key"
  45. ],
  46. "id" : "example/0",
  47. "selectivityEstimate" : 1,
  48. "sparse" : false,
  49. "type" : "primary",
  50. "unique" : true
  51. }
  52. ]

Hide execution results

Revalidating whether an index is used

finds an index

So you’ve created an index, and since its maintainance isn’t for free,you definitely want to know whether your query can utilize it.

You can use explain to verify whether skiplists or hash indexes are used (if you omit colors: false you will get nice colors in ArangoShell):

  1. arangosh> var explain = require("@arangodb/aql/explainer").explain;
  2. arangosh> db.example.ensureIndex({ type: "skiplist", fields: [ "a", "b" ] });
  3. arangosh> explain("FOR doc IN example FILTER doc.a < 23 RETURN doc", {colors:false});

Show execution results

  1. {
  2. "deduplicate" : true,
  3. "fields" : [
  4. "a",
  5. "b"
  6. ],
  7. "id" : "example/68274",
  8. "isNewlyCreated" : true,
  9. "selectivityEstimate" : 1,
  10. "sparse" : false,
  11. "type" : "skiplist",
  12. "unique" : false,
  13. "code" : 201
  14. }
  15. Query String (47 chars, cacheable: true):
  16. FOR doc IN example FILTER doc.a < 23 RETURN doc
  17.  
  18. Execution plan:
  19. Id NodeType Est. Comment
  20. 1 SingletonNode 1 * ROOT
  21. 6 IndexNode 1 - FOR doc IN example /* skiplist index scan */
  22. 5 ReturnNode 1 - RETURN doc
  23.  
  24. Indexes used:
  25. By Type Collection Unique Sparse Selectivity Fields Ranges
  26. 6 skiplist example false false 100.00 % [ `a`, `b` ] (doc.`a` < 23)
  27.  
  28. Optimization rules applied:
  29. Id RuleName
  30. 1 use-indexes
  31. 2 remove-filter-covered-by-index
  32. 3 remove-unnecessary-calculations-2

Hide execution results