2016-09-30 120 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,但我沒有問題。我必須插入大量的邊緣。創建關係定義本身會從索引(非常龐大的多對多連接)中受益匪淺,但插入操作因索引而顯着減慢,從而消除了性能優勢。 –