I have written a JS transaction to be execute on arango server expecting it to improve the performance and reduce the communication between JS client and arango server. While testing, I found that performance have degraded when using JS transactions.
I have a json data which contains data to be updated to db:
{
col1: {name: "abc", "creator": "xyz", "desc": "this is obj"},
col2: [{id: 1, type: "t1", data: []}, {id: 2, type: "t2", data: []}],
}
col1, col2, col3 represents different collections and their values contains either data to be inserted.
for col2 and col3 -> I need to check if an data with same id exists and if it does, I need to delete it and re-create so that a new db object can be created with the given data.
My action function looks something similar to below:
const action = String(function(params) {
const col1 = params.col1;
const col2 = params.col2;
const { db, aql } = require('@arangodb');
// insert document to collection - col1
let q = aql`INSERT ${col1}
IN col1
OPTIONS {overwrite: true}
RETURN {doc: NEW, type: OLD ? 'update' : 'insert'}`;
let cursor = db._query(q);
const objSaved = cursor.next();
// insert objects for col2
for(let item in col2) {
// delete existing item from collection
let q = aql`for p in col2
FILTER p.id == ${item.id}
REMOVE p in col2
return OLD`;
db._query(q);
q = aql`INSERT ${item}
IN col2
OPTIONS {overwrite:true}`;
db._query(q);
}
});
const obj = {
col1: {name: "abc", "creator": "xyz", "desc": "this is obj"},
col2: [{id: 1, type: "t1", data: []}, {id: 2, type: "t2", data: []}],
col3: [{id: 11, type: "m1", data: []}, {id: 22, type: "m1", data: []}]
}
db.executeTransaction({'write': ['col1', 'col2']}, action, {'params': obj});
The transaction runs properly. But the time this takes increases compare to each query and insert operation I do it manually. Following is the different cases that I have observed.
| Total Transactions | Standalone | With Transaction |
|---|---|---|
| 280 | 12.6 s | 15.4s |
| 2000 | 80.4 s | 112.3s |
| 2000 | 84.6 s | 102 s |
| 2000 | 81.1 s | 105 s |
Note: I am need to add 1000s of objects to database and each transaction represent all steps required in action.
Can someone please explain why running the queries directly on arango server degrade the performance and why using transaction does not help me to gain performance ?