2016-09-30 209 views
0

快速的问题:在ArangoDB,如果我创建唯一索引(例如唯一的哈希索引),并ArangoDB验证该属性的唯一性,或者只是假设,因为我告诉它,它独有的?我很好奇,在创建独特索引之前,是否应该通过验证步骤来验证数据的唯一性。ArangoDB唯一索引验证

回答

3

如您所知,ArangoDB在建立索引之前,可以使用它们。 如果它不能保证唯一性,它会抛出一个异常:

127.0.0.1:[email protected]_system> c = db._create("c") 
[ArangoCollection 169, "c" (type document, status loaded)] 
127.0.0.1:[email protected]_system> c.insert({"abc":1}) 
{ 
    "_id" : "c/172", 
    "_key" : "172", 
    "_rev" : "_T1m73_m---" 
} 
127.0.0.1:[email protected]_system> c.insert({"abc":1}) 
{ 
    "_id" : "c/176", 
    "_key" : "176", 
    "_rev" : "_T1m748K---" 
} 
127.0.0.1:[email protected]_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:[email protected]_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 

类似,如果你尝试插入违反唯一约束的文件它做什么:

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

127.0.0.1:[email protected]_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:[email protected]_system> c.insert({"abc":1}) 
{ 
    "_id" : "c/330", 
    "_key" : "330", 
    "_rev" : "_T1n-B2S---" 
} 

127.0.0.1:[email protected]_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 

所以,如果你在创建索引之前在应用程序设置期间插入文档(出于性能方面的原因,可行的方法),之后创建这些索引时需要处理可能的异常。

+0

谢谢!这回答了我的问题。既然你提出了在创建索引之后添加数据的性能,在插入过程中是否有任何方法暂时关闭索引,然后在最后更新索引?我认为这违反了ACID,但我没有问题。我必须插入大量的边缘。创建关系定义本身会从索引(非常庞大的多对多连接)中受益匪浅,但插入操作因索引而显着减慢,从而消除了性能优势。 –