Persistent indexes
This is an introduction to ArangoDB’s persistent indexes.
It is possible to define a persistent index on one or more attributes (or paths)of documents. The index is then used in queries to locate documents within a given range. If the index is declared unique, then no two documents are allowed to have the same set of attribute values.
Creating a new document or updating a document will fail if the uniqueness is violated. If the index is declared sparse, a document will be excluded from the index and no uniqueness checks will be performed if any index attribute value is not set or has a value of .
ensures that a unique persistent index existscollection.ensureIndex({ type: "persistent", fields: [ "field1", …, "fieldn" ], unique: true })
Creates a unique persistent index on all documents using field1, … _fieldn_as attribute paths. At least one attribute path has to be given. The index willbe non-sparse by default.
All documents in the collection must differ in terms of the indexed attributes. Creating a new document or updating an existing document willwill fail if the attribute uniqueness is violated.
To create a sparse unique index, set the sparse attribute to true
:
collection.ensureIndex({ type: "persistent", fields: [ "field1", …, "fieldn" ], unique: true, sparse: true })
In a sparse index all documents will be excluded from the index that do not contain at least one of the specified index attributes or that have a value of in any of the specified index attributes. Such documents willnot be indexed, and not be taken into account for uniqueness checks.
In a non-sparse index, these documents will be indexed (for non-presentindexed attributes, a value of null
will be used) and will be taken intoaccount for uniqueness checks.
Show execution results
- {
- "deduplicate" : true,
- "fields" : [
- "myId"
- ],
- "id" : "ids/73894",
- "isNewlyCreated" : true,
- "selectivityEstimate" : 1,
- "sparse" : false,
- "type" : "persistent",
- "unique" : true,
- "code" : 201
- }
- {
- "_id" : "ids/73896",
- "_key" : "73896",
- "_rev" : "_ZP4PT9K---"
- }
- {
- "_id" : "ids/73898",
- "_key" : "73898",
- "_rev" : "_ZP4PT9O---"
- }
- {
- "_id" : "ids/73900",
- "_key" : "73900",
- "_rev" : "_ZP4PT9O--A"
- }
- [ArangoError 1210: unique constraint violated - in index 73894 of type rocksdb-persistent over ["myId"]; conflicting key: 73896]
Hide execution results
- arangosh> db.ids.ensureIndex({ type: "persistent", fields: [ "name.first", "name.last" ], unique: true });
- arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
- arangosh> db.ids.save({ "name" : { "first" : "jens", "last": "jensen" }});
- arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
Show execution results
- {
- "deduplicate" : true,
- "fields" : [
- "name.first",
- "name.last"
- ],
- "id" : "ids/73876",
- "isNewlyCreated" : true,
- "selectivityEstimate" : 1,
- "sparse" : false,
- "type" : "persistent",
- "unique" : true,
- "code" : 201
- }
- {
- "_id" : "ids/73878",
- "_key" : "73878",
- "_rev" : "_ZP4PT8C---"
- }
- {
- "_id" : "ids/73880",
- "_key" : "73880",
- "_rev" : "_ZP4PT8G---"
- }
- {
- "_id" : "ids/73882",
- "_key" : "73882",
- "_rev" : "_ZP4PT8K---"
- }
- [ArangoError 1210: unique constraint violated - in index 73876 of type rocksdb-persistent over ["name.first","name.last"]; conflicting key: 73878]
Hide execution results
ensures that a non-unique persistent index existscollection.ensureIndex({ type: "persistent", fields: [ "field1", …, "fieldn" ] })
Creates a non-unique persistent index on all documents using field1, …fieldn as attribute paths. At least one attribute path has to be given.The index will be non-sparse by default.
To create a sparse unique index, set the sparse attribute to true
.
In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
Show execution results
- {
- "deduplicate" : true,
- "fields" : [
- "first"
- ],
- "isNewlyCreated" : true,
- "selectivityEstimate" : 1,
- "sparse" : false,
- "type" : "persistent",
- "unique" : false,
- "code" : 201
- }
- {
- "_id" : "names/73804",
- "_key" : "73804",
- "_rev" : "_ZP4PT3i---"
- }
- {
- "_id" : "names/73806",
- "_key" : "73806",
- "_rev" : "_ZP4PT3m---"
- }
- {
- "_id" : "names/73808",
- "_key" : "73808",
- "_rev" : "_ZP4PT3q---"
- }
- {
- "_id" : "names/73810",
- "_key" : "73810",
- "_rev" : "_ZP4PT3u---"
- }
- {
- "_id" : "names/73812",
- "_key" : "73812",
- "_rev" : "_ZP4PT3u--A"
- }
Hide execution results
Selects all documents from the collection that match the specified example and returns a cursor. A persistent index will be used if present.
You can use toArray, next, or hasNext to access theresult. The result can be limited using the skip and _limit_operator.
An attribute name of the form a.b is interpreted as attribute path,not as attribute. If you use
{ "a" : { "c" : 1 } }
as example, then you will find all documents, such that the attributea contains a document of the form {c : 1 }. For example the document
{ "a" : { "c" : 1 }, "b" : 1 }
will match, but the document
will not.
However, if you use
{ "a.c" : 1 },
then you will find all documents, which contain a sub-document in a_that has an attribute _c of value 1. Both the following documents
and
will match.
To fix persistent indexes after a language change, delete and re-create them.Skiplist indexes are not affected, because they are not persisted andautomatically rebuilt on every server start.