0

Quick question: in ArangoDB, if I create a unique index (for example a unique hash index), does ArangoDB validate the uniqueness of that attribute, or just assume it because I told it it's unique? I'm curious if I should go through a validation step to verify the uniqueness of my data before creating unique indices.

1 Answer 1

3

As you know ArangoDB builds up the indexes before you can use them. If it fails to ensure the uniqueness, it will throw an exception:

127.0.0.1:8529@_system> c = db._create("c")
[ArangoCollection 169, "c" (type document, status loaded)]
127.0.0.1:8529@_system> c.insert({"abc":1})
{ 
  "_id" : "c/172", 
  "_key" : "172", 
  "_rev" : "_T1m73_m---" 
}
127.0.0.1:8529@_system> c.insert({"abc":1})
{ 
  "_id" : "c/176", 
  "_key" : "176", 
  "_rev" : "_T1m748K---" 
}
127.0.0.1:8529@_system> c.ensureIndex(
...> {"type":"hash","unique":true,"fields":["abc"]})
JavaScript exception in file '.../arangosh.js' at 97,7:
 ArangoError 1210: unique constraint violated
!      throw error;
!      ^
stacktrace: ArangoError: unique constraint violated
    at Object.exports.checkRequestResult (.../arangosh.js:95:21)
    at ArangoCollection.ensureIndex (.../arango-collection.js:733:12)
    at <shell command>:1:3

127.0.0.1:8529@_system> c.ensureIndex(
...> {"type":"skiplist","unique":true,"fields":["abc"]})
JavaScript exception in file '.../arangosh.js' at 97,7:
ArangoError 1210: unique constraint violated
!      throw error;
!      ^
stacktrace: ArangoError: unique constraint violated
    at Object.exports.checkRequestResult (.../arangosh.js:95:21)
    at ArangoCollection.ensureIndex (.../arango-collection.js:733:12)
    at <shell command>:1:3

Similar to what it does if you try to insert a document that violates the unique constraint:

127.0.0.1:8529@_system> db._drop("c")
127.0.0.1:8529@_system> c = db._create("c")
[ArangoCollection 315, "c" (type document, status loaded)]

127.0.0.1:8529@_system> c.ensureIndex({
...>"type":"skiplist","unique":true,"fields":["abc"]})
{ 
  "id" : "c/318", 
  "type" : "skiplist", 
  "fields" : [ 
    "abc" 
  ], 
  "unique" : true, 
  "sparse" : false, 
  "isNewlyCreated" : true, 
  "code" : 201 
}

127.0.0.1:8529@_system> c.insert({"abc":1})
{ 
  "_id" : "c/330", 
  "_key" : "330", 
  "_rev" : "_T1n-B2S---" 
}

127.0.0.1:8529@_system> c.insert({"abc":1})
JavaScript exception in file '.../arangosh.js' at 97,7:
 ArangoError 1210: cannot create document, unique constraint violated
!      throw error;
!      ^
stacktrace: ArangoError: cannot create document, unique constraint violated
 at Object.exports.checkRequestResult (.../arangosh.js:95:21)
 at ArangoCollection.save.ArangoCollection.insert 
   (.../arango-collection.js:978:14)
 at <shell command>:1:3

So if you insert your documents during your application setup before creating the index (for performance reasons a viable approach) you need to handle possible exceptions when creating these indices afterwards.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That answers my question. Since you bring up the performance of adding data after and index has been created, is there any way to temporarily turn off indexing during an insert then update the index at the end? I assume this violates ACID, but I'm okay with that. I have to insert a huge number of edges. Creating the relation definitions themselves would benefit a lot from indexing (very huge many-to-many join), but the insert operation gets significantly slowed down by indexing, thus eliminating the performance advantage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.