Wednesday 22 September 2021

ArangoDB: Working with indexes

Indexes are used for faster access of the documents. ArangoDB create some indexes on system attributes by default.

 

What are the system attributes indexed by ArangoDB?

_id, _key, _from and _to are automatically indexed by ArangoDB.

 

Can I create custom indexes on a collection?

Yes, you can create custom indexes on a collection. You can create an index on a single attribute or on combination of multiple document attributes.

 

While creating an index, ArangoDB acquires an exclusive lock on the collection, so the collection is not available while the index creation is in process.

 

If your index creation process is takes long time, you can create the index in Background (RocksDB storage-engine support this).

 

Primary index

Each collection has one primary index. Primary index is used to quickly get the documents by _key or _id.

 

For example, let’s create a new collection ‘employees’ and check the primary index.

127.0.0.1:8529@abc_org> db._create("employees")
[ArangoCollection 77247, "employees" (type document, status loaded)]

127.0.0.1:8529@abc_org> db.employees.getIndexes()
[ 
  { 
    "fields" : [ 
      "_key" 
    ], 
    "id" : "employees/0", 
    "name" : "primary", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "primary", 
    "unique" : true 
  } 
]

 

Primary index is a persistent index on the _key attribute of the collection. For the persistent index, index entries are written to disk when documents are stored or updated.

 

Edge index

Every edge collection has an automatic edge index apart from the primary index. This edge index is created on the _from, _to  attributes. AQL uses edge indexes, whenever you query on fields _from, _to.

 

Let’s create a new edge collection and check the indexes.

127.0.0.1:8529@abc_org> db._createEdgeCollection("reportsTo")
[ArangoCollection 77307, "reportsTo" (type edge, status loaded)]

127.0.0.1:8529@abc_org> db.reportsTo.getIndexes()
[ 
  { 
    "fields" : [ 
      "_key" 
    ], 
    "id" : "reportsTo/0", 
    "name" : "primary", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "primary", 
    "unique" : true 
  }, 
  { 
    "fields" : [ 
      "_from", 
      "_to" 
    ], 
    "id" : "reportsTo/2", 
    "name" : "edge", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "edge", 
    "unique" : false 
  } 
]

 

Persistent Index

Persistent index is a sorted index where the entries are written to the disk whenever a document is created or updated.

 

TTL (time to live) index

TTL index is used to automatically remove the expired documents from a collection.

 

Example

db.collection.ensureIndex({ type: "ttl", fields: ["creationDate"], expireAfter: 600 });


The TTL index is set up by setting an expireAfter value and by selecting a single document attribute which contains a reference point in time.

 

Geo Index

Geo index is used to find the places on earth surfaces fast. You can create geo index on one or more attributes of a collection.

 

Full text index

Full text index is created on one attribute only, and is used to find words, or prefixes of words inside documents.

In my next post posts, you are going to learn.

a.   Get all the indexes of a collection

b.   Create an index

c.    Dropping an index

d.   Types of indexes

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment